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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Joining two tables and getting two tables count

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 tables
have 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.source


but 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 way
SELECT
edge.source,
pf.source,
edge.cnt,
pdf.cnt
FROM
(select source,count(*) as cnt from Tableone group by source) edge
inner join (select source,count(*) as cnt from Tabletwo group by source) pf
ON edge.source = pf.source
WHERE edge.source = 'Source1'
Go to Top of Page

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 TabletwoCount
FROM
(SELECT source,'A' as cat from Tableone
UNION ALL
SELECT source,'B' from Tabletw
)t
GROUP BY source
[/code]
EDIT: corrected MAX to COUNT
------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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 TabletwoCount
FROM
(SELECT source,'A' as cat from Tableone
UNION ALL
SELECT source,'B' from Tabletw
)t
GROUP BY source

Go to Top of Page

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 tables
have 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.source


but am getting records count as product of two tables...
please advise...





PBUH
Go to Top of Page

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 TabletwoCount
FROM
(SELECT source,'A' as cat from Tableone
UNION ALL
SELECT source,'B' from Tabletw
)t
GROUP BY source




Sorry i meant to put COUNT but put MAX by mistake

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -