SQL使用insert和嵌套的select语句不存在

时间:2015-11-25 15:09:40

标签: mysql sql

我正在做一个正在进行的工作总结,为我的老板提供图表,他希望存储数据,以便图表可以显示去年,上个季度,上个月和今天的比较。

我有几个sql语句处理这个问题,但我试图合并到一个语句中。

SQL:

INSERT INTO `wip_summary` (`wk_num`, `year`, `a_cnt`, `ae_cnt`, `b_cnt`, `sm_cnt`, `xx_cnt`, `tm_cnt`, `svc_cnt`, `ci_cnt`, `a_amt`, `ae_amt`, `b_amt`, `tm_amt`, `sm_amt`, `xx_amt`, `svc_amt`, `ci_amt`)
   SELECT
      WEEK(CURDATE()),
      YEAR(CURDATE()),
      SUM(CASE WHEN `emc` = '0'
                  AND `c_price` >= 10000
                  AND `contractInstall` = '0'
                  AND `terms` <> 'TIME AND MATERIAL'
                  AND `wo_type` <> 'SERVICE ORDER'
          THEN 1 ELSE 0 END) AS `a_cnt`,
      SUM(CASE WHEN `emc` = 1 THEN 1 ELSE 0 END) AS `ae_cnt`,
      SUM(CASE WHEN `emc` = '0'
                  AND `c_price` >= 3500
                  AND `c_price` < 10000
                  AND `contractInstall` = '0'
                  AND `terms` <> 'TIME AND MATERIAL'
                  AND `wo_type` <> 'SERVICE ORDER'
          THEN 1 ELSE 0 END) AS `b_cnt`,
      SUM(CASE WHEN `emc` = '0'
                  AND `c_price` >= 600
                  AND `c_price` < 3500
                  AND `contractInstall` = '0'
                  AND `terms` <> 'TIME AND MATERIAL'
                  AND `wo_type` <> 'SERVICE ORDER'
          THEN 1 ELSE 0 END) AS `sm_cnt`,
      SUM(CASE WHEN `emc` = '0'
                  AND `c_price` < 600
                  AND `contractInstall` = '0'
                  AND `terms` <> 'TIME AND MATERIAL'
                  AND `wo_type` <> 'SERVICE ORDER'
          THEN 1 ELSE 0 END) AS `xx_cnt`,
      SUM(CASE WHEN `emc` = '0'
                  AND `contractInstall` = '0'
                  AND `terms` = 'TIME AND MATERIAL'
                  AND `wo_type` <> 'SERVICE ORDER'
          THEN 1 ELSE 0 END) AS `tm_cnt`,
      SUM(CASE WHEN `emc` = '0'
                  AND `wo_type` = 'SERVICE ORDER'
                  AND `contractInstall` = '0'
          THEN 1 ELSE 0 END) AS `svc_cnt`,
      SUM(CASE WHEN `emc` = '0'
                  AND `contractInstall` = '1'
          THEN 1 ELSE 0 END) AS `ci_cnt`,
      SUM(CASE WHEN `emc` = '0'
                  AND `c_price` >= 10000
                  AND `contractInstall` = '0'
                  AND `terms` <> 'TIME AND MATERIAL'
                  AND `wo_type` <> 'SERVICE ORDER'
          THEN `c_price` ELSE 0 END) AS `a_amt`,
      SUM(CASE WHEN `emc` = 1 THEN `c_price` ELSE 0 END) AS `ae_amt`,
      SUM(CASE WHEN `emc` = '0'
                  AND `c_price` >= 3500
                  AND `c_price` < 10000
                  AND `contractInstall` = '0'
                  AND `terms` <> 'TIME AND MATERIAL'
                  AND `wo_type` <> 'SERVICE ORDER'
          THEN `c_price` ELSE 0 END) AS `b_amt`,
      SUM(CASE WHEN `emc` = '0'
                  AND `contractInstall` = '0'
                  AND `terms` = 'TIME AND MATERIAL'
                  AND `wo_type` <> 'SERVICE ORDER'
          THEN `c_price` ELSE 0 END) AS `tm_amt`,
      SUM(CASE WHEN `emc` = '0'
                  AND `c_price` >= 600
                  AND `c_price` < 3500
                  AND `contractInstall` = '0'
                  AND `terms` <> 'TIME AND MATERIAL'
                  AND `wo_type` <> 'SERVICE ORDER'
          THEN `c_price` ELSE 0 END) AS `sm_amt`,
      SUM(CASE WHEN `emc` = '0'
                  AND `c_price` < 600
                  AND `contractInstall` = '0'
                  AND `terms` <> 'TIME AND MATERIAL'
                  AND `wo_type` <> 'SERVICE ORDER'
          THEN `c_price` ELSE 0 END) AS `xx_amt`,
      SUM(CASE WHEN `emc` = '0'
                  AND `wo_type` = 'SERVICE ORDER'
                  AND `contractInstall` = '0'
          THEN `c_price` ELSE 0 END) AS `svc_amt`,
      SUM(CASE WHEN `emc` = '0'
                  AND `contractInstall` = '1'
          THEN `c_price` ELSE 0 END) AS `ci_amt`
    FROM (SELECT CAST(REPLACE(REPLACE(`contract_price`, ',', ''), '$', '') AS Decimal(10,2)) AS `c_price`,
                 `emc`,
                 `contractInstall`,
                 `wo_type`,
                 `terms`
            FROM `orders`
    WHERE `auth_status` = 'ACTIVE') AS `cp_tbl`
