SQL 服务器查询语法,用于计算变量的最大出现次数并输出相同的
SQL Server Query syntax to count maximum occurrences of a variable and output same
我正在使用 SQL Server 2014,我有一个简单的查询,如果我 运行 它与相关 table 相对应,它会给我以下输出。
所以这是查询:
use MyDatabase
select ReservationStayID
, NonRoomBundleID
,StayDate
from ResStayNonRoomBundle
where ReservationStayID = 11676
输出如下:
ReservationStayID NonRoomBundleID StayDate
11676 2 2014-07-23 00:00:00.000
11676 2 2014-07-24 00:00:00.000
11676 2 2014-07-25 00:00:00.000
11676 2 2014-07-26 00:00:00.000
11676 2 2014-07-27 00:00:00.000
11676 2 2014-07-28 00:00:00.000
11676 4 2014-07-29 00:00:00.000
11676 4 2014-07-30 00:00:00.000
11676 4 2014-07-31 00:00:00.000
11676 4 2014-08-01 00:00:00.000
11676 4 2014-08-02 00:00:00.000
11676 4 2014-08-03 00:00:00.000
11676 4 2014-08-04 00:00:00.000
11676 1 2014-08-05 00:00:00.000
现在,我需要修改查询以获得以下输出:
ReservationStayID NonRoomBundle ID MTH
11676 2 July 2014
11676 4 August 2014
这就是我所做的:
USE MyDatabase
select ReservationStayID,max(NonRoomBundleID) AS [NonRoomBundle ID],datename(m,StayDate) + ' ' + cast(datepart(yyyy,StayDate) as varchar) as [MTH]
from ResStayNonRoomBundle
where ReservationStayID = 11676
group by datename(m,StayDate) + ' ' + cast(datepart(yyyy,StayDate) as varchar), ReservationStayID
它给了我以下输出:
ReservationStayID NonRoomBundle ID MTH
11676 4 July 2014
11676 4 August 2014
我需要将 max(NonRoomBundleID) 更改为逻辑,输出出现次数最多的 'NonRoomBundleID' 而不仅仅是最大值。
换句话说,我希望查询计算每个月 NonRoomBundleID 的最大出现次数并输出那个。在 2014 年 7 月的情况下,出现次数最多的 NonRoomBundleID 是 2。因此,我希望查询输出 2 作为 2014 年 7 月的结果。
可能正在使用 COUNT 函数?如果是,我如何将它实现到我现有的查询中?
试试这个:
SELECT ReservationStayID, NonRoombundleID, Tot,
CONVERT(VARCHAR, DATEPART(yyyy, DATEADD(month, M,'1970-1-1')))+'-'+CONVERT(VARCHAR,DATEPART(month, DATEADD(month, M,'1970-1-1'))) AS [Month]
FROM
(
SELECT ReservationStayID, NonRoombundleID, COUNT(*) AS Tot,
DATEDIFF(month, '1970-1-1', staydate) m,
ROW_NUMBER() OVER (
partition by ReservationStayID,
DATEDIFF(month, '1970-1-1', staydate)
ORDER BY Count(*) DESC
) AS rownum
FROM ResStayNonRoomBundle
GROUP BY ReservationStayID, NonRoombundleID,
DATEDIFF(month, '1970-1-1', staydate)
)
e
WHERE rownum =1
要使用您自己的月份格式:
SELECT ReservationStayID, NonRoombundleID, Tot,
datename(m,DATEADD(MONTH, M, '1970-1-1')) + ' ' + cast(datepart(yyyy,DATEADD(MONTH, M,'1970-1-1')) as varchar)
FROM
(
SELECT ReservationStayID, NonRoombundleID, COUNT(*) AS Tot,
DATEDIFF(month, '1970-1-1', staydate) m,
ROW_NUMBER() OVER (partition by ReservationStayID, DATEDIFF(month, '1970-1-1', staydate) order by Count(*) DESC) As rownum
FROM ResStayNonRoomBundle
GROUP BY ReservationStayID, NonRoombundleID, DATEDIFF(month, '1970-1-1', staydate)
)
e
WHERE rownum =1
如果您想列出每个月的所有热门地点,试试这个:
SELECT ReservationStayID, NonRoombundleID, Tot,
datename(m,DATEADD(MONTH, M, '1970-1-1')) + ' ' + cast(datepart(yyyy,DATEADD(MONTH, M,'1970-1-1')) as varchar) [Month]
FROM
(
SELECT ReservationStayID, NonRoombundleID, COUNT(*) AS Tot,
DATEDIFF(month, '1970-1-1', staydate) m
FROM ResStayNonRoomBundle L
GROUP BY ReservationStayID, NonRoombundleID, DATEDIFF(month, '1970-1-1', staydate)
HAVING COUNT(*) = (
SELECT MAX(Tot) FROM (
SELECT COUNT(*) AS Tot
FROM ResStayNonRoomBundle T
WHERE DATEDIFF(month, '1970-1-1', T.staydate) = DATEDIFF(month, '1970-1-1', L.staydate)
GROUP BY ReservationStayID, NonRoombundleID
) c
)
)
e
ORDER BY m
我正在使用 SQL Server 2014,我有一个简单的查询,如果我 运行 它与相关 table 相对应,它会给我以下输出。 所以这是查询:
use MyDatabase
select ReservationStayID
, NonRoomBundleID
,StayDate
from ResStayNonRoomBundle
where ReservationStayID = 11676
输出如下:
ReservationStayID NonRoomBundleID StayDate
11676 2 2014-07-23 00:00:00.000
11676 2 2014-07-24 00:00:00.000
11676 2 2014-07-25 00:00:00.000
11676 2 2014-07-26 00:00:00.000
11676 2 2014-07-27 00:00:00.000
11676 2 2014-07-28 00:00:00.000
11676 4 2014-07-29 00:00:00.000
11676 4 2014-07-30 00:00:00.000
11676 4 2014-07-31 00:00:00.000
11676 4 2014-08-01 00:00:00.000
11676 4 2014-08-02 00:00:00.000
11676 4 2014-08-03 00:00:00.000
11676 4 2014-08-04 00:00:00.000
11676 1 2014-08-05 00:00:00.000
现在,我需要修改查询以获得以下输出:
ReservationStayID NonRoomBundle ID MTH
11676 2 July 2014
11676 4 August 2014
这就是我所做的:
USE MyDatabase
select ReservationStayID,max(NonRoomBundleID) AS [NonRoomBundle ID],datename(m,StayDate) + ' ' + cast(datepart(yyyy,StayDate) as varchar) as [MTH]
from ResStayNonRoomBundle
where ReservationStayID = 11676
group by datename(m,StayDate) + ' ' + cast(datepart(yyyy,StayDate) as varchar), ReservationStayID
它给了我以下输出:
ReservationStayID NonRoomBundle ID MTH
11676 4 July 2014
11676 4 August 2014
我需要将 max(NonRoomBundleID) 更改为逻辑,输出出现次数最多的 'NonRoomBundleID' 而不仅仅是最大值。
换句话说,我希望查询计算每个月 NonRoomBundleID 的最大出现次数并输出那个。在 2014 年 7 月的情况下,出现次数最多的 NonRoomBundleID 是 2。因此,我希望查询输出 2 作为 2014 年 7 月的结果。
可能正在使用 COUNT 函数?如果是,我如何将它实现到我现有的查询中?
试试这个:
SELECT ReservationStayID, NonRoombundleID, Tot,
CONVERT(VARCHAR, DATEPART(yyyy, DATEADD(month, M,'1970-1-1')))+'-'+CONVERT(VARCHAR,DATEPART(month, DATEADD(month, M,'1970-1-1'))) AS [Month]
FROM
(
SELECT ReservationStayID, NonRoombundleID, COUNT(*) AS Tot,
DATEDIFF(month, '1970-1-1', staydate) m,
ROW_NUMBER() OVER (
partition by ReservationStayID,
DATEDIFF(month, '1970-1-1', staydate)
ORDER BY Count(*) DESC
) AS rownum
FROM ResStayNonRoomBundle
GROUP BY ReservationStayID, NonRoombundleID,
DATEDIFF(month, '1970-1-1', staydate)
)
e
WHERE rownum =1
要使用您自己的月份格式:
SELECT ReservationStayID, NonRoombundleID, Tot,
datename(m,DATEADD(MONTH, M, '1970-1-1')) + ' ' + cast(datepart(yyyy,DATEADD(MONTH, M,'1970-1-1')) as varchar)
FROM
(
SELECT ReservationStayID, NonRoombundleID, COUNT(*) AS Tot,
DATEDIFF(month, '1970-1-1', staydate) m,
ROW_NUMBER() OVER (partition by ReservationStayID, DATEDIFF(month, '1970-1-1', staydate) order by Count(*) DESC) As rownum
FROM ResStayNonRoomBundle
GROUP BY ReservationStayID, NonRoombundleID, DATEDIFF(month, '1970-1-1', staydate)
)
e
WHERE rownum =1
如果您想列出每个月的所有热门地点,试试这个:
SELECT ReservationStayID, NonRoombundleID, Tot,
datename(m,DATEADD(MONTH, M, '1970-1-1')) + ' ' + cast(datepart(yyyy,DATEADD(MONTH, M,'1970-1-1')) as varchar) [Month]
FROM
(
SELECT ReservationStayID, NonRoombundleID, COUNT(*) AS Tot,
DATEDIFF(month, '1970-1-1', staydate) m
FROM ResStayNonRoomBundle L
GROUP BY ReservationStayID, NonRoombundleID, DATEDIFF(month, '1970-1-1', staydate)
HAVING COUNT(*) = (
SELECT MAX(Tot) FROM (
SELECT COUNT(*) AS Tot
FROM ResStayNonRoomBundle T
WHERE DATEDIFF(month, '1970-1-1', T.staydate) = DATEDIFF(month, '1970-1-1', L.staydate)
GROUP BY ReservationStayID, NonRoombundleID
) c
)
)
e
ORDER BY m