Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

join/merge sql statements 1

Status
Not open for further replies.

lorca

Technical User
Dec 20, 2005
64
GB
I have a SQL statement (i have a report in visual studio that creates a dataset) and calls this

sqlstmt1
Code:
SELECT WARD_NAME, COUNT(*) AS CustomerCount, WARD_ID
FROM  dbo.vwLPGWards
GROUP BY WARD_NAME, WARD_ID
ORDER BY COUNT(*), WARD_NAME, WARD_ID

this is based on a view (shown below) but the code above basically returns a row each for each ward name and the total number of calls raised for that ward.
This works great.

Code:
SELECT DISTINCT 
TOP 100 PERCENT WARDS.WARD_NAME, ADDR.CUSTOMER, 
WARDS.WARD_ID FROM WARDS INNER JOIN
WARDUPRN ON WARDS.WARD_ID = WARDUPRN.WARD_ID INNER JOIN
CADTABLE ON WARDUPRN.UPRN = CADTABLE.UPRN INNER JOIN
ADDR ON CADTABLE.OSADDESSP = ADDR.UPRN
WHERE (ADDR.EDATE IS NULL)
ORDER BY ADDR.CUSTOMER

i have another dataset from this sql statement (shown below) which returns, as above, a row for each ward with a grand total of uprns's within that ward this time.

I'd like to get a percentage, dividing the total number of calls raised for the ward (as above sqlstmt1) by the total number of uprn's in that ward. (below sqlstmt2)

does that make sense ?
thanks for any help....................

sqlstmt2
Code:
SELECT WARDS.WARD_NAME, Count(WARDS.WARD_NAME) as WardCount, WARDS.WARD_ID FROM WARDUPRN INNER JOIN
WARDS ON crmuser.CTS_WARD_UPRN.WARD_ID = WARDS.WARD_ID 
GROUP BY WARDS.WARD_NAME, WARDS.WARD_ID
ORDER BY COUNT(WARDS.WARD_NAME), WARDS.WARD_NAME
 
Make both select statements views. See "Create View" in BOL.

Then Join the two views on Ward_id and Ward_Name. I would think you could just join on Ward_id but it does not look like it is unique based on your select statements.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top