表连接主查询和子查询

时间:2013-12-05 16:17:31

标签: mysql

我的数据库包含以下表格:

TABLE SALESCOUNT
-----------------------------------
| id | startWeek  | repID | sales |
-----------------------------------
| 1  | FY13M01W01 | 0001  |   4   |
| 2  | FY13M01W01 | 0002  |   8   |
| 3  | FY13M01W02 | 0001  |   5   |
| 4  | FY13M01W02 | 0002  |   9   |
...

TABLE SALES
----------------------------------------------------
| id | salesRepID | salesDate  | division  | team  |
----------------------------------------------------
| 1  |   0001     | 2013-01-01 |   divA    | teamA |
| 2  |   0002     | 2013-01-01 |   divC    | teamA |
| 3  |   0001     | 2013-01-01 |   divD    | teamD |
| 4  |   0002     | 2013-01-08 |   divB    | teamC |
...

TABLE DEPT
-------------------------------
| id | repID | region | dept  |
-------------------------------
| 1  | 0001  |   01   |   A   |
| 2  | 0002  |   02   |   B   |
| 3  | 0001  |   C    |   A   |
| 4  | 0002  |   02   |   B   |
...

TABLE GEO
-----------------------------------
| id | geo   | regionA | regionB  |
-----------------------------------
| 1  | geoA  |   01    |     A    |
| 2  | geoA  |   02    |     B    |
| 3  | geoB  |   03    |     C    |
| 4  | geoB  |   04    |     D    |
...

TABLE FISCALWEEK
--------------------------------
| id | salesDate  | startWeek  |
--------------------------------
| 1  | 2013-01-01 | FY13M01W01 |
| 2  | 2013-01-08 | FY13M01W02 |
...

以下查询返回所需的结果:

SELECT y.startWeek, SUM(y.sales), dept.dept, geo.geo
FROM salesCount as y,
(
    SELECT DISTINCT a.salesRepID
    FROM sales AS a
    WHERE (a.team IN ("teamA", "teamB", "teamC)
        AND a.saleDate BETWEEN "2013-01-01" AND "2013-06-30"
        AND a.division IN ("divA", "divB", "divC")
) as x
INNER JOIN dept
    ON x.salesRepID = dept.repID
INNER JOIN geo
    ON dept.region = geo.regionA OR dept.region = geo.regionB
WHERE x.salesRepID = y.repID
    AND y.startWeek BETWEEN "FY13M01W01" AND "FY13M06W04"
GROUP BY y.startWeek, geo.geo, dept.dept
ORDER BY y.startWeek ASC, geo.geo ASC, dept.dept ASC

我的目标是利用FISCALWEEK表生成2个日期对(子查询的WHERE子句中为1,主查询的WHERE子句中为1)。我希望能够在一个地方输入日期对(xxxx-xx-xx)并使用它来填充所需格式的日期。不幸的是,我无法改变数据或表格。

我一直在寻找相当长的一段时间,似乎无法找到我能够转移到我的情况的答案。我已经尝试了许多排列,这是我能想出的唯一一个能给我预期结果的排列。如果您有任何疑问或需要任何其他信息,请与我们联系。任何帮助是极大的赞赏。谢谢。

1 个答案:

答案 0 :(得分:0)

这只是我的头脑,也许你可以先将saleDate解压缩到外部查询中:

SELECT y.startWeek, SUM(y.sales), dept.dept, geo.geo
FROM salesCount as y,
(
    SELECT DISTINCT a.salesRepID, a.saleDate
    FROM sales AS a
    WHERE (a.team IN ("teamA", "teamB", "teamC)
        AND a.division IN ("divA", "divB", "divC")
) as x
INNER JOIN dept
    ON x.salesRepID = dept.repID
INNER JOIN geo
    ON dept.region = geo.regionA OR dept.region = geo.regionB
WHERE x.salesRepID = y.repID
    AND y.startWeek BETWEEN "FY13M01W01" AND "FY13M06W04"
    AND x.saleDate BETWEEN "2013-01-01" AND "2013-06-30"
GROUP BY y.startWeek, geo.geo, dept.dept
ORDER BY y.startWeek ASC, geo.geo ASC, dept.dept ASC

然后从那里你可以看看加入FISCALWEEK表,如:

SELECT y.startWeek, SUM(y.sales), dept.dept, geo.geo
FROM salesCount as y,
(
    SELECT DISTINCT a.salesRepID, a.saleDate
    FROM sales AS a
    WHERE (a.team IN ("teamA", "teamB", "teamC)
        AND a.division IN ("divA", "divB", "divC")
) as x
INNER JOIN dept
    ON x.salesRepID = dept.repID
INNER JOIN geo
    ON dept.region = geo.regionA OR dept.region = geo.regionB
INNER JOIN fiscalweek
    ON y.startWeek = fiscalweek.startWeek
WHERE x.salesRepID = y.repID
    AND y.startWeek BETWEEN "FY13M01W01" AND "FY13M06W04"
    AND x.saleDate = fiscalweek.salesDate 
GROUP BY y.startWeek, geo.geo, dept.dept
ORDER BY y.startWeek ASC, geo.geo ASC, dept.dept ASC

不确定这是否有效,但希望能给你一些想法