查询时怎么实现分条件支查询

jishengzu 2010-06-01 03:30:03
有表ServiceBill
id sfid zgid zlid quantity
----------------------------------------------------
1 1 0 3 1
2 0 2 3 2
3 0 0 3 1
4 1 0 0 3
5 0 2 0 1


和员工表Employee
id type name
--------------------------------
1 1 师傅
2 2 中工
3 3 助理

查询结果是
id type name quantity
---------------------------------------
1 1 师傅 4
2 2 中工 3
3 3 助理 4

我自己写的语句是 结果不行错误。

select *,(select count(quantity) from ServiceBill where 1=1 and
(case when emp.type=1 then sfid=emp.id when emp.type=2 then zgid=emp.id
when emp.type=3 then zlid=emp.id end)) from Employee emp





...全文
67 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jishengzu 2010-06-01
  • 打赏
  • 举报
回复
谢谢啦,我还以为无解。
you_tube 2010-06-01
  • 打赏
  • 举报
回复
ServiceBill跟Employee有关联么?
永生天地 2010-06-01
  • 打赏
  • 举报
回复

select *,quantity=(select sum(quantity) from ServiceBill where 1=1 and emp.id=
(case when emp.type=1 then sfid when emp.type=2 then zgid
when emp.type=3 then zlid end)) from Employee emp

/*
id type name quantity
----------- ----------- ---------- -----------
1 1 师傅 4
2 2 中工 3
3 3 助理 4

(所影响的行数为 3 行)

*/


jishengzu 2010-06-01
  • 打赏
  • 举报
回复
有表ServiceBill
id sfid zgid zlid quantity
----------------------------------------------------
1---1---0-----3----1
2-- 0---2----3-----2
3---0---0----3-----1
4---1---0-----0----3
5---0---2-----0----1


和员工表Employee
id type name
--------------------------------
1- 1--- 师傅
2 -2 ---中工
3 -3--- 助理

查询结果是
id type name quantity
---------------------------------------
1 --1 --师傅 ---4
2-- 2 --中工 ---3
3 --3 --助理 ---4


没答案就散分了,进来接分吧
dawugui 2010-06-01
  • 打赏
  • 举报
回复
create table ServiceBill(id int,sfid int,zgid int,zlid int,quantity int)
insert into ServiceBill values(1 ,1 ,0 ,3 ,1)
insert into ServiceBill values(2 ,0 ,2 ,3 ,2)
insert into ServiceBill values(3 ,0 ,0 ,3 ,1)
insert into ServiceBill values(4 ,1 ,0 ,0 ,3)
insert into ServiceBill values(5 ,0 ,2 ,0 ,1)
create table Employee(id int,type int,name varchar(10))
insert into Employee values(1 ,1 ,'师傅')
insert into Employee values(2 ,2 ,'中工')
insert into Employee values(3 ,3 ,'助理')
go

select m.* , quantity =
isnull((select sum(quantity) from ServiceBill where sfid = m.type),0) +
isnull((select sum(quantity) from ServiceBill where zgid = m.type),0) +
isnull((select sum(quantity) from ServiceBill where zlid = m.type),0)

from Employee m

drop table ServiceBill,Employee

/*
id type name quantity
----------- ----------- ---------- -----------
1 1 师傅 4
2 2 中工 3
3 3 助理 4

(所影响的行数为 3 行)
*/
永生天地 2010-06-01
  • 打赏
  • 举报
回复

select *,(select sum(quantity) from ServiceBill where 1=1 and emp.id=
(case when emp.type=1 then sfid when emp.type=2 then zgid
when emp.type=3 then zlid end)) from Employee emp


id type name
----------- ----------- ---------- -----------
1 1 师傅 4
2 2 中工 3
3 3 助理 4

(所影响的行数为 3 行)

jishengzu 2010-06-01
  • 打赏
  • 举报
回复
有表ServiceBill
id sfid zgid zlid quantity
----------------------------------------------------
1 1 0 3 1
2 0 2 3 2
3 0 0 3 1
4 1 0 0 3
5 0 2 0 1


和员工表Employee
id type name
--------------------------------
1 1 师傅
2 2 中工
3 3 助理

查询结果是
id type name quantity
---------------------------------------
1 1 师傅 4
2 2 中工 3
3 3 助理 4

jishengzu 2010-06-01
  • 打赏
  • 举报
回复
哎发上来后布局全变了

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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