Mysql 如何在父查询中使用子查询,为子查询提供另一个条件?

Mysql 如何在父查询中使用子查询,为子查询提供另一个条件?,mysql,Mysql,我有关于子查询的问题,我有一个模块,我需要根据上传日期对间隔7天的净销售额求和。所以每个 注意:商店有相应的销售记录。 所以现在我有了父选择,条件是子日期当前日期。 在父select中,我插入子查询,其中条件间隔为7天。但很糟糕,子查询返回的错误超过1行。让我们继续前进,我将向你们展示我创建的脚本 错误: 子查询返回超过1行 父查询: SELECT storeid, company, market as store, /* part 1 */ CON

我有关于子查询的问题,我有一个模块,我需要根据上传日期对间隔7天的净销售额求和。所以每个

注意:商店有相应的销售记录。

所以现在我有了父选择,条件是子日期当前日期。 在父select中,我插入子查询,其中条件间隔为7天。但很糟糕,子查询返回的错误超过1行。让我们继续前进,我将向你们展示我创建的脚本

错误:

子查询返回超过1行

父查询:

SELECT 

    storeid,
    company,
    market as store,

    /* part 1 */
    CONCAT(FORMAT(sum(ns), 0)) as NS, 
    CONCAT(FORMAT(sum(nstgt), 0)) as NSTGT,
    CONCAT(ROUND(TRUNCATE(((SUM(ns) / SUM(nstgt)-1) * 100),2),1),"%")  as "nstgt_percentage",
    /* part 1 end*/



    /* part 2 */
    CONCAT(FORMAT(sum(nsly), 0)) as nsly,
    CONCAT(ROUND(TRUNCATE(((SUM(ns) / SUM(nsly)-1) * 100),2),1),"%")  as "nsly_percentage",
    /* part 2 end */


    /* part 3 */
    CONCAT(FORMAT(sum(tc), 0)) as TC, 
    CONCAT(FORMAT(sum(tctgt), 0)) as TCTGT,
    CONCAT(ROUND(TRUNCATE(((SUM(tc) / SUM(tctgt)-1) * 100),2),1),"%")  as "tctgt_percentage",
    /* part 3 end */

    /* part 4 */
    CONCAT(FORMAT(sum(tcly), 0)) as tcly,
    CONCAT(ROUND(TRUNCATE(((SUM(tc) / SUM(tcly)-1) * 100),2),1),"%")  as "tcly_percentage",
    /* part 4 end */

    /* part 5 */
    ROUND((SUM(ns) / SUM(tc)),1)  as "TA",
    ROUND((SUM(nstgt) / SUM(tctgt)),1)  as "TATGT",
    ROUND((SUM(nsly) / SUM(tcly)),1)  as "TALY"
    /* part 5 end */

    FROM xxxx.xxxxxxxxxxx as sales_report1
    LEFT JOIN (SELECT store_code,market,company FROM xxxx.xxxxxxx) sm 
    ON sales_report1.storeid = sm.store_code
    WHERE market = "CGY" AND
    week = (SELECT DISTINCT(week) FROM xxxxxxx.xxxxxxxx WHERE txndate = SUBDATE(CURDATE(),2))
    AND period = (SELECT DISTINCT(period) FROM xxxxx.xxxxxxxxx WHERE txndate = SUBDATE(CURDATE(),2)) AND (nsly != "." OR tcly != ".") GROUP BY storeid
    (
    select 
    ns

    FROM xxxx.xxxxxx as sales_report1
    LEFT JOIN (SELECT store_code,market FROM xxx.xxxxx) sm 
    ON sales_report1.storeid = sm.store_code
    WHERE market = "CGY" AND
    week = (SELECT DISTINCT(week) FROM xxxxx.xxxxxxx WHERE uploaddate = CURDATE() - INTERVAL 7 DAY GROUP BY uploaddate)  
    AND period = (SELECT DISTINCT(period) FROM xxxx.xxxxxxxx WHERE uploaddate = CURDATE() - INTERVAL 7 DAY GROUP BY uploaddate)

) as nslw,
SELECT 

