473,669 Members | 2,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Union ALL Query for 1 Table

283 Contributor
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 2463
gershwyn
122 New Member
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 Recognized Expert Moderator Specialist
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 Contributor
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 Recognized Expert Moderator Specialist
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,569 Recognized Expert Moderator MVP
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
3556
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 from letdata UNION SELECT as ID FROM MEMODATA; I get an ODBC error. The same query runs when the backend files are MDB files and it runs with MYSQL if I only combine 2 tables.
3
10540
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 10 fields that have like data (for instance both have an item description field, an item price field, a general notes field, etc.) but with different field names. The tables have approximately 20/40 other fields that are dissimilar and not needed...
2
2487
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 form, I get a message that says "This recordset is not updatable." Can I somehow make it updatable? Thanks P.S. Using access 2000.
2
4340
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 Count 1234 1 2468 1 1234 1 2468 1
2
805
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 CPTCode
8
10781
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, allowing the user to select from a combo box without knowing whether the part is supplied or manufactured. I need to provide a DEFAULT value for cost (which, if changed later would not be wrong). To do this, I would like to pull the price from the...
27
13778
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 tblVehicleJobs.VehicleJobID=GetCurrentVehicleJobID(); UNION SELECT AS Recipient FROM tblVehicleJobs INNER JOIN tblLienHolders ON tblVehicleJobs.VehicleJobID = tblLienHolders.VehicleJobID WHERE
2
1442
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 decimal but the query % values as percents. e.g. Table, value1 = 3.5% Query, value1 = 4.5%
1
2601
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
2627
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 names of passengers. The two tables are related with a one-to-many relationship. I need to view all the names (Drivers and Passengers) as datasheet view and be able to tick a box to show investigation for this particular person is complete. I...
0
8809
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8588
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8658
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6210
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4386
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2797
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.