Discussions
Categories
- 385.5K All Categories
- 4.9K Data
- 2.5K Big Data Appliance
- 2.4K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
Get Difference Between Timestamps Using Pivot
Answers
-
Here is the sample you asked for. I will have to post the output tomorrow. The results would follow the same concept as my original table above in the first post, but the numbers are going to be different.
insert into z_itemtable values ('1', 'PENCIL', '2019-08-26 05.00.00.000000000 PM', 'A');
insert into z_itemtable values ('2', 'PENCIL', '2019-08-27 05.00.00.000000000 PM', 'B');
insert into z_itemtable values ('3', 'PENCIL', '2019-08-28 05.00.00.000000000 PM', 'C');
insert into z_itemtable values ('4', 'PENCIL', '2019-08-29 05.00.00.000000000 PM', 'D');
insert into z_itemtable values ('5', 'PENCIL', '2019-08-30 05.00.00.000000000 PM', 'E');
insert into z_itemtable values ('6', 'PEN', '2019-08-28 05.00.00.000000000 PM', 'F');
insert into z_itemtable values ('7', 'PEN', '2019-08-29 05.00.00.000000000 PM', 'G');
insert into z_itemtable values ('8', 'PEN', '2019-08-30 05.00.00.000000000 PM', 'H');
insert into z_itemtable values ('9', 'ERASER', '2019-08-29 05.00.00.000000000 PM', 'I');
insert into z_itemtable values ('10', 'ERASER', '2019-08-30 05.00.00.000000000 PM', 'J');
insert into z_itemtable values ('11', 'PENCIL', '2019-08-27 05.00.00.000000000 PM', 'K');
insert into z_itemtable values ('12', 'ERASER', '2019-08-29 05.00.00.000000000 PM', 'L');
select * from z_itemtable;
insert into z_mastertable values ('1', 'DT');
insert into z_mastertable values ('2', 'DT');
insert into z_mastertable values ('3', 'SB');
insert into z_mastertable values ('4', 'SB');
insert into z_mastertable values ('5', 'SB');
insert into z_mastertable values ('6', 'EA');
insert into z_mastertable values ('7', 'EA');
insert into z_mastertable values ('8', 'EA');
insert into z_mastertable values ('9', 'WS');
insert into z_mastertable values ('10', 'WS');
insert into z_mastertable values ('11', 'DT');
insert into z_mastertable values ('12', 'DT');
select * from z_mastertable;
insert into z_storetable values ('DT', 'DOWNTOWN');
insert into z_storetable values ('SB', 'SUBURB');
insert into z_storetable values ('EA', 'EAST');
insert into z_storetable values ('WS', 'WEST');
select * from z_storetable;
insert into z_itemtabledetail values ('A', '2019-09-30 05.00.00.000000000 PM');
insert into z_itemtabledetail values ('B', '2019-09-15 05.00.00.000000000 PM');
insert into z_itemtabledetail values ('C', '2019-09-12 05.00.00.000000000 PM');
insert into z_itemtabledetail values ('D', '2019-09-26 05.00.00.000000000 PM');
insert into z_itemtabledetail values ('E', '2019-09-22 05.00.00.000000000 PM');
insert into z_itemtabledetail values ('F', '2019-09-03 05.00.00.000000000 PM');
insert into z_itemtabledetail values ('G', '2019-09-01 05.00.00.000000000 PM');
insert into z_itemtabledetail values ('H', '2019-09-11 05.00.00.000000000 PM');
insert into z_itemtabledetail values ('I', '2019-09-22 05.00.00.000000000 PM');
insert into z_itemtabledetail values ('J', '2019-09-14 05.00.00.000000000 PM');
insert into z_itemtabledetail values ('K', '2019-09-02 05.00.00.000000000 PM');
insert into z_itemtabledetail values ('L', '2019-09-07 05.00.00.000000000 PM');
select * from z_itemtabledetail;
-
CREATE TABLE "Z_ITEMTABLE"
( "MASTERID" VARCHAR2(20 BYTE),
"ITEMNAME" VARCHAR2(20 BYTE),
"ITEMARRIVALDATE" TIMESTAMP (6),
"DETAILID" VARCHAR2(20 BYTE)
)
CREATE TABLE "Z_ITEMTABLEDETAIL"
( "ID" VARCHAR2(20 BYTE),
"DATESOLD" TIMESTAMP (6)
)
CREATE TABLE "Z_MASTERTABLE"
( "ID" VARCHAR2(20 BYTE),
"STORECODE" VARCHAR2(20 BYTE)
)
CREATE TABLE "Z_STORETABLE"
( "CODE" VARCHAR2(20 BYTE),
"STORENAME" VARCHAR2(20 BYTE)
)
-
Partial...not working query
select * from (
select st.storename, it.itemname, trunc(it.itemarrivaldate) as datesoldz
from z_itemtable it
join z_mastertable m on it.masterid = m.id
join z_storetable st on m.storecode = st.code
join z_itemtabledetail itd on it.detailid = itd.id
where it.itemarrivaldate >= to_date('2019-08-26', 'yyyy-mm-dd') and it.itemarrivaldate < to_date('2019-08-31', 'yyyy-mm-dd')
)
pivot
(
count(*)
for datesoldz in (DATE '2019-08-26',
DATE '2019-08-27',
DATE '2019-08-28',
DATE '2019-08-29',
DATE '2019-08-30')
)
where storename in ('DOWNTOWN', 'SUBURB', 'EAST', 'WEST')
group by storename, itemname, 3
order by storename;
-
Hi,
976563 wrote:Here is the sample you asked for. I will have to post the output tomorrow. The results would follow the same concept as my original table above in the first post, but the numbers are going to be different.insert into z_itemtable values ('1', 'PENCIL', '2019-08-26 05.00.00.000000000 PM', 'A');insert into z_itemtable values ('2', 'PENCIL', '2019-08-27 05.00.00.000000000 PM', 'B');...
Many of those INSERT statements fail on my system
Don't try to insert strings, such as '2019-08-26 05.00.00.000000000 PM', into TIMESTAMP columns, such as z_itemtable.itemarrivaldate.
-
Ahh, the INSERTs worked when I ran them. I am on Oracle Database 11g Release 11.2.0.4.0 - 64bit.
The desired output would be like...
STORE ITEMS 2019-08-26 Sum 2019-08-26 Avg 2019-08-27 Sum 2019-08-27 Avg 2019-08-28 Sum 2019-08-28 Avg 2019-08-29 Sum 2019-08-29 Avg 2019-08-30 Sum 2019-08-30 Avg DOWNTOWN ERASER 0 0 0 0 0 0 9 9 0 0 DOWNTOWN PENCIL 35 35 25 12.5 0 0 0 0 0 0 EAST PEN 0 0 0 0 6 6 3 3 12 12 SUBURB PENCIL 0 0 0 0 15 15 28 28 23 23 Row 1 ==> Only 1 found, the value is 9 because the (datesold minus itemarrivaldate [2019-09-07 minus 2019-08-29 ==> 9]). This becomes the SUM and AVG.
Row 2 ==> 2019-08-26 only 1 account is found. The value is 35 because (datesold minus itemarrivaldate [2019-09-30 minus 2019-08-26 ==> 35]). This becomes the SUM and AVG.
Row 2 ==> 2019-08-27 there are 2 accounts. 1st account (2019-09-15 minus 2019-08-27 ==> 19). 2nd account (2019-09-02 minus 2019-08-27 ==> 6). 19 + 6 => 25 SUM, 12.5 AVG
Row 3 and 4 follow the same logic as Row 1 because there was only 1 row found.
-
Hi,
976563 wrote:Ahh, the INSERTs worked when I ran them. I am on Oracle Database 11g Release 11.2.0.4.0 - 64bit.The desired output would be like...STOREITEMS2019-08-26 Sum
2019-08-26 Avg2019-08-27 Sum2019-08-27 Avg2019-08-28 Sum2019-08-28 Avg2019-08-29 Sum2019-08-29 Avg2019-08-30 Sum2019-08-30 AvgDOWNTOWNERASER0000009900DOWNTOWNPENCIL35352512.5000000EASTPEN000066331212SUBURBPENCIL0000151528282323Row 1 ==> Only 1 found, the value is 9 because the (datesold minus itemarrivaldate [2019-09-07 minus 2019-08-29 ==> 9]). This becomes the SUM and AVG.Row 2 ==> 2019-08-26 only 1 account is found. The value is 35 because (datesold minus itemarrivaldate [2019-09-30 minus 2019-08-26 ==> 35]). This becomes the SUM and AVG.Row 2 ==> 2019-08-27 there are 2 accounts. 1st account (2019-09-15 minus 2019-08-27 ==> 19). 2nd account (2019-09-02 minus 2019-08-27 ==> 6). 19 + 6 => 25 SUM, 12.5 AVGRow 3 and 4 follow the same logic as Row 1 because there was only 1 row found.Earlier, you were interested in a subtotal showing all items from each store, as well as the individual store/item combinations. Now you've changed the requirements to show only the individual items; is that right?
Here's one way to do that:
WITH data_to_pivot AS( SELECT st.storename , it.itemname , TRUNC (it.itemarrivaldate) AS datearrived , TRUNC (itd.datesold) - TRUNC (it.itemarrivaldate) AS ndays FROM z_itemtable it JOIN z_mastertable m ON it.masterid = m.id JOIN z_storetable st ON m.storecode = st.code JOIN z_itemtabledetail itd ON it.detailid = itd.id WHERE it.itemarrivaldate >= TO_DATE ('2019-08-26', 'yyyy-mm-dd') AND it.itemarrivaldate < TO_DATE ('2019-08-31', 'yyyy-mm-dd') AND st.storename IN ('DOWNTOWN', 'SUBURB', 'EAST', 'WEST'))SELECT *FROM data_to_pivotPIVOT ( SUM (ndays) AS sum , AVG (ndays) AS avg FOR datearrived IN ( DATE '2019-08-26' AS d_2019_08_26 , DATE '2019-08-27' AS d_2019_08_27 , DATE '2019-08-28' AS d_2019_08_28 , DATE '2019-08-29' AS d_2019_08_29 , DATE '2019-08-30' AS d_2019_08_30 ) )ORDER BY storename, itemname;
The results I get are a little different from what you posted:
D_2019 D_2019 D_2019 D_2019 D_2019 D_2019 D_2019 D_2019 D_2019 D_2019 _08_26 _08_26 _08_27 _08_27 _08_28 _08_28 _08_29 _08_29 _08_30 _08_30STORENAME ITEMNAME _SUM _AVG _SUM _AVG _SUM _AVG _SUM _AVG _SUM _AVG--------- -------------------- ------ ------ ------ ------ ------ ------ ------ ------ ------ ------DOWNTOWN ERASER 9 9DOWNTOWN PENCIL 35 35 25 12.5EAST PEN 6 6 3 3 12 12SUBURB PENCIL 15 15 28 28 23 23WEST ERASER 24 24 15 15
I get a row for storename='WEST'. Did you mean to include that row in your desired results?
As posted, the solution shows NULLs for missing data. You can use NVL to get 0's instead, if you want them.
-
Frank Kulash wrote:Earlier, you were interested in a subtotal showing all items from each store. Now you've changed the requirements to show only the individual items; is that right?
Thank you for your help. Yes, the requirements changed this morning after I spoke with the user (it seems to be very common from my experience unfortunately). I greatly appreciate your help. When I re-wrote the query, I used a WITH, but then I had all of my dates as a single column that went down by each row and not across as column headers. The PIVOT you used differed much from mine, but I will decode your code to understand it better. Thanks again!
-
Frank Kulash wrote:I get a row for storename='WEST'. Did you mean to include that row in your desired results?As posted, the solution shows NULLs for missing data. You can use NVL to get 0's instead, if you want them.
Sorry, I realized I did not answer your other question. Yes, I had that in the result, but it looks like I did not include it in my desired result set.
-
Hi,
976563 wrote:...When I re-wrote the query, I used a WITH, but then I had all of my dates as a single column that went down by each row and not across as column headers. The PIVOT you used differed much from mine, but I will decode your code to understand it better. Thanks again!
To understand pivots, it often helps to run the exact same query without the pivot, like this:
WITH data_to_pivot AS( SELECT st.storename , it.itemname , TRUNC (it.itemarrivaldate) AS datearrived , TRUNC (itd.datesold) - TRUNC (it.itemarrivaldate) AS ndays FROM z_itemtable it JOIN z_mastertable m ON it.masterid = m.id JOIN z_storetable st ON m.storecode = st.code JOIN z_itemtabledetail itd ON it.detailid = itd.id WHERE it.itemarrivaldate >= TO_DATE ('2019-08-26', 'yyyy-mm-dd') AND it.itemarrivaldate < TO_DATE ('2019-08-31', 'yyyy-mm-dd') AND st.storename IN ('DOWNTOWN', 'SUBURB', 'EAST', 'WEST'))SELECT *FROM data_to_pivot/* PIVOT ( SUM (ndays) AS sum , AVG (ndays) AS avg FOR datearrived IN ( DATE '2019-08-26' AS d_2019_08_26 , DATE '2019-08-27' AS d_2019_08_27 , DATE '2019-08-28' AS d_2019_08_28 , DATE '2019-08-29' AS d_2019_08_29 , DATE '2019-08-30' AS d_2019_08_30 ) ) */ORDER BY storename, itemname;
Make sure you understand why this gets the results it does before trying to understand the pivot.