![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
SELECT ResidentID, LastName & ', ' & FirstName & ' ' & MiddleName AS ResidentName, Status FROM tblDemographics WHERE (Status = 'Active') UNION *ALL SELECT 0, 'N/A', 'Active ' FROM tblDemographics ORDER BY LastName & ', ' & FirstName & ' ' & MiddleName; |
#3
| |||
| |||
|
|
I want to query data from a table and add an extra row to the results. This is what I have so far: SELECT ResidentID, LastName & ', ' & FirstName & ' ' & MiddleName AS ResidentName, Status FROM tblDemographics WHERE (Status = 'Active') UNION ALL SELECT 0, 'N/A', 'Active ' ORDER BY LastName & ', ' & FirstName & ' ' & MiddleName; I want a list of names returned and the extra row "0, N/A, Active". (Just realized that I don't really need Status in the query) When I try to execute this I get the error message "Query input must contain at least one table or query". How do I correct this? By specifying one table or query name in a FROM clause. In this case I would |
#4
| |||
| |||
|
|
emanning wrote: I want to query data from a table and add an extra row to the results. *This is what I have so far: SELECT ResidentID, LastName & ', ' & FirstName & ' ' & MiddleName AS ResidentName, Status FROM tblDemographics WHERE (Status = 'Active') UNION *ALL SELECT 0, 'N/A', 'Active ' ORDER BY LastName & ', ' & FirstName & ' ' & MiddleName; I want a list of names returned and the extra row "0, N/A, Active". (Just realized that I don't really need Status in the query) When I try to execute this I get the error message "Query input must contain at least one table or query". How do I correct this? By specifying one table or query name in a FROM clause. In this case I would simply do: ... UNION *ALL SELECT TOP 1 0, 'N/A', 'Active ' FROM tblDemographics ... There's nothing that says you actually need to return data from the table you are querying. As you can see, you can return calculated fields and nothing else. David Fenton pointed out to me a while ago that the FROM clause is no longer required in Jet and he is correct, but there are a couple situations where one is required and you have found one of them: union queries. |
#5
| |||
| |||
|
|
Thanks for your replies. I took out the redundant Status column and tweaked it a little. It works just fine: SELECT ResidentID, LastName & ', ' & FirstName & ' ' & MiddleName AS ResidentName FROM tblDemographics WHERE (Status = 'Active') UNION SELECT 0, 'N/A' FROM tblDemographics ORDER BY ResidentName |
#6
| |||
| |||
|
|
SELECT ResidentID, LastName & ', ' & FirstName & ' ' & MiddleName AS ResidentName FROM tblDemographics WHERE (Status = 'Active') UNION SELECT 0, 'N/A' FROM tblDemographics ORDER BY ResidentName |
#7
| |||
| |||
|
|
emanning wrote: SELECT ResidentID, LastName & ', ' & FirstName & ' ' & MiddleName AS ResidentName FROM tblDemographics WHERE (Status = 'Active') UNION SELECT 0, 'N/A' FROM tblDemographics ORDER BY ResidentName Oh wait, I see now - you left out the "ALL" keyword so the duplicates returned by the second query were eliminated. From a performance standpoint, if tblDemographics contains a lot of records, I would prefer to use the " UNION ALL SELECT Top 1 ... " version since you aren't forcing the query engine to eliminate duplicate results. |
#8
| |||
| |||
|
|
On Apr 28, 2:46*pm, "Bob Barrows" <reb01... (AT) NOyahooSPAM (DOT) com> wrote: emanning wrote: SELECT ResidentID, LastName & ', ' & FirstName & ' ' & MiddleName AS ResidentName FROM tblDemographics WHERE (Status = 'Active') UNION SELECT 0, 'N/A' FROM tblDemographics ORDER BY ResidentName Oh wait, I see now - you left out the "ALL" keyword so the duplicates returned by the second query were eliminated. From a performance standpoint, if tblDemographics contains a lot of records, I would prefer to use the " UNION ALL SELECT Top 1 ... " version since you aren't forcing the query engine to eliminate duplicate results. Yep, I had to take out the "ALL" otherwise I'd have umpteen duplicates of N/A. Thanks for the idea of "Top 1". I can see where that would be a better way. |
#9
| |||
| |||
|
|
emanning wrote: On Apr 28, 2:46 pm, "Bob Barrows" <reb01... (AT) NOyahooSPAM (DOT) com> wrote: emanning wrote: SELECT ResidentID, LastName & ', ' & FirstName & ' ' & MiddleName AS ResidentName FROM tblDemographics WHERE (Status = 'Active') UNION SELECT 0, 'N/A' FROM tblDemographics ORDER BY ResidentName Oh wait, I see now - you left out the "ALL" keyword so the duplicates returned by the second query were eliminated. From a performance standpoint, if tblDemographics contains a lot of records, I would prefer to use the " UNION ALL SELECT Top 1 ... " version since you aren't forcing the query engine to eliminate duplicate results. Yep, I had to take out the "ALL" otherwise I'd have umpteen duplicates of N/A. Thanks for the idea of "Top 1". I can see where that would be a better way. I prefer to use a one row utility table . . . UNION ALL SELECT 0, 'N/A' FROM tblOneRow and avoid the sort and duplicate removal required by TOP 1 even if it is optimized by an index. |
#10
| |||
| |||
|
|
I want to query data from a table and add an extra row to the results. |
![]() |
| Thread Tools | |
| Display Modes | |
| |