sales_report1.storeid,
company,
market as store,

/* part 1 */
CONCAT(FORMAT(sum(ns), 0)) as NS, 
CONCAT(FORMAT(sum(nstgt), 0)) as NSTGT,
CONCAT(ROUND(TRUNCATE(((SUM(ns) / SUM(nstgt)-1) * 100),2),1),"%")  as "nstgt_percentage",
/* part 1 end*/

/* part 2 */
CONCAT(FORMAT(sum(nsly), 0)) as nsly,
CONCAT(ROUND(TRUNCATE(((SUM(ns) / SUM(nsly)-1) * 100),2),1),"%")  as "nsly_percentage",
/* part 2 end */


/* part 3 */
CONCAT(FORMAT(sum(tc), 0)) as TC, 
CONCAT(FORMAT(sum(tctgt), 0)) as TCTGT,
CONCAT(ROUND(TRUNCATE(((SUM(tc) / SUM(tctgt)-1) * 100),2),1),"%")  as "tctgt_percentage",
/* part 3 end */

/* part 4 */
CONCAT(FORMAT(sum(tcly), 0)) as tcly,
CONCAT(ROUND(TRUNCATE(((SUM(tc) / SUM(tcly)-1) * 100),2),1),"%")  as "tcly_percentage",
/* part 4 end */

/* part 5 */
ROUND((SUM(ns) / SUM(tc)),1)  as "TA",
ROUND((SUM(nstgt) / SUM(tctgt)),1)  as "TATGT",
ROUND((SUM(nsly) / SUM(tcly)),1)  as "TALY",
/* part 5 end */

(
    select 
    CONCAT(FORMAT(sum(ns), 0))
    FROM cron_db.sales_report_export_1 as sales_report1
    LEFT JOIN (SELECT store_code,market FROM cron_db.store_master) sm 
    ON sales_report1.storeid = sm.store_code
    WHERE 
    week IN (SELECT DISTINCT(week) FROM cron_db.sales_report_export_2 WHERE txndate = SUBDATE(CURDATE(),2))
    and period IN (SELECT DISTINCT(period) FROM cron_db.sales_report_export_1 WHERE uploaddate = CURDATE() - INTERVAL 7 DAY GROUP BY uploaddate)
) as nslw



FROM cron_db.sales_report_export_1 as sales_report1
LEFT JOIN (SELECT store_code,market,company FROM cron_db.store_master) sm 
ON sales_report1.storeid = sm.store_code
WHERE market = "CGY"
AND week = (SELECT DISTINCT(week) FROM cron_db.sales_report_export_1 WHERE txndate = SUBDATE(CURDATE(),2))
AND period = (SELECT DISTINCT(period) FROM cron_db.sales_report_export_1 WHERE txndate = SUBDATE(CURDATE(),2)) AND (nsly != "." OR tcly != ".") 
GROUP BY sales_report1.storeid
我的子查询:

