SQL编码 - 向现有查询添加新变量

时间:2014-04-03 23:51:16

标签: mysql sql

我有一个现有的查询非常有效 - 它需要一组声明,并为给定成员提供给定成员的最小开始和最长结束日期,以及医院住宿的付费字符串总和哪里有临时​​账单。我需要加上裁决日期 - 我需要每次入住的最低裁决日期。我尝试添加它并且它不起作用。谁能帮我?两个查询都在下面。谢谢!

第一次查询 - 工作 - 没有裁决日期

 Create Table IPClaimsGrouped2 as
 (SELECT Member_ID , MedID
      , Start_Date   ,End_Date, 
      (SELECT sum(r2.paid)  
          FROM  IPClaims2 AS r2
          WHERE r2.Member_ID = r1.Member_ID
            AND r2.MedID        = r1.MedID
            AND r2.Start_Date  >= r1.Start_Date
            AND r2.End_Date    <= r1.End_Date
        ) AS sum_paid
  FROM  (SELECT Member_ID , MedID
              , Start_Date
              , (SELECT MIN(t3.End_Date) 
                  FROM  IPClaims2 AS t3
                  WHERE NOT EXISTS
                        (SELECT *
                          FROM  IPClaims2 AS t4
                          WHERE t4.Member_ID = t3.Member_ID
                            AND t4.MedID        = t3.MedID
                            AND t4.Start_Date   = t3.End_Date + interval 1 day 
                        )
                    AND t3.Member_ID = t1.Member_ID
                    AND t3.MedID        = t1.MedID
                    AND t3.End_Date    >= t1.Start_Date
                ) as end_date
          FROM  IPClaims2 AS t1
          WHERE NOT EXISTS
                (SELECT *
                  FROM  IPClaims2 AS t2
                  WHERE t2.Member_ID = t1.Member_ID
                    AND t2.MedID        = t1.MedId
                    AND t2.End_Date     = t1.Start_Date - interval 1 day
                )
        ) AS r1
  ORDER BY
        Member_ID , MedID
      , Start_Date);

第2次查询 - 在判决日期中添加 - 破坏

Create Table IPClaimsGrouped2 as
 (SELECT Member_ID , MedID
      , Start_Date   ,End_Date, min(Adj_Date) as min_adj_date,
      (SELECT sum(r2.paid)  
          FROM  IPClaims2 AS r2
          WHERE r2.Member_ID = r1.Member_ID
            AND r2.MedID        = r1.MedID
            AND r2.Start_Date  >= r1.Start_Date
            AND r2.End_Date    <= r1.End_Date
        ) AS sum_paid
  FROM  (SELECT Member_ID , MedID
              , Start_Date, min(Adj_Date) as Adj_Date
              , (SELECT MIN(t3.End_Date) 
                  FROM  IPClaims2 AS t3
                  WHERE NOT EXISTS
                        (SELECT *
                          FROM  IPClaims2 AS t4
                          WHERE t4.Member_ID = t3.Member_ID
                            AND t4.MedID        = t3.MedID
                            AND t4.Start_Date   = t3.End_Date + interval 1 day 
                        )
                    AND t3.Member_ID = t1.Member_ID
                    AND t3.MedID        = t1.MedID
                    AND t3.End_Date    >= t1.Start_Date
                ) as end_date
          FROM  IPClaims2 AS t1
          WHERE NOT EXISTS
                (SELECT *
                  FROM  IPClaims2 AS t2
                  WHERE t2.Member_ID = t1.Member_ID
                    AND t2.MedID        = t1.MedId
                    AND t2.End_Date     = t1.Start_Date - interval 1 day
                )
        ) AS r1
  ORDER BY
        Member_ID , MedID
      , Start_Date);

0 个答案:

没有答案