Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Get Difference Between Timestamps Using Pivot

User_OMEF8Aug 30 2019 — edited Sep 5 2019

Hello,

I am on Oracle 11g and I am trying to formulate a SQL query to produce results using a PIVOT (not sure if it is actually necessary).  The report that I want to create has different "Groups" of information and I was wondering if this could be achieved with a single query.  I know I can do this if I run multiple queries, then combine the information, then do some other operations to get what I need, but there are too many steps if I do it this way.

Here is what I want to do...  I have many stores and different items sold at each store.  For my purposes, I am only concerned with certain stores and certain items.  I also have a timestamp field that tells me when the item arrived and when it was sold.  I need to calculate the difference between the 2 timestamp fields (sold minus arrived, what is the difference between?)

  • Stores - varchar2
  • Items - varchar2
  • Arrived - Timestamp
  • Sold - Timestamp

Below is a sample output of what I want the report to look like.  Note that the column headers (2019-08-01, 2019-08-02, and 2019-08-03) would be dynamic (so I could have more dates across), but effectively, this would be the date difference of when the ITEM was sold.  For example, DOWNTOWN, PENCIL, 2019-08-01 ==> 5.  This is the calculated total number of days for all PENCILS sold that date.

If I had 5 PENCILS and they all had an Arrival Date of 2019-07-31 and a Sold Date of 2019-08-01.  This would equal 5 days.

If I had 3 PENCILS...

  • PENCIL 1 Arrival Date 2019-07-31 and Sold Date 2019-08-01 ==> 1 Day
  • PENCIL 2 Arrival Date 2019-07-30 and Sold Date 2019-08-01 ==> 2 Days
  • PENCIL 3 Arrival Date 2019-07-30 and Sold Date 2019-08-01 ==> 2 Days
  • Total Days ==> 5 days

STOREITEMS
2019-08-01
2019-08-022019-08-03
DOWNTOWNPEN123
DOWNTOWNPENCIL567
DOWNTOWNERASER213
2.67 (AVERAGE DATE DIFFERENCE)3 (AVERAGE DATE DIFFERENCE)4.33 AVERAGE DATE DIFFERENCE
4.SUBURBPEN987
SUBURBPENCIL321
SUBURBERASER456
5.33 AVERAGE DATE DIFFERENCE5 AVERAGE DATE DIFFERENCE4.67 AVERAGE DATE DIFFERENCE

Here is my attempt SQL query...

select * from (

     select sn.storenamecolumn, i.itemcolumn, trunc(d.solddatecolumn)

     from itemtable i

     join storetable s on i.id = s.id

     join storenametable sn on s.storeid = sn.storename

     join itemdatetable d on i.itemid = d.id

     where i.itemcolumn in ('PEN', 'PENCIL', 'ERASER')

     and trunc(d.solddatecolumn) >= to_date('2019-08-01', 'yyyy-mm-dd') and trunc(d.solddatecolumn) < to_date('2019-08-04', 'yyyy-mm-dd')

     )

     pivot

     (

     count(*)

     for trunc(solddatecolumn) in ('2019-08-01', '2019-08-02', '2019-08-03')   <============ error missing IN keyword

     )

where storenamecolumn in ('DOWNTOWN', 'SUBURB')

order by storenamecolumn;

I keep getting the error above and I believe it has to do with the trunc() method.  If I remove the trunc() method from both the "for trunc()" and the SELECT Statement, then it will run, but the results are not right because I am only looking for a date and not the entire "timestamp".  Any suggestions on how I can achieve my desired results?  Thanks.

Also, is it possible to calculate the total average for each day and put it at the bottom of each Date column?  I edited the table above.

This post has been answered by Frank Kulash on Sep 5 2019
Jump to Answer

Comments

unknown-7404

What problem are you trying to solve?


You can use the ALL_IND_COLS view to get info about TABLE_NAME, COLUMN_NAME, COLUMN_POSITION, etc

http://docs.oracle.com/cd/B28359_01/server.111/b28320/statviews_1090.htm

>

ALL_IND_COLUMNS

ALL_IND_COLUMNS describes the columns of indexes on all tables accessible to the current user.

>

1028309

I am new in oracle and like to write to query to compare two index columns and columns order. I have no idea have to do that???

unknown-7404

e27d74c6-5fff-404b-9698-78029354fb67 wrote:

I am new in oracle and like to write to query to compare two index columns and columns order. I have no idea have to do that???

What problem are you trying to solve? What do you plan to do with the information when you have it?

And why are you the one trying to solve it if you have no experience in SQL?

1028309

I like to run this query and depends on this query result I like to do some programming in my C# application. If I would get the query result like yes or no, it would be enough for me to use it in my C# code.

1028309

I used but not worked

select

   table_name,

   index_name,

   column_name

from

   dba_ind_columns

where

   table_owner='XXXX'

order by

   table_name,

   column_position


minus


select

   table_name,

   index_name,

   column_name

from

   dba_ind_columns

where

   table_owner='XXXX'

order by

   table_name,

   column_position


unknown-7404

All you are saying is 'I want to do something based on the result'.

Well, DOH! It's obvious you want to do 'something'. I've never needed to compare indexes like that in over 25 years of SQL.

Since it is clear you don't want to tell us WHAT PROBLEM you have and why you need to do this you'll need to wait for someone else to try to help you.

1028309

you don't need to shout out "WHAT PROBLEM" please!.  I already know that you voluntarily helping me out.  Obviously I need it for something...But Instead of learning why I need it isn't be nice to help. Thanks anyway.

Umesh P

As per your explanation it seems that you want to compare data for two indexes,if all the records for two index matches or not .

Using simple sql query it seems tedious, your index are not of on identical schema. One index made up of composite keys other is not.

One scenario of comparison possible is when one out of two columns in composite key index is empty.

At the same time there could be index with more than two columns as you have shown above.

Index are nothing but set of columns . So comparison will be possible if all index have similar schema.

Etbin

Maybe (if column names might be different)

select *

  from (select t.owner,t.table_name,i.index_name,i.column_name,i.column_position,

               i.descend,t.data_type,t.data_length,t.data_precision,t.data_scale,t.nullable

          from all_ind_columns i,

               all_tab_columns t

         where t.table_name = 'STUDENT_A'

           and t.table_name = i.table_name

           and t.owner = i.table_owner

           and t.column_name = i.column_name

       ) a

       full outer join

       (select t.owner,t.table_name,i.index_name,i.column_name,i.column_position,

               i.descend,t.data_type,t.data_length,t.data_precision,t.data_scale,t.nullable

          from all_ind_columns i,

               all_tab_columns t

         where t.table_name = 'STUDENT_B'

           and t.table_name = i.table_name

           and t.owner = i.table_owner

           and t.column_name = i.column_name

       ) b

    on a.column_position = b.column_position

   and a.data_type = b.data_type

   and a.data_length = b.data_length

   and lnnvl(a.data_precision != b.data_precision)

   and lnnvl(a.data_scale != b.data_scale)

   and a.nullable = b.nullable

   and a.descend = b.descend

Regards

Etbin

1 - 9

Post Details

Added on Aug 30 2019
19 comments
977 views