SQL - Sub select、联合和使用函数
SQL - Sub select, Union and using functions
我有 2 个查询,一个用于导出总订单,另一个用于获取已交付的订单。列名与顺序和值相同。只有条件在子查询中会有所不同。我想分别获取已交付的订单和价值以及单独的整体订单和价值。之后,我想将交付价值从整体价值中除以百分比值。
只有一个table,只是条件不同。
我希望从 2021-01 年到现在这个月明智。
这是我试过但不起作用的例子。请帮助我。
示例:
Select
a.C_Month,
a.C_Orders/B_Orders as per_orders,
a.C_Value/B_Value as per_Value
from(
select
to_char(estDate,'yyyymm') as B_Month,
null as area,
count(distinct order_id) as B_Orders,
sum (Value) as B_Value
from table1
where condition1 is not null
and condition2 is ='1'
and to_char(estDate,'yyyymm')>= '202101'
group by to_char(estDate,'yyyymm')
order by to_char(estDate,'yyyymm')
union
select
to_char(estDate,'yyyymm') as C_Month,
area,
count(distinct order_id) as C_Orders,
sum (Value) as C_Value
from table1
where condition1 is not null
and condition2 is ='1'
where condition3 is not null
where condition4 is not null
and to_char(estDate,'yyyymm')>= '202101'
group by to_char(estDate,'yyyymm')
order by to_char(estDate,'yyyymm')
) as a
group by
a.C_Month,
a.C_Orders/B_Orders as per_orders,
a.C_Value/B_Value as per_Value
;
首先,在您的示例中,您要求的是“JOIN”,但您创建的是“UNION”:
按照您的逻辑,您可能需要一个自连接 on B_Month = C_Month
。
查询:
Select
a.C_Month,
a.C_Orders/aa.B_Orders as per_orders,
a.C_Value/aa.B_Value as per_Value
from(
(select
to_char(estDate,'yyyymm') as B_Month,
null as area,
count(distinct order_id) as B_Orders,
sum (Value) as B_Value
from table1
where condition1 is not null
and condition2 is ='1'
and to_char(estDate,'yyyymm')>= '202101' ) as aa
INNER JOIN
(select
to_char(estDate,'yyyymm') as C_Month,
area,
count(distinct order_id) as C_Orders,
sum (Value) as C_Value
from table2
where condition1 is not null
and condition2 is ='1'
where condition3 is not null
where condition4 is not null
and to_char(estDate,'yyyymm')>= '202101' ) as a
ON aa.B_Month=a.C_Month
查询将 table1 的每一行与 table2 的每一行进行比较,以找到满足连接谓词的所有行对。
请记住,可以根据您想要的输出将“INNER JOIN”替换为其他谓词(LEFT JOIN、RIGHT JOIN、OUTER JOIN、FULL JOIN)。
我有 2 个查询,一个用于导出总订单,另一个用于获取已交付的订单。列名与顺序和值相同。只有条件在子查询中会有所不同。我想分别获取已交付的订单和价值以及单独的整体订单和价值。之后,我想将交付价值从整体价值中除以百分比值。 只有一个table,只是条件不同。 我希望从 2021-01 年到现在这个月明智。 这是我试过但不起作用的例子。请帮助我。
示例:
Select
a.C_Month,
a.C_Orders/B_Orders as per_orders,
a.C_Value/B_Value as per_Value
from(
select
to_char(estDate,'yyyymm') as B_Month,
null as area,
count(distinct order_id) as B_Orders,
sum (Value) as B_Value
from table1
where condition1 is not null
and condition2 is ='1'
and to_char(estDate,'yyyymm')>= '202101'
group by to_char(estDate,'yyyymm')
order by to_char(estDate,'yyyymm')
union
select
to_char(estDate,'yyyymm') as C_Month,
area,
count(distinct order_id) as C_Orders,
sum (Value) as C_Value
from table1
where condition1 is not null
and condition2 is ='1'
where condition3 is not null
where condition4 is not null
and to_char(estDate,'yyyymm')>= '202101'
group by to_char(estDate,'yyyymm')
order by to_char(estDate,'yyyymm')
) as a
group by
a.C_Month,
a.C_Orders/B_Orders as per_orders,
a.C_Value/B_Value as per_Value
;
首先,在您的示例中,您要求的是“JOIN”,但您创建的是“UNION”:
按照您的逻辑,您可能需要一个自连接 on B_Month = C_Month
。
查询:
Select
a.C_Month,
a.C_Orders/aa.B_Orders as per_orders,
a.C_Value/aa.B_Value as per_Value
from(
(select
to_char(estDate,'yyyymm') as B_Month,
null as area,
count(distinct order_id) as B_Orders,
sum (Value) as B_Value
from table1
where condition1 is not null
and condition2 is ='1'
and to_char(estDate,'yyyymm')>= '202101' ) as aa
INNER JOIN
(select
to_char(estDate,'yyyymm') as C_Month,
area,
count(distinct order_id) as C_Orders,
sum (Value) as C_Value
from table2
where condition1 is not null
and condition2 is ='1'
where condition3 is not null
where condition4 is not null
and to_char(estDate,'yyyymm')>= '202101' ) as a
ON aa.B_Month=a.C_Month
查询将 table1 的每一行与 table2 的每一行进行比较,以找到满足连接谓词的所有行对。
请记住,可以根据您想要的输出将“INNER JOIN”替换为其他谓词(LEFT JOIN、RIGHT JOIN、OUTER JOIN、FULL JOIN)。