例如,如果我有一个名为ticket的表,其中包含一个名为ticketid的列。故障单ID包含数字,有时数字会出现多次,因为故障单可能有多次更新。如何创建一个变量来输出ticketid的次数?
例如:
ticketid:7
ticketid:7
timeoccured:2
答案 0 :(得分:1)
SELECT ticketid, count(*) AS timesoccured
FROM tickets
-- WHERE ticketid = 7 -- to just get the one ...
GROUP BY 1;
GROUP BY 1
是GROUP BY ticket_id
的简写语法
Start by reading the manual here.
通常,列ticketid
在tickets
表中是唯一的。您的命名约定或数据模型遵循可疑的逻辑。
答案 1 :(得分:1)
假设TicketId不为NULL,您应该使用:
SELECT TicketId, COUNT(TicketId) AS TicketIdCount
FROM [Table]
GROUP BY TicketId
答案 2 :(得分:0)
SELECT COUNT(*), ticketid
FROM tickets
GROUP BY ticketid;
答案 3 :(得分:0)
如果您只想重复的门票使用having
select ticketid, count(*)
from tickets
group by 1
having count(*) > 1