Sql 我正在尝试在access查询中查找重复项而不删除
我正试图在access中建立一个表达式。如果商品代码和发票号重复,我需要某种标志。我希望该栏显示-1Sql 我正在尝试在access查询中查找重复项而不删除,sql,ms-access,Sql,Ms Access,我正试图在access中建立一个表达式。如果商品代码和发票号重复,我需要某种标志。我希望该栏显示-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
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
选择[Amerinet sales].[sales person],[Amerinet sales].ShipToName[Amerinet sales].ShipToAddress1[Amerinet sales].ShipToAddress2[Amerinet sales].ShipToAddress3[Amerinet sales].ShipToCity[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][sales person],[Amerinet Match]。ShipToName[Amerinet Match]。ShipToAddress1[Amerinet Match]。ShipToAddress2[Amerinet Match]。ShipToAddress3[Amerinet Match]。ShipToCity[Amerinet Match]。ShipToState[Amerinet Match]。ShipToZipCode[Amerinet Match]。项目代码[Amerinet Match]。单价[Amerinet Match]。数量[Amerinet Match].ExtensionAmt[Amerinet Match].InvoiceDate[Amerinet Match].InvoiceNo[Amerinet Match].Totals[Amerinet Match].BillToAddress1[Amerinet Match].BillToAddress2[Amerinet Match].BillToAddress3[Amerinet Match].BillToState[Amerinet Match].BillToZipCode[Amerinet Match].[GPO ID],[Amerinet Match].GLN[Amerinet Match].Field3[Amerinet Match].[DEA],[Amerinet Match].[Name 1],[Amerinet Match].[Address 1],[Amerinet Match].[Amerinet Match].[State/Province],[Amerinet Match].[Name 2],[Amerinet Match].[Address 2],[Amerinet Match].[Address 3],[Amerinet Match].国家[Amerinet Match].电话[Amerinet Match].传真[Amerinet Match].Field17[Amerinet Match].Field18[Amerinet Match].[Direct Parent GPO ID],[Amerinet Match].[Direct Parent Name 1],[Amerinet Match].[Top Parent Name 1],[Amerinet Match].Field23[Amerinet Match].Field24[Amerinet Match].Field25[Amerinet Match].[Class of Trade],[Amerinet Match].Field27[Amerinet Match].Field28[Amerinet Match].Field29[Amerinet Match].Field30[Amerinet Match].Field31[Amerinet Match].Field32[Amerinet Match].Field33[Amerinet Match].Field34[Amerinet Match].[Sub Class],[Amerinet Match].Alliance[Amerinet Match].IDN[Amerinet Match]。[唯一来源Mbr Y/N],[Amerinet匹配][床数]
从[Amerinet sales]左加入[Amerinet sales]上的[Amerinet Match]。项目代码=[Amerinet Match]。项目代码和[Amerinet sales]。发票编号=[Amerinet Match]。发票编号
按[Amerinet sales].[sales person],[Amerinet sales].ShipToName[Amerinet sales].ShipToAddress1[Amerinet sales].ShipToAddress2[Amerinet sales].ShipToAddress3[Amerinet sales].ShipToCity[Amerinet sales].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].BillToAddress1[Amerinet sales].BillToAddress2[Amerinet sales].BillToAddress3[Amerinet sales].BillToCity[Amerinet sales].BillToState[Amerinet sales].BillToZipCode[Amerinet Match]。[sales person],[Amerinet Match]。ShipToName[Amerinet Match]。ShipToAddress1[Amerinet Match]。ShipToAddress2[Amerinet Match]。ShipToAddress3[Amerinet Match]。ShipToCity[Amerinet Match]。ShipToState[Amerinet Match]。ShipToZipCode[Amerinet Match]。项目代码[Amerinet Match]。单价[Amerinet Match].QuantityShipped[Amerinet Match].ExtensionAmt[Amerinet Match].InvoiceDate[Amerinet Match].InvoiceNo[Amerinet Match].Totals[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 Match].Field3[Amerinet Match].[DEA],[Amerinet Match].[Name 1],[Amerinet Match].[Address 1],[Amerinet Match].[State/Province],[Amerinet Match].[Postal Code],[Amerinet Match].[Name 2],[Amerinet Match].[Address Type],[Amerinet Match].[Address 2],[Amerinet Match]。[Address 3],[Amerinet Match]。国家:[Amerinet Match]。电话[Amerinet Match]。传真[Amerinet Match]。字段17[Amerinet Match]。字段18[Amerinet Match]。[Direct Parent GPO ID],[Amerinet Match]。[Direct Parent Name 1],[Amerinet Match]。[Top Parent GPO ID],[Amerinet Match]。[Top Parent Name 1],[Amerinet Match]。字段23,[Amerinet Match].Field24
,[Amerinet Match].Field25[Amerinet Match].[Class of Trade],[Amerinet Match].Field27[Amerinet Match].Field28[Amerinet Match].Field29[Amerinet Match].Field30[Amerinet Match].Field31[Amerinet Match].Field32[Amerinet Match].Field33[Amerinet Match].[Sub Class],[Amerinet Match].Alliance[Amerinet Match].IDN,[Amerinet Match]。[Sole Source Mbr Y/N],[Amerinet Match]。[of Beds] 你想要这样的东西吗
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;
在添加了一个名为dupe的列之后,我使用MySQL用您的数据测试了这个查询。应该可以访问
生成以下输出
"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"
如果只需要找出是否有重复项,最好单独查询,按项目代码和发票号分组,并将计数*作为聚合。[您是否用表名替换了表?我替换了,但我也忘了提到这是查询的结果。我已将我的查询包含在上面。您能帮我将其添加到其中吗?我忘了提到这是查询的结果。这是我用于获取上面列出的结果的查询sql。您能告诉我如何将其添加到这里吗?如果您想添加查询,请告诉我指示符列您可以在单独的查询中执行此操作。然后,您可以根据需要将isDupe列合并到上述查询的选择条件中。否则,如果您必须将此操作作为大型查询的一部分,您可以将其添加到您的FROM子句中。请澄清我的最后一条评论。如果您想添加指示符列,请在单独的u中执行此操作更新查询。然后,您可以根据需要将isDupe列合并到后续查询的选择条件中。否则,如果您必须在大型查询中执行此操作,则可以通过删除UPDATE和SET子句并将查询与结果集关联,将SELECT部分添加到FROM子句中。
"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"