如何编写 OleDbCommand 以仅获取不同的行?
How to write OleDbCommand to get only distinct rows?
我只想拥有与我的 excel sheet.
具有不同 ManufacturerPartNumber 的行
因此,我给出了这样的命令文本:
cmdExcel.CommandText = "SELECT Name, DISTINT [ManufacturerPartNumber],Price From [" + SheetName + "] WHERE [ManufacturerPartNumber] IS NOT NULL";
但这不起作用,如果我将 DISTINCT [ManufacturerPartNumber]
更改为 ManufacturerPartNumber
,它就会起作用。它 returns 重复了我不想发生的行。
这是我得到的错误:
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred
Additional information: Syntax error (missing operator) in query expression 'DISTINCT [ManufacturerPartNumber]'.
示例数据
我的Excelsheet
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
谢谢
Tsql 能用吗?
我以前只见过这么用过distinct
SELECT DISTINCT Name, [ManufacturerPartNumber],Price From ...
更新
而且我刚刚注意到您缺少来自 DISTINCT 的 "c"。
更新 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:
你正在尝试 select from test
这是我的测试 table。您需要替换工作表名称。
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"
我只想拥有与我的 excel sheet.
具有不同 ManufacturerPartNumber 的行因此,我给出了这样的命令文本:
cmdExcel.CommandText = "SELECT Name, DISTINT [ManufacturerPartNumber],Price From [" + SheetName + "] WHERE [ManufacturerPartNumber] IS NOT NULL";
但这不起作用,如果我将 DISTINCT [ManufacturerPartNumber]
更改为 ManufacturerPartNumber
,它就会起作用。它 returns 重复了我不想发生的行。
这是我得到的错误:
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred
Additional information: Syntax error (missing operator) in query expression 'DISTINCT [ManufacturerPartNumber]'.
示例数据
我的Excelsheet
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
谢谢
Tsql 能用吗?
我以前只见过这么用过distinct
SELECT DISTINCT Name, [ManufacturerPartNumber],Price From ...
更新
而且我刚刚注意到您缺少来自 DISTINCT 的 "c"。
更新 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:
你正在尝试 select from test
这是我的测试 table。您需要替换工作表名称。
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"