• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

从列到行,该如何解决

mysql 搞代码 7年前 (2018-05-31) 216次浏览 已收录 0个评论

从列到行

诊疗年月 Day_1 Day_2 Day_3 Day_4 … Day_31 
200904  1    0   1   0  … 0 
200905 0 0 0 0 … 0 
200906 1 0 0 0 … 0 
200907 0 0 0 0 … 1 
  … … … … … … … 

结果如下: 

诊疗年月日 
20090401 
20090403 
20090601 
20090731 

——解决方案——————–
估计你的原数据中不会出现 200902 Day_31=1 的情况,则对日期的有效性不做检查了。

SQL code

 select concat(诊疗年月,'01') from table1 where Day_1=1 union all select concat(诊疗年月,'02') from table1 where Day_2=1 union all select concat(诊疗年月,'03') from table1 where Day_3=1 union all select concat(诊疗年月,'04') from table1 where Day_4=1 union all select concat(诊疗年月,'05') from table1 where Day_5=1 union all select concat(诊疗年月,'06') from table1 where Day_6=1 union all select concat(诊疗年月,'07') from table1 where Day_7=1 union all select concat(诊疗年月,'08') from table1 where Day_8=1 union all select concat(诊疗年月,'09') from table1 where Day_9=1 union all select concat(诊疗年月,'10') from table1 where Day_10=1 union all select concat(诊疗年月,'11') from table1 where Day_11=1 union all select concat(诊疗年月,'12') from table1 where Day_12=1 union all select concat(诊疗年月,'13') from table1 where Day_13=1 union all select concat(诊疗年月,'14') from table1 where Day_14=1 union all select concat(诊疗年月,'15') from table1 where Day_15=1 union all select concat(诊疗年月,'16') from table1 where Day_16=1 union all select concat(诊疗年月,'17') from table1 where Day_17=1 union all select concat(诊疗年月,'18') from table1 where Day_18=1 union all select concat(诊疗年月,'19') from table1 where Day_19=1 union all select concat(诊疗年月,'20') from table1 where Day_20=1 union all select concat(诊疗年月,'21') from table1 where Day_21=1 union all select concat(诊疗年月,'22') from table1 where Day_22=1 union all select concat(诊疗年月,'23') from table1 where Day_23=1 union all select concat(诊疗年月,'24') from table1 where Day_24=1 union all select concat(诊疗年月,'25') from table1 where Day_25=1 union all select concat(诊疗年月,'26') from table1 where Day_26=1 union all select concat(诊疗年月,'27') from table1 where Day_27=1 union all select concat(诊疗年月,'28') from table1 where Day_28=1 union all select concat(诊疗年月,'29') from table1 where Day_29=1 union all select concat(诊疗年月,'30') from table1 where Day_30=1 union all select concat(诊疗年月,'31') from table1 where Day_31=1 
------解决方案--------------------

SQL code

 SELECT * FROM ( SELECT CASE day_1 WHEN 1 THEN CONCAT(诊断年月,'01') END AS yes FROM tabl UNION SELECT CASE day_2 WHEN 1 THEN CONCAT(诊断年月,'02') END AS yes FROM tabl UNION SELECT CASE day_3 WHEN 1 THEN CONCAT(诊断年月,'03') END AS yes FROM tabl UNION SELECT CASE day_4 WHEN 1 THEN CONCAT(诊断年月,'04') END AS yes FROM tabl) t WHERE t.yes IS NOT NULL;


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:从列到行,该如何解决
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址