-1

我有 2 张桌子:

SalesOrder - 有 2 条记录

OrderID   Order Desc
101       My Order1
102       My Order2

SalesOrderLine - 具有以下记录:

OrderID    Order_Line
101        10
101        20
102        10
102        20
102        30

我的预期结果是,当我执行查询时,它应该给我:

  • TotalSalesordercount 为 2;和
  • LineCount作为2 for Order 1013 for Order 102
4

3 回答 3

0
select 
  t1.orderid, 
  count(distinct t1.orderid) as 'TotalSalesordercount', 
  count(*) as 'LineCount'   
from 
  SalesOrder T1, 
  SalesOrderLine T2
where 
  t1.orderid = t2.orderid
group by 
  t1.orderid
于 2013-04-08T10:42:15.637 回答
0

您将要JOIN在两个表上OrderId然后使用聚合函数:

select so.orderid,
  so.order_desc,
  count(ol.orderid) LineCount,
  (select count(*) from salesorder) TotalOrders
from salesorder so
inner join orderline ol
  on so.orderid = ol.orderid
group by so.orderid, so.order_desc;

请参阅带有演示的 SQL Fiddle

于 2013-04-08T10:45:37.857 回答
0

这样的事情将适用于您的数据:

select so.OrderId
  , TotalSalesordercount = (select count(distinct OrderId) from SalesOrderLine)
  , LineCount = count(1)
from SalesOrder so
  inner join SalesOrderLine sol on so.OrderId = sol.OrderId
group by so.OrderId

SQL Fiddle 与演示

于 2013-04-08T10:47:31.080 回答