我正在尝试合并两个基本提供相同功能的不同查询。每个查询的结果都基于一列的结果进行分类。当我尝试将它们组合时,我收到一条错误消息,指出存在算术溢出。
下面是代码的第一行。
select t1.Lives_Category,
Count(t3.clean_ft_ee_cnt) "MVP Number of Clients",
sum(t3.clean_ft_ee_cnt) as "MVP Sum of Lives"
from [mvpGA].[mvp_client_data_t] t3
join
(
select * from
(values ('1-9 Lives',1,9),
('10-49 Lives',10,49),
('50-199 Lives',50,199),
('200-499 Lives',200,499),
('500-1,9999 Lives',500,1999),
('2,000+ Lives',2000,100000000),
('Total Lives',0,100000000),
('500+ Lives',500,1000000000),
('Unknown Lives',0,0)
)
as Base_Table (Lives_Category,MinCnt,MaxCnt)
) t1
on t3.clean_ft_ee_cnt between t1.MinCnt and t1.MaxCnt
where t3.mvp_rpt_id > 1399
and t3.clean_do_not_use_ind is NULL
and t3.clean_client_indv_flag = 'Group'
group by t1.Lives_Category
结果显示为:
Lives_Category | MVP Number of Clients | MVP Sum of Lives
-----------------------------------------------------------
1-9 Lives | 7565 | 33845
10-49 Lives | 7996 | 191190
50-199 Lives | 6820 | 680157
200-499 Lives | 2281 | 683971
500-1,9999 Lives | 1510 | 1424911
2,000+ Lives | 672 | 8282279
| |
Total Lives | 26929 | 11296353
| |
500+ Lives | 2182 | 9707190
| |
Unknown Lives | 85 | 0
第二行代码在这里,结构相同,不同之处在于where子句。“客户数”和“活动总数”两列的结果必须是它们自己的列。
select distinct a1.Lives_Category as "Lives Category",
count(t2.clean_ft_ee_cnt) as "Number of Clients",
sum(t2.clean_ft_ee_cnt) as "Sum of Lives"
from [mvpGA].[mvp_client_data_t] t2
join
(
select * from
(values ('1-9 Lives',1,9),
('10-49 Lives',10,49),
('50-199 Lives',50,199),
('200-499 Lives',200,499),
('500-1,9999 Lives',500,1999),
('2,000+ Lives',2000,100000000),
('Total Lives',0,100000000),
('500+ Lives',500,1000000000),
('Unknown Lives',0,0)
)
as Base_Table (Lives_Category,MinCnt,MaxCnt)
) a1
on t2.clean_ft_ee_cnt between a1.MinCnt and a1.MaxCnt
where t2.mvp_rpt_id = 1400
and t2.clean_mvp_rpt_breakout = 'Fargo'
and t2.clean_do_not_use_ind is NULL
and t2.clean_client_indv_flag = 'Group'
group by a1.Lives_Category
我希望最终结果是5列。请参见下面的结构。
Lives_Category | MVP客户数量| MVP总和|客户数量|生命总和
因此,总而言之,结果的分类与Lives_Category中所示的分类.....但是我想有5列。注意:从技术上说,查询是从同一张表计算出来的,只是where子句不同。
答案 0 :(得分:0)
我对您的数据不太了解,无法知道应该使用内部联接,左联接,右联接还是完全联接。因此,您应该修改联接以适合您的情况,但这应该可以使您走上正确的道路。基本上,我将每个您的select语句放在一个子查询中,并加入公共字段“ Lives Category”。
SELECT COALESCE(x.[Lives Category], y.[Lives Category]) AS [Lives Category]
, x.[MVP Number of Clients]
, x.[MVP Sum of Lives]
, y.[Number of Clients]
, y.[Sum of Lives]
FROM (
SELECT t1.Lives_Category AS [Lives Category]
, COUNT(t3.clean_ft_ee_cnt) AS [MVP Number of Clients]
, SUM(t3.clean_ft_ee_cnt) AS [MVP Sum of Lives]
FROM mvpGA.mvp_client_data_t AS t3
JOIN (
SELECT *
FROM (
VALUES ('1-9 Lives', 1, 9)
, ('10-49 Lives', 10, 49)
, ('50-199 Lives', 50, 199)
, ('200-499 Lives', 200, 499)
, ('500-1,9999 Lives', 500, 1999)
, ('2,000+ Lives', 2000, 100000000)
, ('Total Lives', 0, 100000000)
, ('500+ Lives', 500, 1000000000)
, ('Unknown Lives', 0, 0)
) AS Base_Table (Lives_Category, MinCnt, MaxCnt)
) AS t1 ON t3.clean_ft_ee_cnt BETWEEN t1.MinCnt AND t1.MaxCnt
WHERE t3.mvp_rpt_id > 1399
AND t3.clean_do_not_use_ind IS NULL
AND t3.clean_client_indv_flag = 'Group'
GROUP BY t1.Lives_Category
) AS x
FULL JOIN (
SELECT DISTINCT
a1.Lives_Category AS [Lives Category]
, COUNT(t2.clean_ft_ee_cnt) AS [Number of Clients]
, SUM(t2.clean_ft_ee_cnt) AS [Sum of Lives]
FROM mvpGA.mvp_client_data_t AS t2
JOIN (
SELECT *
FROM (
VALUES ('1-9 Lives', 1, 9)
, ('10-49 Lives', 10, 49)
, ('50-199 Lives', 50, 199)
, ('200-499 Lives', 200, 499)
, ('500-1,9999 Lives', 500, 1999)
, ('2,000+ Lives', 2000, 100000000)
, ('Total Lives', 0, 100000000)
, ('500+ Lives', 500, 1000000000)
, ('Unknown Lives', 0, 0)
) AS Base_Table (Lives_Category, MinCnt, MaxCnt)
) AS a1 ON t2.clean_ft_ee_cnt BETWEEN a1.MinCnt AND a1.MaxCnt
WHERE t2.mvp_rpt_id = 1400
AND t2.clean_mvp_rpt_breakout = 'Fargo'
AND t2.clean_do_not_use_ind IS NULL
AND t2.clean_client_indv_flag = 'Group'
GROUP BY a1.Lives_Category
) AS y ON x.[Lives Category] = y.[Lives Category];