JAVA+SSH+SQL SERVER2000,现在有一张表,是订餐记录,订餐记录有上午和下午之分,订餐记录表只有三个字段,ID,UserName,OrderDate,OrderDate是datetime类型的。
要查询出这么个东西:
按UserName列出一个月的记录,每天分上午下午,定过餐显示*或者其他啥的标记。我现在想着用case then来写,写62个字段,要么就再建张表
要查询出这么个东西:
按UserName列出一个月的记录,每天分上午下午,定过餐显示*或者其他啥的标记。我现在想着用case then来写,写62个字段,要么就再建张表
select username,
sum(case day(ordertime) when 23 then (case tl when 'AM' then 1 else 0 end) else 0 end) as '23上午',
sum(case day(ordertime) when 23 then (case tl when 'PM' then 1 else 0 end) else 0 end) as '23下午'
from
(select ui.username,
substring(CONVERT(varchar(100),ordertime, 0),len(CONVERT(varchar(100),ordertime, 0))-1,2) as tl,
od.ordertime
from
orderdate od,userinfo ui where od.userid in ('060122') and
substring(CONVERT(varchar(100),od.ordertime, 0),len(CONVERT(varchar(100),od.ordertime, 0))-1,2)='PM'
and od.userid = ui.logid
union all
select ui.username,
substring(CONVERT(varchar(100),ordertime, 0),len(CONVERT(varchar(100),ordertime, 0))-1,2) as tl,
ordertime
from
orderdate od,userinfo ui
where od.userid in ('060122') and
substring(CONVERT(varchar(100),od.ordertime, 0),len(CONVERT(varchar(100),od.ordertime, 0))-1,2)='AM'
and od.userid = ui.logid) as t_orderdate group by username好吧,这只是其中一天的记录……
谢谢楼上了,等项目结束了我再仔细想想你那种方法,应该比我这个高效的多。