第一个选择填充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
答案 0 :(得分: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
希望这会有所帮助,对不起,如果第一个例子并不完美,那么它就是一个帮助你的指南。老实说,我很想把你问题中的每个查询都放到一个函数中,然后用上面的样式调用每个函数。