把 SQL 贴出来吧######
SELECT T, L, MIN(H) MIN, MAX(H) MAX, MAX(V) - MIN(V) V FROM (SELECT D.TEN T, D.LOCATION L, CONCAT(W.MONTHNO, W.HOURTIME) H, W.TOTALSTREAM V FROM D02WATERDATA W, B04DEVICE D WHERE W.B04ID = D.ID AND W.MONTHNO >= '20141206' AND W.MONTHNO <= '20141208' AND W.HOURTIME BETWEEN 030000 AND 050000) GROUP BY T, L ORDER BY T
######在外面再写个select,然后用if函数判断
######
SELECT T, L, MIN(Case When MONTHNO = '20141206' Then H Else 0 End) MIN_06, MAX(Case When MONTHNO = '20141206' Then H Else 0 End) MAX_06, MAX(Case When MONTHNO = '20141206' Then H Else 0 End) - MIN(Case When MONTHNO = '20141206' Then H Else 0 End) V_06, MIN(Case When MONTHNO = '20141207' Then H Else 0 End) MIN_07, MAX(Case When MONTHNO = '20141207' Then H Else 0 End) MAX_07, MAX(Case When MONTHNO = '20141207' Then H Else 0 End) - MIN(Case When MONTHNO = '20141206' Then H Else 0 End) V_07, MIN(Case When MONTHNO = '20141208' Then H Else 0 End) MIN_08, MAX(Case When MONTHNO = '20141208' Then H Else 0 End) MAX_08, MAX(Case When MONTHNO = '20141208' Then H Else 0 End) - MIN(Case When MONTHNO = '20141206' Then H Else 0 End) V_08 FROM(SELECT D.TEN T, D.LOCATION L, CONCAT(W.MONTHNO, W.HOURTIME) H, W.TOTALSTREAM V, W.MONTHNO FROM D02WATERDATA W, B04DEVICE D WHERE W.B04ID = D.ID AND W.MONTHNO >='20141206' AND W.MONTHNO <='20141208' AND W.HOURTIME BETWEEN 030000 AND 050000) Group BY T, L Order BY T
######干嘛非要横着列出啊,竖向列出不是更好。而且查询更省时间。
因为不清楚楼主,的实际需求。所以按着楼主思路给出下面解决方法:(横向列出的)
select t1.T, t1.L, t1.MIN, t1.MAX, t1.V, t2.MIN, t2.MAX, t2.V, t3.MIN ...
from (select ....) t1, (select ....) t2, (select ....) t3, ... 这里是查出每天的数据
where t1.T = t2.T and t2.t = 3.t ######SELECT T, L,
case when MONTHNO='20141206' then MIN_H else NULL end MIN_H_20141206,
case when MONTHNO='20141206' then MAX_H else NULL end MAX_H_20141206,
case when MONTHNO='20141206' then DIFF_V else NULL end DIFF_V_20141206,
case when MONTHNO='20141207' then MIN_H else NULL end MIN_H_20141207,
case when MONTHNO='20141207' then MAX_H else NULL end MAX_H_20141207,
case when MONTHNO='20141207' then DIFF_V else NULL end DIFF_V_20141207,
case when MONTHNO='20141208' then MIN_H else NULL end MIN_H_20141208,
case when MONTHNO='20141208' then MAX_H else NULL end MAX_H_20141208,
case when MONTHNO='20141208' then DIFF_V else NULL end DIFF_V_20141208
FROM
(
SELECT D.TEN T,
D.LOCATION L,
W.MONTHNO,
CONCAT(W.MONTHNO, MIN(W.HOURTIME)) MIN_H,
CONCAT(W.MONTHNO, MAX(W.HOURTIME)) MAX_H,
MAX(W.TOTALSTREAM) - MIN(W.TOTALSTREAM) DIFF_V
FROM D02WATERDATA W, B04DEVICE D
WHERE W.B04ID = D.ID
AND W.MONTHNO >= '20141206'
AND W.MONTHNO <= '20141208'
AND W.HOURTIME BETWEEN 030000 AND 050000
GROUP BY D.TEN, D.LOCATION, W.MONTHNO
)
GROUP BY T, L
ORDER BY T, L
######问题解决了吗?###### 解决了 谢谢大家
但是 不是动态的。。。我的时间是页面录入的 怎么要实现动态呢
SELECT T, L, MAX(DECODE(M, '20141206', H)), MIN(DECODE(M, '20141206', H)), MAX(DECODE(M, '20141206', V)) - MIN(DECODE(M, '20141206', V)), MAX(DECODE(M, '20141207', H)), MIN(DECODE(M, '20141207', H)), MAX(DECODE(M, '20141207', V)) - MIN(DECODE(M, '20141207', V)) FROM (SELECT D.TEN T, D.LOCATION L, W.MONTHNO M, CONCAT(W.MONTHNO, W.HOURTIME) H, W.TOTALSTREAM V FROM D02WATERDATA W, B04DEVICE D WHERE W.B04ID = D.ID AND W.HOURTIME BETWEEN 030000 AND 050000) GROUP BY T, L ORDER BY T
############如果这个可行 但是 没有数据那行 怎么不显示 id为 1, 12 ,17
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。