Link to home
Create AccountLog in
Avatar of ASPDEV
ASPDEV

asked on

SQL Query to retrieve Records

Hello Experts,

I need to run a report based on the Datetime range in the tables.

Two tables TXTN and LEVELS.

TxTN
ID         DT                                   Tank                                   Qty
001        01/01/2011 15:00             001                                     20.1
001        01/01/2011 16:00             001                                     25.2
002       01/01/2010  12:00             001                                     20.5
001       01/02/2011  10:00             001                                     20.1
001       01/02/2011  19:00             001                                     25.2
002       01/02/2010   11:00            001                                     20.5


LEVEL

TANK    DT                              LEVEL
001       01/01/2011 12:00        1000
001       01/02/2011  15:00         750


I need to get records from TXTN table on the Last two datetimes it got levels. For instance if I got the LEVEL for Tank#001 as 1000 on 01/01/2011 at 12:00 PM, then I need to take that as start datetime and till the next level I received for the same tank on 01/02/2011 15:00. So the total quantity for tank#001 is 65.4 gallons.And Insert into another table Totalizer as Qty = 65.4 and datetime as 01/02/2011 since the end datetime is 01/02/2011. It means the TXTN's happen for the whole day(01/02/2011).


Totalizer

Tank       Date                    CNT
001       01/02/2011             65.4    

Here the Date  field is End Date and CNT is Sum of Quantity we got.

Business Logic :
I need to get records in the TXTN table from the last time it got record entry and till the next record. And also I need to make sure If I have two levels for the same tank .
For Instance:
LEVEL
TANK    DT                                        LEVEL
001       01/01/2011 12:00                1000
001       01/02/2011  15:00                 750
001       01/02/2011  19:00                 550

Then I need to get the last record datetime as the End Datetime here it is 01/02/2011 19:00. and the Quantity will be 90.6. And Insert into  Totalizer table.
Totalizer:

Tank       Date                    CNT
001       01/02/2011           90.6.  

It's a  reconciliation report, so I need to adjust all the totalizers.

Thanks.
Avatar of jasonduan
jasonduan
Flag of United States of America image

try this:

;WITH LEVELS_CTE(Tank, DTMin, DTMax)
AS
(
    SELECT Tank, MIN(DT) AS DTMin, MAX(DT) AS DTMax
    FROM [LEVELS]
    GROUP BY Tank
)

INSERT INTO [Totalizer](Tank,  [Date], CNT)
SELECT l.Tank, l.DTMax AS DT, SUM(t.Qty) AS CNT
FROM [LEVELS_CTE] l
INNER JOIN [TXTN] t ON t.Tank = l.Tank AND t.DT BETWEEN l.DTMin AND l.DTMax
GROUP BY l.Tank, l.DTMax
Avatar of ASPDEV
ASPDEV

ASKER

Jasonduan,

It does not work on the Min date, and also it find all the SUM of all Quantity. I need to know only last two datetime in the Levels table for that Tank.
ASKER CERTIFIED SOLUTION
Avatar of jasonduan
jasonduan
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Arggh! @jasonduan: my apologies, I was typing away and didn't see you already posted.  You remembered about the EndDT being in the final select too, so mine is less correct without having l.[EndDT] in the SELECT list.  Anyway, sorry for duplicative post.
Avatar of ASPDEV

ASKER

Thanks.