SQL quiries to select attribute base on sequence

dhen21dx

Board Regular
Joined
May 10, 2013
Messages
166
Hello,

Need help on ms access sql, i need to select records base on sequence but i only need to retain 1 line. Sequence are Orange, Lychee, Apple the Mango.


Data
Field 1Field 2
Item 1Apple
Item 1Orange
Item 1Mango
Item 1Lychee
Item 2Apple
Item 3Apple
Item 4Lychee
Item 4Apple
Item 5Lychee
Item 6Mango
Item 6Apple

Result:



Field 1Field 2
Item 1Orange
Item 2Apple
Item 3Apple
Item 4Lychee
Item 5Lychee
Item 6Apple
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
If you do not have a way to sort what you show as Field2 items then I'd say that your only option is a code procedure. Without the sort field there is no way to use a domain function such as DMax or Max. In other words, if you had a table like
ItemIDItemOrderItemName
15Orange
210Apple
315Lychee
420Mango

you could find the max ItemOrder value for a list of items. However, the example I show is based on what you wrote (Apple then Mango) whereas your sample data is the opposite. The reason that the sort order is in multiples of 5 is so that you can slip in other items between 15 and 20 if need be, and they don't have to be in ItemID order or even in ItemOrder order in your records. You'd structure your query to sort by ItemOrder ascending if you need to. So for a set of records that looks like your sample data, you'd query to get the Max ItemOrder value for each ItemName.
 
Upvote 0
Hi Micron,


Thanks, i think i better insert a field to identify the sorting. So it means if i used max, i will likely to get the 5 as orange for item 1? and so on? will this carried through queries or need a sql statement? Thanks for help, appreciate it a lot.
 
Upvote 0
Not sure I understand the meaning of your statement about "sequence". My guess was that you wanted the latter value to be used instead of the "earlier" one in your sequence. Max would give you that, but if I have the goal wrong, then maybe you want the Min. Then again, which you use also depends on the order you have assigned to them. You ought to be able to write a query to get the results, which is really a sql statement - it's just stored as a query so not sure what that question means. A sql statement executed in code should produce the same result as a query. Unless you have some requirement to use coded sql, I don't see why you wouldn't just have a query as it's easier to maintain.
 
Upvote 0
Using Micron's table for setting up a sort order, this query is what I came up with:

Code:
SELECT 
    t3.Field1, t4.ItemName 
FROM    
    (
    SELECT t1.Field1, Min(t2.ItemOrder) AS MinOfItemOrder
    FROM Table1 AS t1 INNER JOIN Table2 AS t2 ON t1.Field2 = t2.ItemName
    GROUP BY t1.Field1
    ) as t3
INNER JOIN 
    Table2 t4
    ON t3.MinOfItemOrder = t4.ItemOrder 
ORDER BY
    t3.Field1
;
 
Upvote 0
Hello,

Yeah, what i mean is that i will create new table to define the sequence of the Field 2 and indicated 5,10,15, 20 and so.. Thank you very much.
 
Upvote 0

Forum statistics

Threads
1,215,011
Messages
6,122,680
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top