SELECT 

    storeid,
    company,
    market as store,

    /* part 1 */
    CONCAT(FORMAT(sum(ns), 0)) as NS, 
    CONCAT(FORMAT(sum(nstgt), 0)) as NSTGT,
    CONCAT(ROUND(TRUNCATE(((SUM(ns) / SUM(nstgt)-1) * 100),2),1),"%")  as "nstgt_percentage",
    /* part 1 end*/



    /* part 2 */
    CONCAT(FORMAT(sum(nsly), 0)) as nsly,
    CONCAT(ROUND(TRUNCATE(((SUM(ns) / SUM(nsly)-1) * 100),2),1),"%")  as "nsly_percentage",
    /* part 2 end */


    /* part 3 */
    CONCAT(FORMAT(sum(tc), 0)) as TC, 
    CONCAT(FORMAT(sum(tctgt), 0)) as TCTGT,
    CONCAT(ROUND(TRUNCATE(((SUM(tc) / SUM(tctgt)-1) * 100),2),1),"%")  as "tctgt_percentage",
    /* part 3 end */

    /* part 4 */
    CONCAT(FORMAT(sum(tcly), 0)) as tcly,
    CONCAT(ROUND(TRUNCATE(((SUM(tc) / SUM(tcly)-1) * 100),2),1),"%")  as "tcly_percentage",
    /* part 4 end */

    /* part 5 */
    ROUND((SUM(ns) / SUM(tc)),1)  as "TA",
    ROUND((SUM(nstgt) / SUM(tctgt)),1)  as "TATGT",
    ROUND((SUM(nsly) / SUM(tcly)),1)  as "TALY"
    /* part 5 end */

    FROM xxxx.xxxxxxxxxxx as sales_report1
    LEFT JOIN (SELECT store_code,market,company FROM xxxx.xxxxxxx) sm 
    ON sales_report1.storeid = sm.store_code
    WHERE market = "CGY" AND
    week = (SELECT DISTINCT(week) FROM xxxxxxx.xxxxxxxx WHERE txndate = SUBDATE(CURDATE(),2))
    AND period = (SELECT DISTINCT(period) FROM xxxxx.xxxxxxxxx WHERE txndate = SUBDATE(CURDATE(),2)) AND (nsly != "." OR tcly != ".") GROUP BY storeid
    (
    select 
    ns

    FROM xxxx.xxxxxx as sales_report1
    LEFT JOIN (SELECT store_code,market FROM xxx.xxxxx) sm 
    ON sales_report1.storeid = sm.store_code
    WHERE market = "CGY" AND
    week = (SELECT DISTINCT(week) FROM xxxxx.xxxxxxx WHERE uploaddate = CURDATE() - INTERVAL 7 DAY GROUP BY uploaddate)  
    AND period = (SELECT DISTINCT(period) FROM xxxx.xxxxxxxx WHERE uploaddate = CURDATE() - INTERVAL 7 DAY GROUP BY uploaddate)

) as nslw,
SELECT 

sales_report1.storeid,
company,
market as store,

/* part 1 */
CONCAT(FORMAT(sum(ns), 0)) as NS, 
CONCAT(FORMAT(sum(nstgt), 0)) as NSTGT,
CONCAT(ROUND(TRUNCATE(((SUM(ns) / SUM(nstgt)-1) * 100),2),1),"%")  as "nstgt_percentage",
/* part 1 end*/

/* part 2 */
CONCAT(FORMAT(sum(nsly), 0)) as nsly,
CONCAT(ROUND(TRUNCATE(((SUM(ns) / SUM(nsly)-1) * 100),2),1),"%")  as "nsly_percentage",
/* part 2 end */


/* part 3 */
CONCAT(FORMAT(sum(tc), 0)) as TC, 
CONCAT(FORMAT(sum(tctgt), 0)) as TCTGT,
CONCAT(ROUND(TRUNCATE(((SUM(tc) / SUM(tctgt)-1) * 100),2),1),"%")  as "tctgt_percentage",
/* part 3 end */

/* part 4 */
CONCAT(FORMAT(sum(tcly), 0)) as tcly,
CONCAT(ROUND(TRUNCATE(((SUM(tc) / SUM(tcly)-1) * 100),2),1),"%")  as "tcly_percentage",
/* part 4 end */

/* part 5 */
ROUND((SUM(ns) / SUM(tc)),1)  as "TA",
ROUND((SUM(nstgt) / SUM(tctgt)),1)  as "TATGT",
ROUND((SUM(nsly) / SUM(tcly)),1)  as "TALY",
/* part 5 end */

(
    select 
    CONCAT(FORMAT(sum(ns), 0))
    FROM cron_db.sales_report_export_1 as sales_report1
    LEFT JOIN (SELECT store_code,market FROM cron_db.store_master) sm 
    ON sales_report1.storeid = sm.store_code
    WHERE 
    week IN (SELECT DISTINCT(week) FROM cron_db.sales_report_export_2 WHERE txndate = SUBDATE(CURDATE(),2))
    and period IN (SELECT DISTINCT(period) FROM cron_db.sales_report_export_1 WHERE uploaddate = CURDATE() - INTERVAL 7 DAY GROUP BY uploaddate)
) as nslw



FROM cron_db.sales_report_export_1 as sales_report1
LEFT JOIN (SELECT store_code,market,company FROM cron_db.store_master) sm 
ON sales_report1.storeid = sm.store_code
WHERE market = "CGY"
AND week = (SELECT DISTINCT(week) FROM cron_db.sales_report_export_1 WHERE txndate = SUBDATE(CURDATE(),2))
AND period = (SELECT DISTINCT(period) FROM cron_db.sales_report_export_1 WHERE txndate = SUBDATE(CURDATE(),2)) AND (nsly != "." OR tcly != ".") 
GROUP BY sales_report1.storeid
完整查询:

SELECT 

    storeid,
    company,
    market as store,

    /* part 1 */
    CONCAT(FORMAT(sum(ns), 0)) as NS, 
    CONCAT(FORMAT(sum(nstgt), 0)) as NSTGT,
    CONCAT(ROUND(TRUNCATE(((SUM(ns) / SUM(nstgt)-1) * 100),2),1),"%")  as "nstgt_percentage",
    /* part 1 end*/



    /* part 2 */
    CONCAT(FORMAT(sum(nsly), 0)) as nsly,
    CONCAT(ROUND(TRUNCATE(((SUM(ns) / SUM(nsly)-1) * 100),2),1),"%")  as "nsly_percentage",
    /* part 2 end */


    /* part 3 */
    CONCAT(FORMAT(sum(tc), 0)) as TC, 
    CONCAT(FORMAT(sum(tctgt), 0)) as TCTGT,
    CONCAT(ROUND(TRUNCATE(((SUM(tc) / SUM(tctgt)-1) * 100),2),1),"%")  as "tctgt_percentage",
    /* part 3 end */

    /* part 4 */
    CONCAT(FORMAT(sum(tcly), 0)) as tcly,
    CONCAT(ROUND(TRUNCATE(((SUM(tc) / SUM(tcly)-1) * 100),2),1),"%")  as "tcly_percentage",
    /* part 4 end */

    /* part 5 */
    ROUND((SUM(ns) / SUM(tc)),1)  as "TA",
    ROUND((SUM(nstgt) / SUM(tctgt)),1)  as "TATGT",
    ROUND((SUM(nsly) / SUM(tcly)),1)  as "TALY"
    /* part 5 end */

    FROM xxxx.xxxxxxxxxxx as sales_report1
    LEFT JOIN (SELECT store_code,market,company FROM xxxx.xxxxxxx) sm 
    ON sales_report1.storeid = sm.store_code
    WHERE market = "CGY" AND
    week = (SELECT DISTINCT(week) FROM xxxxxxx.xxxxxxxx WHERE txndate = SUBDATE(CURDATE(),2))
    AND period = (SELECT DISTINCT(period) FROM xxxxx.xxxxxxxxx WHERE txndate = SUBDATE(CURDATE(),2)) AND (nsly != "." OR tcly != ".") GROUP BY storeid
    (
    select 
    ns

    FROM xxxx.xxxxxx as sales_report1
    LEFT JOIN (SELECT store_code,market FROM xxx.xxxxx) sm 
    ON sales_report1.storeid = sm.store_code
    WHERE market = "CGY" AND
    week = (SELECT DISTINCT(week) FROM xxxxx.xxxxxxx WHERE uploaddate = CURDATE() - INTERVAL 7 DAY GROUP BY uploaddate)  
    AND period = (SELECT DISTINCT(period) FROM xxxx.xxxxxxxx WHERE uploaddate = CURDATE() - INTERVAL 7 DAY GROUP BY uploaddate)

) as nslw,
SELECT 

sales_report1.storeid,
company,
market as store,

/* part 1 */
CONCAT(FORMAT(sum(ns), 0)) as NS, 
CONCAT(FORMAT(sum(nstgt), 0)) as NSTGT,
CONCAT(ROUND(TRUNCATE(((SUM(ns) / SUM(nstgt)-1) * 100),2),1),"%")  as "nstgt_percentage",
/* part 1 end*/

/* part 2 */
CONCAT(FORMAT(sum(nsly), 0)) as nsly,
CONCAT(ROUND(TRUNCATE(((SUM(ns) / SUM(nsly)-1) * 100),2),1),"%")  as "nsly_percentage",
/* part 2 end */


/* part 3 */
CONCAT(FORMAT(sum(tc), 0)) as TC, 
CONCAT(FORMAT(sum(tctgt), 0)) as TCTGT,
CONCAT(ROUND(TRUNCATE(((SUM(tc) / SUM(tctgt)-1) * 100),2),1),"%")  as "tctgt_percentage",
/* part 3 end */

/* part 4 */
CONCAT(FORMAT(sum(tcly), 0)) as tcly,
CONCAT(ROUND(TRUNCATE(((SUM(tc) / SUM(tcly)-1) * 100),2),1),"%")  as "tcly_percentage",
/* part 4 end */

/* part 5 */
ROUND((SUM(ns) / SUM(tc)),1)  as "TA",
ROUND((SUM(nstgt) / SUM(tctgt)),1)  as "TATGT",
ROUND((SUM(nsly) / SUM(tcly)),1)  as "TALY",
/* part 5 end */

(
    select 
    CONCAT(FORMAT(sum(ns), 0))
    FROM cron_db.sales_report_export_1 as sales_report1
    LEFT JOIN (SELECT store_code,market FROM cron_db.store_master) sm 
    ON sales_report1.storeid = sm.store_code
    WHERE 
    week IN (SELECT DISTINCT(week) FROM cron_db.sales_report_export_2 WHERE txndate = SUBDATE(CURDATE(),2))
    and period IN (SELECT DISTINCT(period) FROM cron_db.sales_report_export_1 WHERE uploaddate = CURDATE() - INTERVAL 7 DAY GROUP BY uploaddate)
) as nslw



FROM cron_db.sales_report_export_1 as sales_report1
LEFT JOIN (SELECT store_code,market,company FROM cron_db.store_master) sm 
ON sales_report1.storeid = sm.store_code
WHERE market = "CGY"
AND week = (SELECT DISTINCT(week) FROM cron_db.sales_report_export_1 WHERE txndate = SUBDATE(CURDATE(),2))
AND period = (SELECT DISTINCT(period) FROM cron_db.sales_report_export_1 WHERE txndate = SUBDATE(CURDATE(),2)) AND (nsly != "." OR tcly != ".") 
GROUP BY sales_report1.storeid
我在子查询中得到的输出:

SELECT 

    storeid,
    company,
    market as store,

    /* part 1 */
    CONCAT(FORMAT(sum(ns), 0)) as NS, 
    CONCAT(FORMAT(sum(nstgt), 0)) as NSTGT,
    CONCAT(ROUND(TRUNCATE(((SUM(ns) / SUM(nstgt)-1) * 100),2),1),"%")  as "nstgt_percentage",
    /* part 1 end*/



    /* part 2 */
    CONCAT(FORMAT(sum(nsly), 0)) as nsly,
    CONCAT(ROUND(TRUNCATE(((SUM(ns) / SUM(nsly)-1) * 100),2),1),"%")  as "nsly_percentage",
    /* part 2 end */


    /* part 3 */
    CONCAT(FORMAT(sum(tc), 0)) as TC, 
    CONCAT(FORMAT(sum(tctgt), 0)) as TCTGT,
    CONCAT(ROUND(TRUNCATE(((SUM(tc) / SUM(tctgt)-1) * 100),2),1),"%")  as "tctgt_percentage",
    /* part 3 end */

    /* part 4 */
    CONCAT(FORMAT(sum(tcly), 0)) as tcly,
    CONCAT(ROUND(TRUNCATE(((SUM(tc) / SUM(tcly)-1) * 100),2),1),"%")  as "tcly_percentage",
    /* part 4 end */

    /* part 5 */
    ROUND((SUM(ns) / SUM(tc)),1)  as "TA",
    ROUND((SUM(nstgt) / SUM(tctgt)),1)  as "TATGT",
    ROUND((SUM(nsly) / SUM(tcly)),1)  as "TALY"
    /* part 5 end */

    FROM xxxx.xxxxxxxxxxx as sales_report1
    LEFT JOIN (SELECT store_code,market,company FROM xxxx.xxxxxxx) sm 
    ON sales_report1.storeid = sm.store_code
    WHERE market = "CGY" AND
    week = (SELECT DISTINCT(week) FROM xxxxxxx.xxxxxxxx WHERE txndate = SUBDATE(CURDATE(),2))
    AND period = (SELECT DISTINCT(period) FROM xxxxx.xxxxxxxxx WHERE txndate = SUBDATE(CURDATE(),2)) AND (nsly != "." OR tcly != ".") GROUP BY storeid
    (
    select 
    ns

    FROM xxxx.xxxxxx as sales_report1
    LEFT JOIN (SELECT store_code,market FROM xxx.xxxxx) sm 
    ON sales_report1.storeid = sm.store_code
    WHERE market = "CGY" AND
    week = (SELECT DISTINCT(week) FROM xxxxx.xxxxxxx WHERE uploaddate = CURDATE() - INTERVAL 7 DAY GROUP BY uploaddate)  
    AND period = (SELECT DISTINCT(period) FROM xxxx.xxxxxxxx WHERE uploaddate = CURDATE() - INTERVAL 7 DAY GROUP BY uploaddate)

) as nslw,
SELECT 

sales_report1.storeid,
company,
market as store,

/* part 1 */
CONCAT(FORMAT(sum(ns), 0)) as NS, 
CONCAT(FORMAT(sum(nstgt), 0)) as NSTGT,
CONCAT(ROUND(TRUNCATE(((SUM(ns) / SUM(nstgt)-1) * 100),2),1),"%")  as "nstgt_percentage",
/* part 1 end*/

/* part 2 */
CONCAT(FORMAT(sum(nsly), 0)) as nsly,
CONCAT(ROUND(TRUNCATE(((SUM(ns) / SUM(nsly)-1) * 100),2),1),"%")  as "nsly_percentage",
/* part 2 end */


/* part 3 */
CONCAT(FORMAT(sum(tc), 0)) as TC, 
CONCAT(FORMAT(sum(tctgt), 0)) as TCTGT,
CONCAT(ROUND(TRUNCATE(((SUM(tc) / SUM(tctgt)-1) * 100),2),1),"%")  as "tctgt_percentage",
/* part 3 end */

/* part 4 */
CONCAT(FORMAT(sum(tcly), 0)) as tcly,
CONCAT(ROUND(TRUNCATE(((SUM(tc) / SUM(tcly)-1) * 100),2),1),"%")  as "tcly_percentage",
/* part 4 end */

/* part 5 */
ROUND((SUM(ns) / SUM(tc)),1)  as "TA",
ROUND((SUM(nstgt) / SUM(tctgt)),1)  as "TATGT",
ROUND((SUM(nsly) / SUM(tcly)),1)  as "TALY",
/* part 5 end */

