简体   繁体   English

Oracle sql-将一个表中的sql行值作为列值计数添加到另一表中

[英]Oracle sql - Adding sql row value from one table as column value count to another table

I have the following tables and values in my schema 我的架构中包含以下表格和值

CREATE TABLE products
(product_id NUMBER(5) NOT NULL,
 product_name VARCHAR2(10) NOT NULL,
 CONSTRAINT product_pk PRIMARY KEY (product_id)
);

CREATE TABLE packages
(package_id NUMBER(5) NOT NULL,
 package_name VARCHAR2(10) NOT NULL,
 CONSTRAINT package_pk PRIMARY KEY (package_id)
);

CREATE TABLE product_packages
(product_id NUMBER(5) NOT NULL,
 package_id NUMBER(5) NOT NULL,
 CONSTRAINT product_fk FOREIGN KEY (product_id) REFERENCES products(product_id),
 CONSTRAINT package_fk FOREIGN KEY (package_id) REFERENCES packages(package_id)
);

CREATE TABLE customers
(customer_id NUMBER(10) NOT NULL,
 customer_name VARCHAR2(50) NOT NULL,
 CONSTRAINT customer_pk PRIMARY KEY (customer_id)
);

CREATE TABLE orders
(order_id NUMBER(5) NOT NULL,
 customer_id NUMBER(10) NOT NULL,
 product_id NUMBER(5) NOT NULL,
 quantity NUMBER(5) NOT NULL,
 CONSTRAINT ord_customer_fk FOREIGN KEY (customer_id) REFERENCES customers(customer_id),
 CONSTRAINT ord_product_fk FOREIGN KEY (product_id) REFERENCES products(product_id)
);


INSERT  INTO products (product_id, product_name) VALUES (1, 'BARBB');
INSERT  INTO products (product_id, product_name) VALUES (2, 'BARHB');
INSERT  INTO products (product_id, product_name) VALUES (3, 'BARFB');
INSERT  INTO packages (package_id, package_name) VALUES (1, 'BRF');
INSERT  INTO packages (package_id, package_name) VALUES (2, 'LNC');
INSERT  INTO packages (package_id, package_name) VALUES (3, 'DNR');
INSERT  INTO product_packages (product_id, package_id) VALUES (1, 1);
INSERT  INTO product_packages (product_id, package_id) VALUES (2, 1);
INSERT  INTO product_packages (product_id, package_id) VALUES (2, 3);
INSERT  INTO product_packages (product_id, package_id) VALUES (3, 1);
INSERT  INTO product_packages (product_id, package_id) VALUES (3, 2);
INSERT  INTO product_packages (product_id, package_id) VALUES (3, 3);
INSERT  INTO customers (customer_id, customer_name) VALUES (1, 'John Smith');
INSERT  INTO customers (customer_id, customer_name) VALUES (2, 'Adam Nash');
INSERT  INTO orders (order_id, customer_id, product_id, quantity) VALUES (1, 1, 2, 1);
INSERT  INTO orders (order_id, customer_id, product_id, quantity) VALUES (2, 2, 3, 2);

What I want to do is to add columns to the ORDER table with package names in PACKAGES table; 我想做的是在PACKAGES表中使用包名称向ORDER表添加列; if that product contains the corresponding package, then indicate QUANTITY column value, otherwise 0; 如果该产品包含相应的包装,则指示QUANTITY列值,否则为0; the result should look like 结果应该看起来像

ORDER_ID    CUSTOMER_ID PRODUCT_ID  QUANTITY    BRF  LNC    DNR
--------    ----------- ----------  --------    ---  ---    ---
1           1           2           1           1    0      1
2           2           3           2           2    2      2

--UPDATE: NEW QUESTION And can I do multiple pivots on one table; -更新:新问题而且我可以在一张桌子上做多个枢轴操作吗? like I modify my ORDERS table to 就像我将ORDERS表修改为

