Reputation: 141
ok..i have 2 tables...they are exactly the same except for the column name in one of them because they deal with 2different "companies", though..the data i want is in columns that have the same name.pretty much what i want to do...is if tableA.Col2 is > 0 or tableB.Col2 is > 0 then 1..else 0..they also need to be distinct...i can get them as separate tables...but i cant 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 statments that are being executed in this one stored procedure like this (also i wasnt the one who set this up). So if f 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.
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
UNION ALL
SELECT 'a' + CONVERT(nvarchar, orderagencies.risk_rating) AS cat, COUNT(DISTINCT orderagencies.prnt_id) AS stat
FROM orderheader, orderaudits, orderagencies
WHERE orderheader.id = orderaudits.orderheader_id AND orderaudits.ID = orderagencies.prnt_id
AND orderheader.id = @id AND orderagencies.risk_rating > 0 AND orderaudits.Investor_Name LIKE '%' + @invest + '%'
GROUP BY orderagencies.risk_rating
-------------------
/*
Results from 1st statement
cat | stat
-----------
a1 | 4
-----------
a2 | 5
-----------
Results from 2nd statement
cat | stat
-----------
a1 | 3
-----------
a2 | 2
-----------
I pretty much want
Results from new awesome statement
cat | stat
-----------
a1 | 5
-----------
a2 | 5
-----------
I think those are the numbers that would be accurate
Simple version--- if tableA.Col2 is > 0 or tableB.Col2 is > 0 then 1 else 0
|Table A | Table B | result of query
| col_1 col2 | col_1 col2 |
--------------------------------------------------------------------
1 | 1 5 | 1 3 | 1
2 | 1 45 | 1 0 | 1
3 | 1 0 | 1 0 | 0
4 | 1 0 | 1 3 | 1
so a1 would be 3...4 records in the tables..but only 3 of them matter because row 3 has 0
whatevers in col 2 for both tables..there might be 100 records that have 1 for col 1..there
might only be 1
i hope this makes more sense...it is kinda a weird task
Upvotes: 1
Views: 289
Reputation: 6594
Try this:
DECLARE @id INT;
DECLARE @invest nvarchar(50);
SET @id = '7633';
SET @invest = '';
SELECT Findings.cat [cat],
CASE WHEN Findings.stat > 0 OR Agencies.stat > 0 THEN 1 ELSE 0 END [stat]
FROM (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) Findings
INNER JOIN (SELECT 'a' + CONVERT(nvarchar, orderagencies.risk_rating) AS cat, COUNT(DISTINCT orderagencies.prnt_id) AS stat
FROM orderheader, orderaudits, orderagencies
WHERE orderheader.id = orderaudits.orderheader_id AND orderaudits.ID = orderagencies.prnt_id
AND orderheader.id = @id AND orderagencies.risk_rating > 0 AND orderaudits.Investor_Name LIKE '%' + @invest + '%'
GROUP BY orderagencies.risk_rating) Agencies
ON Findings.cat = Agencies.cat
Upvotes: 1
Reputation: 5233
You can use another level of subquery to include them in summary result like from your example here.
Select cat , SUM(stat) as stat
FROM
(
YOUR FIRST QUERY
UNION ALL
YOUR SECOND SECOND QUERY
) AS subquery
GROUP BY cat
Upvotes: 1