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 |
|
cheilig
Starting Member
3 Posts |
Posted - 2005-05-12 : 16:57:38
|
| I have the following query that returns extra header info in the results when there is more than 1 day with no records in the query. I'm trying to figure out how to run this and not get the additional header information. Here is the query:SET NOCOUNT ONDECLARE @datestart intDECLARE @datestop intSET @datestart = 1SET @datestop = 2WHILE(SELECT count(1)FROM [tables]WHERE (a.Start > DATEADD(d, @datestart, DATEDIFF(d, 0, GETDATE())))AND (pp.Class = 17) AND (a.Start < DATEADD(d, @datestop, DATEDIFF(d, 0,GETDATE())))) = 0BEGINSET @datestart = @datestart + 1SET @datestop = @datestop + 1SELECT [statement]FROM [tables]WHERE (a.Start > DATEADD(d, @datestart, DATEDIFF(d, 0, GETDATE()))) AND(pp.Class = 17) AND (a.Start < DATEADD(d, @datestop, DATEDIFF(d, 0, GETDATE())))ORDER BY pp.LastENDHere's the output:Date Name Class --------------------Date Name Class----------- ---------Date Name Class----------- -----------------------------------May 16 2005 David L Afor 17May 16 2005 Tina M Coll 17May 16 2005 Dan O Doer 17 |
|
|
mk_garg20
Constraint Violating Yak Guru
343 Posts |
Posted - 2005-05-12 : 19:28:40
|
| If you can post table structure and data. I think someone can suggest you better solution that looping.cheersmk_garg |
 |
|
|
cheilig
Starting Member
3 Posts |
Posted - 2005-05-13 : 09:54:53
|
| This query returns records correctly when there is only one day of norecords. It is when I hit two days of zero records that I get an additionalheader row with no data that messes it up. I set the @datestart and@datestop at 1 and 2 respectfully to look for tomorrows records, if zero thenincrement by 1 and rerun till it finds a count. Once that is satisfied thenit needs to run the second part of the query to retun data.------------ Here is the entire query with table names changed -----------------SET NOCOUNT ONDECLARE @datestart intDECLARE @datestop intSET @datestart = 1SET @datestop = 2WHILE(SELECT count(*)FROM dbo.TableA a LEFT OUTER JOIN dbo.TableAC ac ON a.ApptChainId = ac.ApptChainId LEFTOUTER JOIN dbo.TableASET aset ON a.ApptSetId = aset.ApptSetId INNERJOIN dbo.TableDFF dff ON a.FacilityId = dff.DoctorFacilityIdINNER JOIN dbo.TablePP pp ON a.OwnerId = pp.PatientProfileId INNER JOIN dbo.TableDFF dfr ON a.ResourceId = dfr.DoctorFacilityIdLEFT OUTER JOINdbo.TableAT at ON a.ApptTypeId = at.ApptTypeId LEFT OUTER JOINdbo.TableMLFC mlfc ON pp.FinancialClassMId = mlfc.MedListsId LEFT OUTERJOINdbo.TableC c ON a.CasesId = c.CasesId LEFT OUTER JOINdbo.TableMLFCC mlfcc ON c.FinancialClassMId = mlfcc.MedListsIdWHERE (a.ApptStart > DATEADD(d, @datestart, DATEDIFF(d, 0,GETDATE()))) AND (pp.FinancialClassMId = 1739) AND (a.ApptStart < DATEADD(d,@datestop, DATEDIFF(d, 0, GETDATE())))) = 0BEGINSET @datestart = @datestart + 1SET @datestop = @datestop + 1SELECT CAST(a.ApptStart AS varchar(11)) AS Date, ISNULL(pp.First + ' ','') + ISNULL(pp.Middle + ' ', '') + pp.Last AS Name, dfr.ListName ASResourceName, pp.SSN AS SSN, pp.PatientId, ISNULL(at.Name,'Unknown') AS ApptType, pp.Phone1 AS Phone1, CAST(pp.Birthdate ASvarchar(11)) AS Birthdate, pp.FinancialClassMIdFROM dbo.TableA a LEFT OUTER JOIN dbo.TableAC ac ON a.ApptChainId = ac.ApptChainId LEFTOUTER JOIN dbo.TableASET aset ON a.ApptSetId = aset.ApptSetId INNERJOIN dbo.TableDFF dff ON a.FacilityId = dff.DoctorFacilityIdINNER JOIN dbo.TablePP pp ON a.OwnerId = pp.PatientProfileId INNER JOIN dbo.TableDFF dfr ON a.ResourceId = dfr.DoctorFacilityIdLEFT OUTER JOINdbo.TableAT at ON a.ApptTypeId = at.ApptTypeId LEFT OUTER JOINdbo.TableMLFC mlfc ON pp.FinancialClassMId = mlfc.MedListsId LEFT OUTERJOINdbo.TableC c ON a.CasesId = c.CasesId LEFT OUTER JOINdbo.TableMLFCC mlfcc ON c.FinancialClassMId = mlfcc.MedListsIdWHERE (a.ApptStart > DATEADD(d, @datestart, DATEDIFF(d, 0, GETDATE())))AND (pp.FinancialClassMId = 1739) AND (a.ApptStart < DATEADD(d, @datestop,DATEDIFF(d, 0, GETDATE())))ORDER BY pp.LastEND |
 |
|
|
mr_mist
Grunnio
1870 Posts |
Posted - 2005-05-13 : 10:18:35
|
| You shouldn't need to loop through like that. Join your main query to a sub query that selects counts for ranges of dates, and return all the dates within the lowest range that has a count > 0.-------Moo. :) |
 |
|
|
cheilig
Starting Member
3 Posts |
Posted - 2005-05-13 : 10:29:49
|
quote: Originally posted by mr_mist You shouldn't need to loop through like that. Join your main query to a sub query that selects counts for ranges of dates, and return all the dates within the lowest range that has a count > 0.-------Moo. :)
I need the query to look at the following day for records that match the query. If there are no records I need it to increment the variables and run the query again. It needs to continue this until it gets a count. At that point then run the query that gives me data for that day. Not visualizing how your answer would work and not use a WHILE statement. |
 |
|
|
|
|
|
|
|