1 sql中的2个计数
产品表1 sql中的2个计数,sql,tsql,sql-server-2008-r2,Sql,Tsql,Sql Server 2008 R2,产品表 ProductID ProductDesc 401 Hotdog 402 Ham 403 Bacon 订单表 OrderID OrderPayment NumOrder OrderDate 5001 Cash 3 9-15-2013 5002 Credit 2 9-16-2013 5003 Credit
ProductID ProductDesc
401 Hotdog
402 Ham
403 Bacon
订单表
OrderID OrderPayment NumOrder OrderDate
5001 Cash 3 9-15-2013
5002 Credit 2 9-16-2013
5003 Credit 2 9-17-2013
5004 Cash 3 9-18-2013
OrderDetailsTable
OrderDetailsID OrderID ProductID
70001 5001 401 -
70002 5001 401 -
70003 5001 403 -
70004 5002 401
70005 5002 402
70006 5003 402
70007 5003 403
70008 5004 403 -
70009 5004 402 -
70010 5004 401 -
如何根据每个日期的现金订单数量计算ProductID,然后获得每个产品的总数量
样本输出
ProductID ProductDesc CountOnCash OrderDate
401 Hotdog 2 9-15-2013
401 Hotdog 1 9-18-2013
401 Hotdog 3 ---------
402 Ham 1 9-18-2013
402 Ham 1 ---------
403 Bacon 1 9-15-2013
403 Bacon 1 9-18-2013
403 Bacon 2 ---------
Select p.ProductID, p.ProductDesc, count(p.ProductId) as NumOrder, o.OrderDate
from Product p
inner join OrderDetails od on p.productid = od.productid
inner join Order o on o.orderid = od.orderid
where orderpayment = 'cash'
Group by p.ProductID, p.ProductDesc, o.OrderDate
您需要条件求和,因此将条件从
where
子句移动到case
语句:
Select p.ProductID, p.ProductDesc,
sum(case when orderpayment = 'cash' then 1 else 0 end) as NumCash,
count(*) as NumOrder, o.OrderDate
from Product p
inner join OrderDetails od on p.productid = od.productid
inner join Order o on o.orderid = od.orderid
Group by p.ProductID, p.ProductDesc, o.OrderDate
您需要条件求和,因此将条件从
where
子句移动到case
语句:
Select p.ProductID, p.ProductDesc,
sum(case when orderpayment = 'cash' then 1 else 0 end) as NumCash,
count(*) as NumOrder, o.OrderDate
from Product p
inner join OrderDetails od on p.productid = od.productid
inner join Order o on o.orderid = od.orderid
Group by p.ProductID, p.ProductDesc, o.OrderDate
据我所知,你想要小计
Select p.ProductID, p.ProductDesc, count(p.ProductId) as NumOrder, o.OrderDate
from Product p
inner join OrderDetails od on p.productid = od.productid
inner join Order o on o.orderid = od.orderid
where orderpayment = 'cash'
Group by p.ProductID, p.ProductDesc, o.OrderDate
Union
Select p.ProductID, p.ProductDesc, count(p.ProductId) as NumOrder, '------'
from Product p
inner join OrderDetails od on p.productid = od.productid
inner join Order o on o.orderid = od.orderid
where orderpayment = 'cash'
Group by p.ProductID, p.ProductDesc
我会给你数据的。据我所知,你想要小计
Select p.ProductID, p.ProductDesc, count(p.ProductId) as NumOrder, o.OrderDate
from Product p
inner join OrderDetails od on p.productid = od.productid
inner join Order o on o.orderid = od.orderid
where orderpayment = 'cash'
Group by p.ProductID, p.ProductDesc, o.OrderDate
Union
Select p.ProductID, p.ProductDesc, count(p.ProductId) as NumOrder, '------'
from Product p
inner join OrderDetails od on p.productid = od.productid
inner join Order o on o.orderid = od.orderid
where orderpayment = 'cash'
Group by p.ProductID, p.ProductDesc
将为您获取数据。使用:
GROUP BY p.ProductID, p.ProductDesc, o.OrderDate WITH ROLLUP
见
小计行将使用NULL
代替OrderDate
。每个ProductID
和整个查询也会有小计。您可以使用HAVING
子句或使用发出SQL
调用的应用程序中的代码过滤掉这些内容。添加到查询的末尾:
HAVING p.ProductDesc IS NOT NULL
使用:
GROUP BY p.ProductID, p.ProductDesc, o.OrderDate WITH ROLLUP
见
小计行将使用NULL
代替OrderDate
。每个ProductID
和整个查询也会有小计。您可以使用HAVING
子句或使用发出SQL
调用的应用程序中的代码过滤掉这些内容。添加到查询的末尾:
HAVING p.ProductDesc IS NOT NULL
对不起,你的问题是什么?我看到一个SQL查询。有什么问题吗?我想他是在问如何得到包含所有日期的小计行。在MySQL中,它将是
groupby。。。对于ROLLUP,我不知道TSQL是否有这样的功能。它看起来有:对不起,你的问题是什么?我看到一个SQL查询。有什么问题吗?我想他是在问如何得到包含所有日期的小计行。在MySQL中,它将是groupby。。。对于ROLLUP,我不知道TSQL是否有这样的功能。它看起来是这样的:这是我最初认为他要求的,但从他的示例输出来看,我认为不是这样。看我上面的评论。这是我一开始以为他想要的,但看看他的示例输出,我认为不是。见我上面的评论。+1。你可能对这个问题有更好的解释。这基本上就是你想要的,不是吗?是的。但是我将如何删除那些空的productdesc。我试过这个,我在回答中告诉过你:使用have。我将如何用它的小计过滤I<代码>其中CountOnCash=3
+1。你可能对这个问题有更好的解释。这基本上就是你想要的,不是吗?是的。但是我将如何删除那些空的productdesc。我试过这个,我在回答中告诉过你:使用have。我将如何用它的小计过滤I<代码>其中CountOnCash=3