如何在SQL Server中不使用OR条件选择特定记录

时间:2017-12-13 23:09:33

标签: sql sql-server sql-server-2014

Item_Code   |Item_Description        |EAN Code      |Price   |Company_Code| Unit|
------------+------------------------+--------------+--------+------------+-----+
S-035-ED-TU |TCCA 35ml Everyday (100)|0000093507752 |4.220000|SKH         |EACH      
S-035-ED-TU |TCCA 35ml Everyday (100)|10000093507759|4.220000|SKH         |SHIPPER  
S-035-ED-TU |TCCA 35ml Everyday (100)|10000093507759|4.220000|SKH         |INNER   
S-050-PP-50P|TCCA 50ml Kids Peppa Pig|10000093545324|9.500000|SKH         |SHIPPER   

问题 - 对于某些商品代码,只有1个单位(发货人)可用,但对于某些商品代码,有2或3个单位代码(每个,发件人,INNER),如上例所示。

我想使用一个查询,如果表中的特定项目有多个unit_codes,则选择“Each”,否则选择“Shipper”或“EACH”或“{{1}无论什么都可用。

由于 图莎尔

4 个答案:

答案 0 :(得分:0)

这是SQL Server中的两个解决方案..第二个我认为更好

CREATE TABLE #test (
    item_code NVARCHAR(1000),
    item_description NVARCHAR(1000),
    ean_code NVARCHAR(1000),
    price NVARCHAR(1000),
    campany_code NVARCHAR(1000),
    unit NVARCHAR(1000),
    )

INSERT INTO #test (
    item_code,
    item_description,
    ean_code,
    price,
    campany_code,
    unit
    )
VALUES (
    'S-035-ED-TU',
    'TCCA 35ml Everyday (100)',
    '0000093507752',
    '4.220000',
    'SKH',
    'EACH'
    ),
    (
    'S-035-ED-TU',
    'TCCA 35ml Everyday (100)',
    '0000093507752',
    '4.220000',
    'SKH',
    'SHIPPER'
    ),
    (
    'S-035-ED-TU',
    'TCCA 35ml Everyday (100)',
    '0000093507752',
    '4.220000',
    'SKH',
    'INNER'
    ),
    (
    'S-050-PP-50P',
    'TCCA 50ml Kids Peppa Pig',
    '10000093545324',
    '9.500000',
    'SKH',
    'SHIPPER'
    )

--Get all records just to see
SELECT *
FROM #test

--METHOD 1: One way you could do it
SELECT *
FROM #test t
WHERE unit = CASE 
        WHEN (
                SELECT count(*)
                FROM #test t2
                WHERE t.item_code = t2.item_code
                ) > 1
            THEN 'EACH'
        ELSE unit
        END

--METHOD 2: Maybe a better way to do it using a temp table
SELECT t.item_code,
    count(*) RecordsPerGroup
INTO #itemCodeCount
FROM #test t
GROUP BY item_code

SELECT t.*
FROM #test t
LEFT JOIN #itemCodeCount icc
    ON t.item_code = icc.item_code
WHERE t.unit = CASE 
        WHEN icc.RecordsPerGroup > 1
            THEN 'EACH'
        ELSE t.unit
        END

DROP TABLE #test

DROP TABLE #itemCodeCount

答案 1 :(得分:0)

对于支持"窗口功能的数据库"这可以做你想要的:

SELECT
      d.*
FROM (
      SELECT
            t.*
          , ROW_NUMBER() OVER (PARTITION BY Item_Code
            ORDER BY CASE WHEN Unit = 'EACH' THEN 1 
                          WHEN unint = 'SHIPPER' THEN 2 ELSE 3 END) AS unit_pref
      FROM yourtable t
      ) d
WHERE d.unit_pref = 1

nb:MySQL(目前为5.​​7)尚未支持"窗口函数",但会在版本8中执行此操作

答案 2 :(得分:0)

试试这个:

    create table #tmp(Item_Code VARCHAR(50),EAN_Code VARCHAR(100),Unit VARCHAR(10))



    insert into #tmp VALUES('S-035-ED-TU','10000093507759','SHIPPER')
    insert into #tmp VALUES('S-035-ED-TU','10000093507759','INNER')
    insert into #tmp VALUES('S-035-ED-TU','0000093507752','EACH')

    insert into #tmp VALUES('S-050-PP-50P','10000093545324','SHIPPER')


    select Item_Code 
    into #tmpMultiUnit
    from #tmp 
    Group by Item_Code
    Having Count(Unit)>1

    SELECT t.* from #tmp t
    LEFT JOIN #tmpMultiUnit tm on t.Item_Code=tm.Item_Code
    WHERE tm.Item_Code IS NULL OR t.Unit='EACH'

    Drop table #tmp

    DRop table #tmpMultiUnit

