473,395 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Union ALL Query for 1 Table

283 100+
Hello all,

I am wondering if its possible to do a Union All Query for 1 table instead of mulitiple tables.

I have 7 columns that im trying to Union. I have it working to an extent right now but its way to slow. I'm wondering what I can do to speed it up. (Im sorting threw about 300,000 records)(Time it takes is a min or more) I also noticed that its doubling the record count when i do it this way which could be why its taking so long.

Also Im using another query to pull the information from this one to fill in a form with the results.

Thanks in advance!

Here is what i have so far,

Expand|Select|Wrap|Line Numbers
  1. SELECT  [Date],[Number],[Name1],[Name2],[Name3],[Name4],[Count]
  2. FROM [Table]
  3. ORDER BY [Date]
  4. UNION ALL SELECT  [Date],[Number],[Name1],[Name2],[Name3],[Name4],[Count]
  5. FROM [Table]
  6. ORDER BY [Date];
  7.  
Dec 2 '10 #1
5 2445
gershwyn
122 100+
What exactly are you trying to achieve by this?

Yes, it's possible to do, but as you mentioned it is doubling all the records. That's what a Union query is for - it combines the records from various sources into one.
Dec 2 '10 #2
Stewart Ross
2,545 Expert Mod 2GB
I echo what gershwyn has said. Unioning the same table? Why would you consider doing that, and for what purpose?

Strictly, SQL should not return duplicate rows. It is a flaw of the implementation of relational databases that a plain SELECT statement can return identical rows (as generated by your UNION ALL on the same table). If you used SELECT DISTINCT in your query you'd find that the duplicates vanish, and the UNION ALL would simply be a very inefficient way to run a SELECT query.

As gershwyn said, UNION queries are intended to create single-table output from multi-source inputs - effectively ORing the results of independent tables or queries together. UNION was never meant for use with the same table.

-Stewart
Dec 2 '10 #3
slenish
283 100+
Thanks for the replys :D

Well the reason im trying to use a Union query is because it should be faster than a normal Select Query for searching through records. I built a Union query for another part but it was putting two tables together and was really fast so i was trying to use it again for one table but found there were issues.

I will try the Select Distinct and see how that goes any other ideas I could try??

thanks for the help :D
Dec 3 '10 #4
Stewart Ross
2,545 Expert Mod 2GB
Hi. I'm not clear by what you mean by 'searching'. If you are using a WHERE clause to select matching fields (for example, to find partial matches for a person's name) then neither UNION nor SELECT DISTINCT are pertinent; they are for very different purposes and don't have anything to do with the speed at which a match can be found.

UNION is, as already mentioned, intended for separate (non-joined) tables being ORd together. If applied to the same table it could decrease performance drastically, as instead of the query engine searching through N rows it would potentially have to search through N x N = N^2 rows - although its internal optimisation may help prevent that worst-case from arising.

Again, if you could advise what you are really trying to do we'd be happy to help in coming up with an efficient way to do it.

-Stewart
Dec 4 '10 #5
NeoPa
32,556 Expert Mod 16PB
As a general rule using UNION is likely to slow down data retrieval drastically.

It can be used on a single table, when you want to separate fields from a table into distinct records (EG as below).
Expand|Select|Wrap|Line Numbers
  1. SELECT   [Date]
  2.        , [Number]
  3.        , [Name1] AS [Name]
  4. FROM     [Table]
  5. UNION ALL
  6. SELECT   [Date]
  7.        , [Number]
  8.        , [Name2]
  9. FROM     [Table]
  10. UNION ALL
  11. SELECT   [Date]
  12.        , [Number]
  13.        , [Name3]
  14. FROM     [Table]
  15. ORDER BY [Date]
  16.        , [Number]
  17.        , [Name]
Notice - There can only be one ORDER BY clause. This determines the order of the resultant recordset.

Again, without a better understanding of what you're asking for, it's hard to help further.
Dec 5 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Paradigm | last post by:
I am using Access 2K as a front end to a MYSQL database. I am trying to run a Union query on the MYSQL database. The query is (much simplified) SELECT as ID from faxdata UNION SELECT as ID ...
3
by: Dalan | last post by:
From reading Access 97 help text, it seems that to do what I need to do will require a Union Query. As this would be my first, I think I might require a little guidance. I have two tables with...
2
by: Enterprise | last post by:
Hi, Here is my code for the Union query "Table union Table union Table ;" The tables are actually queries themselves. I made a form for the query, but when I try to update the...
2
by: mattytee123 | last post by:
I have about 20 tables, of which I would like to do a union query and count of how many of each different code there is? The simplified verson of the table is structured like this. Code ...
2
by: Thomas | last post by:
Hi All. I think that if I explain the database layout first it may be easier to ask my question. Table Name Field Name Test TestCode Description SpecimenRequirements FeeSchedule TestCode...
8
by: kepston | last post by:
I have a situation where I need to record scrap quantities for parts that we sell and parts that are supplied, in a single table. I have a UNION query that combines the part numbers successfully,...
27
by: MLH | last post by:
How can I turn the following into a make-table query? SELECT & " " & AS Recipient FROM tblVehicleJobs INNER JOIN tblAddnlOwnrs ON tblVehicleJobs.VehicleJobID = tblAddnlOwnrs.VehicleJobID WHERE...
2
by: billelev | last post by:
Hi There, I am performing a UNION query on a Table containing % formatted values with a Query, also containing % formatted values. The resulting union query displays the table % values as...
1
by: cashotte | last post by:
Hi, Is there a way to have a union query (2 tables) in an other query then join a third table having the ability to edit records in the third table? Thanks, Stephane
5
by: tasawer | last post by:
I need some assistance with SQL query. I will be grateful if you can provide help please. I have a database that records details of a road traffic accident and name of driver. sub-form records...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.