我下面有三个查询(除了WHERE子句的第一行以外,其他查询都一样)在我的脚本中都可以正常工作。第一个查询客户的订单,第二个查询分配给代表的所有订单,第三个是整个公司的所有订单期。
同样,它们都在给定了各自的变量的情况下工作(所有变量都来自同一页面),但我试图为所有3种情况填充表中的列。
有没有办法将它们组合起来并创建一个查询,使每个子句的值都相同?
因此,我希望为一个查询返回所有6列。这是在db2上运行的,所以我不知道最佳的处理方式,但是我可以创建一个更大的基于CASE的查询吗?
//query on orders for this customer
SELECT
count(*) as sales_180Cust,
180/count(*) as velocityCust
FROM orders g
inner join dates i
on g.date1 = i.acyyyymmdd
WHERE g.cust = $customer
AND g.frm = $frm
AND g.cvr = $cvr
AND g.clr = $clr
AND i.aciso between current_Date - 180 DAY AND current_Date;
//orders belonging to representative
SELECT
count(*) as sales_180Rep,
180/count(*) as velocityRep
FROM orders g
inner join dates i
on g.date1 = i.acyyyymmdd
WHERE g.rep = $rep
AND g.frm = $frm
AND g.cvr = $cvr
AND g.clr = $clr
AND i.aciso between current_Date - 180 DAY AND current_Date;
//query across ALL orders
SELECT
count(*) as sales_180Company,
180/count(*) as velocityCompany
FROM orders g
inner join dates i
on g.date1 = i.acyyyymmdd
WHERE g.frm = $frm
AND g.cvr = $cvr
AND g.clr = $clr
AND i.aciso between current_Date - 180 DAY AND current_Date;
答案 0 :(得分:1)
select q1.*, q2.*, q3.*
from
(select count(*) as sales_180Cust, 180/count(*) as velocityCust from table(values 1) t(i)) q1
, (select count(*) as sales_180Rep, 180/count(*) as velocityRep from table(values 1, 2) t(i)) q2
, (select count(*) as sales_180Company, 180/count(*) as velocityCompany from table(values 1, 2, 3) t(i)) q3
我修改了您的FROM和WERE子句以显示想法。
答案 1 :(得分:1)
这将是另一种方式
SQL> select h.id, h.name,
2 decode(f.id, null, 'No', 'Yes') full_time_position
3 from t_first h left join t_second f on h.id = f.id
4 order by h.id;
ID NAME FUL
---------- -------------------- ---
1 Little No
2 Foot No
3 Scott Yes
4 Tiger Yes
SQL>
返回例如
WITH CTE AS (
SELECT g.cust
, g.rep
FROM orders g
inner join dates i
on g.date1 = i.acyyyymmdd
WHERE
g.frm = $frm
AND g.cvr = $cvr
AND g.clr = $clr
)
SELECT
count(*) as sales_180Company
, 180/count(*) as velocityCompany
, 'cust' as query
FROM CTE
WHERE cust = $customer
UNION ALL
SELECT
count(*) as sales_180Company
, 180/count(*) as velocityCompany
, 'rep' as query
FROM CTE
WHERE rep = $rep
UNION ALL
SELECT
count(*) as sales_180Company
, 180/count(*) as velocityCompany
, 'all' as query
FROM CTE
答案 2 :(得分:0)
很多方法可以做到这一点。 UNION
是显而易见的方式。 GROUPING SET
s更加聪明。
create table orders(date1 int, rep int, cust int,frm int, cvr int, clr int, aci int);
create table dates(acyyyymmdd int, aciso date);
create variable $frm int default 1;
create variable $cvr int default 1;
create variable $clr int default 1;
create variable $customer int default 1;
create variable $rep int default 1;
insert into orders values (1,0,0,1,1,1,1), (1,1,1,1,1,1,1), (1,2,1,1,1,1,1), (1,3,1,1,1,1,1), (1,1,2,1,1,1,1);
insert into dates values (1, current date);
然后这个
SELECT
count(*) as sales_180Company
, 180/count(*) as velocityCompany
, g.cust
, g.rep
FROM orders g
inner join dates i
on g.date1 = i.acyyyymmdd
WHERE
g.frm = $frm
AND g.cvr = $cvr
AND g.clr = $clr
AND i.aciso between current_Date - 180 DAY AND current_Date
GROUP BY GROUPING SETS ( (), (cust), (rep) )
HAVING (cust = $customer AND rep is null)
OR (cust is null AND rep = $rep)
OR (cust is null AND rep is null)
为此
SALES_180COMPANY VELOCITYCOMPANY CUST REP
---------------- --------------- ---- ----
5 36 NULL NULL
3 60 1 NULL
2 90 NULL 1
答案 3 :(得分:0)
或者这个...通常有很多方法可以用SQL进行操作
WITH CTE AS (
SELECT g.cust
, g.rep
FROM orders g
inner join dates i
on g.date1 = i.acyyyymmdd
WHERE
g.frm = $frm
AND g.cvr = $cvr
AND g.clr = $clr
) , CUST AS (
SELECT
count(*) as sales_180Company
, 180/count(*) as velocityCompany
FROM CTE
WHERE cust = $customer
) , REP AS (
SELECT
count(*) as sales_180Company
, 180/count(*) as velocityCompany
FROM CTE
WHERE rep = $rep
) , ALL AS (
SELECT
count(*) as sales_180Company
, 180/count(*) as velocityCompany
FROM CTE
)
SELECT * FROM CUST, REP, ALL
调整
SALES_180COMPANY VELOCITYCOMPANY SALES_180COMPANY VELOCITYCOMPANY SALES_180COMPANY VELOCITYCOMPANY
---------------- --------------- ---------------- --------------- ---------------- ---------------
3 60 2 90 5 36