KVA wrote:vc wrote:The code is much more efficient (only one join as opposed to four in the previous versions) and handles duplicates nicely, but I am not sure how to translate "row_number() "( SQL'99 function) to the SQL Server dialect.
Thanks a lot! Я дам посмотреть этот SQL нашему DBA. Может он что-нибудь посоветует на счет диалектов. Правда я не думаю что мне это решение поможет. IOI у order-a обычно мало < 5, но мы не ограничивает юзеров сколько IOI они могут ввести.
The translation I came up with turned out to be quite ugly:
Code: Select all
select a.ord_id,
b.ord_id,
case when a.data=b.data then 'eq' else 'not eq' end
from
(select ord_id,
cast(max(case when rn = 1 then ioi_amt else null end)as varchar) +'/'+
cast(max(case when rn = 2 then ioi_amt else null end)as varchar) +'/'+
cast(max(case when rn = 3 then ioi_amt else null end)as varchar) +'/'+
cast(max(case when rn = 4 then ioi_amt else null end)as varchar) +'/'+
cast(max(case when rn = 5 then ioi_amt else null end)as varchar) +'/'+
cast(max(case when rn = 1 then price else null end)as varchar) +'/'+
cast(max(case when rn = 2 then price else null end)as varchar) +'/'+
cast(max(case when rn = 3 then price else null end)as varchar) +'/'+
cast(max(case when rn = 4 then price else null end)as varchar) +'/'+
cast(max(case when rn = 5 then price else null end)as varchar) data
from
(select t1.ord_id, t1.ioi_amt, t1.price, x.cnt rn from t1 join
(select a.ord_id, cast(a.ioi_amt as varchar)+'/'+ cast(a.price as varchar) ap, count(*) cnt
from t1 a join t1 b on a.ord_id=b.ord_id and cast(a.ioi_amt as varchar)+'/'+cast(a.price as varchar)>= cast(b.ioi_amt as varchar)+'/'+cast(b.price as varchar)
group by a.ord_id, cast(a.ioi_amt as varchar)+'/'+cast(a.price as varchar)) x on t1.ord_id=x.ord_id and cast(t1.ioi_amt as varchar)+'/'+cast(t1.price as varchar)=x.ap) x
group by ord_id) a
join
(select ord_id,
cast(max(case when rn = 1 then ioi_amt else null end)as varchar) +'/'+
cast(max(case when rn = 2 then ioi_amt else null end)as varchar) +'/'+
cast(max(case when rn = 3 then ioi_amt else null end)as varchar) +'/'+
cast(max(case when rn = 4 then ioi_amt else null end)as varchar) +'/'+
cast(max(case when rn = 5 then ioi_amt else null end)as varchar) +'/'+
cast(max(case when rn = 1 then price else null end)as varchar) +'/'+
cast(max(case when rn = 2 then price else null end)as varchar) +'/'+
cast(max(case when rn = 3 then price else null end)as varchar) +'/'+
cast(max(case when rn = 4 then price else null end)as varchar) +'/'+
cast(max(case when rn = 5 then price else null end)as varchar) data
from
(select t1.ord_id, t1.ioi_amt, t1.price, x.cnt rn from t1 join
(select a.ord_id, cast(a.ioi_amt as varchar)+'/'+ cast(a.price as varchar) ap, count(*) cnt
from t1 a join t1 b on a.ord_id=b.ord_id and cast(a.ioi_amt as varchar)+'/'+cast(a.price as varchar)>= cast(b.ioi_amt as varchar)+'/'+cast(b.price as varchar)
group by a.ord_id, cast(a.ioi_amt as varchar)+'/'+cast(a.price as varchar)) x on t1.ord_id=x.ord_id and cast(t1.ioi_amt as varchar)+'/'+cast(t1.price as varchar)=x.ap) x
group by ord_id) b on a.ord_id < b.ord_id
The SQL is not very performant because my row_number() 'implementation' added 4 extra joins although it appears to handle duplicates correctly.
For those who might bother to run the SQL above, SET CONCAT_NULL_YIELDS_NULL should be OFF to allow null concatenation (null yields an empty string). The maximum number of items per order is 5.
Rgds.