Solved

SQL Query

Posted on 2014-11-14
7
241 Views
Last Modified: 2014-11-14
Hi EE,

I need to calculate the number of hours that an item was on a specific location.

My table is as follow :

Location        Item                ReceivingDate                       TransferDate                  
  NA                0123                   2014-10-20 8:00                 NULL
  EE01            0123                    2014-10-20 8:00                2014-10-22 11:04
  RD                0123                   2014-10-20 8:00                  2014-10-23  15:32


I would like as a results to have this :

Location        Item                ReceivingDate                       TransferDate                     DaysInLocation      
  NA                0123                   2014-10-20 8:00                 NULL                                            49
  EE01            0123                    2014-10-20 8:00                2014-10-22 11:04                        27
  RD                0123                   2014-10-20 8:00                  2014-10-23  15:32                     531

I went approximate with the hours... but the idea is :

hours for NA is the numbers of hours from Receiving date to the transferDate of the item to another place (here is EE01)
hours of EE01 is the numbers of hours from the TransferDate to the next (here is RD)
and RD is the last location for item 0123 so we would calculate of to getdate()


so I need a query to give me all info from table but with an extra column ...
note that Im using sql 2008 .. not 2012..


Thanks...!!!
0
Comment
Question by:PhilippeRenaud
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 34

Expert Comment

by:ste5an
ID: 40442850
Please rephrase your question and take a closer look at your sample data.

531 days? Where does this number comes from?
Another problem: How can be the same item at three different locations at the same time?
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 40442861
Sorr my column should be called:  Hours inLocations

since RD is the last location, it means it still there today.

so from getdate() to  2014-10-23  15:32   its like 22 days so like 530 hours, im approximate.


im just displaying the receiving date on all rows Ste5an.  I could put NULL on all rows except "NA" if you want. so you would have only 1 receiving date. for 1 location
0
 
LVL 34

Accepted Solution

by:
ste5an earned 500 total points
ID: 40442902
It still makes no sense.  '2014-10-20 8:00' and '2014-10-23  15:32' are approximatly three days, thus 72 hours.

im just displaying the receiving date on all rows Ste5an.  I could put NULL on all rows except "NA" if you want. so you would have only 1 receiving date. for 1 location
???

When I need to guess: This is a transactional state table, mixed with the inital state. So clean it up.

DECLARE @Sample TABLE
    (
      Location NVARCHAR(255) ,
      Item NVARCHAR(255) ,
      TransferDate DATETIME
	  -- with a UNIQUE constraint on Item, TransferDate
    );

SET DATEFORMAT YMD;

INSERT  INTO @Sample
        ( Location, Item, TransferDate )
VALUES  ( 'NA  ', '0123', '2014-10-20 8:00' ),
        ( 'EE01', '0123', '2014-10-22 11:04' ),
        ( 'RD  ', '0123', '2014-10-23 15:32' );

WITH    Ordered
          AS ( SELECT   * ,
                        ROW_NUMBER() OVER ( PARTITION BY Item ORDER BY S.TransferDate ) AS RN
               FROM     @Sample S
             )
    SELECT  O1.Item ,
            O1.TransferDate ,
            O1.Location ,
            DATEDIFF(HOUR, O1.TransferDate, COALESCE(O2.TransferDate, GETDATE())) AS HoursInLocation
    FROM    Ordered O1
            LEFT JOIN Ordered O2 ON O2.Item = O1.Item
                                    AND O2.RN = O1.RN + 1
    ORDER BY Item ,
            O1.TransferDate;

Open in new window


After building this sample: Now I know where your 530 came from.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 40443051
I think you do not understand.

Look I have remove 1 column it might help you to understand. I  have removed ReceivingDate and only kept TransferDate.


RD is the final location for the item 0123.
it has been transfered on  2014-10-23  15:32.
so The number of days the item has been into RD  (since TODAY it it still in that location otherwise you would have a fourth row), since today is 11/14/2014 11:59   the numbers of days from Today to 2014-10-23  15:32.  is not 3 days. its something like 31.         31 days  * 24  is 744


Sorry I put 531 earlier.  but do you get the idea ? Your 72 hours you calculated was from NA to RD  but I dont need to know that time because you have skipped 1 location.  I need the time for each without skipping.



Location        Item                 TransferDate                     HoursInLocation      
  NA                0123              2014-10-20 8:00                       49
  EE01            0123              2014-10-22 11:04                      27
  RD                0123              2014-10-23  15:32                    744



it is roughly 49 hours in row NA because it was transfered on 10-22 11:04 into EE01  so from  10-20 8:00 to 10-22-11:04  this is the time I want.

it is roughly 27 hours in row EE01 because it was transfered on 10-23 15:32 into RD  so from  10-22 11:04 to 10-23 15:32  this is the time I want.

it is roughly 744 hours in row RD because it was NEVER transfered elsewhere so from   2014-10-23  15:32 to GETDATE()  this is the time I want.
0
 
LVL 34

Expert Comment

by:ste5an
ID: 40443091
Have you run my sample.. why is its result wrong??

Result pane
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 40443100
It seems good yes, I just ran it.  My god im way out with my 744 hours   yes 531 seems good. sorry

your code seems ok
0
 
LVL 1

Author Closing Comment

by:PhilippeRenaud
ID: 40443117
thanks
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Display field if column exists 7 56
Estimating my database size 7 47
I am new to using JSON in SQL Server 2 53
how to use ROW_NUMBER() correctly 8 40
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question