注册 登录
编程论坛 SQL Server论坛

求助:多表关联查询的问题

ycbbg 发布于 2012-04-05 10:36, 812 次点击
以下是我的SQL语句:
select  distinct
tu.user_name as 员工姓名,
td.dept_name as 部门,
tdp.dept_name as 平台,
tc.contract_quantity  as 本月订单,
tc2.contract_quantity as 关联了机会的订单
from tc_user  tu
inner join tc_department td on tu.dept_id = td.dept_id and =1
inner join tc_department tdp on td.parent_id = tdp.dept_id and =1
inner join tc_contract tc on tc.owner_user_id=tu.user_id and month(tc.audit_time)=month(getdate())
inner join tc_contract tc2 on tc2.owner_user_id=tu.user_id and month(tc2.audit_time)=month(getdate()) and tc2.optnty_id!=0   
where and tu.user_name='刘丹'
得到如下结果:
员工姓名    部门    平台    本月订单    关联了机会的订单
刘丹    商务部    财务中心    1    1
刘丹    商务部    财务中心    1    2
刘丹    商务部    财务中心    1    4
刘丹    商务部    财务中心    1    40
刘丹    商务部    财务中心    2    1
刘丹    商务部    财务中心    2    2
刘丹    商务部    财务中心    2    4
刘丹    商务部    财务中心    2    40
刘丹    商务部    财务中心    4    1
刘丹    商务部    财务中心    4    2
刘丹    商务部    财务中心    4    4
刘丹    商务部    财务中心    4    40
刘丹    商务部    财务中心    40    1
刘丹    商务部    财务中心    40    2
刘丹    商务部    财务中心    40    4
刘丹    商务部    财务中心    40    40

我想要的是:
员工姓名    部门    平台    本月订单    关联了机会的订单
刘丹    商务部    财务中心    1    1
刘丹    商务部    财务中心    2    2
刘丹    商务部    财务中心    2    0
刘丹    商务部    财务中心    4    4
刘丹    商务部    财务中心    40    40
5 回复
#2
ycbbg2012-04-05 11:04
有人知道吗,求高手啊
#3
k18685482012-04-23 16:13
回复 2楼 ycbbg
加 distinct
#4
netlin2012-04-24 17:28
很想帮你,不过,对你提的问题,还是有一些地方不清楚!
或者,你对你想要的结果,做一些说明,这样容易解决问题!
#5
wjw200404012012-04-27 16:14
问题都没说清楚,没看出你像要的结果和你查询出来的结果有什么区别
#6
z64391862012-05-10 11:10
一些什么条件都没加,楼主自己也没说要得到什么结果,相帮都帮不了啊
1