【问题标题】:how can i combine multiple union in single query如何在单个查询中组合多个联合
【发布时间】:2015-03-18 20:51:10
【问题描述】:

查询返回站点和时间的平均工作日账单。 以下查询使用多个联合我想在单个查询中组合所有 我该怎么做。

select 
        a.Month,
        'Weekday' as Type,
        'Lunch' as 'Sale',
        sum(case
            when a.siteId = '102' then a.AvgBill
            else '--'
        end) as '102',
        sum(case
            when a.siteId = '103' then a.AvgBill
            else '--'
        end) as '103',
        sum(case
            when a.siteId = '104' then a.AvgBill
            else '--'
        end) as '104',
        sum(case
            when a.siteId = '105' then a.AvgBill
            else '--'
        end) as '105',
        sum(case
            when a.siteId = '106' then a.AvgBill
            else '--'
        end) as '106'
    from
        (select 
            date_format(o.dayStatus, '%M') as Month,
                c.companyId as CompanyId,
                o.companyId as siteId,
                o.bill,
                round((sum(o.bill) / count(orderId)), 2) as AvgBill,
                date_format(DTTM, '%a') as Day
        from
            orders o, mdm_sites s, mdm_company c
        where
            s.siteId = o.companyId
                and o.isBilled = 1
                and billMode = 0
                and s.companyid = c.CompanyId
                and time(DTTM) <= '15:00'
                and date_format(DTTM, '%a') not in ('Sat' , 'Sun')
                and year(o.dayStatus) = (2014)
                and case
                when 0 in (1) then c.companyId in (c.companyid)
                else c.companyId in (1)
            end
                and case
                when 0 in (0) then o.companyId in (o.companyId)
                else o.companyId in (0)
            end
                and case
                when 0 in (12) then month(o.dayStatus) in (month(o.dayStatus))
                else month(o.dayStatus) in (12)
            end
        group by c.companyId , o.companyId) as a
    group by a.CompanyId 
    union (select 
        a.Month,
        'Weekday' as Type,
        'Evening' as 'Sale',
        sum(case
            when a.siteId = '102' then a.AvgBill
            else '--'
        end) as '102',
        sum(case
            when a.siteId = '103' then a.AvgBill
            else '--'
        end) as '103',
        sum(case
            when a.siteId = '104' then a.AvgBill
            else '--'
        end) as '104',
        sum(case
            when a.siteId = '105' then a.AvgBill
            else '--'
        end) as '105',
        sum(case
            when a.siteId = '106' then a.AvgBill
            else '--'
        end) as '106'
    from
        (select 
            date_format(o.daystatus, '%M') as Month,
                c.companyId as CompanyId,
                o.companyId as siteId,
                o.bill,
                round((sum(o.bill) / count(orderId)), 2) as AvgBill,
                date_format(DTTM, '%a') as Day
        from
            orders o, mdm_sites s, mdm_company c
        where
            s.siteId = o.companyId
                and o.isBilled = 1
                and billMode = 0
                and s.companyid = c.CompanyId
                and time(DTTM) > '15:00'
                and time(DTTM) < '19:00'
                and date_format(DTTM, '%a') not in ('Sat' , 'Sun')
                and year(o.dayStatus) = (2014)
                and case
                when 0 in (1) then c.companyId in (c.companyid)
                else c.companyId in (1)
            end
                and case
                when 0 in (0) then o.companyId in (o.companyId)
                else o.companyId in (0)
            end
                and case
                when 0 in (12) then month(o.dayStatus) in (month(o.dayStatus))
                else month(o.dayStatus) in (12)
            end
        group by c.companyId , o.companyId) as a
    group by a.CompanyId) union (select 
        a.Month,
        'Weekday' as Type,
        'Dinner' as 'Sale',
        sum(case
            when a.siteId = '102' then a.AvgBill
            else '--'
        end) as '102',
        sum(case
            when a.siteId = '103' then a.AvgBill
            else '--'
        end) as '103',
        sum(case
            when a.siteId = '104' then a.AvgBill
            else '--'
        end) as '104',
        sum(case
            when a.siteId = '105' then a.AvgBill
            else '--'
        end) as '105',
        sum(case
            when a.siteId = '106' then a.AvgBill
            else '--'
        end) as '106'
    from
        (select 
            date_format(o.dayStatus, '%M') as Month,
                c.companyId as CompanyId,
                o.companyId as siteId,
                o.bill,
                round((sum(o.bill) / count(orderId)), 2) as AvgBill,
                date_format(DTTM, '%a') as Day
        from
            orders o, mdm_sites s, mdm_company c
        where
            s.siteId = o.companyId
                and o.isBilled = 1
                and billMode = 0
                and s.companyid = c.CompanyId
                and time(DTTM) >= '19:00'
                and date_format(DTTM, '%a') not in ('Fri' , 'Sat', 'Sun')
                and year(o.dayStatus) = (2014)
                and case
                when 0 in (1) then c.companyId in (c.companyid)
                else c.companyId in (1)
            end
                and case
                when 0 in (0) then o.companyId in (o.companyId)
                else o.companyId in (0)
            end
                and case
                when 0 in (12) then month(o.dayStatus) in (month(o.dayStatus))
                else month(o.dayStatus) in (12)
            end
        group by c.companyId , o.companyId) as a
    group by a.CompanyId) union (select 
        a.Month,
        'Weekend' as Type,
        'Lunch' as 'Sale',
        sum(case
            when a.siteId = '102' then a.AvgBill
            else '--'
        end) as '102',
        sum(case
            when a.siteId = '103' then a.AvgBill
            else '--'
        end) as '103',
        sum(case
            when a.siteId = '104' then a.AvgBill
            else '--'
        end) as '104',
        sum(case
            when a.siteId = '105' then a.AvgBill
            else '--'
        end) as '105',
        sum(case
            when a.siteId = '106' then a.AvgBill
            else '--'
        end) as '106'
    from
        (select 
            date_format(o.daystatus, '%M') as Month,
                c.companyId as CompanyId,
                o.companyId as siteId,
                o.bill,
                round((sum(o.bill) / count(orderId)), 2) as AvgBill,
                date_format(DTTM, '%a') as Day
        from
            orders o, mdm_sites s, mdm_company c
        where
            s.siteId = o.companyId
                and o.isBilled = 1
                and billMode = 0
                and s.companyid = c.CompanyId
                and time(DTTM) <= '15:00'
                and date_format(DTTM, '%a') in ('Sat' , 'Sun')
                and year(o.dayStatus) = (2014)
                and case
                when 0 in (1) then c.companyId in (c.companyid)
                else c.companyId in (1)
            end
                and case
                when 0 in (0) then o.companyId in (o.companyId)
                else o.companyId in (0)
            end
                and case
                when 0 in (12) then month(o.dayStatus) in (month(o.dayStatus))
                else month(o.dayStatus) in (12)
            end
        group by c.companyId , o.companyId) as a
    group by a.CompanyId) union (select 
        a.Month,
        'Weekend' as Type,
        'Evening' as 'Sale',
        sum(case
            when a.siteId = '102' then a.AvgBill
            else '--'
        end) as '102',
        sum(case
            when a.siteId = '103' then a.AvgBill
            else '--'
        end) as '103',
        sum(case
            when a.siteId = '104' then a.AvgBill
            else '--'
        end) as '104',
        sum(case
            when a.siteId = '105' then a.AvgBill
            else '--'
        end) as '105',
        sum(case
            when a.siteId = '106' then a.AvgBill
            else '--'
        end) as '106'
    from
        (select 
            date_format(o.dayStatus, '%M') as Month,
                c.companyId as CompanyId,
                o.companyId as siteId,
                o.bill,
                round((sum(o.bill) / count(orderId)), 2) as AvgBill,
                date_format(DTTM, '%a') as Day
        from
            orders o, mdm_sites s, mdm_company c
        where
            s.siteId = o.companyId
                and o.isBilled = 1
                and billMode = 0
                and s.companyid = c.CompanyId
                and time(DTTM) > '15:00'
                and time(DTTM) < '19:00'
                and date_format(DTTM, '%a') in ('Sat' , 'Sun')
                and year(o.dayStatus) = (2014)
                and case
                when 0 in (1) then c.companyId in (c.companyid)
                else c.companyId in (1)
            end
                and case
                when 0 in (0) then o.companyId in (o.companyId)
                else o.companyId in (0)
            end
                and case
                when 0 in (12) then month(o.dayStatus) in (month(o.dayStatus))
                else month(o.dayStatus) in (12)
            end
        group by c.companyId , o.companyId) as a
    group by a.CompanyId) union (select 
        a.Month,
        'Weekend' as Type,
        'Dinner' as 'Sale',
        sum(case
            when a.siteId = '102' then a.AvgBill
            else '--'
        end) as '102',
        sum(case
            when a.siteId = '103' then a.AvgBill
            else '--'
        end) as '103',
        sum(case
            when a.siteId = '104' then a.AvgBill
            else '--'
        end) as '104',
        sum(case
            when a.siteId = '105' then a.AvgBill
            else '--'
        end) as '105',
        sum(case
            when a.siteId = '106' then a.AvgBill
            else '--'
        end) as '106'
    from
        (select 
            date_format(o.dayStatus, '%M') as Month,
                c.companyId as CompanyId,
                o.companyId as siteId,
                o.bill,
                round((sum(o.bill) / count(orderId)), 2) as AvgBill,
                date_format(DTTM, '%a') as Day
        from
            orders o, mdm_sites s, mdm_company c
        where
            s.siteId = o.companyId
                and o.isBilled = 1
                and billMode = 0
                and s.companyid = c.CompanyId
                and time(DTTM) >= '19:00'
                and date_format(DTTM, '%a') in ('Fri' , 'Sat', 'Sun')
                and year(o.dayStatus) = (2014)
                and case
                when 0 in (1) then c.companyId in (c.companyid)
                else c.companyId in (1)
            end
                and case
                when 0 in (0) then o.companyId in (o.companyId)
                else o.companyId in (0)
            end
                and case
                when 0 in (12) then month(o.dayStatus) in (month(o.dayStatus))
                else month(o.dayStatus) in (12)
            end
        group by c.companyId , o.companyId) as a
    group by a.CompanyId);


