开发者社区> 问答> 正文

求助 ~~这个sql 怎么写:报错

展开
收起
kun坤 2020-06-09 11:47:46 430 0
1 条回答
写回答
取消 提交回答
  • 把 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 

    2020-06-09 11:47:56
    赞同 展开评论 打赏
问答分类:
SQL
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
SQL Server 2017 立即下载
GeoMesa on Spark SQL 立即下载
原生SQL on Hadoop引擎- Apache HAWQ 2.x最新技术解密malili 立即下载