SQL查询内容差异最大的ID

SQL Query to find the ID with highest difference of contents

我是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

现在要求查找"The hotel which has more number of automatic orders and less backend orders ?"

虽然我能够回答大部分问题,但我真的不知道我应该怎么做才能破解这个问题!

如果我对你的问题理解正确,你想要 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
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

排序依据 (a.cnt-b.cnt)

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

如果我没理解错的话, 您想要自动订单和后台订单差价最高的酒店吗?

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

假设这样的 table 存在,并且填充了您提供的值:

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

该查询应该return酒店列表按需求排序。

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

我理解你的问题如下:首先找到自动订单数量最多的酒店,然后在其中找到后端订单最少的酒店。如果这是真的,那么这个查询有效

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

感谢你们每一位试图帮助我的人。通过您提出的问题,我得到了很多想法。正如@Y.S所说,我也认为找到自动订单和后端订单之间差异最大的酒店将解开这个结。我写了一个像这样的简单代码,它将return差价最高的酒店。

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

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

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

顺便说一句,如何避免在最终结果中显示'score'?!?

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