我有以下疑问:
SELECT cust_id,cust_name,SUM(calls.duration) as duration
FROM
calls, telephone
WHERE calls.from_t_no = telephone.t_no
AND
calls.duration <=60
group by telephone.cust_id, telephone.cust_name它给出了以下结果;
cust_id cust_name duration
0123456789 Avi 18
1234567890 Benny 27
2345678901 Gadi 13
3456789012 Dalia 69
4567890123 Hilla 5
5678901234 Varda 14
7890123456 Haim 20
8901234567 Tali 20
9012345678 Yoram 46我也有这样的疑问:
SELECT cust_id,cust_name,SUM(calls.duration) as duration
FROM calls ,telephone
WHERE to_t_no = telephone.t_no
AND
calls.duration >=500
group by telephone.cust_id, telephone.cust_name它提供了以下结果:
cust_id cust_name duration
2345678901 Gadi 50022
4567890123 Hilla 50000第二个查询实际上是第一个查询应该匹配的另一个过滤器或条件。我不能将第二个查询包含在第一个查询中,因为它使用了不同的列。我需要从第一个查询中“减去”第二个查询。
我该怎么做?
发布于 2011-09-25 21:12:02
如果你确实不能合并查询,你可以像这样连接结果:
SELECT T0.cust_id, T0.cust_name, T0.duration FROM
( SELECT cust_id,cust_name,SUM(calls.duration) as duration
FROM
calls, telephone
WHERE calls.from_t_no = telephone.t_no
AND
calls.duration <=60
group by telephone.cust_id, telephone.cust_name) T0
JOIN
(SELECT cust_id,cust_name,SUM(calls.duration) as duration
FROM calls ,telephone
WHERE to_t_no = telephone.t_no
AND
calls.duration >=500
group by telephone.cust_id, telephone.cust_name) T1
ON T0.cust_id = T1.cust_id发布于 2011-09-25 21:17:08
试着发布你的表格的完整结构,并描述你真正需要的东西。
如果我对问题的理解正确的话,你所暴露的条件是相互排斥的。我认为任何记录都不可能同时满足这两个条件,因为最终查询将如下所示:
SELECT cust_id,cust_name,SUM(calls.duration) as duration
FROM calls ,telephone
WHERE
to_t_no = telephone.t_no
AND calls.from_t_no = telephone.t_no
AND calls.duration <=60
AND calls.duration >=500
group by telephone.cust_id, telephone.cust_name;如果你想要的是一个通用的解决方案来进行"query over a query“,你可以使用这样的句子:
SELECT field1, field2 FROM
(SELECT field1, field2, ..., fieldn FROM table WHERE condition) AS filteredTable
WHERE anyCondition;https://stackoverflow.com/questions/7545639
复制相似问题