sql - Use a variable instead of UNION query -
i trying write query variable criteria. there anyway below without using union, instead using list of dates in table?
select '2014-03-03' snapshotdate ,t2.pre_ppi_cycle ,sum(isnull(t2.pre_ppi_balance, 0))as pre_balance ,count(isnull(t2.account_number, 0)) pre_volume ,row_number() on (partition account_number order account_number, snapshotdate desc) row #table_1 t2 t2.snapshotdate <= '2014-03-03' group t2.snapshotdate, t2.pre_ppi_cycle, account_number union select '2014-03-04' snapshotdate ,t2.pre_ppi_cycle ,sum(isnull(t2.pre_ppi_balance, 0))as pre_balance ,count(isnull(t2.account_number, 0)) pre_volume ,row_number() on (partition account_number order account_number, snapshotdate desc) row #ppi_3 t2 t2.snapshotdate <= '2014-03-04' group t2.snapshotdate, t2.pre_ppi_cycle, account_number
edit
thanks far - clarify - code have.
select t2.capture_date ,t1.pre_ppi_cycle ,sum(isnull(t1.pre_ppi_balance, 0))as pre_balance ,count(isnull(t1.account_number, 0)) pre_volume ,row_number() on (partition t1.account_number order t1.account_number, t1.snapshotdate desc) row #ppi_3 t1 inner join (select child.capture_date #dates child child.capture_date <= '2014-03-04') t2 on t2.capture_date = t1.snapshotdate group t2.capture_date, t1.snapshotdate, t1.pre_ppi_cycle, t1.account_number
i've mentioned below in comments i'm looking change date '2014-03-04' list of dates kept in table #dates. example if date <= '2014-03-03' returns 32 records. if date <= '2014-03-04' returns 55 records. total query return 87 records. that's part i'm struggling with!
thanks everyone's far - feel we're making progress!
thanks, sam
declare @mydate smalldatetime set @mydate = '2014-03-03'; select '2014-03-03' snapshotdate ,t2.pre_ppi_cycle ,sum(isnull(t2.pre_ppi_balance, 0))as pre_balance ,count(isnull(t2.account_number, 0)) pre_volume ,row_number() on (partition account_number order account_number, snapshotdate desc) row #table_1 t2 inner join ( select t2.id, mydate #table_1 t2 t2.snapshotdate <= @mydate ) x on x.mydate = t2.snapshotdate , x.id = t2.id group t2.snapshotdate, t2.pre_ppi_cycle, account_number
or
select '2014-03-03' snapshotdate ,t2.pre_ppi_cycle ,sum(isnull(t2.pre_ppi_balance, 0))as pre_balance ,count(isnull(t2.account_number, 0)) pre_volume ,row_number() on (partition account_number order account_number, snapshotdate desc) row #table_1 t2 t2.snapshotdate in ( select mydate #table_1 t2 t2.snapshotdate <= '2014-03-03' ) group t2.snapshotdate, t2.pre_ppi_cycle, account_number
based on latest update. following bit easier:
select t1.capture_date ,t1.pre_ppi_cycle ,sum(isnull(t1.pre_ppi_balance, 0))as pre_balance ,count(isnull(t1.account_number, 0)) pre_volume ,row_number() on (partition t1.account_number order t1.account_number, t1.snapshotdate desc) row #ppi_3 t1 t1.capture_date in ( select child.capture_date #dates child child.capture_date <= '2014-03-04') group t1.capture_date, t1.snapshotdate, t1.pre_ppi_cycle, t1.account_number
Comments
Post a Comment