日期:2014-05-18  浏览次数:20619 次

求条SQL语句,谢谢
SQL code
--总单数
select twoqudao as 客户,COUNT(Invoice) as 总单数 from Connect where twoqudao is not null group by twoqudao
/*客户    总单数
道外    3
盛物    211
邦物    437
勤美    5011
邦快    836
丰速    1
鸿物    568
莞包    2
*/
--已完成单数
select  twoqudao as 客户,COUNT(Invoice) as 已完成单数 from Connect where twoqudao is not null and khdate is not null group by twoqudao
/*客户    已完成单数
道外    3
盛物    204
邦物    349
勤美    4986
邦快    822
丰速    1
鸿物    518
莞包    2
*/
--未完成单数
select  twoqudao as 客户,COUNT(Invoice) as 未完成单数 from Connect where twoqudao is not null and khdate is null group by twoqudao
/*客户    未完成单数
道外    0
盛物    7
邦物    88
勤美    25
邦快    14
丰速    0
鸿物    50
莞包    0
*/
--超期单数
select  twoqudao as 客户,COUNT(Invoice) as 超期单数 from Connect where twoqudao is not null and khdate is null and cqdate<0 group by twoqudao 
/*
客户    超期单数
道外    0
盛物    6
邦物    48
勤美    3
邦快    12
丰速    0
鸿物    38
莞包    0 */

把四条语句合并成一条,得出以下结果
--得出结果
/*
客户    总单数    已完成单数    未完成单数    超期单数
道外    3    3    0    0
盛物    211    204    7    6
邦物    437    349    88    48
勤美    5011    4986    25    3
邦快    836    822    14    12
丰速    1    1    0    0
鸿物    568    518    50    38
莞包    2    2    0    0
*/




------解决方案--------------------
SQL code

SELECT * FROM T1.twoqudao as 客户,T2.总单数,T3.已完成单数,T4.超期单数
FROM (select twoqudao ,COUNT(Invoice) as 总单数 from Connect where twoqudao is not null group by twoqudao) T1,
(select  twoqudao ,COUNT(Invoice) as 已完成单数 from Connect where twoqudao is not null and khdate is not null group by twoqudao) T2,
(select  twoqudao ,COUNT(Invoice) as 未完成单数 from Connect where twoqudao is not null and khdate is null group by twoqudao) T3,
(select  twoqudao ,COUNT(Invoice) as 超期单数 from Connect where twoqudao is not null and khdate is null and cqdate<0 group by twoqudao )T4
WHERE T1.twoqudao=T2.twoqudao AND T2.twoqudao=T3.twoqudao AND  T3.twoqudao=T4.twoqudao

------解决方案--------------------
SQL code
SELECT T1.twoqudao as 客户,T2.总单数,T3.已完成单数,T4.超期单数
FROM (select twoqudao ,COUNT(Invoice) as 总单数 from Connect where twoqudao is not null group by twoqudao) T1,
(select  twoqudao ,COUNT(Invoice) as 已完成单数 from Connect where twoqudao is not null and khdate is not null group by twoqudao) T2,
(select  twoqudao ,COUNT(Invoice) as 未完成单数 from Connect where twoqudao is not null and khdate is null group by twoqudao) T3,
(select  twoqudao ,COUNT(Invoice) as 超期单数 from Connect where twoqudao is not null and khdate is null and cqdate<0 group by twoqudao )T4
WHERE T1.twoqudao=T2.twoqudao AND T2.twoqudao=T3.twoqudao AND  T3.twoqudao=T4.twoqudao

------解决方案--------------------
select twoqudao as 客户,(select COUNT(1) from Connect b where b.twoqudao=a.twoqudao) as '总单数',
(select COUNT(1) from Connect c where khdate is not null and c.twoqudao=a.twoqudao) as '已完成单数',
(select COUNT(1) from Connect d where khdate is null and d.twoqudao=a.twoqudao) as '未完成单数',
(select COUNT(1) from Connect e where khdate is not null and cqdate<0 and e.twoqudao=a.twoqudao) as '超期单数'
from Connect a
group by twoqudao
order by '总单数' desc