简体   繁体   English

将多个结果合并为列而不是行

[英]Combine multiple results as columns, not rows

I need to perform a number of counts on various tables in the database 我需要对数据库中的各个表进行大量计数
and I would like to combine those counts into a single result. 我想将这些计数合并为一个结果。

Conider the following queries: 考虑以下查询:

SELECT 100 As SomeCount
SELECT 200 As SomeOtherCount
SELECT 300 As YetAnotherCount

If I combine them using UNION , each of the results will be a row in the final result: 如果我使用UNION合并它们,则每个结果将在最终结果中排成一行:

SELECT 100 As SomeCount
UNION
SELECT 200 As SomeOtherCount
UNION
SELECT 300 As YetAnotherCount

Output: 输出:

> SomeCount
> --------- 
> 100 
> 200 
> 300

What I want instead is 我想要的是

> SomeCount | SomeOtherCount | YetAnotherCount
> --------------------------------------------
>   100     |      200       |     300

The only other way I could think off to 'name' the results is using something like this: 我可以想到的“命名”结果的唯一其他方法是使用类似以下内容的方法:

SELECT 'SomeCount' As Name, 100 As Value
UNION ALL
SELECT 'SomeOtherCount', 200
UNION ALL
SELECT 'YetAnotherCount', 300

In which case the result looks like: 在这种情况下,结果如下所示:

>     Name          |      Value
> ---------------------------------
> 'SomeCount'       |       100
> 'SomeOtherCount'  |       200
> 'YetAnotherCount' |       300

Is there a way to get the results I want, or is the last method the way to go? 有没有一种方法可以得到我想要的结果,还是最后一种方法呢?

I should mention the queries above are very simple in order to explain the core problem. 我应该提到上面的查询非常简单,以解释核心问题。 In reality two queries that need to be combined might look like this: 实际上,需要组合的两个查询可能看起来像这样:

Query 1: 查询1:

SELECT Count(Id) As UndeliveredSms
FROM
(
 SELECT Id
 FROM IncomingSms
 WHERE Id NOT IN (SELECT IncomingSmsId
                  FROM DeliveryAttempt
                  WHERE Status = 'Delivered' OR Status = 'FailedPermanently')
 )

Query 2: 查询2:

SELECT Count(Id) As UndeliveredEMail FROM
(
 SELECT Id
 FROM IncomingEMail
 WHERE Id NOT IN (SELECT IncomingEMailId
                  FROM DeliveryAttempt
                  WHERE Status = 'Delivered' OR Status = 'FailedPermanently')
)

Wrapping these in another SELECT statement does not work with SQlite. 将它们包装在另一个SELECT语句中不适用于SQlite。

Using the last method in the examples does work, and I might go with that solution unless it's a bad idea: 使用示例中的最后一种方法确实可行,除非有坏主意,否则我可以选择该解决方案:

SELECT 'UndeliveredSms' As Name,  Count(Id) As Value
FROM
(
 SELECT Id
 FROM IncomingSms
 WHERE Id NOT IN (SELECT IncomingSmsId
                  FROM DeliveryAttempt
                  WHERE Status = 'Delivered' OR Status = 'FailedPermanently')
 )

UNION

SELECT 'UndeliveredEMail', Count(Id) FROM
(
 SELECT Id
 FROM IncomingEMail
 WHERE Id NOT IN (SELECT IncomingEMailId
                  FROM DeliveryAttempt
                  WHERE Status = 'Delivered' OR Status = 'FailedPermanently')
)

Which results in something like this: 结果是这样的:

>     Name           |     Value
> ---------------------------------
> UndeliveredEMail   |       82
> UndeliveredSms     |       0

And of course, in reality, there are a lot more things to count 当然,实际上,还有很多事情要数

You should be able to use a CROSS JOIN between the queries: 您应该能够在查询之间使用CROSS JOIN:

SELECT *
FROM
(
  SELECT Count(Id) As UndeliveredSms
  FROM
  (
   SELECT Id
   FROM IncomingSms
   WHERE Id NOT IN (SELECT IncomingSmsId
                    FROM DeliveryAttempt
                    WHERE Status = 'Delivered' OR Status = 'FailedPermanently')
  )
)
CROSS JOIN
(
  SELECT Count(Id) As UndeliveredEMail FROM
  (
   SELECT Id
   FROM IncomingEMail
   WHERE Id NOT IN (SELECT IncomingEMailId
                    FROM DeliveryAttempt
                    WHERE Status = 'Delivered' OR Status = 'FailedPermanently')
  )
);

See SQL Fiddle with Demo 参见带有演示的SQL Fiddle

I don't understand the use of the UNION in your queries, but perhaps I'm misunderstanding. 我不理解在您的查询中使用UNION ,但也许我误会了。 Not in a subset union not in all = not in a subset... 不在子集中并集不是全部=不在子集中...

Seems like it can be simplified to the following: 似乎可以简化为以下内容:

select *
from
(
  SELECT Count(Id) As UndeliveredSms
  FROM IncomingSms
  WHERE Id NOT IN (SELECT IncomingSmsId
                    FROM DeliveryAttempt
                    WHERE Status = 'Delivered' OR Status = 'FailedPermanently')
) t, 
(
  SELECT Count(Id) As UndeliveredEMail 
  FROM IncomingEMail
  WHERE Id NOT IN (SELECT IncomingEMailId
                    FROM DeliveryAttempt
                    WHERE Status = 'Delivered' OR Status = 'FailedPermanently')
  ) t2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM