如何将这两个查询的结果并排作为一个结果

时间:2015-09-24 18:38:02

标签: sql tsql

数据库是Adventureworks 2014,我使用2014开发人员版。我试过UNION ALL它没有用。我也尝试了一些东西,我认为这是加入。它把它们组合成一个列,不是我想要做的。

我想结合下面的2来得到这个结果而不是2作为单独的结果,这是我目前的。

YearOfOrderDate   CountOfOrders
2011              1406
2012              3162
2013              11095
2014              10354


select count(SalesOrderID) as CountOfOrders
from AdventureWorks2014.Sales.SalesOrderHeader
group by datepart(year,AdventureWorks2014.Sales.SalesOrderHeader.OrderDate)
order by datepart(year,AdventureWorks2014.Sales.SalesOrderHeader.OrderDate) 

select datepart(year,OrderDate) as YearOfOrderDate
from AdventureWorks2014.Sales.SalesOrderHeader
group by datepart(year,AdventureWorks2014.Sales.SalesOrderHeader.OrderDate)
order by datepart(year,AdventureWorks2014.Sales.SalesOrderHeader.OrderDate)

1 个答案:

答案 0 :(得分:5)

你想要这个吗?

select datepart(year,OrderDate) as YearOfOrderDate , count(SalesOrderID) as CountOfOrders
from AdventureWorks2014.Sales.SalesOrderHeader s
group by datepart(year,s.OrderDate)
order by datepart(year,s.OrderDate)