CREATE TABLE orders
(order_id NUMBER(5) NOT NULL,
 customer_id NUMBER(10) NOT NULL,
 product_id NUMBER(5) NOT NULL,
 adl_q NUMBER(5) NOT NULL,
 kid_q NUMBER(5),
 CONSTRAINT ord_customer_fk FOREIGN KEY (customer_id) REFERENCES customers(customer_id),
 CONSTRAINT ord_product_fk FOREIGN KEY (product_id) REFERENCES products(product_id)
);

and when kid_qty is not zero that means the new corresponding package names will be created and can do pivot on them as well; 当kid_qty不为零时,这意味着将创建新的对应包名称,并且也可以对其进行透视; for the ORDER table like 对于像这样的ORDER表

INSERT  INTO orders (order_id, customer_id, product_id, adl_qty, kid_qty) VALUES (1, 1, 1, 1, 1);
INSERT  INTO orders (order_id, customer_id, product_id, adl_qty, kid_qty) VALUES (2, 2, 2, 2, 1);

you should get the result: 您应该得到结果:

ORDER_ID  CUSTOMER_ID  PRODUCT_ID  ADL_Q  KD_Q  BRF LNC  DNR  KDBRF KDLNC KDDNR
--------  ----------- ----------  ------ ----  ---  ---  ---  -----  ----  ----
       1            1          1       1    1    1    0    1     1     0      0
       2            2          2       2    2    1    2    2     1     0      2 

This pivot query worked for me: 这个pivot查询对我有用:

SQL Fiddle demo SQL Fiddle演示

select order_id, customer_id, product_id, 
       nvl(brf, 0) brf, nvl(lnc, 0) lnc, nvl(dnr, 0) dnr
  from orders 
  left join (
    select product_id, package_name 
      from product_packages 
      join packages using (package_id))
    using (product_id) 
  pivot (max(quantity) for package_name in ('BRF' brf, 'LNC' lnc, 'DNR' dnr))
  order by order_id

Result: 结果:

ORDER_ID CUSTOMER_ID PRODUCT_ID        BRF        LNC        DNR
-------- ----------- ---------- ---------- ---------- ----------
       1           1          2          1          0          1
       2           2          3          2          2          2

You can do this using conditional aggregation (as well as pivot ): 您可以使用条件聚合(以及pivot )来做到这一点:

select o.order_id, o.customer_id, o.product_id, 
       sum(case when p.package_name = 'BRF' then o.quantity else 0 end) as brf,
       sum(case when p.package_name = 'LNC' then o.quantity else 0 end) as lnc,
       sum(case when p.package_name = 'DNR' then o.quantity else 0 end) as dnr
from orders o left join
     product_packages pp
     on o.product_id = pp.product_id left join
     packages p
     on pp.package_id = p.package_id
group by o.order_id, o.customer_id, o.product_id
order by o.order_id;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Oracle SQL更新一个表列以及另一表的值 - Oracle SQL Update one table column with the value of another table SQL-基于一个表与另一个表的联接(第一个表的行值到第二个表的列值) - SQL - Joining one table with another based on ( Row value from first table to Column value of second table) 如何从一个表向另一个Oracle SQL添加随机值 - How to add a random value from one table to another Oracle SQL 如何使用Apex Oracle SQL中另一个表的行值更新表行值? - how to update a table row value using a row value from another table in Apex Oracle SQL? 如何使用Apex Oracle SQL中另一个表的行值触发表行值的更新? - how to trigger an update on a table row value using a row value from another table in Apex Oracle SQL? Oracle - SQL 查询根据列值将数据从一个表分装到另一个表? - Oracle - SQL query to bin the data from one table to another based on a column value? Sql Query的问题(​​从一个表到另一个表的随机添加值) - Issue with Sql Query (randomly adding value from one table to another) Oracle:SQL根据同一表的另一行中的值更新同一表的行 - Oracle: Sql to update row of the same table based on value from another row of the same table 如何使用来自同一表中另一特定行的列值更新sql表中每一行的列 - How to update a column of every row in a sql table with the value of that column from another specific row in the same table 如何将一个表列行分配给 SQL Server 中的另一个表列值 - How to assign one table column row to another table column value in SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM