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
 General SQL Server Forums
 New to SQL Server Programming
 distinct column

Author  Topic 

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2014-01-08 : 22:52:01
i get the amount from table.

select ID, Amount1 from tableA

ID Name Amount1
1 Mary 10
1 John 10
1 Jane 10

select ID, Amount2 from tableA

ID Name Amount2
1 Mary 25
1 John 25

then i combine it into one.

ID Name Amount1 Amount2
1 Mary 10 25
1 John 10 25
1 Jane 10 0

how can i combine into like this?

NoOfPeople ID Amount1 Amount2
3 1 10 25

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2014-01-08 : 23:27:18
It's not clear why you get the numbers you do.

- Why do they all have ID of 1 ? What's the PK?
- Why do you have different # of rows in the queries to tableA?
- What if there are different combinations of ID, name, amount etc?
Go to Top of Page

VeeranjaneyuluAnnapureddy
Posting Yak Master

169 Posts

Posted - 2014-01-08 : 23:35:24
SELECT (SELECT COUNT(a.name)FROM Table1 AS a)AS 'NoOfPeople'
, a.Id
, Amount1
, Amount2
FROM Table1 AS a
INNER JOIN Table2 AS b
ON a.id=b.id
GROUP BY a.Id,Amount1,Amount2

veeranjaneyulu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-01-09 : 04:56:31
if they're all in same table this is enough

SELECT COUNT(1) OVER (PARTITION BY ID) AS NumberOfPeople,
ID,
Name,
Amount1,
Amount2
FROM TableA


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -