合并两个都根据结果对数据进行分类的查询
Combine two queries that both categorize the data based upon the results
我正在尝试合并两个基本上提供相同功能的不同查询。每个查询的结果都根据一列的结果进行分类。当我尝试合并它们时,我收到一条错误消息,指出存在算术溢出。
下面是第一行代码。
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
第二行代码在这里,结构相同,不同之处在于 clause.The 两列的结果 "Number of Clients" 和 "Sum of Live" 需要是它们自己的列.
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 列。注意:查询在技术上是基于相同的 table 计算的,只是 where 子句不同。
我对您的数据了解不多,无法判断您是否应该使用内部联接、左联接、右联接或全联接。所以你应该修改连接以适应你的情况,但这应该让你走上正轨。基本上,我将每个 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];
我正在尝试合并两个基本上提供相同功能的不同查询。每个查询的结果都根据一列的结果进行分类。当我尝试合并它们时,我收到一条错误消息,指出存在算术溢出。
下面是第一行代码。
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
第二行代码在这里,结构相同,不同之处在于 clause.The 两列的结果 "Number of Clients" 和 "Sum of Live" 需要是它们自己的列.
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 列。注意:查询在技术上是基于相同的 table 计算的,只是 where 子句不同。
我对您的数据了解不多,无法判断您是否应该使用内部联接、左联接、右联接或全联接。所以你应该修改连接以适应你的情况,但这应该让你走上正轨。基本上,我将每个 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];