Share via

sorting union query

Anonymous
2013-06-08T05:45:49+00:00

hi

I have a combo box from a union query of four tables the sql currently looks like this

how can I auto arrange the data alphabetically/number please

SELECT Labour.Item, Labour.[Purchase Price]

FROM Labour;

UNION ALL SELECT Materials.Item, Materials.[Current Value]

FROM Materials;

UNION ALL SELECT Prelims.Item, Prelims.[Current Value]

FROM Prelims;

UNION ALL SELECT AllInRates.Item, AllInRates.[Total]

FROM AllInRates;

thankyou

Microsoft 365 and Office | Access | For home | 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

4 answers

Sort by: Most helpful
  1. Anonymous
    2013-06-09T14:00:39+00:00

    Union queries get their field names from the first subselect. In other words, the fields in your query will be [Item] and [Purchase Price]. You should be able to use either

    SELECT Labour.Item, Labour.[Purchase Price]

    FROM Labour

    UNION ALL SELECT Materials.Item, Materials.[Current Value]

    FROM Materials

    UNION ALL SELECT Prelims.Item, Prelims.[Current Value]

    FROM Prelims

    UNION ALL SELECT AllInRates.Item, AllInRates.[Total]

    FROM AllInRates

    ORDER BY Item

    or

    SELECT Labour.Item, Labour.[Purchase Price]

    FROM Labour

    UNION ALL SELECT Materials.Item, Materials.[Current Value]

    FROM Materials

    UNION ALL SELECT Prelims.Item, Prelims.[Current Value]

    FROM Prelims

    UNION ALL SELECT AllInRates.Item, AllInRates.[Total]

    FROM AllInRates

    ORDER BY 1

    Note, btw, that the original query you posted was invalid: you cannot have semi-colons inside the SQL.

    Was this answer helpful?

    3 people found this answer helpful.
    0 comments No comments
  2. Anonymous
    2013-06-08T15:28:58+00:00

    is there now way to do it by the combined tableds

    I do not understand the question.   

    What I posted will sort the complete record set on the field(s) in the ORDER BY statement.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2013-06-08T07:04:07+00:00

    Try this --

    SELECT Labour.Item, Labour.[Purchase Price]

    FROM Labour

    ORDER BY Labour.Item

    UNION ALL SELECT Materials.Item, Materials.[Current Value] 

    FROM Materials

    UNION ALL SELECT Prelims.Item, Prelims.[Current Value] 

    FROM Prelims

    UNION ALL SELECT AllInRates.Item, AllInRates.[Total] 

    FROM AllInRates;

    It will only sort by what is in the first part of the union.

    thanks Karl

    is there now way to do it by the combined tableds

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2013-06-08T06:44:19+00:00

    Try this --

    SELECT Labour.Item, Labour.[Purchase Price]

    FROM Labour

    ORDER BY Labour.Item

    UNION ALL SELECT Materials.Item, Materials.[Current Value] 

    FROM Materials

    UNION ALL SELECT Prelims.Item, Prelims.[Current Value] 

    FROM Prelims

    UNION ALL SELECT AllInRates.Item, AllInRates.[Total] 

    FROM AllInRates;

    It will only sort by what is in the first part of the union.

    Was this answer helpful?

    0 comments No comments