【问题标题】:Two sql query as one两个sql查询合二为一
【发布时间】:2012-02-07 05:28:43
【问题描述】:

有两个sql查询,我想写成一个查询。 第二个查询显示不同记录的计数,其中 action_text 像 '%STAFF%'。 我尝试使用 UNION 但它不起作用。

select date(dated) date_ord,
count(DISTINCT order_number) as orders_placed, 
sum(case when action_text like '%STAFF%' then 1 else 0 end) AS orders_staff,
sum(case when action_text in (
                'CBA Capture attempt',
                'GCO Capture attempt',
                'PPP Capture',
                'PPE Capture',
                'Staff CC capture',
                'Web CC capture',
                'Staff Finance WIRE authorized',
                'Staff Finance PO authorized',
                'Staff Finance COD authorized',
                'Authorized The CPIC') then 1 else 0 end)     AS     orders_manuallycaptured
 from stats.sales_actions
 group by date_ord 
 order by dated desc


SELECT COUNT(DISTINCT order_number) as unique_orderstouched,
date(dated) date_ords
FROM sales_actions
WHERE action_text like '%STAFF%' 
group by date_ords 
order by dated desc

【问题讨论】:

    标签: mysql sql join union


    【解决方案1】:

    据我所知,第二个查询中唯一的新列是COUNT(DISTINCT order_number) ... WHERE action_text LIKE '%STAFF%'

    然后您可以在原始查询中添加COUNT(DISTINCT IF(action-text LIKE '%STAFF%',order_number,NULL)) as unique_orderstouched

    (另外我假设您的第一个查询中的表 stats.sales_actions 与您的第二个查询中的表 sales_actions 相同?)。

    你最终会得到:

    select date(dated) date_ord,
    count(DISTINCT order_number) as orders_placed, 
    sum(case when action_text like '%STAFF%' then 1 else 0 end) AS orders_staff,
    sum(case when action_text in (
                    'CBA Capture attempt',
                    'GCO Capture attempt',
                    'PPP Capture',
                    'PPE Capture',
                    'Staff CC capture',
                    'Web CC capture',
                    'Staff Finance WIRE authorized',
                    'Staff Finance PO authorized',
                    'Staff Finance COD authorized',
                    'Authorized The CPIC') then 1 else 0 end) AS orders_manuallycaptured,
    -- new line follows
    COUNT(DISTINCT IF(action-text LIKE '%STAFF%',order_number,NULL)) 
       AS unique_orderstouched
     from stats.sales_actions
     group by date_ord 
     order by dated desc
    

    COUNT( DISTINCT IF( condition, order_number, NULL ) ) 就像COUNT( DISTINCT order_number ) ... WHERE <condition> 一样——你也可以把它想象成你的SUM( CASE WHEN condition THEN 1 ELSE 0),但里面有一个DISTINCT

    【讨论】:

    • 下达订单日期 触及订单 手动捕获的唯一订单 2012-02-03 329 162 77 135 2012-02-02 299 148 70 122 2012-02-01 340 169 102 156 2012-01- 31 271 143 78 126 2012-01-30 436 211 129 187 2012-01-27 275 138 66 112 2012-01-26 318 150 87 128 2012-01-25 297 145-701-27 152 16-27
    【解决方案2】:

    如果您进行联合,则您选择的列在两个查询之间必须相同。您需要具有相同数量的列、相同的数据类型,并且它们需要具有相同的顺序。在第一个查询中,您选择了四列,而第二个查询只选择了两列。

    【讨论】:

    • 好吧,那么灵魂可能是什么呢?
    • 解决方案的第一步是编辑答案,添加一个您期望得到的结果示例
    • 您能否详细说明您要对查询进行哪些操作?
    • 其中第 1 列是日期,第 2 列是放置的,第 3 条是订单的,第 3 条是订单的,第 4 条是手动捕获的,第 5 条是唯一的_订单的
    • 如果第一个列是日期,那么为什么您的第二个查询执行 count()?这导致数字而不是日期。请写下您期望的 4 或 5 行结果(包括列名!),以便我们了解您的预期结果。写得很好的问题示例:stackoverflow.com/questions/9133068/…
    猜你喜欢
    • 1970-01-01
    • 2014-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    相关资源
    最近更新 更多