(
    select 
    CONCAT(FORMAT(sum(ns), 0))
    FROM cron_db.sales_report_export_1 as sales_report1
    LEFT JOIN (SELECT store_code,market FROM cron_db.store_master) sm 
    ON sales_report1.storeid = sm.store_code
    WHERE 
    week IN (SELECT DISTINCT(week) FROM cron_db.sales_report_export_2 WHERE txndate = SUBDATE(CURDATE(),2))
    and period IN (SELECT DISTINCT(period) FROM cron_db.sales_report_export_1 WHERE uploaddate = CURDATE() - INTERVAL 7 DAY GROUP BY uploaddate)
) as nslw



FROM cron_db.sales_report_export_1 as sales_report1
LEFT JOIN (SELECT store_code,market,company FROM cron_db.store_master) sm 
ON sales_report1.storeid = sm.store_code
WHERE market = "CGY"
AND week = (SELECT DISTINCT(week) FROM cron_db.sales_report_export_1 WHERE txndate = SUBDATE(CURDATE(),2))
AND period = (SELECT DISTINCT(period) FROM cron_db.sales_report_export_1 WHERE txndate = SUBDATE(CURDATE(),2)) AND (nsly != "." OR tcly != ".") 
GROUP BY sales_report1.storeid

示例输出必须在我的子查询中:

SELECT 

    storeid,
    company,
    market as store,

    /* part 1 */
    CONCAT(FORMAT(sum(ns), 0)) as NS, 
    CONCAT(FORMAT(sum(nstgt), 0)) as NSTGT,
    CONCAT(ROUND(TRUNCATE(((SUM(ns) / SUM(nstgt)-1) * 100),2),1),"%")  as "nstgt_percentage",
    /* part 1 end*/



    /* part 2 */
    CONCAT(FORMAT(sum(nsly), 0)) as nsly,
    CONCAT(ROUND(TRUNCATE(((SUM(ns) / SUM(nsly)-1) * 100),2),1),"%")  as "nsly_percentage",
    /* part 2 end */


    /* part 3 */
    CONCAT(FORMAT(sum(tc), 0)) as TC, 
    CONCAT(FORMAT(sum(tctgt), 0)) as TCTGT,
    CONCAT(ROUND(TRUNCATE(((SUM(tc) / SUM(tctgt)-1) * 100),2),1),"%")  as "tctgt_percentage",
    /* part 3 end */

    /* part 4 */
    CONCAT(FORMAT(sum(tcly), 0)) as tcly,
    CONCAT(ROUND(TRUNCATE(((SUM(tc) / SUM(tcly)-1) * 100),2),1),"%")  as "tcly_percentage",
    /* part 4 end */

    /* part 5 */
    ROUND((SUM(ns) / SUM(tc)),1)  as "TA",
    ROUND((SUM(nstgt) / SUM(tctgt)),1)  as "TATGT",
    ROUND((SUM(nsly) / SUM(tcly)),1)  as "TALY"
    /* part 5 end */

    FROM xxxx.xxxxxxxxxxx as sales_report1
    LEFT JOIN (SELECT store_code,market,company FROM xxxx.xxxxxxx) sm 
    ON sales_report1.storeid = sm.store_code
    WHERE market = "CGY" AND
    week = (SELECT DISTINCT(week) FROM xxxxxxx.xxxxxxxx WHERE txndate = SUBDATE(CURDATE(),2))
    AND period = (SELECT DISTINCT(period) FROM xxxxx.xxxxxxxxx WHERE txndate = SUBDATE(CURDATE(),2)) AND (nsly != "." OR tcly != ".") GROUP BY storeid
    (
    select 
    ns

    FROM xxxx.xxxxxx as sales_report1
    LEFT JOIN (SELECT store_code,market FROM xxx.xxxxx) sm 
    ON sales_report1.storeid = sm.store_code
    WHERE market = "CGY" AND
    week = (SELECT DISTINCT(week) FROM xxxxx.xxxxxxx WHERE uploaddate = CURDATE() - INTERVAL 7 DAY GROUP BY uploaddate)  
    AND period = (SELECT DISTINCT(period) FROM xxxx.xxxxxxxx WHERE uploaddate = CURDATE() - INTERVAL 7 DAY GROUP BY uploaddate)

) as nslw,
SELECT 

