问题
I have 2 tables...they are basically the same except for the column name in one of them because they deal with 2 different names, though.. the data I want is in columns that have the same name. Pretty much what I want to do... is .they also need to be distinct so I don't count duplicates... I can get them as separate tables... but I can't get them together.. I need them in 1 column because of how it is sent to the C3 code page and how it reads it... the structure has already been previously set.. and there are about 5 other statements that are being executed in this one stored procedure like this (also I wasn't the one who set this up). So if this is possible.. let me know please.. I will also paste the exact code I have if you feel like seeing the real life code.. I just tried to simplify it to make it easier to understand. I'm going to post some images to hopefully explain it a little better.
Image 1 is what is currently set up:
top part is what is stored in tables.. bottom is more of the result it basically runs this code to get the bottom
DECLARE @id INT;
DECLARE @invest nvarchar(50);
SET @id = '7633';
SET @invest = '';
SELECT 'a' + CONVERT(nvarchar, orderfindings.risk_rating) AS cat, COUNT(DISTINCT orderfindings.prnt_id) AS stat
FROM orderheader, orderaudits, orderfindings
WHERE orderheader.id = orderaudits.orderheader_id AND orderaudits.ID = orderfindings.prnt_id
AND orderheader.id = @id AND orderfindings.risk_rating > 0 AND orderaudits.Investor_Name LIKE '%' + @invest + '%'
GROUP BY orderfindings.risk_rating
If I want agencies instead of findings.. just replace it.. agencies and findings are the 2 tables.. they are the pretty much identical column wise... but I want the result together.. I've tried several ways.. but I can't seem to get it image 2- the table at the bottom is more what I'm looking for.. it combines them both into 1:
If an order has a finding or agency or both in it.. then it gets marked as a 1 for that risk rating... if it doesn't.. then 0 for that risk rating.. and then sum them all up to see what I got.. any help is appreciated .. I'll clarify what I can if you don't understand;
Edit:
I've been working with it.. .did this http://pastebin.com/117Z2TVh .. got it to display 2 columns.. but still not the right result... I'm getting a1 = 1...a2 = 1... so its not running through all the orders... or it needs a way to count it... I put a sum at beginning of case statement.. erro because of counts... so I took counts out... didn't really work.. any advice?
回答1:
Have you tried using a UNION clause?
If you are not familiar: http://msdn.microsoft.com/en-us/library/ms180026.aspx
Basically you write two queries returning one of the two columns in question on either side of the UNION and they are returned as one column in the final result set. If the column names are not the same alias them on either side so that they can be combined in the final unioned result set.
回答2:
;WITH finding AS (select oa.id, oa.Risk_Rating, COUNT (orf.Prnt_ID) findingcount FROM orderaudits oa LEFT JOIN orderfindings orf on oa.ID = orf.Prnt_ID --WHERE oa.risk_rating > 0 and oa.Investor_Name like '%' + @invest + '%' GROUP BY oa.id, oa.Risk_Rating), Agency AS (select oa.id, oa.Risk_Rating, COUNT (ora.Prnt_ID) agencycount FROM orderaudits oa LEFT JOIN orderagencies ora on oa.ID = ora.Prnt_ID --WHERE oa.risk_rating > 0 and oa.Investor_Name like '%' + @invest + '%' GROUP BY oa.id, oa.Risk_Rating) /* SELECT 'Finding Only', oa.Risk_Rating, COUNT(oa.id) as a, SUM(CASE WHEN f.findingcount > 0 then 1 ELSE 0 END ) as b FROM orderaudits oa LEFT JOIN finding f ON oa.id = f.id LEFT JOIN orderheader oh ON oh.id = oa.orderheader_id WHERE oh.id = @id and oa.risk_rating > 0 and oa.Investor_Name like '%' + @invest + '%' GROUP BY oa.Risk_Rating
UNION ALL
SELECT 'Agency Only', oa.Risk_Rating, COUNT(oa.id) as a, SUM(CASE WHEN a.agencycount > 0 then 1 ELSE 0 END ) as b FROM orderaudits oa LEFT JOIN Agency a ON oa.id = a.id LEFT JOIN orderheader oh ON oh.id = oa.orderheader_id WHERE oh.id = @id and oa.risk_rating > 0 and oa.Investor_Name like '%' + @invest + '%' GROUP BY oa.Risk_Rating
UNION ALL */ --Together SELECT 'aa' + convert(nvarchar, oa.risk_rating) as cat, SUM(CASE WHEN f.findingcount > 0 OR a.agencycount > 0 then 1 ELSE 0 END ) as b FROM orderaudits oa LEFT JOIN finding f ON oa.id = f.id LEFT JOIN Agency a ON oa.id = a.id LEFT JOIN orderheader oh ON oh.id = oa.orderheader_id WHERE oh.id = @id and oa.risk_rating > 0 and oa.Investor_Name like '%' + @invest + '%' GROUP BY oa.Risk_Rating
来源:https://stackoverflow.com/questions/10130318/checking-2-sql-columns-and-displaying-result-in-1-column-pt2