我有这样的表格 VB(名称字符(25),数量数字(6),ret_date 日期):
Name Amount Ret_date ---- ------ -------- abc 500 2011-01-21 def 200 2011-01-20 ghi 1000 xyz 800 2011-01-22 def 200 def 400 2011-02-01 pqr 2100 2011-02-12 abc 3000 abc 4100 xyz 700 2011-01-22
我想要的是(基于不同名称的 ret_date 计数:总计数、返回计数、待处理计数):
Name tc rc pc
abc 3 1 2
def 3 2 1
xyz 2 2 0
ghi 1 0 1
pqr 1 1 0
我已经尝试了以下查询,没有任何效果!
SELECT vb.name, COUNT(*) tc, r.rc, p.pc FROM vb, ;
(SELECT name, COUNT(*) rc FROM vb WHERE ret_date != {} group BY name) r, ;
(SELECT name, COUNT(*) pc FROM vb WHERE ret_date = {} group BY name) p ;
GROUP BY vb.name ORDER BY tc desc
SELECT vb.name, COUNT(*) tc, r.rc, p.pc FROM vb ;
LEFT JOIN (select name, COUNT(*) rc FROM vb WHERE ret_date != {} group BY name) r ON r.name = vb.name ;
LEFT JOIN (select name, COUNT(*) pc FROM vb WHERE ret_date = {} group BY name) p ON p.name = vb.name ;
GROUP BY vb.name
SELECT vb.name, COUNT(*) tc, r.rc, p.pc FROM vb ;
LEFT JOIN (select r.name, COUNT(*) rc FROM vb r WHERE ret_date != {} group BY name) r ON r.name = vb.name ;
LEFT JOIN (select p.name, COUNT(*) pc FROM vb p WHERE ret_date = {} group BY name) p ON p.name = vb.name ;
GROUP BY vb.name
我能够通过 3 个单独的查询获得计数(总计数,ret_date = {} 的计数,ret_date != {}),但无法使用单个查询同时获得。请帮助我获得所需的输出。我是 SQL 新手,可以理解基础知识,但连接、子查询之类的东西仍然很难理解!如果您能解释您的查询是做什么的,那将会很有帮助。
注意:我使用的是 MS Visual Foxpro 9.0 (Windows 7)。{ }是 Foxpro 中用于表示日期类型为空的符号,!=不等于运算符。
非常感谢。