检查2个sql列并在1列中显示结果

时间:2012-04-12 14:35:45

标签: sql logic case

好吧..我有2个表......它们完全相同,除了其中一个的列名,因为它们处理2个不同的“公司”,尽管...我想要的数据是在具有相同的列中name.pretty很多我想要做的......如果tableA.Col2是>那么 0或tableB.Col2是> 0然后1 ..否则0 ..他们也需要区别......我可以把它们作为单独的表...但我不能把它们放在一起..我需要它们在1列中因为它如何被发送到C3代码页以及它是如何读取的...结构已经预先设置了......并且在这个存储过程中有大约5个其他正在执行的语句(也是我不是设置的那个)这个)。所以,如果这是可能的..我知道请...我也会粘贴我所拥有的确切代码,如果你想看到真实生活代码..我只是试图简化它,使其更容易理解。

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

我希望这更有意义......这是一项奇怪的任务

2 个答案:

答案 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