SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 distinct column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

peace
Constraint Violating Yak Guru

373 Posts

Posted - 01/08/2014 :  22:52:01  Show Profile  Reply with Quote
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

938 Posts

Posted - 01/08/2014 :  23:27:18  Show Profile  Reply with Quote
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

India
158 Posts

Posted - 01/08/2014 :  23:35:24  Show Profile  Reply with Quote
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

Edited by - VeeranjaneyuluAnnapureddy on 01/08/2014 23:36:11
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 01/09/2014 :  04:56:31  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000