SQL Count使用来自连接的信息

时间:2018-02-06 22:14:47

标签: sql sql-server

我需要计算InternalMenuLinkItemNumber每个sitenumber和每个订单模式出现的次数。然后我需要显示MenuItemID,我使用项目编号使用内部联接,但是当我添加此联接时,它会扭曲QTY结果。我已尝试在COUNT中使用distinct,但所有QTY都是1.请协助。

查询和结果,其中QTY​​结果是100%正确但没有MenuItemID。

 SELECT ST_Sites.BusinessUnit,[ST_SalesMixTransactions_RealTimeFeed].SiteNumber,InternalMenuLinkItemNumber,[ST_SalesMix].MenuItemID,OrderMode,SellingPrice,COUNT(ST_SalesMixTransactions_RealTimeFeed.InternalMenuLinkItemNumber) as QTY 

FROM ST_AlohaSalesMixTransactions_RealTimeFeed

inner join ST_Sites on ST_Sites.SiteNumber= [ST_SalesMixTransactions_RealTimeFeed].SiteNumber 

where [ST_SalesMixTransactions_RealTimeFeed].BusinessDate between'2017-06-27'and'2017-07-03' and [ST_SalesMixTransactions_RealTimeFeed].SiteNumber = '1001006'

group by InternalMenuLinkItemNumber,[ST_SalesMixTransactions_RealTimeFeed].SiteNumber,OrderMode,SellingPrice,ST_Sites.BusinessUnit,[ST_SalesMix].MenuItemID

order by InternalMenuLinkItemNumber 

QTY按预期出现的结果:

enter image description here

如果我添加内部联接以获取MenuItemID: 查询:

SELECT ST_Sites.BusinessUnit,[ST_SalesMixTransactions_RealTimeFeed].SiteNumber,InternalMenuLinkItemNumber,[ST_SalesMix].MenuItemID,OrderMode,SellingPrice,COUNT(ST_SalesMixTransactions_RealTimeFeed.InternalMenuLinkItemNumber) as QTY 

FROM ST_AlohaSalesMixTransactions_RealTimeFeed

inner join ST_SalesMix on [ST_AlohaSalesMixTransactions_RealTimeFeed].InternalMenuLinkItemNumber= ST_SalesMix.ItemNumber

inner join ST_Sites on ST_Sites.SiteNumber= [ST_SalesMixTransactions_RealTimeFeed].SiteNumber 

where [ST_SalesMixTransactions_RealTimeFeed].BusinessDate between'2017-06-27'and'2017-07-03' and [ST_SalesMixTransactions_RealTimeFeed].SiteNumber = '1001006'

group by InternalMenuLinkItemNumber,[ST_SalesMixTransactions_RealTimeFeed].SiteNumber,OrderMode,SellingPrice,ST_Sites.BusinessUnit,[ST_SalesMix].MenuItemID

order by InternalMenuLinkItemNumber

现在QTY的结果如下: enter image description here

如果我使用distinct: 查询:

SELECT ST_Sites.BusinessUnit,[ST_SalesMixTransactions_RealTimeFeed].SiteNumber,InternalMenuLinkItemNumber,[ST_SalesMix].MenuItemID,OrderMode,SellingPrice,COUNT(distinct ST_SalesMixTransactions_RealTimeFeed.InternalMenuLinkItemNumber) as QTY 

FROM ST_AlohaSalesMixTransactions_RealTimeFeed

inner join ST_SalesMix on [ST_AlohaSalesMixTransactions_RealTimeFeed].InternalMenuLinkItemNumber= ST_SalesMix.ItemNumber

inner join ST_Sites on ST_Sites.SiteNumber= [ST_SalesMixTransactions_RealTimeFeed].SiteNumber 

where [ST_SalesMixTransactions_RealTimeFeed].BusinessDate between'2017-06-27'and'2017-07-03' and [ST_SalesMixTransactions_RealTimeFeed].SiteNumber = '1001006'

group by InternalMenuLinkItemNumber,[ST_SalesMixTransactions_RealTimeFeed].SiteNumber,OrderMode,SellingPrice,ST_Sites.BusinessUnit,[ST_SalesMix].MenuItemID

order by InternalMenuLinkItemNumber

QTY的结果现在全部为1: enter image description here

3 个答案:

答案 0 :(得分:0)

如果我理解正确,你需要像

这样的东西
SELECT SiteNumber, OrderMode, count([DISTINCT?] InternalMenuLinkItemNumber)
...
GROUP BY SiteNumber, OrderMode

您想要计算InternalMenuLinkItemNumber,因此不能在GROUP BY子句中出现InternalMenuLinkItemNumber。

修改 使用GROUP BY时,SELECT列表可能只包含GROUP BY子句中提到的列或聚合函数(在任意列上)。

答案 1 :(得分:0)

试试这个:

SELECT a.InternalMenuLinkItemNumber, a.SiteNumber, a.OrderMode, a.SellingPrice, a.BusinessUnit, a.MenuItemID, a.QTY, CASE WHEN MAX(b.MenuItemID) = MIN(b.MenuItemID) THEN MAX(b.MenuItemID) ELSE -1 END AS MenuItemID
    FROM
        (SELECT ST_Sites.BusinessUnit, [ST_SalesMixTransactions_RealTimeFeed].SiteNumber, InternalMenuLinkItemNumber, [ST_SalesMix].MenuItemID, OrderMode, SellingPrice, COUNT(ST_SalesMixTransactions_RealTimeFeed.InternalMenuLinkItemNumber) as QTY 
            FROM ST_AlohaSalesMixTransactions_RealTimeFeed
            INNER JOIN ST_Sites on ST_Sites.SiteNumber = [ST_SalesMixTransactions_RealTimeFeed].SiteNumber 
            WHERE [ST_SalesMixTransactions_RealTimeFeed].BusinessDate between'2017-06-27'and'2017-07-03' and [ST_SalesMixTransactions_RealTimeFeed].SiteNumber = '1001006'
            GROUP BY InternalMenuLinkItemNumber, [ST_SalesMixTransactions_RealTimeFeed].SiteNumber, OrderMode, SellingPrice, ST_Sites.BusinessUnit, [ST_SalesMix].MenuItemID
         ) a
    INNER JOIN ST_SalesMix b ON a.InternalMenuLinkItemNumber = b.ItemNumber
    GROUP BY a.InternalMenuLinkItemNumber, a.SiteNumber, a.OrderMode, a.SellingPrice, a.BusinessUnit, a.MenuItemID, a.QTY
    ORDER BY a.InternalMenuLinkItemNumber

它的工作理论是你的第一个查询提供了很好的计数,所以保持原样(它现在是内部查询),然后在它之外进行有问题的连接。显然,对于第一个查询中的每个正确计数的行,ST_SalesMix中有很多行,因此我在原始组列表上进行分组,但这意味着您可能会获得多个MenuItemID。我通过测试MAX和MIN MenuItemID来检查CASE语句中的那个 - 如果它们是相同的返回MAX(MenuItemID),否则我返回-1作为错误标志以指示有多个MenuItemID与该组相关联。它可能不是最有效的方法,但我没有太多的事情要继续下去。

我希望这会有所帮助。

答案 2 :(得分:0)

现在全部排序。谢谢大家。 @jwolf你建议的查询就是答案。