I have an order table holding columns :
ID
date
order_number
cost
In the table there are the following rows :
1,TODAY,123,100
2,YESTERDAY, 123,150
3,TODAY, 144,20
4,TODAY, 144,20
5,TODAY, 144,200
(Notice that the 3rd column is the order number)
In the result set returned there should return :
1,TODAY,123,100
3,TODAY, 144,20
4,TODAY, 144,20
I am a poor SQL know-how (A web designer, go figure).
Fixed and found the answer
After searching and researching i found what i needed. I put all the records in a temporary table and run a cursor holding the order_number and the count of rows for the order_number like this :
declare cur cursor for
SELECT order_number,((COUNT(*) + 1) / 2) as counter from @tmpTbl group by order_number
Now i run over the cursor and insert into another table the top(counter) from the @tmpTbl into another table with the same structure (lets call it @slimTbl). So, we have this piece of code :
>fetch next from cur into @order_number,@counter
>
>while @@FETCH_STATUS = 0
>
> begin
>
> INSERT INTO @slimTbl (id,date,order_number,cost)
>
> (SELECT TOP(@counter) id,date,order_number,cost FROM @tmpTbl
> WHERE order_number = @order_number )
>
> fetch next from cur into @order_number,@counter
>
> end
>
>close cur
>
>deallocate cur
I hope this is clear now. I thank you all for helping me... Feel free to contact me to see the full solution I created.. a little long but works like magic :)