我只想拥有与我的Excel工作表中具有不同ManufacturerPartNumber的行。
所以,我给出了这样的命令文本:
cmdExcel.CommandText = "SELECT Name, DISTINT [ManufacturerPartNumber],Price From [" + SheetName + "] WHERE [ManufacturerPartNumber] IS NOT NULL";
但这不起作用,如果我将DISTINCT [ManufacturerPartNumber]
更改为ManufacturerPartNumber
,则可行。并且它返回重复的行,我不想发生这些行。
这是我得到的错误:
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred
Additional information: Syntax error (missing operator) in query expression 'DISTINCT [ManufacturerPartNumber]'.
示例数据
我的Excel表格
Name ManufacturerPartNumber Price
PenDrive AGBN514A 5.4
HardDisk NKDNKHS5 9.6
DataCard AGBN514A 6.3
//这里AGBN514A出现了两次,在PenDrive和DataCard中出现
预期输出
Name ManufacturerPartNumber Price
PenDrive AGBN514A 5.4
HardDisk NKDNKHS5 9.6
//它应该省略ManufacturerPartNumbers
由于
答案 0 :(得分:2)
Tsql有用吗?
我之前只见过像
这样的明显区别SELECT DISTINCT Name, [ManufacturerPartNumber],Price From ...
更新
我刚注意到你错过了一个" c"来自DISTINCT。
更新2:
试试这个SQL:
select Name, [ManufacturerPartNumber],Price
from (
select Name, [ManufacturerPartNumber],Price ,
row_number() over (partition by [ManufacturerPartNumber] order by [ManufacturerPartNumber]) as row_number
from test
) as rows
where row_number = 1
更新3:
您正在尝试选择from test
这是我的测试表。您将要替换您的工作表名称。
cmdExcel.CommandText = "select Name, [ManufacturerPartNumber],Price from ( select Name, [ManufacturerPartNumber],Price , row_number() over (partition by [ManufacturerPartNumber] order by [ManufacturerPartNumber]) as row_number from [" + SheetName + "]) as rows where row_number = 1"