复杂查询

foxe 2006-08-02 05:03:35
表1:
id rid emp empdate emp1 emp1date emp1rej emp2 emp2date epm2rej
1 1 1001 2006-1-1 2002 2006-1-1 false 3003 2006-1-1 true
2 2 1002 2006-1-2 2002 2006-1-2 false 3003 2006-1-2 true
3 2 1002 2006-1-2 2002 2006-1-2 false 3003 2006-1-3 true

表2:
id rid sid rej_issue rej_type
1 1 1 NG DD
2 1 1 NG1 DM
3 2 2 NG DD
4 2 3 NG2 DE

表关系:表2的sid为表1的id
求:主报表:每个emp在empdate里emp,emp2rej = true 和emp3rej = true的数量
子报表:得到每个rej数量的详情,及1001 在 2006-1-1 那天 1 个 emprej2 的详情(查询表2可得)
...全文
260 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
foxe 2006-08-03
  • 打赏
  • 举报
回复
http://www.microsoft.com/china/MSDN/library/data/sqlserver/FiveWaystoRevupYourSQLPerformanCE.mspx?mfr=true
希望这个文章对我有所作用.
子陌红尘 2006-08-03
  • 打赏
  • 举报
回复
select
emp = b.emp,
date = a.empdate,
emp_Qty = sum(case when a.empdate=b.empdate then 1 else 0 end),
emp1rej_Qty = sum(case when a.empdate=b.emp1date and b.emp1rej=true then 1 else 0 end),
emp2rej_Qty = sum(case when a.empdate=b.emp2date and b.emp2rej=true then 1 else 0 end)
from
(select distinct empdate from 表1 union select distinct emp1date from 表1 union select distinct emp2date from 表1) a
cross join
表1 b
group by
b.emp,a.empdate
子陌红尘 2006-08-03
  • 打赏
  • 举报
回复
try:

select
b.emp,
a.empdate,
sum(case when a.empdate=b.empdate then 1 else 0 end),
sum(case when a.empdate=b.emp1date and b.emp1rej=true then 1 else 0 end),
sum(case when a.empdate=b.emp2date and b.emp2rej=true then 1 else 0 end)
from
(select distinct empdate from 表1
union
select distinct emp1date from 表1
union
select distinct emp2date from 表1) a
cross join
表1 b
group by
b.emp,a.empdate
foxe 2006-08-03
  • 打赏
  • 举报
回复
救火啊,大家快帮看看
foxe 2006-08-03
  • 打赏
  • 举报
回复
一早上线,往前提一下,请大家帮忙啊
foxe 2006-08-02
  • 打赏
  • 举报
回复
就是根据 1001/1002 在每天出现的次数
paoluo 2006-08-02
  • 打赏
  • 举报
回复
emp date emp_Qty emp1rej_Qty emp2rej_Qty
1001 2006-1-1 1 0 1
1002 2006-1-2 2 0 1
1002 2006-1-3 0 0 1
------------------------------------------------
emp_Qty的1,2,0怎麼計算的??
foxe 2006-08-02
  • 打赏
  • 举报
回复
报歉,描述不清楚,还是用表直观一点
foxe 2006-08-02
  • 打赏
  • 举报
回复
主报表:
emp date emp_Qty emp1rej_Qty emp2rej_Qty
1001 2006-1-1 1 0 1
1002 2006-1-2 2 0 1
1002 2006-1-3 0 0 1

子表:(当点击emp2rej_qty时展开)
点第1个
-----------------------------
rej_issue rej_type
NG DD
NG1 DM
-------------------------------

点第2个
-----------------------------
rej_issue rej_type
NG DD
-----------------------------

点第3个
-----------------------------
rej_issue rej_type
NG2 DE
----------------------------
LouisXIV 2006-08-02
  • 打赏
  • 举报
回复
...

What a poor chinese... --;
yyairr 2006-08-02
  • 打赏
  • 举报
回复
select count(emp) from 表1 where emp2rej = true and emp3rej = true order by empdate
select count(rej),emprej2 from 表2 where emp=1001 and empdate=2006-1-1
昵称被占用了 2006-08-02
  • 打赏
  • 举报
回复
重写下这两句

求:主报表:每个emp在empdate里emp,emp2rej = true 和emp3rej = true的数量
子报表:得到每个rej数量的详情,及1001 在 2006-1-1 那天 1 个 emprej2 的详情(查询表2可得)

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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