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 |
|
visa123
Yak Posting Veteran
54 Posts |
Posted - 2010-10-30 : 07:39:43
|
| HiI have below scenariotable1Germany StuttgartGermany WuerzburgGermany BerlinGermany DresdenGermany DuesseldorfGermany ErfurtGermany Frankfurt a.Mtable21 Stuttgart Germany2 Stuttgart Germany3 Duesseldorf Germany4 Stuttgart Germany5 Frankfurt a.M GermanyI have to get likeStuttgart Stuttgart(3) ALL(5)Wuerzburg Wuerzburg(0) ALL(5)Berlin Berlin(0) ALL(5) Dresden Dresden(0) ALL(5)Duesseldorf Duesseldorf(1) ALL(5)Erfurt Erfurt(0) ALL(5)Frankfurt a.M Frankfurt a.M(1) ALL(5)I have put the below querySelect rm.Location,rm.Location + '(' + cast(Count(hj.identifier) as varchar) + ')' AS LocationCount,'ALL' + '(' + cast(sum(count(hj.identifier)) over() as varchar) + ')' AS AllCount from rm LEFT OUTER JOIN table2 hj on rm.Country=hj.Country AND rm.Location = hj.Location1 where rm.Country like '%germany%' GROUP BY rm.LocationBut it returns only Duesseldorf Duesseldorf(1) ALL(5)Frankfurt a.M Frankfurt a.M(1) ALL(5)Stuttgart Stuttgart(3) ALL(5)But i need the below resultStuttgart Stuttgart(3) ALL(5)Wuerzburg Wuerzburg(0) ALL(5)Berlin Berlin(0) ALL(5) Dresden Dresden(0) ALL(5)Duesseldorf Duesseldorf(1) ALL(5)Erfurt Erfurt(0) ALL(5)Frankfurt a.M Frankfurt a.M(1) ALL(5)can anyone help me pls for the above scenarioThanks Visa.G |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-10-30 : 09:16:36
|
| Not sure what you want but maybe something likeselect t1.Location, t1.Country, sum(case when t2.Country is null then 0 else 1 end), t3.totfrom table1left join table2 t2on t1.Country=t2.Country AND t1.Location = t2.Location1cross join (select tot = count(*) from table2) t3group by t1.Location, t1.Country==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|