Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
s_anr
Yak Posting Veteran
81 Posts |
Posted - 2009-10-20 : 17:00:47
|
| Hi I have a query which i have put in a while loop. There are two problems for which i'm seeking help :1. The while loop shows the correct result but it does it as a separate query for each loop executed. I'd like all the results in a single output 2. How can i loop so that it shows today, yesterday, day b4 yesterdays date as the loop executes so if i execute it for two loops by setting @intflag <=2, the query will be executed two times :First result2009-10-19 00:00:00.000 I 1 1 0 32009-10-19 00:00:00.000 P 0 0 3 42009-10-19 00:00:00.000 R 0 0 3 6second result :2009-10-19 00:00:00.000 car 1 1 0 32009-10-19 00:00:00.000 bus 0 0 3 42009-10-19 00:00:00.000 truck 0 0 3 6also it just shows the current date.i'd like the results like the one given below 2009-10-19 00:00:00.000 car 1 1 0 32009-10-19 00:00:00.000 bus 0 0 3 42009-10-19 00:00:00.000 truck 0 0 3 62009-10-18 00:00:00.000 car 1 1 0 32009-10-18 00:00:00.000 bus 0 0 3 42009-10-18 00:00:00.000 truck 0 0 3 6DECLARE @intFlag INTDECLARE @dt DATETIMESET @intFlag = 1SET @dt = getdate()WHILE (@intFlag <=30)BEGINSELECT * FROM( SELECTCONVERT(datetime, CONVERT(varchar(10), @dt, 111), 111)[reference date],d.sym [fuel_type],a.type [vehicle_type]FROM call_req aleft outer join mdb..fuel d on a.fuel = d.enum left outer join mdb..contact c on a.group = c.contact where dateadd (hh,-7,dateAdd(ss, a.open_date, '19700101')) <= @dt - @intFlagand (dateadd (hh,-7,dateAdd(ss, a.close_date, '19700101')) > @dtor a.status != 'filled')and c.last_name like 'BMW') AS abcPIVOT(COUNT(fuel_type)FOR fuel_type IN([petrol],[diesel],[natural],[hybrid])) AS PvtSET @intFlag = @intFlag + 1END |
|
|
s_anr
Yak Posting Veteran
81 Posts |
Posted - 2009-10-21 : 11:41:18
|
| I think i made it too complicated to suit my needs. I have simplified it now. It prints the results of each loop separately. How do I get the results in a single table.. mabe be in a temp tableDECLARE @intFlag INTDECLARE @intFlag2 INTDECLARE @dt DATETIMEDECLARE @ref_date DATETIMESET @intFlag = 0SET @intFlag2 = 0SET @dt = getdate()SET @ref_date = @date - @intflagWHILE (@intFlag <=30)BEGINSELECT col1,col2,col3 from testtablewhere dateadd (hh,-7,dateAdd(ss, open_date, '19700101')) <= @dt - @intFlagand (dateadd (hh,-7,dateAdd(ss, close_date, '19700101')) > @dt - intflagSET @intFlag = @intFlag + 1END |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-10-21 : 11:48:31
|
| where do you get first and second result from? are they different queries? |
 |
|
|
|
|
|
|
|