如何将 return 段的更大值组合成 unique id 组合?
How to return greater value of the period for unique id combination?
如果对于 section_id
和 store_id
组合的 period_id
的较大值,paid_amount
不为 NULL 或大于 0,则商店将获得checked
列的 Y 值。这是我根据上面列出的条件创建的查询:
select section_id, store_id, paid_amount, period_id, secttion_id + store_id as unique_id
from store_data
where paid_amount is not null and paid_amount > 0
order by store_id, paid_amount desc;
以上查询产生以下数据:
section_id store_id paid_amount period_id unique_id
3604 30545 10000.00 3 30545
3604 30545 5000.00 2 30545
5967 32105 8470.00 3 38072
5967 32105 8470.00 2 38072
1367 46144 23456.00 2 47511
1367 46144 23456.00 3 47511
1367 46144 23456.00 4 47511
1367 46144 23456.00 5 47511
1376 72181 19975.00 2 73557
如果有多个数据,我只需要获取一行数据。例如 unique_id
30545
我只想要数量较多的行。在那种情况下,它应该与 10000.00
的 paid_amount
排在一起。如果行只有一条记录,我只需要该行。在 Sybase
?
中是否有一种简单的方法可以实现此目的?
因为 sybase 不支持 window 函数,这里是一种方法:
select s1.* from store_data s1
join (
select unique_id , max(paid_amount) paid_amount
from store_data s2
where s2.paid_amount is not null and s2.paid_amount > 0
group by s2.unique_id
) s2
on s1.unique_id = s2.unique_id
and s1.paid_amount = s2.paid_amount
但是,如果您仍然有重复项,则可以缩小条件范围:
select s1.* from store_data s1
join (
select unique_id , max(paid_amount) paid_amount, max(period) period
from store_data s2
where s2.paid_amount is not null and s2.paid_amount > 0
group by s2.unique_id
) s2
on s1.unique_id = s2.unique_id
and s1.paid_amount = s2.paid_amount
and s1.period_id = s2.period_id
如果对于 section_id
和 store_id
组合的 period_id
的较大值,paid_amount
不为 NULL 或大于 0,则商店将获得checked
列的 Y 值。这是我根据上面列出的条件创建的查询:
select section_id, store_id, paid_amount, period_id, secttion_id + store_id as unique_id
from store_data
where paid_amount is not null and paid_amount > 0
order by store_id, paid_amount desc;
以上查询产生以下数据:
section_id store_id paid_amount period_id unique_id
3604 30545 10000.00 3 30545
3604 30545 5000.00 2 30545
5967 32105 8470.00 3 38072
5967 32105 8470.00 2 38072
1367 46144 23456.00 2 47511
1367 46144 23456.00 3 47511
1367 46144 23456.00 4 47511
1367 46144 23456.00 5 47511
1376 72181 19975.00 2 73557
如果有多个数据,我只需要获取一行数据。例如 unique_id
30545
我只想要数量较多的行。在那种情况下,它应该与 10000.00
的 paid_amount
排在一起。如果行只有一条记录,我只需要该行。在 Sybase
?
因为 sybase 不支持 window 函数,这里是一种方法:
select s1.* from store_data s1
join (
select unique_id , max(paid_amount) paid_amount
from store_data s2
where s2.paid_amount is not null and s2.paid_amount > 0
group by s2.unique_id
) s2
on s1.unique_id = s2.unique_id
and s1.paid_amount = s2.paid_amount
但是,如果您仍然有重复项,则可以缩小条件范围:
select s1.* from store_data s1
join (
select unique_id , max(paid_amount) paid_amount, max(period) period
from store_data s2
where s2.paid_amount is not null and s2.paid_amount > 0
group by s2.unique_id
) s2
on s1.unique_id = s2.unique_id
and s1.paid_amount = s2.paid_amount
and s1.period_id = s2.period_id