我想将两个sql语句作为一个。 第一个显示每日下达的总订单,第二个显示每天只有员工作为操作文本的订单数。
select date(dated) date_ord, count(*) as orders_placed
from stats.sales_actions
group by date_ord
order by dated desc
select date(dated) date_order, count(*) as orders_modified
from stats.sales_actions
where action_text LIKE '%STAFF%'
group by date_ord
order by dated desc
如果可能,我想在一个表/ sql查询中显示。
答案 0 :(得分:2)
只需使用UNION
:
select date(dated) date_ord, count(*), 'orders_places' as orders_placed
from stats.sales_actions
group by date_ord
order by dated desc
UNION
select date(dated) date_order, count(*), 'orders_modified' as orders_modified
from stats.sales_actions
where action_text LIKE '%STAFF%'
group by date_ord
order by dated desc
答案 1 :(得分:1)
您可以使用UNION来实现此目的 - 然后您可以使用AS关键字为count列指定一个名称,以便将它们彼此区分开来。例如:
select date(dated) date_ord, count(*) AS placed, 'orders_places' as orders_placed
from stats.sales_actions
group by date_ord
order by dated desc
UNION
select date(dated) date_order, count(*) AS modified, orders_modified' as orders_modified
from stats.sales_actions
where action_text LIKE '%STAFF%'
group by date_ord
order by dated desc
答案 2 :(得分:1)
select date_ord, sum( orders_placed) as orders_placed ,
sum( orders_modified) as orders_modified
from(
select date(dated) date_ord, count(*) as orders_placed , 0 as orders_modified
from stats.sales_actions
group by date_ord
order by dated desc
UNION
select date(dated) date_ord, 0 as orders_placed, count(*) as orders_modified
from stats.sales_actions
where action_text LIKE '%STAFF%'
group by date_ord
order by dated desc
) temp
group by date_ord
order by dated desc
答案 3 :(得分:0)
select date(dated) date_ord,
count(*) as orders_placed,
SUM (
CASE
WHEN action_text LIKE '%STAFF%'
THEN 1 ELSE 0 END) AS ORDERS_STAFF
from stats.sales_actions
group by date_ord
order by dated desc
这会在一行中显示两个值。如果您喜欢2行,只需在查询之间使用联合。