Reputation: 121
I would like to do this in a View.
I have a crystal reprt which uses the table ordertrans. This table contains data about the order. This is a MFG. company and so orders are often custom and each piece of the puzzle will be listed on this table. packing codes PDB. THE CR before was joining on the order number and record selecting on 001. I added record select also on PDB. I want a view that will only select the PDB one time. If they have more than 1, it is very likely some sort of error or rare condition and not applicable to the report. The issue is that the second PDB is causing Format errors not in the report but in the EXCEL EXPORT. I should like a view to use instead of the current one. Although the current one does not select only the 001 or packing codes, I think in theory we can only select the 001 and pdb ones theres actually 3 such code i mention one for simplification.
order# TRNCDE
123 001
123 999
123 PDB
123 AAA
123 BBB
123 PDB
123 CCC
Upvotes: 0
Views: 62
Reputation: 4532
RRN() will cause an index build, causing a performance hit. (according to Birgitta Hauser)
If there's no unique line number, time stamp or sequence number to help you distinguish the PDB rows from each other, consider generating a ROW_NUMBER():
with min as
(select order#, trncde,
ROW_NUMBER() OVER(PARTITION BY order# ORDER BY another_col) as pick
from ordertrans
group by order#, trncde
)
select *
from ordertrans o
join min m on o.order#=m.order#
and o.trncde=m.trncde
and m.pick = 1
order by order#, trncde;
Upvotes: 0
Reputation: 7648
Assuming there's a line number or sequence number in ORDERTRANS, choose the lowest one for each unique order/trncde combination:
with min as
(select order#, trncde, min(line#) as line#
from ordertrans
group by order#, trncde)
select *
from ordertrans o
join min m on o.order#=m.order# and
o.trncde=m.trncde and
o.line#=m.line#
order by order#, trncde;
If there's no unique line number, time stamp or sequence number to help you distinguish the PDB rows from each other, consider trying RRN instead:
with min as
(select order#, trncde, rrn(ordertrans) as line#
from ordertrans
group by order#, trncde)
select *
from ordertrans o
join min m on o.order#=m.order# and
o.trncde=m.trncde and
rrn(o)=m.line#
order by order#, trncde;
Upvotes: 3