the output of above query

month       Type    Sale    102     103    104     105      106   
------------------------------------------------------------------
December    Weekday Lunch   566.63  530.19  644.9   556.9   467.65
December    Weekday Evening 433.89  404.69  457.18  547.22  396.6
December    Weekday Dinner  427.68  354.54  371.42  386.21  366.03
December    Weekend Lunch   410.57  381.36  383.86  365.94  394.14
December    Weekend Evening 418.45  305.31  429.12  464.81  301.9
December    Weekend Dinner  415.91  374.95  375.28  376.93  360.45

如何在单个查询中做到这一点,谢谢

【问题讨论】:

  • 您能否发布一个仅包含核心问题的查询的简化版本?否则我怀疑有人会经历那件事。
  • 查询需要更多时间....所以我想在更短的时间内执行

标签: mysql select union


【解决方案1】:

支点将是这里的理想解决方案,并且可以节省执行计划。您的 Pivot 组将是 Month、Type、Sale。您的计算组将是 102,103,104,105,106。

枢轴也将在一个查询中完成。

枢轴一般指南: https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx

一个与您尝试完成的非常相似的示例: http://blogs.msdn.com/b/spike/archive/2009/03/03/pivot-tables-in-sql-server-a-simple-sample.aspx

【讨论】:

    猜你喜欢
    • 2021-05-27
    • 2013-09-06
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 2017-10-09
    • 2012-11-01
    • 1970-01-01
    相关资源
    最近更新 更多