请问各位大神如何写这个语句,拜托了各位

ina459371407 2016-08-30 04:11:17
来自一个sql学的很烂的新手程序员。
是这样的,我有一个表T_Customer,其字段如下图所示,

为了统计汇总以及连接别的表,我希望把这张表变成含有客户ID,客户名,操作员ID,以及拜访星期,这四个字段,因为某一个客户一周可能会拜访不止一次,我希望,在变更后的新表或新视图中拜访几次就有几条记录,比如图中第一个客户星期五和星期六都为True,新表中就要在星期这个字段出现两条记录,前三个字段都一样,会有冗余。第一次提问,不知我表述清楚了没有,恳请各位大大不吝赐教,谢谢
...全文
136 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
mzq3214 2016-08-31
  • 打赏
  • 举报
回复
-- 感觉使用 union 好些 select custID , custName , opeID , 'Monday' from mytable where Monday = 'true' union select custID , custName , opeID , 'TuesDay' from mytable where TuesDay = 'true '
ina459371407 2016-08-31
  • 打赏
  • 举报
回复
引用 7 楼 wmxcn2000 的回复:
[quote=引用 6 楼 ina459371407 的回复:] 什么来了两次?是说每周拜访两次?那就是在结果里有两行

-- 使用 union all 
select custID , custName , opeID , 'Monday'
from mytable where Monday = 'true'
union all
select custID , custName , opeID , 'TuesDay'
from mytable where TuesDay = 'true
'
[/quote] 大神,太棒了,就是我想要的结果,又学到了,分分送上~
卖水果的net 2016-08-31
  • 打赏
  • 举报
回复
引用 6 楼 ina459371407 的回复:
什么来了两次?是说每周拜访两次?那就是在结果里有两行

-- 使用 union all 
select custID , custName , opeID , 'Monday'
from mytable where Monday = 'true'
union all
select custID , custName , opeID , 'TuesDay'
from mytable where TuesDay = 'true
'
ina459371407 2016-08-31
  • 打赏
  • 举报
回复
引用 5 楼 wmxcn2000 的回复:
如果来了两次呢 ?
什么来了两次?是说每周拜访两次?那就是在结果里有两行
卖水果的net 2016-08-30
  • 打赏
  • 举报
回复
如果来了两次呢 ?
ina459371407 2016-08-30
  • 打赏
  • 举报
回复
引用 3 楼 wmxcn2000 的回复:
select custID , custName , opeID , 
case when Monday = 'true' then ',Monday ' end + case when TuesDay = 'true' then ',TuesDay ' end 
--  这样?
from mytable
其实我想实现的效果,拿图片里第一行来说,就是这样的 CusID CusName OpID WeekDay --------- ------------- ------------ ----------- 0011 客户11 111 Friday 0011 客户11 111 Saturday
卖水果的net 2016-08-30
  • 打赏
  • 举报
回复
select custID , custName , opeID , 
case when Monday = 'true' then ',Monday ' end + case when TuesDay = 'true' then ',TuesDay ' end 
--  这样?
from mytable
ina459371407 2016-08-30
  • 打赏
  • 举报
回复
引用 1 楼 wmxcn2000 的回复:
select custID , custName , opeID , 
case when Monday = 'true' then 1 end + case when TuesDay = 'true' then 1 end 
-- 都写全了
from mytable
版主大大,可能是我没表述清楚,那个星期字段不是统计次数,是要显示Monday,Tuesday这些具体的内容
卖水果的net 2016-08-30
  • 打赏
  • 举报
回复
select custID , custName , opeID , 
case when Monday = 'true' then 1 end + case when TuesDay = 'true' then 1 end 
-- 都写全了
from mytable

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