Is it possible to use an if clause in select / group by statement? Or another solution?
Using SQLServer 2008
I currently have a function that implements functionality as shown below (though there are variables in the function I just filled them in here to give a better idea of what the inputs might be like).
I was wondering if there is a way when you define the @id
as null (currently used in the Coalesce on v.ID) to match everything as if you hadn't defined it in the select / group by clauses?
SELECT
'ALL' as GeogType,
CAST(v.AdmissionOn 开发者_运维知识库as date) as dtAdmission,
(DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) as timeInterval,
CAST((DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * (24/1) as nvarchar) + ':00' as fromTime,
CAST((DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * (24/1) + (24/1) as nvarchar) + ':00' as toTime,
v.ID,
COUNT(*) as nVisits
FROM dbo.Table1 v INNER JOIN dbo.Table2 t ON v.FSLDU = t.FSLDU5
WHERE v.AdmissionOn >= '01-01-2010' AND v.AdmissionOn < DATEADD(day,1,'02-01-2010')
AND v.ID = Coalesce(@id,ID)
GROUP BY
CAST(v.AdmissionOn as date),
(DATEPART(hour,CAST(AdmissionOn as time)))/(24/1),
(DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * (24/1),
(DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * (24/1) + (24/1),
v.ID
ORDER BY 2,3,4
It will return a result set similar to this when the @id
is null:
ALL 2010-01-01 0 0:00 24:00 7 4
ALL 2010-01-01 0 0:00 24:00 4 54
ALL 2010-01-01 0 0:00 24:00 0 2303
ALL 2010-01-01 0 0:00 24:00 3 412
ALL 2010-01-01 0 0:00 24:00 6 57
ALL 2010-01-01 0 0:00 24:00 2 64
ALL 2010-01-01 0 0:00 24:00 5 5
ALL 2010-01-01 0 0:00 24:00 1 103
But I would like it to instead be:
ALL 2010-01-01 0 0:00 24:00 ? 3002
I am not sure what would go in the ? column, possibly something like 1,2,3,4,5,6,7 ?
Is this at all possible by something such as a case in the select / group by clause or a stored procedure that I do not know about? If not I can just create a new function but I was hoping there would be a way to do this by just inputting the @id
as null.
Change the Group By to:
GROUP BY CAST(v.AdmissionOn as date),
(DATEPART(hour,CAST(AdmissionOn as time)))/(24/1),
(DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * (24/1),
(DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * (24/1) + (24/1),
Case @Id When Null Then 0 Else Id End
What I had edited this to but will not work by itself is:
GROUP BY CAST(v.AdmissionOn as date),
(DATEPART(hour,CAST(AdmissionOn as time)))/(24/1),
(DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * (24/1),
(DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * (24/1) + (24/1),
@Id
-- Either of these by themselves will causes an error because Id
in the select statement is not in the Group By. Either can be correctted by making the select consistent with the Group By
SELECT 'ALL' as GeogType, CAST(v.AdmissionOn as date) as dtAdmission,
(DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) as timeInterval,
CAST((DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) *
(24/1) as nvarchar) + ':00' as fromTime,
CAST((DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) *
(24/1) + (24/1) as nvarchar) + ':00' as toTime,
Case @Id When Null Then 0 Else Id End Id,
COUNT(*) as nVisits
FROM dbo.Table1 v
JOIN dbo.Table2 t
ON v.FSLDU = t.FSLDU5
WHERE v.AdmissionOn >= '01-01-2010'
AND v.AdmissionOn < DATEADD(day,1,'02-01-2010')
AND v.ID = Coalesce(@id,ID)
GROUP BY CAST(v.AdmissionOn as date),
(DATEPART(hour,CAST(AdmissionOn as time)))/(24/1),
(DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * (24/1),
(DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * (24/1) + (24/1),
Case @Id When Null Then 0 Else Id End
or
SELECT 'ALL' as GeogType, CAST(v.AdmissionOn as date) as dtAdmission,
(DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) as timeInterval,
CAST((DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) *
(24/1) as nvarchar) + ':00' as fromTime,
CAST((DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) *
(24/1) + (24/1) as nvarchar) + ':00' as toTime,
@Id Id, COUNT(*) as nVisits
FROM dbo.Table1 v
JOIN dbo.Table2 t
ON v.FSLDU = t.FSLDU5
WHERE v.AdmissionOn >= '01-01-2010'
AND v.AdmissionOn < DATEADD(day,1,'02-01-2010')
AND v.ID = Coalesce(@id,ID)
GROUP BY CAST(v.AdmissionOn as date),
(DATEPART(hour,CAST(AdmissionOn as time)))/(24/1),
(DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * (24/1),
(DATEPART(hour,CAST(AdmissionOn as time)))/(24/1) * (24/1) + (24/1),
@Id
GROUP BY @id
instead of GROUP BY v.id
, and replace v.id
with @id
in the SELECT statement.
精彩评论