SQL 2005 SELECT CASE <column>语句</column>

时间:2009-01-31 20:26:58

标签: sql sql-server case unpivot

我真的很感激这方面的帮助,我被严重困扰了。基本上我有一张表

看起来像这样:

SSS_DOWID Name Mon  Tue  Wed  Thu   Fri   Sat   Sun   Description  
2         M    Y    N    N    N     N     N     N     Monday
3         MF   Y    N    N    N     Y     N     N     Monday, Friday
.......
18        T    N    Y    N    N     N     N     N     Tuesday
........
etc.

我需要做的是将此表中的值转换为仅包含

的表

星期几的相应数字, 例如,1表示星期日,2表示星期一,3表示星期二等,一直到8星期日。

所以我有一点SQL:

DECLARE @strDays table
(SSS_DOWID int)

INSERT INTO @strDays
SELECT  
  case (sun) when 'Y' then '1' else '' end +  
  case (mon) when 'Y' then '2' else '' end +   
  case (tue) when 'Y' then '3' else '' end +  
  case (wed) when 'Y' then '4' else '' end +   
  case (thu) when 'Y' then '5' else '' end +  
  case (fri) when 'Y' then '6' else '' end +   
  case (sat) when 'Y' then '7' else '' end  
 FROM   
  [dbo].SSS_DOW  WITH (NOLOCK)  
 WHERE  
  SSS_DOWID IN (28,41,44)  

SELECT * FROM @strDays

除了日间组合外,哪些日子适用于个别日子。所以在这种情况下,当我传入

28(周三),41(周五)和44(SaSun),我得到4(完美),6(完美)和17(哦废话 - 应该是

1和7,分别)。

任何人都可以帮我重组我的SQL,所以我得到一个包含1,4,6和7的表格

的4,6,17?

6 个答案:

答案 0 :(得分:2)

听起来你需要一个不支持。

DECLARE @Days TABLE
(
  DayId int PRIMARY KEY,
  found int
)

INSERT INTO @Days(DayId, found) SELECT 1, 0
INSERT INTO @Days(DayId, found) SELECT 2, 0
INSERT INTO @Days(DayId, found) SELECT 3, 0
INSERT INTO @Days(DayId, found) SELECT 4, 0
INSERT INTO @Days(DayId, found) SELECT 5, 0
INSERT INTO @Days(DayId, found) SELECT 6, 0
INSERT INTO @Days(DayId, found) SELECT 7, 0
--
UPDATE d
SET d.found = 1
FROM  @Days d JOIN SSS_DOW s
  ON 
 CASE
    WHEN d.DayId = 1 and s.Mon == 'Y' THEN 1
    WHEN d.DayId = 2 and s.Tue == 'Y' THEN 1
    WHEN d.DayId = 3 and s.Wed == 'Y' THEN 1
    WHEN d.DayId = 4 and s.Thu == 'Y' THEN 1
    WHEN d.DayId = 5 and s.Fri == 'Y' THEN 1
    WHEN d.DayId = 6 and s.Sat == 'Y' THEN 1
    WHEN d.DayId = 7 and s.Sun == 'Y' THEN 1
    ELSE 0
  END = 1
WHERE SSS_DowID in (28, 41, 44)
--
SELECT *
FROM @Days
WHERE found = 1

答案 1 :(得分:1)

WITH days AS (
    SELECT SSS_DOWID, 1 AS DayOfWeek FROM dbo.SSS_DOW WHERE sun = 'Y'
    UNION ALL
    SELECT SSS_DOWID, 2 AS DayOfWeek FROM dbo.SSS_DOW WHERE mon = 'Y'
    UNION ALL
    SELECT SSS_DOWID, 3 AS DayOfWeek FROM dbo.SSS_DOW WHERE tue = 'Y'
    UNION ALL
    SELECT SSS_DOWID, 4 AS DayOfWeek FROM dbo.SSS_DOW WHERE wed = 'Y'
    UNION ALL
    SELECT SSS_DOWID, 5 AS DayOfWeek FROM dbo.SSS_DOW WHERE thu = 'Y'
    UNION ALL
    SELECT SSS_DOWID, 6 AS DayOfWeek FROM dbo.SSS_DOW WHERE fri = 'Y'
    UNION ALL
    SELECT SSS_DOWID, 7 AS DayOfWeek FROM dbo.SSS_DOW WHERE sat = 'Y')
