从选择查询中删除SQL中的几乎重复行

时间:2017-07-19 08:56:34

标签: sql sql-server

我正在尝试选择一些数据,但我得到'几乎重复的行' - 除了'gtin'列之外,所有列都是相同的,我期望的结果是只获得每个“几乎重复的行”中的一行

enter image description here

我想要达到的结果是只获得第1行和第1行。 3。

这是我正在使用的查询:

use Ixudra\Curl\Facades\Curl;

$response = Curl::to('https://api.blockcypher.com/v1/eth/main/addrs?token=YOURTOKEN')->post();

请帮助,谢谢。

2 个答案:

答案 0 :(得分:1)

您不能使用distinct来执行此操作,因为distinct仅适用于整行。 您最好的选择是使用group by:

SELECT 'Y' AS isEnterprise,
       ii.ITEM_ID AS itemId,
       ii.PARENT_ITEM_ID as PID,
       ii.ITEM_IDENTIFIER AS productNo,
       id.DESCRIPTION_1 AS description,
       id.DESCRIPTION_4 AS packValue,
       MIN(pid.PACKAGE_IDENTIFIER) AS gtin, -- Or max, if you want to...
       pac.PRE_PRICE_AMOUNT AS eachPrice
   FROM PWRNXGDTA.ITEM_INFORMATION AS ii
   JOIN PWRNXGDTA.ITEM_ASSORTMENT AS ia ON ii.ITEM_ID = ia.ITEM_ID
   JOIN PWRNXGDTA.ITEM_DESCRIPTION AS id ON id.ITEM_ID = ii.ITEM_ID
   JOIN PWRNXGDTA.BUSINESS_UNIT AS bu ON ii.LOGISTICS_BU_ID = bu.BU_ID
   LEFT JOIN PWRNXGDTA.ITEM_EXTN_NONMDM AS ien ON ien.ITEM_IDENTIFIER = ii.ITEM_IDENTIFIER
   LEFT JOIN PWRNXGDTA.LKP_CLASSIFICATION_CLASS AS lcc ON lcc.CLASSIFICATION_CLASS_ID = ii.CLASSIFICATION_CLASS_ID
   LEFT JOIN PWRNXGDTA.LKP_CLASSIFICATION_PBH AS lcp ON lcp.CLASSIFICATION_PBH_ID = ii.CLASSIFICATION_PBH_ID
   LEFT JOIN PWRNXGDTA.LKP_CLASSIFICATION_PBHF AS lcpf ON lcpf.CLASSIFICATION_PBHF_ID = ii.CLASSIFICATION_PBHF_ID
   LEFT JOIN PWRNXGDTA.LIST_PROPRIETARY_ITEM AS lpi ON lpi.ITEM_IDENTIFIER = ii.ITEM_IDENTIFIER
   JOIN PWRNXGDTA.PA_ASSORTMENT AS pa ON pa.PACKAGE_ASSORTMENT_ID = ia.PACKAGE_ASSORTMENT_ID
   JOIN PWRNXGDTA.ITEM_PRODUCT AS ip ON ip.ITEM_ID = ii.ITEM_ID
   JOIN PWRNXGDTA.PRODUCT_INFO AS pin ON pin.PRODUCT_ID = ip.PRODUCT_ID
   LEFT JOIN PWRNXGDTA.PRODUCT_NUTRITIONAL AS pn ON pn.PRODUCT_ID = ip.PRODUCT_ID
   LEFT JOIN PWRNXGDTA.LKP_BRAND_INFORMATION AS lbi ON lbi.BRAND_ID = pin.BRAND_ID
   AND lbi.RECORD_STATUS = 'A'
   JOIN PWRNXGDTA.PACKAGE_IDENTIFIER AS pid ON pid.PACKAGE_ID = pa.PACKAGE_ID
   JOIN PWRNXGDTA.PA_CONSUMABLE AS pac ON pac.PACKAGE_ASSORTMENT_ID = pa.PACKAGE_ASSORTMENT_ID 
   where id.DESCRIPTION_1 like '%HAAGEN DAZ VAN MLK CHO BR%'
