简体   繁体   中英

Oracle - Converting a column to rows and getting a sum

Im trying to convert a column to rows and get a sum of the items ordered. Below is how the data is currently in the database. Notice how the DollarDays store gives the indication that not all stores have ordered all items.

Store         ItemNumber    Description    Ordered
----          ----------    ----------     ---------
WallyMart     10021         Corn           10         
J-Mart        10021         Corn           4
Big-H Foods   10021         Corn           32
WallyMart     20055         Beans          11         
J-Mart        20055         Beans          3
Big-H Foods   20055         Beans          21
DollarDays    50277         Onions         48

This is my goal below. It looks to be pretty much grouping by ItemNumber

ItemNumber    Description    WallyMart    J-Mart    Big-H Foods  DollarDays  TotalOrdered
----------    -----------    ---------    ------    -----------  ----------  ------------
10021         Corn           10           4         32           0            46
20055         Beans          11           3         21           0            35
50277         Onions         0            0         0            48           48

When I try PIVOT this is shortened example of what I get. Im totally lost.

ItemNumber    Description    WallyMart    J-Mart    Big-H Foods  DollarDays  
----------    -----------    ---------    ------    -----------  ----------
10021         Corn           10           Null      Null         Null
10021         Corn           Null         4         Null         Null
10021         Corn           Null         Null      32           Null
10021         Corn           Null         Null      Null         0

FYI I am of course a beginner and a student so please forgive me I haven't posted exactly right. Any help is appreciated.

Try this:

select 
itemnumber, description, 
coalesce("WallyMart",0) as "WallyMart",
coalesce("J-Mart",0) as "J-Mart",
coalesce("Big-H Foods",0) as "Big-H Foods",
coalesce("DollarDays",0) as "DollarDays",
coalesce("WallyMart",0) + coalesce("J-Mart",0) + coalesce("Big-H Foods",0) +     coalesce("DollarDays",0) as "Total"
from
(select * from stores) s
pivot
(max(ordered)
 for store in 
 ('WallyMart' as "WallyMart",
  'J-Mart' as "J-Mart",
  'Big-H Foods' as "Big-H Foods",
  'DollarDays' as "DollarDays")) p

To explain a bit, the PIVOT clause consists of 2 parts - the aggregation and the list of values used for this aggregation. For your case, the aggregation used is MAX , since it looks like one store can have only one record for a particular product. If that is not so, SUM would be the right function to use. Likewise, since we want store-wise details, and not product-wise, we specify the distinct values of the store column in the list.

COALESCE is used to default null values in the ordered column to 0. Finally, we add the 4 derived columns (after coalescing to 0) to get the total value.

SQLFiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM