sql - 将两个单独查询的结果合并到两列中

标签 sql sql-server

我正在处理两个 sql 查询,我想将它们合并为一个查询,因此第一个查询的结果将在第一列中,第二个查询的结果将在第二列中。我怎样才能做到这一点?

我试过 union,但它会将结果放在两行中......那不是我想要的......

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
(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

您试图通过 order by statement 实现什么目标?

关于sql - 将两个单独查询的结果合并到两列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30987822/

相关文章:

sql - 条件排名

SQL:查找包含列表用户的团队

sql - 如何使用嵌套关联查询 STI 驱动的模型?

java - 如何从 native SQL 查询中绑定(bind)实体对象中的 @Transient 字段

sql - MySQL 重复——如何指定两条记录实际上不重复?

sql - 用于将数据从一个表插入到具有相同列名的另一个表的存储过程

c# - 如何以编程方式关闭与数据库的所有现有连接

sql - 如何获取从当前日期算起的最后 12 个月以及截至检索到的上个月 1 日的额外天数

sql - 从 SQL Server 中的 VARCHAR 中删除非数字字符的最快方法

sql - 如何在没有行时使 SSIS 包失败