sales_report1.storeid,
company,
market as store,

/* part 1 */
CONCAT(FORMAT(sum(ns), 0)) as NS, 
CONCAT(FORMAT(sum(nstgt), 0)) as NSTGT,
CONCAT(ROUND(TRUNCATE(((SUM(ns) / SUM(nstgt)-1) * 100),2),1),"%")  as "nstgt_percentage",
/* part 1 end*/

/* part 2 */
CONCAT(FORMAT(sum(nsly), 0)) as nsly,
CONCAT(ROUND(TRUNCATE(((SUM(ns) / SUM(nsly)-1) * 100),2),1),"%")  as "nsly_percentage",
/* part 2 end */


/* part 3 */
CONCAT(FORMAT(sum(tc), 0)) as TC, 
CONCAT(FORMAT(sum(tctgt), 0)) as TCTGT,
CONCAT(ROUND(TRUNCATE(((SUM(tc) / SUM(tctgt)-1) * 100),2),1),"%")  as "tctgt_percentage",
/* part 3 end */

/* part 4 */
CONCAT(FORMAT(sum(tcly), 0)) as tcly,
CONCAT(ROUND(TRUNCATE(((SUM(tc) / SUM(tcly)-1) * 100),2),1),"%")  as "tcly_percentage",
/* part 4 end */

/* part 5 */
ROUND((SUM(ns) / SUM(tc)),1)  as "TA",
ROUND((SUM(nstgt) / SUM(tctgt)),1)  as "TATGT",
ROUND((SUM(nsly) / SUM(tcly)),1)  as "TALY",
/* part 5 end */

(
    select 
    CONCAT(FORMAT(sum(ns), 0))
    FROM cron_db.sales_report_export_1 as sales_report1
    LEFT JOIN (SELECT store_code,market FROM cron_db.store_master) sm 
    ON sales_report1.storeid = sm.store_code
    WHERE 
    week IN (SELECT DISTINCT(week) FROM cron_db.sales_report_export_2 WHERE txndate = SUBDATE(CURDATE(),2))
    and period IN (SELECT DISTINCT(period) FROM cron_db.sales_report_export_1 WHERE uploaddate = CURDATE() - INTERVAL 7 DAY GROUP BY uploaddate)
) as nslw



FROM cron_db.sales_report_export_1 as sales_report1
LEFT JOIN (SELECT store_code,market,company FROM cron_db.store_master) sm 
ON sales_report1.storeid = sm.store_code
WHERE market = "CGY"
AND week = (SELECT DISTINCT(week) FROM cron_db.sales_report_export_1 WHERE txndate = SUBDATE(CURDATE(),2))
AND period = (SELECT DISTINCT(period) FROM cron_db.sales_report_export_1 WHERE txndate = SUBDATE(CURDATE(),2)) AND (nsly != "." OR tcly != ".") 
GROUP BY sales_report1.storeid


谢谢。

我不确定您是如何将这两个问题组合在一起的,因为您已经分别介绍了这两个问题。大多数情况下,当我们使用=组合查询时,子查询返回的错误超过1行

SELECT *
FROM Products
WHERE Price = (SELECT MAX(Price) FROM Products) 
为了避免这种情况,我们可以在关键字中使用

SELECT *
FROM Products
WHERE Price IN 
      (SELECT Price 
       FROM Products
       Where ExpDate>xxxxxxx)    

为什么要放弃投票?你们不明白我的问题吗?3.谢谢你的指正。。我将替换我的工作script@SoulAiker你的问题没有多大意义,
sales_report1
没有任何关于
sales_report2
@SudiptaMondal的参考。我已经对此进行了更改。在我的查询中如何使用它?@SoulAiker正如我提到的,很难理解父查询和子查询之间的关系。你能提供完整的查询吗?你在哪里使用子查询的输出(nslw)