GROUP BY ii.ITEM_ID, 
         ii.PARENT_ITEM_ID, 
         ii.ITEM_IDENTIFIER, 
         id.DESCRIPTION_1,
         id.DESCRIPTION_4,
         pac.PRE_PRICE_AMOUNT 

答案 1 :(得分:0)

另一种方法是使用Row_Number来避免这个组:

Select * from (
    SELECT DISTINCT 'Y' AS isEnterprise,
                    ii.ITEM_ID AS itemId,
                    ii.PARENT_ITEM_ID as PID,
                    ii.ITEM_IDENTIFIER AS productNo,
                    id.DESCRIPTION_1 AS description,
                    id.DESCRIPTION_4 AS packValue,
                    pid.PACKAGE_IDENTIFIER AS gtin,
                    RowN = Row_Number() over (Partition by ii.Item_Id, ii.Parent_Item_id order by pid.PACKAGE_IDENTIFIER) --Added new row_number if you want max you can use desc
                    pac.PRE_PRICE_AMOUNT AS eachPrice
    FROM PWRNXGDTA.ITEM_INFORMATION AS ii
    JOIN PWRNXGDTA.ITEM_ASSORTMENT AS ia ON ii.ITEM_ID = ia.ITEM_ID
    JOIN PWRNXGDTA.ITEM_DESCRIPTION AS id ON id.ITEM_ID = ii.ITEM_ID
    JOIN PWRNXGDTA.BUSINESS_UNIT AS bu ON ii.LOGISTICS_BU_ID = bu.BU_ID
    LEFT JOIN PWRNXGDTA.ITEM_EXTN_NONMDM AS ien ON ien.ITEM_IDENTIFIER = ii.ITEM_IDENTIFIER
    LEFT JOIN PWRNXGDTA.LKP_CLASSIFICATION_CLASS AS lcc ON lcc.CLASSIFICATION_CLASS_ID = ii.CLASSIFICATION_CLASS_ID
    LEFT JOIN PWRNXGDTA.LKP_CLASSIFICATION_PBH AS lcp ON lcp.CLASSIFICATION_PBH_ID = ii.CLASSIFICATION_PBH_ID
    LEFT JOIN PWRNXGDTA.LKP_CLASSIFICATION_PBHF AS lcpf ON lcpf.CLASSIFICATION_PBHF_ID = ii.CLASSIFICATION_PBHF_ID
    LEFT JOIN PWRNXGDTA.LIST_PROPRIETARY_ITEM AS lpi ON lpi.ITEM_IDENTIFIER = ii.ITEM_IDENTIFIER
    JOIN PWRNXGDTA.PA_ASSORTMENT AS pa ON pa.PACKAGE_ASSORTMENT_ID = ia.PACKAGE_ASSORTMENT_ID
    JOIN PWRNXGDTA.ITEM_PRODUCT AS ip ON ip.ITEM_ID = ii.ITEM_ID
    JOIN PWRNXGDTA.PRODUCT_INFO AS pin ON pin.PRODUCT_ID = ip.PRODUCT_ID
    LEFT JOIN PWRNXGDTA.PRODUCT_NUTRITIONAL AS pn ON pn.PRODUCT_ID = ip.PRODUCT_ID
    LEFT JOIN PWRNXGDTA.LKP_BRAND_INFORMATION AS lbi ON lbi.BRAND_ID = pin.BRAND_ID
    AND lbi.RECORD_STATUS = 'A'
    JOIN PWRNXGDTA.PACKAGE_IDENTIFIER AS pid ON pid.PACKAGE_ID = pa.PACKAGE_ID
    JOIN PWRNXGDTA.PA_CONSUMABLE AS pac ON pac.PACKAGE_ASSORTMENT_ID = pa.PACKAGE_ASSORTMENT_ID 
    where id.DESCRIPTION_1 like '%HAAGEN DAZ VAN MLK CHO BR%'
) a
Where a.RowN = 1