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 |
|
biogem
Starting Member
13 Posts |
Posted - 2009-12-07 : 16:29:41
|
| SELECT Sites.SiteName,(select COUNT (*) from tenant where tenanttype=2 and ([non-active] is null or [non-active] = 0) and not tenantname like 'CoreSite%' GROUP BY SITE) AS [OFFICE],(select COUNT (*) from tenant where tenanttype IN ( 1, 3) and ([non-active] is null or [non-active] = 0) and not tenantname like 'CoreSite%' GROUP BY SITE) AS [TELECOM],(select COUNT (*) from tenant where tenanttype=4 and ([non-active] is null or [non-active] = 0) and not tenantname like 'CoreSite%' GROUP BY SITE) AS [ANY2]I forgot to add the result set(this is what I need it to look like)FROM Sites INNER JOIN Tenant ON Sites.SiteID = Tenant.Sitegroup by TENANT.site, sitename I can get the results using a temp table, but not via case or ctu.Here's the temp table query.Create Table #tmp1( site char(50), telecom int) insert into #tmp1 (site, telecom)select site, COUNT(*) AS telecom from tenant where (tenanttype=1 or tenanttype=3) and ([non-active] is null or [non-active] = 0) and not tenantname like 'CoreSite%' group by site-- Office Create Table #tmp2( site char(50), office int,)insert into #tmp2 (site, office)select site, COUNT(*) AS office from tenantwhere tenanttype=2 and ([non-active] is null or [non-active] = 0) and not tenantname like 'CoreSite%' group by site-- Any2Create Table #tmp3( site char(50), any2 int) insert into #tmp3 (site, any2)select site, COUNT(*) AS any2 from tenantwhere tenanttype=4 and ([non-active] is null or [non-active] = 0) and not tenantname like 'CoreSite%' group by siteselect s.sitename, t1.telecom, t2.office, t3.any2 from sites s left outer join #tmp1 t1 on t1.site = s.siteid left outer join #tmp2 t2 on t2.site = s.siteid left outer join #tmp3 t3 on t3.site = s.siteidwhere s.Internal is null or s.Internal = 0drop table #tmp1drop table #tmp2drop table #tmp3The result set I need.sitename telecom office any2One Wilshire 295 5 4055 S. Market 113 7 101275 K Street 51 0 12115 NW 22nd Street 32 0 0900 N. Alameda 65 4 21656 McCarthy 31 0 1427 S. LaSalle 60 0 070 Innerbelt 38 0 032 Ave. of the Americas 18 0 112100 Sunrise Valley 60 3 02901 Coronado Dr 0 8 0Coronado Stender Business Park 0 1 0 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-12-10 : 14:10:23
|
| [code]SELECT Sites.SiteName,COUNT (case when tenanttype=2 and ([non-active] is null or [non-active] = 0) and not tenantname like 'CoreSite%' THEN 1 ELSE NULL END) AS [OFFICE],COUNT (case when tenanttype IN ( 1, 3) and ([non-active] is null or [non-active] = 0) and not tenantname like 'CoreSite%' THEN 1 ELSE NULL END) AS [TELECOM],COUNT (case when tenanttype=4 and ([non-active] is null or [non-active] = 0) and not tenantname like 'CoreSite%' THEN 1 ELSE NULL END) AS [ANY2]FROM Sites INNER JOIN Tenant ON Sites.SiteID = Tenant.Sitegroup by sitename [/code] |
 |
|
|
|
|
|
|
|