我有一个插入查询进入链接到QuickBooks的一个表。表Test_InvoiceLine具有很多完全相同的Part_ID和Descriptions。
<?php
$url=$_GET["url"]; // URL to remote server that has the video file
$s = fopen($url, "rb");
if (!$s ) {
echo "open failed";
$url = str_replace("https", "http",$url);
$s = fopen($url, "rb");
if (!$s) {
echo "\n<br>FAILED cant open:". $url;
exit();
}
}
if (isset($http_response_header)) {
foreach ($http_response_header as $h) {
header($h);
}
}
else {
$type = 'Content-Type:video/mp4';
header($type);
}
ini_set('max_execution_time', 0);
while (!feof($s)) { // read from other server, send to browser
echo fread($s, 4096);
}
fclose($s);
?>
如何编写查询进入InvoiceLine表并删除具有相同Part_ID和Description的重复项?
答案 0 :(得分:0)
这是SQL Server,但是ROW_NUMBER被其他RDMS广泛使用。
这是您需要的查询。
;WITH Data AS
(
SELECT
Part_ID,Description,
RowNumber = ROW_NUMBER() OVER(PARTITION BY Part_Id,Description ORDER BY Part_Id,Description)
FROM Test_InvoiceLine
)
DELETE FROM Data WHERE RowNumber > 1
根据您的帖子,我不知道More_Info在这里有什么不同,因为重复键不包含它,但是,如果您需要检查delete语句中的more_info值,那么也许可以使用类似的方法转到下面的查询。
;WITH Data AS
(
SELECT
More_Info,
Part_ID,Description,
RowNumber = ROW_NUMBER() OVER(PARTITION BY Part_Id,Description ORDER BY Part_Id,Description)
FROM Test_InvoiceLine
)
DELETE T
FROM Test_InvoiceLine T
INNER JOIN Data D ON D.RowNumber > 1 AND D.MoreInfo = "Y" AND D.Part_Id = T.Part_ID
答案 1 :(得分:0)
使用CTE作为临时结果集。本质上,您将查询按条件划分的重复记录,然后删除不是第一条记录的记录(保留原始记录)。
在吹走任何东西之前,请仔细检查我,因为距离我这样做已经有一段时间了,但这应该可以工作。
WITH CTE AS(
SELECT Part_ID, Description,
RowNum = ROW_NUMBER()OVER(PARTITION BY Part_ID, Description ORDER BY Part_ID, Description)
FROM Test_InvoiceLine
)
DELETE FROM CTE WHERE RowNum > 1
答案 2 :(得分:0)
是否有理由需要插入然后删除?如果您要插入->删除,听起来好像您实际上是在尝试通过Part_ID
更新数据。
update InvoiceLine a
set More_Info = b.Description
from ( select Part_ID,
Description
from Test_InvoiceLine ) b
where b.Part_ID = a.Part_ID
这将用More_Info
更新InvoiceLine
表中的Description
。