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 |
|
harlingtonthewizard
Constraint Violating Yak Guru
352 Posts |
Posted - 2011-07-05 : 22:54:08
|
| Count to include SiteName with zero counts within where clause. If I run this it returns the count for entire db include those with zero. OK so far.Declare @StartDateRange AS DateTimeDeclare @EndDateRange AS DateTimeSET @StartDateRange = ('2011-07-06 00:00:00.000')SET @EndDateRange = ('2011-07-07 00:00:00.000')SELECT SiteName, TransmitterType, Count(EventID) AS EventCountFROM SiteDetailsLEFT JOIN EventDetails ON EventDetails.SiteID = SiteDetails.SiteIDGROUP BY SiteName, TransmitterTypeORDER BY EventCount Desc, SiteName AscSiteName TransmitterType EventCountBravo FastTrace2 275494Lima FastTrace2 176588India FastTrace2 133340Foxtrot FastTrace2 701506 FastTrace2 19586Kilo FastTrace2 3392 FastTrace2 05 FastTrace2 0But if I run this it returns only those with counts greater than zero within the search.Declare @StartDateRange AS DateTimeDeclare @EndDateRange AS DateTimeSET @StartDateRange = ('2011-07-06 00:00:00.000')SET @EndDateRange = ('2011-07-07 00:00:00.000')SELECT SiteName, TransmitterType, Count(EventID) AS EventCountFROM SiteDetailsLEFT JOIN EventDetails ON EventDetails.SiteID = SiteDetails.SiteIDWHERE LocalStartTime Between @StartDateRange and @EndDateRangeGROUP BY SiteName, TransmitterTypeORDER BY EventCount Desc, SiteName AscSiteName TransmitterType EventCountLima FastTrace2 1516 FastTrace2 147India FastTrace2 147What I need is this.Lima FastTrace2 1516 FastTrace2 147India FastTrace2 1472 FastTrace2 05 FastTrace2 0Bravo FastTrace2 0Foxtrot FastTrace2 0Kilo FastTrace2 0 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-07-05 : 23:00:11
|
[code]SELECT SiteName, TransmitterType, Count(CASE WHEN LocalStartTime Between @StartDateRange and @EndDateRange THEN EventID END) AS EventCountFROM SiteDetailsLEFT JOIN EventDetails ON EventDetails.SiteID = SiteDetails.SiteIDGROUP BY SiteName, TransmitterTypeORDER BY EventCount Desc, SiteName Asc[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
harlingtonthewizard
Constraint Violating Yak Guru
352 Posts |
Posted - 2011-07-05 : 23:36:49
|
| Thankyou for your support. This has provided an answer for the question I asked. I now need to add some more filters but cannot move them up into the case can I? With these additional filters it no longer works as expected as it only includes the sitename for the causes which have an event at some point in time. So if sites called Delta, Echo and Bravo and all but Bravo have a event called '1' in the db and Delta is outside the time range and Echo within then you get. Bravo does not get included because it does not have one of these events in the db.Echo 10 Delta 0SELECT SiteName, TransmitterType, Count(CASE WHEN LocalStartTime Between @StartDateRange and @EndDateRange THEN EventID END) AS EventCountFROM SiteDetailsLEFT JOIN EventDetails ON EventDetails.SiteID = SiteDetails.SiteIDWHERE SiteDetails.SiteID IN (Select Param From fn_MVParam (@SiteID,','))AND SiteDetails.TransmitterType IN (Select Param From fn_MVParam (@TransmitterType+,','))AND Cause IN (Select Param From fn_MVParam (@Cause,','))GROUP BY SiteName, TransmitterTypeORDER BY EventCount Desc, SiteName Asc |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-07-05 : 23:51:33
|
quote: I now need to add some more filters but cannot move them up into the case can I?
It depends what do you want. If you want to count based on the filter / condition than you can place it inside the CASE WHEN.quote: With these additional filters it no longer works as expected as it only includes the sitename for the causes which have an event at some point in time. So if sites called Delta, Echo and Bravo and all but Bravo have a event called '1' in the db and Delta is outside the time range and Echo within then you get. Bravo does not get included because it does not have one of these events in the db.
Not very sure what are you trying to achieve here. Perhaps you can post some sample data & expected result to clarify KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
harlingtonthewizard
Constraint Violating Yak Guru
352 Posts |
Posted - 2011-07-06 : 00:01:10
|
| What I need is the SiteId and TransmitterType filters where they are but the Cause filter up inside the case statement. When I put the code there I get.Msg 130, Level 15, State 1, Line 9Cannot perform an aggregate function on an expression containing an aggregate or a subquery.(1 row(s) affected)'SELECT SiteName, TransmitterType,Count(CASE WHEN LocalStartTime Between @StartDateRange and @EndDateRange AND Cause IN (Select Param From fn_MVParam ('''+@Cause+''','','')) THEN EventID END) AS EventCountFROM [' + @DBName + '].dbo.SiteDetails LEFT JOIN[' + @DBName + '].dbo.EventDetails ON [' + @DBName + '].dbo.EventDetails.SiteID = [' + @DBName + '].dbo.SiteDetails.SiteIDWHERE SiteDetails.SiteID IN (Select Param From fn_MVParam ('''+@SiteID+''','',''))AND SiteDetails.TransmitterType IN (Select Param From fn_MVParam ('''+@TransmitterType+''','',''))AND (SiteDetails.IsDeleted = ''False'')GROUP BY SiteName, TransmitterType ' |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-07-06 : 00:21:02
|
try something like this; with cte as (SELECT SiteName, TransmitterType,(CASE WHEN LocalStartTime Between @StartDateRange and @EndDateRange AND Cause IN (Select Param From fn_MVParam ('''+@Cause+''','','')) THEN EventID END) AS EventCountFROM [' + @DBName + '].dbo.SiteDetails LEFT JOIN[' + @DBName + '].dbo.EventDetails ON [' + @DBName + '].dbo.EventDetails.SiteID = [' + @DBName + '].dbo.SiteDetails.SiteIDWHERE SiteDetails.SiteID IN (Select Param From fn_MVParam ('''+@SiteID+''','',''))AND SiteDetails.TransmitterType IN (Select Param From fn_MVParam ('''+@TransmitterType+''','',''))AND (SiteDetails.IsDeleted = ''False''))select SiteName, TransmitterType, count(EventCount) as EventCountfrom cteGROUP BY SiteName, TransmitterType KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
harlingtonthewizard
Constraint Violating Yak Guru
352 Posts |
Posted - 2011-07-06 : 00:57:15
|
| Thankyou for your additional support. This works well. |
 |
|
|
|
|
|
|
|