我试图在访问查询中找到Duplicate而不删除

时间:2014-06-16 20:29:44

标签: sql ms-access

我正在尝试在访问中添加表达式。如果商品代码和InvoiceNo重复,我需要有一些标志。我希望该列表示-1。

ItemCode    UnitPrice   InvoiceNo
R50         18.60       0446394
R50         18.60       0446395
R64         20.60       0444965
R64         20.60       0444965
R64         22.60       0446394
R64         22.60       0446394
G32         24.60       0445689
你能帮忙吗? 这是我用来获得这些结果的sql。

SELECT [Amerinet sales]。[销售人员],[Amerinet销售] .ShipToName,[Amerinet销售] .ShipToAddress1,[Amerinet销售] .ShipToAddress2,[Amerinet销售] .ShipToAddress3,[Amerinet销售] .ShipToCity,[ Amerinet销售] .ShipToState,[Amerinet销售] .ShipToZipCode,[Amerinet销售] .ItemCode,[Amerinet销售] .UnitPrice,[Amerinet销售] .QuantityShipped,[Amerinet销售] .ExtensionAmt,[Amerinet销售] .InvoiceDate,[Amerinet销售] .InvoiceNo,[Amerinet销售]。总计,[Amerinet销售] .BillToName,[Amerinet销售] .BillToAddress1,[Amerinet销售] .BillToAddress2,[Amerinet销售] .BillToAddress3,[Amerinet销售] .BillToCity,[Amerinet销售] ] .BillToState,[Amerinet sales] .BillToZipCode,[Amerinet Match]。[销售人员],[Amerinet Match] .ShipToName,[Amerinet Match] .ShipToAddress1,[Amerinet Match] .ShipToAddress2,[Amerinet Match] .ShipToAddress3,[ Amerinet Match] .ShipToCity,[Amerinet Match] .ShipToState,[Amerinet Match] .ShipToZipCode,[Amerinet Match] .ItemCode,[Amerinet Match] .UnitPrice,[Amerinet匹配] .QuantityShipped,[Amerinet Match] .ExtensionAmt,[Amerinet Match] .InvoiceDate,[Amerinet Match] .InvoiceNo,[Amerinet Match] .Totals,[Amerinet Match] .BillToName,[Amerinet Match] .BillToAddress1,[Amerinet Match] ] .BillToAddress2,[Amerinet Match] .BillToAddress3,[Amerinet Match] .BillToCity,[Amerinet Match] .BillToState,[Amerinet Match] .BillToZipCode,[Amerinet Match]。[GPO ID],[Amerinet Match] .GLN,[ Amerinet比赛] .Field3,[Amerinet比赛]。[DEA#],[Amerinet比赛]。[名字1],[Amerinet比赛]。[地址1],[Amerinet比赛] .City,[Amerinet比赛]。[状态] /省],[Amerinet比赛]。[邮政编码],[Amerinet比赛]。[名称2],[Amerinet比赛]。[地址类型],[Amerinet比赛]。[地址2],[Amerinet比赛]。[地址3],[Amerinet比赛]。国家,[Amerinet比赛] .Phone,[Amerinet比赛] .Fax,[Amerinet比赛] .Field17,[Amerinet比赛] .Field18,[Amerinet比赛]。[直接父GPO ID] ,[Amerinet Match]。[直接父母姓名1],[Amerinet比赛]。[顶级家长GPO ID],[Amerinet比赛]。[Top Parent Na]我1,[Amerinet比赛] .Field23,[Amerinet比赛] .Field24,[Amerinet比赛] .Field25,[Amerinet比赛]。[交易等级],[Amerinet比赛] .Field27,[Amerinet比赛] .Field28, [Amerinet比赛] .Field29,[Amerinet比赛] .Field30,[Amerinet比赛] .Field31,[Amerinet比赛] .Field32,[Amerinet比赛] .Field33,[Amerinet比赛] .Field34,[Amerinet比赛]。[Sub- Class],[Amerinet Match] .Alliance,[Amerinet Match] .IDN,[Amerinet Match]。[Sole Source Mbr Y / N],[Amerinet Match]。[床位数] FROM [Amerinet sales] LEFT JOIN [Amerinet Match] ON([Amerinet sales] .ItemCode = [Amerinet Match] .ItemCode)AND([Amerinet sales] .InvoiceNo = [Amerinet Match] .InvoiceNo) GROUP BY [Amerinet销售]。[销售人员],[Amerinet销售] .ShipToName,[Amerinet销售] .ShipToAddress1,[Amerinet销售] .ShipToAddress2,[Amerinet销售] .ShipToAddress3,[Amerinet销售] .ShipToCity,[Amerinet销售] .ShipToState,[Amerinet sales] .ShipToZipCode,[Amerinet sales] .ItemCode,[Amerinet sales] .UnitPrice,[Amerinet sales] .QuantityShipped,[Amerinet sales] .ExtensionAmt,[Amerinet sales] .InvoiceDate,[Amerinet sales] .InvoiceNo,[Amerinet sales] .Totals,[Amerinet sales] .BillToName,[Amerinet sales] .BillToAddress1,[Amerinet sales] .BillToAddress2,[Amerinet sales] .BillToAddress3,[Amerinet sales] .BillToCity,[Amerinet sales]。 BillToState,[Amerinet sales] .BillToZipCode,[Amerinet Match]。[销售人员],[Amerinet Match] .ShipToName,[Amerinet Match] .ShipToAddress1,[Amerinet Match] .ShipToAddress2,[Amerinet Match] .ShipToAddress3,[Amerinet Match ] .ShipToCity,[Amerinet Match] .ShipToState,[Amerinet Match] .ShipToZipCode,[Amerinet Match] .ItemCode,[Amerinet Match] .UnitPrice,[Amerinet]匹配] .QuantityShipped,[Amerinet Match] .ExtensionAmt,[Amerinet Match] .InvoiceDate,[Amerinet Match] .InvoiceNo,[Amerinet Match] .Totals,[Amerinet Match] .BillToName,[Amerinet Match] .BillToAddress1,[Amerinet Match] ] .BillToAddress2,[Amerinet Match] .BillToAddress3,[Amerinet Match] .BillToCity,[Amerinet Match] .BillToState,[Amerinet Match] .BillToZipCode,[Amerinet Match]。[GPO ID],[Amerinet Match] .GLN,[ Amerinet比赛] .Field3,[Amerinet比赛]。[DEA#],[Amerinet比赛]。[名字1],[Amerinet比赛]。[地址1],[Amerinet比赛] .City,[Amerinet比赛]。[状态] /省],[Amerinet比赛]。[邮政编码],[Amerinet比赛]。[名称2],[Amerinet比赛]。[地址类型],[Amerinet比赛]。[地址2],[Amerinet比赛]。[地址3],[Amerinet比赛]。国家,[Amerinet比赛] .Phone,[Amerinet比赛] .Fax,[Amerinet比赛] .Field17,[Amerinet比赛] .Field18,[Amerinet比赛]。[直接父GPO ID] ,[Amerinet Match]。[直接父母姓名1],[Amerinet比赛]。[顶级家长GPO ID],[Amerinet比赛]。[Top Parent Nam] e 1],[Amerinet Match] .Field23,[Amerinet Match] .Field24,[Amerinet Match] .Field25,[Amerinet Match]。[贸易等级],[Amerinet Match] .Field27,[Amerinet Match] .Field28, [Amerinet比赛] .Field29,[Amerinet比赛] .Field30,[Amerinet比赛] .Field31,[Amerinet比赛] .Field32,[Amerinet比赛] .Field33,[Amerinet比赛] .Field34,[Amerinet比赛]。[Sub- Class],[Amerinet Match] .Alliance,[Amerinet Match] .IDN,[Amerinet Match]。[Sole Source Mbr Y / N],[Amerinet Match]。[床位数];

2 个答案:

答案 0 :(得分:0)

你想要这样的东西吗?

select t.*,
       iif(cnt > 1, -1, 0) as HasDuplicate
from test as t inner join
      (select itemcode, invoiceno, count(*) as cnt
       from test as t
       group by itemcode, invoiceno
      ) as ii
      on ii.itemcode = t.itemcode and ii.invoiceno = t.invoiceno;

答案 1 :(得分:0)

Have you tried it with an inner join?

添加一个名为dupe的列后,我使用MySQL测试了这个查询。应该可以访问。

update test a
join (SELECT itemCode, invoiceNo
            from test t 
    GROUP BY itemCode, invoiceNo 
    HAVING COUNT(*) > 1 ) x 
ON (a.itemCode = x.itemCode AND a.invoiceNo = x.invoiceNo)
SET dupe = "-1"

产生以下输出

"itemCode"  "UnitPrice" "InvoiceNo" "dupe"
"R50"            "19"    "0446394"  "0"
"R50"            "19"    "0446395"  "0"
"R64"            "21"    "0444965"  "-1"
"R64"            "21"    "0444965"  "-1"
"R64"            "23"    "0446394"  "-1"
"R64"            "23"    "0446394"  "-1"
"G32"            "25"    "0445689"  "0"