1

好的..我有 2 个表...除了其中一个中的列名之外,它们完全相同,因为它们处理 2 个不同的“公司”,但是..我想要的数据在具有相同名称的列中。漂亮我想做的很多...如果 tableA.Col2 > 0 或 tableB.Col2 > 0 然后 1..else 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

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

4

2 回答 2

1

您可以使用另一个级别的子查询将它们包含在摘要结果中,就像您在此处的示例一样。

Select cat , SUM(stat) as stat
FROM
(
  YOUR FIRST QUERY
  UNION ALL
  YOUR SECOND SECOND QUERY 
) AS subquery
GROUP BY cat
于 2012-04-12T14:51:41.767 回答
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
于 2012-04-12T16:13:29.313 回答