[英]Combining the results from two separate queries in to two columns
我正在处理两个SQL查询,我想将它们合并为一个,因此第一个查询的结果将在第一列中,而第二个查询的结果将在第二列中。 我该如何实现?
我尝试了联合,但它将结果分成两行..那不是我想要的...
select count(*) as ColumnA from Inventory i, Sale s where i.vin=s.vin and i.condition='new'
select count(*) as ColumnB from Inventory i, Sale s where i.vin=s.vin and i.condition='used' order by 1 desc;
您可以通过稍有不同的查询同时获得两个计数,这将比组合两个查询的效率略高:
SELECT
SUM(CASE WHEN i.condition = 'new' THEN 1 ELSE 0 END),
SUM(CASE WHEN i.condition = 'used' THEN 1 ELSE 0 END)
FROM
Inventory i
JOIN
Sale s ON i.vin = s.vin
您可以在一个查询中使用两个子查询合并,如下所示:
select
(select count(*) from Inventory i, Sale s where i.vin=s.vin and i.condition='new') as New,
(select count(*) from Inventory i, Sale s where i.vin=s.vin and i.condition='used') as Used
您试图通过按订单实现什么?
一种简单的方法是:
select
(select count(*) as ColumnA from Inventory i, Sale s where i.vin=s.vin and i.condition='new') as newCount,
(select count(*) as ColumnB from Inventory i, Sale s where i.vin=s.vin and i.condition='used') as usedCount
您可以使用另一个SELECT
合并结果:
SELECT
(select count(*) from Inventory i, Sale s where i.vin=s.vin and i.condition='new') as ColumnA,
(select count(*) from Inventory i, Sale s where i.vin=s.vin and i.condition='used') as ColumnB
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.