我是SQL的新手,并且无法找到任何文章来解释我需要过滤数据以便从票务系统中提取重复票证的方式。我需要检查商店是否在同一日期提交同一类别的多张票。以下是我正在使用的样本。任何帮助将不胜感激!
USE [HelpDesk]
declare
@date1 date = '5-1-2017',
@date2 date = '6-2-2017'
SELECT [SectionName]
,[CategoryName]
,[RequesterName]
,[IssueDate]
,[StatusId]
FROM [HelpDesk].[dbo].[uvTESTMasterQueryIssues]
WHERE [IssueDate] > @date1 AND [IssueDate] < @date2;
答案 0 :(得分:1)
我没有机会运行或试用它但在SQL Server中它应该是这样的:
-- Number of occurrences of Duplicate records
SELECT [SectionName]
,[CategoryName]
,[RequesterName]
,[IssueDate]
,[StatusId]
,COUNT(*) AS [NUMBER OF OCCURENCES]
FROM [dbo].[uvTESTMasterQueryIssues]
WHERE [IssueDate] > @date1
AND [IssueDate] < @date2
GROUP BY [SectionName]
,[CategoryName]
,[RequesterName]
,[IssueDate]
,[StatusId]
HAVING COUNT(*) > 1
ORDER BY [SectionName]
答案 1 :(得分:0)
这是一个简单的查询,您可以使用Group by并且必须达到您想要的效果。
请参阅:https://www.w3schools.com/sql/sql_having.asp
创建表#temp (
IssueID int,
SectionName varchar(50),
CategoryName varchar(50),
RequesterName varchar(50),
IssueDate date
) 插入#temp值(123,&#39;维护&#39;,&#39;其他警报&#39;,&#39;存储0009550&#39;,&#39; 2017-05-17&#39;) 插入#temp值(124,&#39;维护&#39;,&#39;其他警报&#39;,&#39;存储0003561&#39; 2017-05-17&#39;) 插入#temp值(125,&#39;维护&#39;,&#39;其他警报&#39;,&#39;存储0009550&#39;,&#39; 2017-05-17&#39;) 插入#temp值(126,&#39;维护&#39;,&#39;其他警报&#39;,&#39;存储0003561&#39; 2017-05-17&#39;) 插入#temp值(127,&#39;维护&#39;,&#39;目标移动&#39;,&#39;存储0003561&#39; 2017-05-17&#39;) 插入#temp值(128,&#39;维护&#39;,&#39;其他警报&#39;,&#39;存储0007426&#39; 2017-05-17&#39;) 插入#temp值(129,&#39;维护&#39;,&#39;目标移动&#39;,&#39;存储0007750&#39;,#17; 2017-05-17&#39;)
从#temp
中选择*声明@ date1 date =&#39; 5-1-2017&#39; 声明@ date2 date =&#39; 6-2-2017&#39;
;以cts为( 选择 [分类名称] [IssueDate],RequesterName 来自#temp 在@ date1和@ date2之间的[IssueDate] 按类别分组,[IssueDate],RequesterName具有count(*)&gt; 1 )
选择t。*来自#temp t inner join cts ON t.CategoryName = cts.CategoryName和t.IssueDate = cts.IssueDate和t.RequesterName = cts.RequesterName order by t.RequesterName,t.IssueID
drop table #temp
答案 2 :(得分:0)
您可以使用group by找到重复记录,如果您想根据Category和requesterName查找重复记录,则执行以下查询将为您提供结果:
SELECT [CategoryName]
,[RequesterName]
,Count(*)
FROM [HelpDesk].[dbo].[uvTESTMasterQueryIssues]
WHERE [IssueDate] > @date1 AND [IssueDate] < @date2
GROUP BY
RequesterName, CategoryName
HAVING
COUNT(*) > 1