SQL Server 中的解码问题
我需要此查询的帮助:
SELECT
a.id
CASE
WHEN a.sco = 0
THEN DECODE (a.x, 1, '1', 2, '2', 33, '3' null, '0', 0, '0', a.x)
ELSE DECODE (a.sco, 1, '1', 2, '2',33, '3' null, '0', 0, '0', a.sco)
FORMAT a.dt, 'yyyy-MM') as date
DECODE (a.sco, 1, '1', 2, '2',33, '3' null, '0', 0, '0', a.sco)
DECODE (a.x, 1, '1', 2, '2',33, '3' null, '0', 0, '0', a.x)
FROM
aaa a
我想根据 sco 的值替换 x 中的值:
when sco = 0 values from x should be 1 if 1, if 2, 2 if 33, 3 if null, 0 if 0, 0
and when sco = 1,1, if 2, 2 if 33, 3 if null, 0 if 0, 0
and when x = 1,1, if 2, 2 if 33, 3 if null, 0 if 0, 0
id | sco | x | date |
---|---|---|---|
001 | 0 | 1 | 2019-04 |
002 | 0 | 2 | 2021-07 |
003 | 0 | 33 | 2009-09 |
004 | 0 | null | 2011-02 |
005 | 0 | 0 | 2020-02 |
001 | 1 | 1 | 2019-04 |
002 | 2 | 2 | 2021-07 |
003 | 33 | 33 | 2009-09 |
004 | null | null | 2011-02 |
005 | 2 | 0 | 2020-02 |
所需:
id | sco | x | 日期 |
---|---|---|---|
001 | 0 | 1 | 2019-04 |
002 | 0 | 2 | 2021-07 |
003 | 0 | 3 | 2009-09 |
004 | 0 | 0 | 2011-02 |
005 | 0 | 0 | 2020-02 |
001 | 1 | 1 | 2019-04 |
002 | 2 | 2 | 2021-07 |
003 | 3 | 3 | 2009-09 |
004 | 0 | 0 | 2011-02 |
005 | 2 | 0 | 2020年02月 |
I need help with this query:
SELECT
a.id
CASE
WHEN a.sco = 0
THEN DECODE (a.x, 1, '1', 2, '2', 33, '3' null, '0', 0, '0', a.x)
ELSE DECODE (a.sco, 1, '1', 2, '2',33, '3' null, '0', 0, '0', a.sco)
FORMAT a.dt, 'yyyy-MM') as date
DECODE (a.sco, 1, '1', 2, '2',33, '3' null, '0', 0, '0', a.sco)
DECODE (a.x, 1, '1', 2, '2',33, '3' null, '0', 0, '0', a.x)
FROM
aaa a
I want to replace values from x depending on values of sco:
when sco = 0 values from x should be 1 if 1, if 2, 2 if 33, 3 if null, 0 if 0, 0
and when sco = 1,1, if 2, 2 if 33, 3 if null, 0 if 0, 0
and when x = 1,1, if 2, 2 if 33, 3 if null, 0 if 0, 0
id | sco | x | date |
---|---|---|---|
001 | 0 | 1 | 2019-04 |
002 | 0 | 2 | 2021-07 |
003 | 0 | 33 | 2009-09 |
004 | 0 | null | 2011-02 |
005 | 0 | 0 | 2020-02 |
001 | 1 | 1 | 2019-04 |
002 | 2 | 2 | 2021-07 |
003 | 33 | 33 | 2009-09 |
004 | null | null | 2011-02 |
005 | 2 | 0 | 2020-02 |
desired:
id | sco | x | date |
---|---|---|---|
001 | 0 | 1 | 2019-04 |
002 | 0 | 2 | 2021-07 |
003 | 0 | 3 | 2009-09 |
004 | 0 | 0 | 2011-02 |
005 | 0 | 0 | 2020-02 |
001 | 1 | 1 | 2019-04 |
002 | 2 | 2 | 2021-07 |
003 | 3 | 3 | 2009-09 |
004 | 0 | 0 | 2011-02 |
005 | 2 | 0 | 2020-02 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论