SQL Query查找内容差异最大的ID

时间:2015-01-13 07:39:48

标签: sql sql-server

我是SQL(服务器)的新手,以下问题确实让我一无所知。

ID  Order Type    
=====================    
26  Automatic Orders    
26  Backend Orders    
41  Automatic Orders    
58  Automatic Orders    
86  Automatic Orders    
86  Automatic Orders    
86  Automatic Orders    
111 Automatic Orders    
104 Automatic Orders    
112 Backend Orders    
112 Backend Orders    
119 Backend Orders    
119 Backend Orders    
119 Backend Orders

现在要求找到“拥有更多自动订单和更少后端订单的酒店?”

虽然我能够回答大多数问题,但我真的没有得到我应该做的事情来解决这个问题!

5 个答案:

答案 0 :(得分:1)

如果我理解您的问题,您希望酒店的Automatic Orders多于Backend Orders。您可以使用SUMHAVING功能。

SELECT
    ID
FROM temp
GROUP BY ID
HAVING
    SUM(CASE WHEN [Order Type] = 'Automatic Orders' THEN 1 ELSE 0 END) > 
    SUM(CASE WHEN [Order Type] = 'Backend Orders' THEN 1 ELSE 0 END)

修改

根据您对Asketh答案的评论,这可能是您所需要的:

SELECT
    TOP 1 *
FROM(
    SELECT
        ID,
        [Automatic Orders] = SUM(CASE WHEN [Order Type] = 'Automatic Orders' THEN 1 ELSE 0 END),
        [Backend Orders] = SUM(CASE WHEN [Order Type] = 'Backend Orders' THEN 1 ELSE 0 END)
    FROM temp
    GROUP BY ID
)t
ORDER BY ([Automatic Orders] - [Backend Orders]) DESC

答案 1 :(得分:1)

不确定这正是您所需要的, 但它可以指导您找到解决方案。

如果我理解正确, 您想要自动和后端订单之间差异最大的酒店吗?

如果是这样, 这可能会有所帮助。

假设存在此表,并使用您提供的值填充:

CREATE TABLE [dbo].[Orders](
    [Id] [int] NULL,
    [Type] [varchar](50) NULL
) ON [PRIMARY]

此查询应返回根据需求排序的酒店列表。

SELECT  a.HotelId , a.NumOfAutomaticOrders - a.NumOfBackendOrders
FROM    
(
    SELECT  Id as HotelId , 
    sum(case when [type] = 'Automatic Orders' then 1 else 0 end ) NumOfAutomaticOrders,
    sum(case when [type] = 'Backend Orders' then 1 else 0 end ) NumOfBackendOrders
    FROM Orders
    where 1=1
    group by Id 
) a
order by 2 desc

答案 2 :(得分:0)

SELECT 
    ID, 
    a.cnt,  
    b.cnt 
FROM (
    SELECT 
        id, COUNT(ordertype) cnt 
    FROM tab
    WHERE ordertype = 'Automatic Order' 
    GROUP BY id
) a
JOIN (
    SELECT 
        id, COUNT(ordertype) cnt 
    FROM tab
    WHERE ordertype = 'Backend Order' 
    GROUP BY id
) b
ON a.id = b.id 
WHERE a.cnt> b.cnt

修改: 如果您想要最差异的酒店,请执行以下操作:

SELECT TOP 1 
    ID, 
    a.cnt,  
    b.cnt 
FROM (
    SELECT 
        id, COUNT(ordertype) cnt 
    FROM tab
    WHERE ordertype = 'Automatic Order' 
    GROUP BY id
) a
JOIN (
    SELECT 
        id, COUNT(ordertype) cnt 
    FROM tab
    WHERE ordertype = 'Backend Order' 
    GROUP BY id
) b
ON a.id = b.id 
WHERE a.cnt> b.cnt

ORDER BY(a.cnt-b.cnt)

答案 3 :(得分:0)

我理解您的问题如下:首先查找具有最大自动订单数量的酒店,其中找到具有最小后端订单的酒店。如果是这样,则此查询有效

DECLARE @t TABLE (ID INT, Type NVARCHAR(MAX))

INSERT INTO @t VALUES(26, 'Automatic Orders')
INSERT INTO @t VALUES(26, 'Backend Orders')
INSERT INTO @t VALUES(41, 'Automatic Orders')
INSERT INTO @t VALUES(58, 'Automatic Orders')
INSERT INTO @t VALUES(86, 'Automatic Orders')
INSERT INTO @t VALUES(86, 'Automatic Orders')
INSERT INTO @t VALUES(86, 'Automatic Orders')
INSERT INTO @t VALUES(111, 'Automatic Orders')
INSERT INTO @t VALUES(104, 'Automatic Orders')
INSERT INTO @t VALUES(104, 'Automatic Orders')
INSERT INTO @t VALUES(104, 'Automatic Orders')
INSERT INTO @t VALUES(112, 'Backend Orders')
INSERT INTO @t VALUES(112, 'Backend Orders')
INSERT INTO @t VALUES(119, 'Backend Orders')
INSERT INTO @t VALUES(119, 'Backend Orders')
INSERT INTO @t VALUES(119, 'Backend Orders');



WITH cte AS
(
SELECT ao.ID, ao.Count AS AutomaticOrdersCount, ISNULL(bo.Count, 0) AS BackendOrdersCount FROM 
(
SELECT ID, Type, COUNT(*) AS Count FROM @t
WHERE Type = 'Automatic Orders'
GROUP BY ID, Type
)ao
LEFT JOIN 
(
SELECT ID, Type, COUNT(*) AS Count FROM @t
WHERE Type = 'Backend Orders'
GROUP BY ID, Type
)bo ON bo.ID = ao.ID
ORDER BY ao.Count DESC, ISNULL(bo.Count, 0) ASC
)
SELECT * FROM cte 
WHERE AutomaticOrdersCount = (SELECT MAX(AutomaticOrdersCount) FROM cte) AND 
        cte.BackendOrdersCount = (SELECT TOP 1 BackendOrdersCount FROM cte)
ORDER BY cte.ID

答案 4 :(得分:0)

感谢你们每个人试图帮助我。我通过你建议的查询得到了很多想法。正如@ Y.S所说,我也认为找到自动和后端订单之间差异最大的酒店将解开结。我写了一个这样的简单代码,它将返回差异最大的酒店。

SELECT TOP 1 id, SUM(IIF(Order_Type='Automatic orders',1,-1)) AS Score
FROM P1
GROUP BY id
ORDER BY score DESC

这给了我答案,但我怀疑这是我应该写的正确查询来找到它。在我的回答中,我会得到(计算)和相应的酒店ID。

Score  id
-----  ----
xx     xxx

如何避免显示'得分'最后的结果顺便说一下?!?

我只想在最终结果中显示唯一的酒店ID。由于计算/条件在SELECT,我也可以离开SUM(IIF(Order_Type='Automatic orders',1,-1))