Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.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
删除SQL Server中的第二行或相同编号_Sql_Sql Server_Sql Server 2008_Tsql - Fatal编程技术网

删除SQL Server中的第二行或相同编号

删除SQL Server中的第二行或相同编号,sql,sql-server,sql-server-2008,tsql,Sql,Sql Server,Sql Server 2008,Tsql,我正在使用SQL Server。我使用了这个代码: select distinct A.CustomerNo, A.INV_Nr, B.Code, A.Price from INVOICE A. INVOICE_LINE B where A.IVC_Nr = B.IVC_Nr 输出如下: | CustomerNo | INV_Nr | Code | Price | ======================================

我正在使用SQL Server。我使用了这个代码:

 select distinct A.CustomerNo, A.INV_Nr, B.Code, A.Price 
 from INVOICE A.
      INVOICE_LINE B
 where
      A.IVC_Nr = B.IVC_Nr
输出如下:

| CustomerNo   |    INV_Nr   |   Code   | Price |
=================================================
| 1100021      |    500897   |   1404   | 2500  |
| 1100021      |    500897   |   1403   | 2500  |
| 1100022      |    500898   |   1405   | 3500  |
| 1100023      |    500899   |   1405   | 3000  |
| 1100023      |    500899   |   1403   | 3000  |
| CustomerNo   |    INV_Nr   |   Code   | Price |
=================================================
| 1100021      |    500897   |   1404   | 2500  |
| 1100022      |    500898   |   1405   | 3500  |
| 1100023      |    500899   |   1405   | 3000  |
如何删除第二行并仅获取相同编号的第一行,应如下所示:

| CustomerNo   |    INV_Nr   |   Code   | Price |
=================================================
| 1100021      |    500897   |   1404   | 2500  |
| 1100021      |    500897   |   1403   | 2500  |
| 1100022      |    500898   |   1405   | 3500  |
| 1100023      |    500899   |   1405   | 3000  |
| 1100023      |    500899   |   1403   | 3000  |
| CustomerNo   |    INV_Nr   |   Code   | Price |
=================================================
| 1100021      |    500897   |   1404   | 2500  |
| 1100022      |    500898   |   1405   | 3500  |
| 1100023      |    500899   |   1405   | 3000  |

谢谢,

因为问题是用
SQL Server 2005+
标记的,所以您可以在这个问题上使用
通用表表达式
窗口函数

WITH recordsList
AS
(
    SELECT  A.CustomerNo, A.INV_Nr, B.Code, A.Price ,
            ROW_NUMBER() OVER (PARTITION BY A.CustomerNo
                                ORDER BY B.Code DESC) rn
    FROM    INVOICE A
            INNER JOIN INVOICE_LINE B
                ON A.IVC_Nr = B.IVC_Nr
)
SELECT  CustomerNo, INV_Nr, Code, Price
FROM    recordsList
WHERE   rn = 1

警告一句——你的“第二行”是随机的。插入后,顺序可能会更改。如果您需要订单,请将已定义的订单提交到select中。-旧式的逗号分隔表列表样式在ANSI-92 SQL标准(20年前!)中已不再使用。
+1
我不知道通用表表达式。