Share via

Ordering by specific field values with MSAccess

Anonymous
2023-08-24T15:18:40+00:00

Dear All,

I would like to order one field by using a list of values.

If Use the below query

SELECT [History Date], [FieldName], [Message], [Performed by] FROM [HISTORY_TABLE] WHERE (((ID_Request)=571)) ORDER BY [ID_History] 

I get this result:

but I would like to have this:

So, I'm try to find a way to apply two filter:

  1. ORDER BY [ID_History]
  2. ORDER BY [FieldName] but sorted in this way ('Date Created', 'Part Number', User Modified', 'Solution', 'Status')

Thanks

Microsoft 365 and Office | Access | Other | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2023-08-25T02:50:34+00:00

SELECT * FROM product WHERE pro_id IN (5,3,6,1)

ORDER BY INSTR('5,3,6,1',pro_id)

ORDER BY instr("Date Created,Part Numb",fieldName)

use instr to order by specific words

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2023-08-24T15:54:56+00:00

    With your table as it stands you would need to create a referenced table with columns FieldName and FieldOrder.  The FieldName column would be the primary key and the FieldOrder column would contain integer values indicating the ordinal position of each FieldName value.  You could then join the two tables on FieldName and sort the query with:

        ORDER BY  ID_History, FieldOrder

    This would of course require the ID_History column to have the same value in each subset of rows within which you wish to return the rows by FieldOrder value.

    However, the need to do this results from the incorrect table design.  The FieldName column contains the names of attribute types, whereas a fundamental principle of the database relational model is that each column position in a row in a table should contain an attribute value.  The table should therefore be radically amended so that each of the distinct values in the FieldName column is a column name, and each of the corresponding values in the Message column is a value in the relevant column.  You can then return the columns in whatever order you wish.

    Was this answer helpful?

    0 comments No comments