在多個查詢和子查詢中更正連接語法

[英]Correct join syntax within multiple queries and sub queries


I have two queries that end up having the same format. Each has a Month, a year, and some relevant data per month/year. The schema looks like this:

我有兩個查詢最終具有相同的格式。每個月/每年都有一個月,一年和一些相關數據。架構如下所示:

subs    Month   Year
8150    1      2015
11060   1      2016
5       2      2014
6962    2      2015
8736    2      2016

Cans    months  years
2984    1       2015
2724    1       2016
13      2       2014
2563    2       2015
1901    2       2016

The first query syntax looks like this:

第一個查詢語法如下所示:

SELECT 
    COUNT(personID) AS subs_per_month,
    MONTH(Date_1) AS month_1,
    YEAR(Date_1) AS year_1
FROM
    (SELECT 
        personID, MIN(date) AS Date_1
    FROM
        orders
    WHERE
        isSubscription = 1
    GROUP BY personID
    ORDER BY Date_1) AS my_sub_q
GROUP BY month_1 , year_1

The second query:

第二個查詢:

SELECT 
    COUNT(ID), MONTH(date) AS months, YEAR(date) AS years
FROM
    orders
WHERE
    status = 4 AND isSubscription = 1
GROUP BY months , years
ORDER BY months, years

The end goal is to write a simple join so that the final dataset looks like this:

最終目標是編寫一個簡單的連接,以便最終的數據集如下所示:

subs    cans months years
8150    2984    1   2015
11060   2724    1   2016
5         13    2   2014
6962    2563    2   2015
8736    1901    2   2016

I'm a little overwhelmed with how to do this correctly, and after a lot of trial and all error, I thought I'd ask for help. What's confusing is where the JOIN goes, and how that looks relative to the rest of the syntax.

我對如何正確地做到這一點感到有些不知所措,經過大量試驗和所有錯誤后,我想我會尋求幫助。令人困惑的是JOIN的位置,以及相對於其余語法的外觀。

3 个解决方案

#1


1  

Without giving consideration to simplifying your queries you can use your two queries as inline views and simply select from both (I aliased Q1 and Q2 for your queries and named fields the same within each for simplicity.

在不考慮簡化查詢的情況下,您可以將兩個查詢用作內聯視圖,並且只需從兩個選項中進行選擇(為了簡單起見,我將查詢和命名字段別名為Q1和Q2。

Select Q1.cnt as Subs, Q2.cnt as Cans, Q1.months, Q1.years
from (SELECT 
    COUNT(personID) AS Cnt,
    MONTH(Date_1) as Months,
    YEAR(Date_1) AS years
    FROM  (SELECT personID, MIN(date) AS Date_1
           FROM orders
           WHERE isSubscription = 1
           GROUP BY personID) AS my_sub_q
    GROUP BY month_1 , year_1) Q1
INNER JOIN (SELECT COUNT(ID) cnt, MONTH(date) AS months, YEAR(date) AS years
            FROM orders
            WHERE status = 4 
              AND isSubscription = 1
            GROUP BY months, years) Q2

  ON Q1.Months = Q2.Months
 and Q1.Years = Q2.years
Order by Q1.years, Q2.months

#2


0  

Temporary table approach:

臨時表方法:

create temporary table first_query
<<your first query here>>;

create temporary table second_query
<<your second query here>>;

select fq.subs, sq.cans, fq.months, fq.years
from first_query fq
join second_query sq using (months, years)

Your table preview and query columns do not match for first query, so I assumed both tables have columns - months and years.

您的表預覽和查詢列與第一個查詢不匹配,因此我假設兩個表都有列 - 月和年。

One messy query approach:

一種凌亂的查詢方法:

SELECT fq.subs_per_month subs, sq.cans, sq.months, sq.years
FROM
(SELECT 
    COUNT(personID) AS subs_per_month,
    MONTH(Date_1) AS month_1,
    YEAR(Date_1) AS year_1
FROM
    (SELECT 
    personID, MIN(date) AS Date_1
    FROM
    orders
    WHERE
    isSubscription = 1
    GROUP BY personID
    ORDER BY Date_1) AS my_sub_q
GROUP BY month_1 , year_1) fq
JOIN
(SELECT 
    COUNT(ID) cans, MONTH(date) AS months, YEAR(date) AS years   -- I added 'cans'
FROM
    orders
WHERE
    status = 4 AND isSubscription = 1
GROUP BY months , years
ORDER BY months, years) sq
ON fq.month_1 = sq.months AND fq.year_1 = sq.years

#3


0  

Please use following query

請使用以下查詢

select t1.subs as subs,t2.Cans as cans,t1.months,t1.year as years from table1 t1 inner join
table2 t2 on t1.month=t2.months and t1.year=t2.years

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/04/19/a091ebe3e736d6fed7b5926a514a9932.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com