当数据不存在时,T-SQL添加带零的行

时间:2016-07-14 08:44:43

标签: sql-server tsql

我知道这个问题已被回答了几次,但我仍然无法添加额外的行,而数据在它存在之前不存在。

DECLARE @Test TABLE 

(
ID NVARCHAR(10)
,Number INT

)
INSERT INTO @Test VALUES ('TG32',6)
INSERT INTO @Test VALUES ('TG32',6)
INSERT INTO @Test VALUES ('TG32',6)
INSERT INTO @Test VALUES ('TG32',2)
INSERT INTO @Test VALUES ('QD65',2)
INSERT INTO @Test VALUES ('QD65',3)
INSERT INTO @Test VALUES ('QD65',3)
INSERT INTO @Test VALUES ('HT76',7)
INSERT INTO @Test VALUES ('HT76',1)
INSERT INTO @Test VALUES ('HT76',5)

 SELECT CASE WHEN ID = 'TG32' THEN 'Hats'
            WHEN ID =  'QD65' THEN 'Coats'
            WHEN ID =  'HT76' THEN 'Shoes'
            WHEN ID =   'WR52' THEN 'Shirts' END
            , Number


 FROM @Test

我试图在结果中添加一个额外的行,其中显示了衬衫0,而没有ID WR52的数据,直到有?

enter image description here

4 个答案:

答案 0 :(得分:1)

试试这个

DECLARE @Test TABLE 
(
ID NVARCHAR(10)
,Number INT

)
INSERT INTO @Test VALUES ('TG32',6)
INSERT INTO @Test VALUES ('TG32',6)
INSERT INTO @Test VALUES ('TG32',6)
INSERT INTO @Test VALUES ('TG32',2)
INSERT INTO @Test VALUES ('QD65',2)
INSERT INTO @Test VALUES ('QD65',3)
INSERT INTO @Test VALUES ('QD65',3)
INSERT INTO @Test VALUES ('HT76',7)
INSERT INTO @Test VALUES ('HT76',1)
INSERT INTO @Test VALUES ('HT76',5)

--
SELECT CASE WHEN ID = 'TG32' THEN 'Hats'
            WHEN ID =  'QD65' THEN 'Coats'
            WHEN ID =  'HT76' THEN 'Shoes'
            WHEN ID =   'WR52' THEN 'Shirts' END
            , Number
FROM @Test

UNION ALL

SELECT 'Shirts', 0
WHERE
    NOT EXISTS
    (
        SELECT TOP 1 1 FROM @Test
        WHERE
            ID = 'WR52'
    )

UNION ALL

SELECT 'Hats', 0
WHERE
    NOT EXISTS
    (
        SELECT TOP 1 1 FROM @Test
        WHERE
            ID = 'TG32'
    )


UNION ALL

SELECT 'Coats', 0
WHERE
    NOT EXISTS
    (
        SELECT TOP 1 1 FROM @Test
        WHERE
            ID = 'QD65'
    )

UNION ALL

SELECT 'Shoes', 0
WHERE
    NOT EXISTS
    (
        SELECT TOP 1 1 FROM @Test
        WHERE
            ID = 'HT76'
    )

答案 1 :(得分:1)

您可以声明第二个表并使用左连接来确保始终显示您的类别。

Failed to Set Up SDK
Error:Module'app': platform 'Google Inc.: APIs 11' not found

这样您只需在一个地方添加新类别,而不必手动列出哪些类别没有任何项目。

此外,您可能希望获得每个项目的总计。你可以这样做:

AndroidStudioPreview

答案 2 :(得分:0)

SELECT
  CASE
    WHEN ID = 'TG32' THEN 'Hats'
    WHEN ID = 'QD65' THEN 'Coats'
    WHEN ID = 'HT76' THEN 'Shoes'
    WHEN ID = 'WR52' THEN 'Shirts'
  END AS 'Item',
  Number
FROM @Test
UNION ALL
SELECT
  'Shirts',
  0

答案 3 :(得分:0)

这看起来像是您应该使用单独的查找表将Id值映射到Item值的情况。使用公用表表达式(CTE)创建具有非常短寿命的查找表可以获得相同的结果:

-- Sample data.
declare @Test as Table ( Id NVarChar(10), Number Int );
insert into @Test ( Id, Number ) values
  ('TG32',6), ('TG32',6), ('TG32',6), ('TG32',2), ('QD65',2),
  ('QD65',3), ('QD65',3), ('HT76',7), ('HT76',1), ('HT76',5);
select * from @Test;  

-- Using a CTE with a lookup table.
with ProductLookup as (
  select ProductCode, Item
    from ( values ( 'TG32', 'Hats' ), ( 'QD65', 'Coats' ), ( 'HT76', 'Shoes' ), ( 'WR2', 'Shirts' ) ) as
      Alpha( ProductCode, Item ) )
  select PL1.Item, T.Number
    from @Test as T inner join
      ProductLookup as PL1 on PL1.ProductCode = T.Id
  union all
  select Item, 0
    from ProductLookup as PL2
    where not exists ( select 42 from @Test where Id = PL2.ProductCode );

请注意,此解决方案将生成一个"零" 任何缺少产品类型的行,而不仅仅是衬衫。

假设:您正在使用SQL Server 2005或更高版本。使用适当的软件(MySQL,Oracle,DB2,...)和版本标记数据库问题是有帮助的,例如: sql-server-2014