WHERE NOT EXISTS (SELECT 1 FROM `wip_summary` WHERE `wk_num` = WEEK(CURDATE()) AND `year` = YEAR(CURDATE()));

我正在做什么细分:

  1. 检查今年本周的记录是否在数据库中
  2. 根据指定的标准计算或汇总美元金额
  3. 除非星期和年份在表中,否则它会很好用,然后我会得到一行列默认值。

    所以WHERE NOT EXISTS是让我失望的,但我似乎无法理解为什么或如何做。

    如果没有嵌套的SELECT语句INSERT ... WHERE NOT EXISTSINSERT ... WHERE NOT EXISTS语句无效,SELECT语句无效{/ 1}}。

2 个答案:

答案 0 :(得分:1)

用AND替换第二个WHERE。 如果不这样,Select将始终生成数据。如果这样做,select将不会提供数据,因为wip_summary中已经存在年/弱组合。

更改此部分:

        FROM `orders`
    WHERE `auth_status` = 'ACTIVE') 
    AND NOT EXISTS 
(SELECT 1 FROM `wip_summary` WHERE `wk_num` = WEEK(CURDATE()) AND `year` = YEAR(CURDATE()))
AS `cp_tbl`;

String cmd = "/var/tmp/./myscript";
Process process = Runtime.getRuntime().exec(cmd);
stdin = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while((line = stdin.readLine()) != null) {
    System.out.println(line);
}

答案 1 :(得分:0)

我能够通过添加SELECT * FROM ()语句来解决我复杂的问题。

INSERT INTO `wip_summary` (`wk_num`, `year`, `a_cnt`, `ae_cnt`, `b_cnt`, `sm_cnt`, `xx_cnt`, `tm_cnt`, `svc_cnt`, `ci_cnt`, `a_amt`, `ae_amt`, `b_amt`, `tm_amt`, `sm_amt`, `xx_amt`, `svc_amt`, `ci_amt`)
   SELECT * FROM (SELECT
      WEEK(CURDATE()),
      YEAR(CURDATE()),
      SUM(CASE WHEN `emc` = '0' [...]) AS `a_cnt`,
      SUM(CASE WHEN `emc` =  1  [...]) AS `ae_cnt`,
      SUM(CASE WHEN `emc` = '0' [...]) AS `b_cnt`,
      SUM(CASE WHEN `emc` = '0' [...]) AS `sm_cnt`,
      SUM(CASE WHEN `emc` = '0' [...]) AS `xx_cnt`,
      SUM(CASE WHEN `emc` = '0' [...]) AS `tm_cnt`,
      SUM(CASE WHEN `emc` = '0' [...]) AS `svc_cnt`,
      SUM(CASE WHEN `emc` = '0' [...]) AS `ci_cnt`,
      SUM(CASE WHEN `emc` = '0' [...]) AS `a_amt`,
      SUM(CASE WHEN `emc` =  1  [...]) AS `ae_amt`,
      SUM(CASE WHEN `emc` = '0' [...]) AS `b_amt`,
      SUM(CASE WHEN `emc` = '0' [...]) AS `tm_amt`,
      SUM(CASE WHEN `emc` = '0' [...]) AS `sm_amt`,
      SUM(CASE WHEN `emc` = '0' [...]) AS `xx_amt`,
      SUM(CASE WHEN `emc` = '0' [...]) AS `svc_amt`,
      SUM(CASE WHEN `emc` = '0' [...]) AS `ci_amt`
    FROM (SELECT [...] AS `c_price`,
                 `emc`,
                 `contractInstall`,
                 `wo_type`,
                 `terms`
            FROM `orders`
            WHERE `auth_status` = 'ACTIVE') AS `cp_tbl`) AS `vals`
WHERE NOT EXISTS (SELECT 1 FROM `wip_summary` WHERE `wk_num` = WEEK(CURDATE()) AND `year` = YEAR(CURDATE()));

我可能错了,但我认为问题在于代表价值的SELECT语句与INSERT WHERE条款冲突。

WHERE语句中的FROM没有冲突,当我删除它并简化FROM时它仍然有效(无论是否存在记录都插入一行)