SELECT DayOfWeek
FROM days
WHERE SSS_DOWID IN (28,41,44);

答案 2 :(得分:1)

我觉得你在引用你的数字:

case (sun) when 'Y' then '1' else '' end +  
case (mon) when 'Y' then '2' else '' end + 

所以当你使用'+'时你得到的是串联而不是添加。这就是为什么你得到17作为答案。

至于将其分为两个单独的答案,您可能需要尝试此处列出的答案之一。

答案 3 :(得分:1)

在SQL Server 2005中,您可以使用显式UNPIVOT

/*
CREATE TABLE SSS_DOW (
    SSS_DOWID int NOT NULL
    ,[Name] varchar(50) NOT NULL
    ,Mon char(1) NOT NULL
    ,Tue char(1) NOT NULL
    ,Wed char(1) NOT NULL
    ,Thu char(1) NOT NULL
    ,Fri char(1) NOT NULL
    ,Sat char(1) NOT NULL
    ,Sun Char(1) NOT NULL
    ,[Description] varchar(50) NOT NULL
)

INSERT INTO SSS_DOW VALUES (2, 'M', 'Y', 'N', 'N', 'N', 'N', 'N', 'N', 'Monday')
INSERT INTO SSS_DOW VALUES (3, 'MF', 'Y', 'N', 'N', 'N', 'Y', 'N', 'N', 'Monday, Friday')
INSERT INTO SSS_DOW VALUES (28, 'W', 'N', 'N', 'Y', 'N', 'N', 'N', 'N', 'Wednesday')
INSERT INTO SSS_DOW VALUES (41, 'F', 'N', 'N', 'N', 'N', 'Y', 'N', 'N', 'Friday')
INSERT INTO SSS_DOW VALUES (44, 'SaSun', 'N', 'N', 'N', 'N', 'N', 'Y', 'Y', 'Satruday, Sunday')
*/

;WITH DateAbbrev AS (
    SELECT number, LEFT(DATENAME(dw, number - 2), 3) AS abbrev
    FROM master..spt_values
    WHERE type = 'P'
        AND number BETWEEN 1 AND 7
)
SELECT SSS_DOWID, DateAbbrev.number AS DayOfWeek
FROM SSS_DOW
UNPIVOT (Flag FOR dow IN ([Mon], [Tue], [Wed], [Thu], [Fri], [Sat], [Sun])) AS pvt
INNER JOIN DateAbbrev
    ON DateAbbrev.abbrev = dow
WHERE Flag = 'Y'
    AND SSS_DOWID IN (28, 41, 44)

/*
DROP TABLE SSS_DOW
*/

如果您已经有一个日期缩写表,您可以加入到该表中,而不是我用来创建日期缩写的临时CTE。

答案 4 :(得分:0)

尝试制作七个插入语句:

INSERT INTO @strDays 
  SELECT SSS_DOWID, 1 FROM SSS_DOW WHERE SSS_DOWID IN (28,41,44) AND Sun = 'Y';


INSERT INTO @strDays 
  SELECT SSS_DOWID, 2 FROM SSS_DOW WHERE SSS_DOWID IN (28,41,44) AND Mon = 'Y';

etc.

答案 5 :(得分:0)

尝试这样做,如果这不起作用,请告诉我。

DECLARE @strDays table
(SSS_DOWID int)

  INSERT INTO @strDays
 select substring(
 (SELECT  
 case (sun) when 'Y' then ',1' else '' end +  
 case (mon) when 'Y' then ',2' else '' end +   
 case (tue) when 'Y' then ',3' else '' end +  
 case (wed) when 'Y' then ',4' else '' end +   
 case (thu) when 'Y' then ',5' else '' end +  
 case (fri) when 'Y' then ',6' else '' end +   
  case (sat) when 'Y' then ',7' else '' end  
FROM   
  [dbo].SSS_DOW  WITH (NOLOCK)  
 WHERE  
 SSS_DOWID IN (28,41,44)  ),2,100)

SELECT * FROM @strDays