Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否删除除Microsoft SQL Server中的一行之外的重复行查询结果?_Sql_Sql Server_Sql Server 2008_Duplicate Removal - Fatal编程技术网

是否删除除Microsoft SQL Server中的一行之外的重复行查询结果?

是否删除除Microsoft SQL Server中的一行之外的重复行查询结果?,sql,sql-server,sql-server-2008,duplicate-removal,Sql,Sql Server,Sql Server 2008,Duplicate Removal,如何从Microsoft SQL Server表中删除所有重复的月份 例如,使用我刚刚创建的以下语法: SELECT * FROM Cash WHERE Id = '2' AND TransactionDate between '2014/07/01' AND '2015/02/28' 查询结果为: +----+-------------------------+ |Id | TransactionDate | +----+-------------------------+

如何从Microsoft SQL Server表中删除所有重复的月份

例如,使用我刚刚创建的以下语法:

SELECT * FROM Cash WHERE Id = '2' AND TransactionDate between '2014/07/01' AND '2015/02/28'
查询结果为:

+----+-------------------------+
|Id  | TransactionDate         |
+----+-------------------------+
| 2  | 2014-07-22 00:00:00.000 |
| 2  | 2014-08-09 00:00:00.000 |
| 2  | 2014-08-25 00:00:00.000 |
| 2  | 2014-08-29 00:00:00.000 |
| 2  | 2015-01-27 00:00:00.000 |
| 2  | 2015-01-28 00:00:00.000 |
+----+-------------------------+ 
如何删除每个月仅返回任何1个值的重复项,如以下结果:

+----+-------------------------+
|Id  | TransactionDate         |
+----+-------------------------+
| 2  | 2014-07-22 00:00:00.000 |
| 2  | 2014-08-09 00:00:00.000 |
| 2  | 2015-01-27 00:00:00.000 |
+----+-------------------------+ 

每月仅选择第一行

SELECT *
FROM Cash c
WHERE c.Id = '2' 
AND c.TransactionDate between '2014/07/01' AND '2015/02/28'
AND NOT EXISTS ( SELECT 'a'
                   FROM Cash c2
                   WHERE c2.Id = c.Id
                   AND YEAR(c2.TransactionDate) * 100 + MONTH(c2.TransactionDate) = YEAR(c.TransactionDate) * 100 + MONTH(c.TransactionDate)
                   AND c2.TransactionDate < c.TransactionDate
               )

你可以在老师的帮助下做这件事

这将告诉您将保留哪些行

SELECT id,transactionDate, ROW_NUMBER() OVER ( PARTITION BY YEAR(TransactionDate ),MONTH(TransactionDate ) ORDER BY TransactionDate ) firstTrans
FROM Cash 
WHERE Id = '2' AND 
TransactionDate between '2014/07/01' AND '2015/02/28'
您可以使用CTE删除其他行

with myCTE (id,transactionDate, firstTrans) AS (
 SELECT id,transactionDate, ROW_NUMBER() OVER ( PARTITION BY YEAR(TransactionDate ),MONTH(TransactionDate ) ORDER BY TransactionDate ) firstTrans
    FROM Cash 
    WHERE Id = '2' AND 
    TransactionDate between '2014/07/01' AND '2015/02/28'
)
delete from myCTE where firstTrans <> 1

运行此查询时,您将获得每年每个月的最高Id


例如,如果需要,您可以延迟删除其Id不在此查询中的行。

是否要删除或只是不选择它们?只是不选择它们,这样就不会有来自重复月份的查询结果。
 select id, transactionDate from (SELECT id,transactionDate, ROW_NUMBER() OVER ( PARTITION BY YEAR(TransactionDate ),MONTH(TransactionDate ) ORDER BY TransactionDate ) firstTrans
    FROM Cash 
    WHERE Id = '2' AND 
    TransactionDate between '2014/07/01' AND '2015/02/28') where firstTrans = 1
SELECT MAX(<IdColumn>) AS Id, YEAR(<DateColumn>) AS YE, MONTH(<DateColumn>) AS MO FROM <YourTable>
GROUP BY YEAR(<DateColumn>), MONTH(<DateColumn>)