Windows SQL 中的 Max() 函数获取不正确的最大值
Windows Max() function in SQL fetches incorrect max values
我正在使用 max() windows 函数来获取每年筹集的最大资金,考虑到活动长度分为三类。我需要查看哪个广告系列长度,即“<=30 天”、“36 - 60 天”和“>60 天”能够逐年筹集更多资金。
我写了
select
year,
campaign_length_category,
money_raised,
max(money_raised) over (partition by year ) as max_money_raised
FROM
(
SELECT
year,
campaign_length_category,
concat('$', format(sum(pledged),2,'en_US')) as money_raised
FROM
(
select
extract(year from launched) Year,
case
when campaign_length >= 1 and campaign_length <= 30 then '<=30 days'
when campaign_length > 35 and campaign_length <= 60 then '36 - 60 days'
else '>60 days'
end as campaign_length_category,
pledged
from
(
select
launched,
outcome,
datediff(cast(deadline as date),cast(launched as date)) campaign_length,
pledged
from
campaign
) t1
)t2
group by 1, 2
)t3
order by 1
但是,它没有在输出中显示每年的最大值。对于 2011 年和 2012 年,它获取了错误的值。
year
campaign_length_category
money_raised
max_money_raised
2009
<=30 days
,852.12
,088.48
2009
>60 days
,088.48
,088.48
2009
36 - 60 days
,978.64
,088.48
2010
<=30 days
1,063.08
7,862.95
2010
>60 days
7,862.95
7,862.95
2010
36 - 60 days
2,416.84
7,862.95
2011
<=30 days
,634,463.10
2,348.24
2011
36 - 60 days
,710,892.85
2,348.24
2011
>60 days
2,348.24
2,348.24
2012
36 - 60 days
,492,257.73
,410,974.02
2012
<=30 days
,410,974.02
,410,974.02
2012
>60 days
,434,506.99
,410,974.02
我不确定为什么 max() window 函数会在给定的某些年中获取不正确的最大值。
如有错误请赐教
您需要将格式移动到仅第一个外部 select。通过在内部 select 中进行格式化,您使 max(money_raised)
对格式化字符串进行字符串最大值处理,并且“$742,348.24”确实大于“$1,710,892.85”,因为第一个字符相等,并且对于第二个字符,'7'大于'1'。
所以:
select
year,
campaign_length_category,
concat('$', format(pledged,2,'en_US')) as money_raised
concat('$', format(max(pledged) over (partition by year),2,'en_US')) as max_money_raised
FROM
(
SELECT
year,
campaign_length_category,
sum(pledged) as pledged
我正在使用 max() windows 函数来获取每年筹集的最大资金,考虑到活动长度分为三类。我需要查看哪个广告系列长度,即“<=30 天”、“36 - 60 天”和“>60 天”能够逐年筹集更多资金。
我写了
select
year,
campaign_length_category,
money_raised,
max(money_raised) over (partition by year ) as max_money_raised
FROM
(
SELECT
year,
campaign_length_category,
concat('$', format(sum(pledged),2,'en_US')) as money_raised
FROM
(
select
extract(year from launched) Year,
case
when campaign_length >= 1 and campaign_length <= 30 then '<=30 days'
when campaign_length > 35 and campaign_length <= 60 then '36 - 60 days'
else '>60 days'
end as campaign_length_category,
pledged
from
(
select
launched,
outcome,
datediff(cast(deadline as date),cast(launched as date)) campaign_length,
pledged
from
campaign
) t1
)t2
group by 1, 2
)t3
order by 1
但是,它没有在输出中显示每年的最大值。对于 2011 年和 2012 年,它获取了错误的值。
year | campaign_length_category | money_raised | max_money_raised |
---|---|---|---|
2009 | <=30 days | ,852.12 | ,088.48 |
2009 | >60 days | ,088.48 | ,088.48 |
2009 | 36 - 60 days | ,978.64 | ,088.48 |
2010 | <=30 days | 1,063.08 | 7,862.95 |
2010 | >60 days | 7,862.95 | 7,862.95 |
2010 | 36 - 60 days | 2,416.84 | 7,862.95 |
2011 | <=30 days | ,634,463.10 | 2,348.24 |
2011 | 36 - 60 days | ,710,892.85 | 2,348.24 |
2011 | >60 days | 2,348.24 | 2,348.24 |
2012 | 36 - 60 days | ,492,257.73 | ,410,974.02 |
2012 | <=30 days | ,410,974.02 | ,410,974.02 |
2012 | >60 days | ,434,506.99 | ,410,974.02 |
我不确定为什么 max() window 函数会在给定的某些年中获取不正确的最大值。 如有错误请赐教
您需要将格式移动到仅第一个外部 select。通过在内部 select 中进行格式化,您使 max(money_raised)
对格式化字符串进行字符串最大值处理,并且“$742,348.24”确实大于“$1,710,892.85”,因为第一个字符相等,并且对于第二个字符,'7'大于'1'。
所以:
select
year,
campaign_length_category,
concat('$', format(pledged,2,'en_US')) as money_raised
concat('$', format(max(pledged) over (partition by year),2,'en_US')) as max_money_raised
FROM
(
SELECT
year,
campaign_length_category,
sum(pledged) as pledged