答案 3 :(得分:0)

我知道这已经晚了三 (3) 年,但以防万一有人有同样的问题,这是我的贡献。

使用您插图中的列,我需要创建表格:

CREATE TABLE #tmp (
    Item_Code NVARCHAR(100),
    Item_Description NVARCHAR(100),
    EAN_Code VARCHAR(100),
    Price DECIMAL(22,6),
    Company_Code NVARCHAR(50),
    Unit VARCHAR(10)
)

INSERT INTO #tmp VALUES('S-035-ED-TU','TCCA 35ml Everyday (100)', '10000093507759', 4.220000, 'SKH' ,'EACH')
INSERT INTO #tmp VALUES('S-035-ED-TU','TCCA 35ml Everyday (100)', '10000093507759', 4.220000, 'SKH' ,'SHIPPER')
INSERT INTO #tmp VALUES('S-035-ED-TU','TCCA 35ml Everyday (100)', '10000093507759', 4.220000, 'SKH' ,'INNER')
insert into #tmp VALUES('S-050-PP-50P','TCCA 50ml Kids Peppa Pig', '10000093545324', 9.500000, 'SKH', 'SHIPPER')

那么,查询可以这样表达:

SELECT t.Item_Code, t.Item_Description, t.EAN_Code, t.Price, t.Company_Code, 
        CASE WHEN uom.Count > 1 THEN 'EACH' ELSE t.Unit END AS Unit,
        t.Unit AS [(OriginalUnit)]
FROM #tmp t
    INNER JOIN (SELECT COUNT(1) AS Count, Item_Code FROM #tmp GROUP BY Item_Code) uom ON t.Item_Code = uom.Item_Code

结果:

Query result 1

现在,只要希望出现等效于 EACH 的新度量单位,此查询就不会通用,因为查询已被硬编码以在项目的多个单位时显示 EACH遇到代码。从众多的单位中选择单位,必须有依据。我冒昧地修改了表格,添加了一个 Conversion 列作为指示符。

CREATE TABLE #tmp (
    Item_Code NVARCHAR(100),
    Item_Description NVARCHAR(100),
    EAN_Code VARCHAR(100),
    Price DECIMAL(22,6),
    Company_Code NVARCHAR(50),
    Unit VARCHAR(10),
    Conversion float /* Added to automatically select which ever has the lowest conversion */
)

INSERT INTO #tmp VALUES('S-035-ED-TU','TCCA 35ml Everyday (100)', '10000093507759', 4.220000, 'SKH' ,'EACH', 1)    
INSERT INTO #tmp VALUES('S-035-ED-TU','TCCA 35ml Everyday (100)', '10000093507759', 4.220000, 'SKH' ,'INNER', 4)
INSERT INTO #tmp VALUES('S-035-ED-TU','TCCA 35ml Everyday (100)', '10000093507759', 4.220000, 'SKH' ,'SHIPPER', 24)
INSERT INTO #tmp VALUES('S-050-PP-50P','TCCA 50ml Kids Peppa Pig', '10000093545324', 9.500000, 'SKH', 'SHIPPER', 1)

然后我们只需要找到转换率最低的单位:

SELECT t.Item_Code, t.Item_Description, t.EAN_Code, t.Price, t.Company_Code, 
        CASE WHEN uom.NumUnit > 1 THEN uom.Unit ELSE t.Unit END AS Unit,
        t.Unit AS [(OriginalUnit)]
FROM #tmp t
    INNER JOIN (SELECT t2.NumUnit, t1.Item_Code, t1.Unit 
                FROM #tmp t1
                    INNER JOIN (SELECT COUNT(1) AS NumUnit, Item_Code, MIN(Conversion) AS Conversion
                                FROM #tmp       
                                GROUP BY Item_Code) t2 ON t1.Item_Code = t2.Item_Code AND t1.Conversion = t2.Conversion) uom ON t.Item_Code = uom.Item_Code

产生相同的结果,但 EACH 不是硬编码的:

Query 2

要验证加入的子查询是否正确选择,从主查询中提取并运行:

SELECT t2.NumUnit, t1.Item_Code, t1.Unit 
FROM #tmp t1
    INNER JOIN (SELECT COUNT(1) AS NumUnit, Item_Code, MIN(Conversion) AS Conversion
                FROM #tmp       
                GROUP BY Item_Code) t2 ON t1.Item_Code = t2.Item_Code AND t1.Conversion = t2.Conversion

结果:

Query 3