SQL 2005 SELECT CASE<列>陈述列>
我真的很感谢这方面的帮助,我严重陷入困境。 基本上我有一个
如下所示的表:
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(周六周日)时,我得到 4(完美)、6(完美)和 17(哦废话 - 应该
分别是 1 和 7) )。
谁能帮我重构我的 SQL,以便我得到一个包含 1、4、6 和 7 而不是
4、6、17 的表?
I would really appreciate help with this, I am seriously stuck. Basically I have a table that
looks like this:
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.
What I need to do is to convert the values in this table to a table that contains only the
corresponding numbers for days of the week,
e.g, 1 for Sunday, 2 for Monday, 3 for Tuesday, etc., all the way until 8 for Sunday.
SO I have this little bit of 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
Which works fine for individual days, EXCEPT day combinations. So in this case, when I pass in
28 (Wed), 41 (Fri), and 44 (SaSun), I get 4 (perfect), 6 (perfect), and 17 (oh crap - should be
1 and 7, separately).
Can anyone please help me restructure my SQL so I get a table containing 1, 4, 6, and 7 instead
of 4, 6, 17?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
听起来你需要取消旋转。
Sounds like you need an un-pivot.
在我看来,您正在引用您的数字:
因此,使用“+”时您得到的是连接而不是加法。 这就是为什么你的答案是 17。
至于将其分成一列中的两个单独的答案,您可能需要尝试此处列出的答案之一。
It looks to me like you are quoting your numbers:
So what you are getting when using '+' is concatenation instead of addition. That is why you are getting 17 as your answer.
As for breaking it up into two separate answers in a column, you may need to try one of the answers listed here.
在 SQL Server 2005 中,您可以使用显式
UNPIVOT
:如果您已有日期缩写表,则可以加入该表,而不是使用我用来创建的临时 CTE。
In SQL Server 2005, you can use the explicit
UNPIVOT
:If you already have a table of date abbreviations, you can join to that instead of the ad hoc CTE I used to create one.
尝试制作七个插入语句:
Try making seven insert statements:
尝试这样做,如果这不起作用请告诉我。
Try doing like this and let me know if this does not work.