简体   繁体   English

在sql连接中计算唯一记录

[英]Calculate unique records in sql join

Below query gives 400 record out of which 20 records is same based on UPC. 下面的查询给出了400条记录,其中基于UPC的20条记录是相同的。 In other words I want 20 distinct records based on UPC. 换句话说,我想要基于UPC的20条不同的记录。 How can I do it? 我该怎么做?

 select tc.ChainID,tc.UPC,tc.SupplierInvoiceNumber,pc.DateTimeCreated
     from #tmpCompareData tc
     left join ProductCatalog pc
    ON tc.ProductID=pc.ProductID 

Well, depending on what you are after, one solution would be to group by tc.UPC . 好吧,根据您的需求,一种解决方案是按tc.UPC

For example: 例如:

SELECT
    MIN(tc.ChainID),
    tc.UPC,
    MIN(tc.SupplierInvoiceNumber),
    MIN(pc.DateTimeCreated)
FROM #tmpCompareData tc
    LEFT JOIN ProductCatalog pc
ON tc.ProductID = pc.ProductID
GROUP BY tc.UPC

Note, that you need to apply aggregate functions ( MIN() , MAX() , etc.) to the other selected columns to tell SQL how you want to return that data. 请注意,您需要将聚合函数( MIN()MAX()等)应用于其他选定的列,以告诉SQL您希望如何返回该数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM