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 |
vision.v1
Yak Posting Veteran
72 Posts |
Posted - 2010-02-16 : 11:27:50
|
Am having two tables Tableone and Tabletwo, both the tableshave diff. no. of records...I want to get count of both the tables, so i tried like: SELECT edge.source, pf.source, COUNT(edge.source), COUNT(pf.source) FROM Tableone edge JOIN Tabletwo pf ON edge.source = pf.source WHERE edge.SOURCE = 'Source1' GROUP BY edge.source, pf.sourcebut am getting records count as product of two tables...please advise... |
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-02-16 : 11:36:04
|
Here's one waySELECTedge.source,pf.source,edge.cnt, pdf.cntFROM(select source,count(*) as cnt from Tableone group by source) edgeinner join (select source,count(*) as cnt from Tabletwo group by source) pfON edge.source = pf.sourceWHERE edge.source = 'Source1' |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-16 : 12:09:06
|
[code]SELECT source,MAXCOUNT(CASE WHEN cat='A' THEN 1 ELSE NULL END) AS TableoneCount,MAXCOUNT(CASE WHEN cat='B' THEN 1 ELSE NULL END) AS TabletwoCountFROM(SELECT source,'A' as cat from Tableone UNION ALL SELECT source,'B' from Tabletw)tGROUP BY source[/code] EDIT: corrected MAX to COUNT------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-02-16 : 12:23:16
|
Visakh...probably shud have been SUM instead of MAX?SELECT source,SUM(CASE WHEN cat='A' THEN 1 ELSE NULL END) AS TableoneCount,SUM(CASE WHEN cat='B' THEN 1 ELSE NULL END) AS TabletwoCountFROM(SELECT source,'A' as cat from Tableone UNION ALL SELECT source,'B' from Tabletw)tGROUP BY source |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-02-16 : 12:32:35
|
Another way?quote: Originally posted by vision.v1 Am having two tables Tableone and Tabletwo, both the tableshave diff. no. of records...I want to get count of both the tables, so i tried like: SELECT edge.source, pf.source, COUNT(edge.source) over(partition by 1) , COUNT(pf.source) over(partition by 1) FROM Tableone edge JOIN Tabletwo pf ON edge.source = pf.source WHERE edge.SOURCE = 'Source1' GROUP BY edge.source, pf.sourcebut am getting records count as product of two tables...please advise...
PBUH |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-16 : 12:40:31
|
quote: Originally posted by vijayisonly Visakh...probably shud have been SUM instead of MAX?SELECT source,SUM(CASE WHEN cat='A' THEN 1 ELSE NULL END) AS TableoneCount,SUM(CASE WHEN cat='B' THEN 1 ELSE NULL END) AS TabletwoCountFROM(SELECT source,'A' as cat from Tableone UNION ALL SELECT source,'B' from Tabletw)tGROUP BY source
Sorry i meant to put COUNT but put MAX by mistake ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|