-1

第一个选择填充 countOfActivePlans:

SELECT COUNT(DISTINCT a.txtprogram) AS countOfActivePlans, c.txtLevel1
FROM tblOfferings a, tblClaims b, tblRetailers c
WHERE a.ysnActive=1 
      AND c.intRetailer=b.intRetailer 
      AND c.intRetailer=a.intRetailer
GROUP BY c.txtLevel1
ORDER BY c.txtLevel1

第二个选择填充plansWithActivity:

SELECT COUNT(DISTINCT a.txtProgram) AS plansWithActivity,   c.txtLevel1
FROM tblClaims a, tblOfferings b, tblRetailers 
WHERE a.intOffering=b.intOffering 
      AND c.intRetailer=a.intRetailer 
      AND c.intRetailer=b.intRetailer
GROUP BY c.txtlevel1
ORDER BY c.txtLevel1

第三个填充 Approved、Denied 和 sumOfcurPaid:

SELECT COUNT(a.dblTransaction) AS invoicesSubmitted, SUM(curPaid) AS sumOfcurPaid, b.txtLevel1, 
       CASE WHEN a.intTransCode=0 THEN 'Denied' 
            WHEN a.intTransCode=1 THEN 'Approved' 
            ELSE 'Reissued' 
       END AS txtLabel
FROM tblClaims a, tblRetailers b, tbLTransCode c
WHERE a.intRetailer=b.intRetailer 
      AND a.intTransCode=c.intTransCode
GROUP BY b.txtLevel1, 
       CASE WHEN a.intTransCode=0 THEN 'Denied' 
            WHEN a.intTransCode=1 THEN 'Approved' 
            ELSE 'Reissued' 
       END
ORDER BY b.txtLevel1 asc

下表如下。

我怎样才能使这个INSERT填充表格?

Area countOfActivePlans plansWithActivity   approved    denied  sumOfCurPaid
-----------------------------------------------------------------------------
CE   237                230                 89898       11438   1238152.858
NE   189                178                 54159       5667    1665176.757
SE   177                165                 58675       5873    1406241.758
SW   118                111                 32450       6934    1329101.503
WE   152                145                 40266       3473    529956.6875
4

1 回答 1

2
INSERT INTO
  MyTable (Area,countOfActivePlans,plansWithActivity,approved,denied,sumOfCurPaid)
SELECT
  f1.Area, f2.countOfActivePlans, f3.plansWithActivity, f4.Approved, f4.Denied, f4.sumOfcurPaid,
FROM
  (SELECT [THAT GETS THE AREA])           AS f1
CROSS JOIN
  (SELECT COUNT(DISTINCT a.txtprogram) AS countOfActivePlans, c.txtLevel1
    FROM tblOfferings a, tblClaims b, tblRetailers c
    WHERE a.ysnActive=1 
      AND c.intRetailer=b.intRetailer 
      AND c.intRetailer=a.intRetailer
    GROUP BY c.txtLevel1
    ORDER BY c.txtLevel1 )           AS f2
CROSS JOIN
  (SELECT COUNT(DISTINCT a.txtProgram) AS plansWithActivity,   c.txtLevel1
    FROM tblClaims a, tblOfferings b, tblRetailers c
    WHERE a.intOffering=b.intOffering 
      AND c.intRetailer=a.intRetailer 
      AND c.intRetailer=b.intRetailer
    GROUP BY c.txtlevel1
    ORDER BY c.txtLevel1) AS f3
CROSS JOIN
  (SELECT COUNT(a.dblTransaction) AS invoicesSubmitted, SUM(curPaid) AS sumOfcurPaid, b.txtLevel1, 
   CASE WHEN a.intTransCode=0 THEN 'Denied' 
        WHEN a.intTransCode=1 THEN 'Approved' 
        ELSE 'Reissued' 
   END AS txtLabel
   FROM tblClaims a, tblRetailers b, tbLTransCode c
   WHERE a.intRetailer=b.intRetailer 
    AND a.intTransCode=c.intTransCode
   GROUP BY b.txtLevel1, 
   CASE WHEN a.intTransCode=0 THEN 'Denied' 
        WHEN a.intTransCode=1 THEN 'Approved' 
        ELSE 'Reissued' 
   END
   ORDER BY b.txtLevel1 asc)           AS f4

我认为这没问题,但我在下面包含了基本思想,以防我上面的代码有误。

INSERT INTO
  MyTable (Col1,Col2,Col3,Col4,Col5,Col6)
SELECT
  f1.col1, f2.col2, f3.col3, f4.col4, f4.col5, f4.col6
FROM
  (SELECT Col1 FROM QUERYWITHAREA)    AS f1
CROSS JOIN
  (SELECT Col2 FROM QUERY1)           AS f2
CROSS JOIN
  (SELECT Col3 FROM QUERY2)           AS f3
CROSS JOIN
  (SELECT Col4,Col5,Col6 FROM QUERY3) AS f4

希望这会有所帮助,抱歉,如果第一个示例不完美,它可以帮助您。老实说,我很想将您问题中的每个查询放入一个函数中,并以上述样式调用每个函数。

于 2013-03-08T15:28:54.177 回答