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
我希望这更有意义......这是一项奇怪的任务
答案 0 :(得分:1)
您可以使用其他级别的子查询将它们包含在摘要结果中 就像你在这里的例子一样。
Select cat , SUM(stat) as stat
FROM
(
YOUR FIRST QUERY
UNION ALL
YOUR SECOND SECOND QUERY
) AS subquery
GROUP BY cat
答案 1 :(得分:1)
试试这个:
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