473,796 Members | 2,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Union Query

G'day ppl.

Can anyone assist me with the correct structure of the following in a Union
Query.

tblBookings.Fin Year
tblBookings.Dep PrefPay
tblBookings.Int PrefPay
tblBookingsFinP refPay

I tried structuring it as:
SELECT tblBookings.Fin Year, tblBookings.Dep PrefPay
FROM tblBookings
UNION SELECT tblBookings.Int PrefPay, tblBookings.Int FinPay; AS
PaymentPreferen ce;

The object is to combine all 3 PrefPay fields to determine which payment
method is the overall preference of customers in a given financial year.

As you may have guessed my attempt didn't work, hence the reason I am here.

Many thx for any assistance

TIA

Mark.
Jan 19 '06 #1
3 3703
On Thu, 19 Jan 2006 21:27:38 +1100, "NoodNutt" <no******@iprim us.com.au> wrote:
G'day ppl.

Can anyone assist me with the correct structure of the following in a Union
Query.

tblBookings.Fi nYear
tblBookings.De pPrefPay
tblBookings.In tPrefPay
tblBookingsFin PrefPay

I tried structuring it as:
SELECT tblBookings.Fin Year, tblBookings.Dep PrefPay
FROM tblBookings
UNION SELECT tblBookings.Int PrefPay, tblBookings.Int FinPay; AS
PaymentPrefere nce;

The object is to combine all 3 PrefPay fields to determine which payment
method is the overall preference of customers in a given financial year.

As you may have guessed my attempt didn't work, hence the reason I am here.

Many thx for any assistance

TIA

Mark.

You haven't given much detail about the table structure, but something like this should work -

SELECT tblBookings.Fin Year, tblBookings.Dep PrefPay As PrefPay
FROM tblBookings
WHERE Not (tblBookings.De pPrefPay) Is Null And FinYear = 1
UNION ALL SELECT tblBookings.Fin Year, tblBookings.Int PrefPay As PrefPay
FROM tblBookings
WHERE Not (tblBookings.In tPrefPay) Is Null And FinYear = 1
UNION ALL SELECT tblBookings.Fin Year, tblBookings.Fin PrefPay As PrefPay
FROM tblBookings
WHERE Not (tblBookings.Fi nPrefPay) Is Null And FinYear = 1;

As the 3 PrefPay fields are to be combined they should all be aliased with the same name.

UNION ALL will return all matching records.
UNION will remove duplicates and return 1 row for each distinct data value.

Wayne Gillespie
Gosford NSW Australia
Jan 19 '06 #2
G'day Wayne

My apologies, you're right, I haven't provided enough detail.

the finYear field is text "2005/2006" etc.....

All 3 PrefPay fields are numeric (long)

and is recorded as this:
1 = Cash
2 = Cheq
3 = Card

The end result I would like to count each matching criteria then sum the
total count returned and hope the ensuing report would reflect the
following:

FinYear Cash Cheq Card
2004/2005 500 50 1,000
2005/2006 300 10 2,500

I am hoping a union query can achieve the same result as opposed to 3
individual crosstabs.

Thx again Wayne.

Mark.
Melbourne, VIC. Australia.
"NoodNutt" <no******@iprim us.com.au> wrote in message
news:43******** **@news.iprimus .com.au...
G'day ppl.

Can anyone assist me with the correct structure of the following in a
Union Query.

tblBookings.Fin Year
tblBookings.Dep PrefPay
tblBookings.Int PrefPay
tblBookingsFinP refPay

I tried structuring it as:
SELECT tblBookings.Fin Year, tblBookings.Dep PrefPay
FROM tblBookings
UNION SELECT tblBookings.Int PrefPay, tblBookings.Int FinPay; AS
PaymentPreferen ce;

The object is to combine all 3 PrefPay fields to determine which payment
method is the overall preference of customers in a given financial year.

As you may have guessed my attempt didn't work, hence the reason I am
here.

Many thx for any assistance

TIA

Mark.

Jan 20 '06 #3
On Fri, 20 Jan 2006 23:27:49 +1100, "NoodNutt" <no******@iprim us.com.au> wrote:
G'day Wayne

My apologies, you're right, I haven't provided enough detail.

the finYear field is text "2005/2006" etc.....

All 3 PrefPay fields are numeric (long)

and is recorded as this:
1 = Cash
2 = Cheq
3 = Card

The end result I would like to count each matching criteria then sum the
total count returned and hope the ensuing report would reflect the
following:

FinYear Cash Cheq Card
2004/2005 500 50 1,000
2005/2006 300 10 2,500

I am hoping a union query can achieve the same result as opposed to 3
individual crosstabs.

Thx again Wayne.

Mark.
Melbourne, VIC. Australia.


Others more conversant with SQL than me may have a better solution, but this seems to work.

Save the following SQL as a stored query (qryMyQuery)

SELECT FinYear, Count(DepPrefPa y) AS Csh, 0 As Chq, 0 As Crd
FROM tblBookings
WHERE (((DepPrefPay)= 1))
GROUP BY FinYear
UNION ALL SELECT FinYear, Count(IntPrefPa y) AS Csh, 0 As Chq, 0 As Crd
FROM tblBookings
WHERE (((IntPrefPay)= 1))
GROUP BY FinYear
UNION ALL SELECT FinYear, Count(FinPrefPa y) AS Csh, 0 As Chq, 0 As Crd
FROM tblBookings
WHERE (((FinPrefPay)= 1))
GROUP BY FinYear
UNION ALL SELECT FinYear, 0 AS Csh, Count(DepPrefPa y) As Chq, 0 As Crd
FROM tblBookings
WHERE (((DepPrefPay)= 2))
GROUP BY FinYear
UNION ALL SELECT FinYear, 0 AS Csh, Count(IntPrefPa y) As Chq, 0 As Crd
FROM tblBookings
WHERE (((IntPrefPay)= 2))
GROUP BY FinYear
UNION ALL SELECT FinYear, 0 AS Csh, Count(FinPrefPa y) As Chq, 0 As Crd
FROM tblBookings
WHERE (((FinPrefPay)= 2))
GROUP BY FinYear
UNION ALL SELECT FinYear, 0 AS Csh, 0 As Chq, Count(DepPrefPa y) As Crd
FROM tblBookings
WHERE (((DepPrefPay)= 3))
GROUP BY FinYear
UNION ALL SELECT FinYear, 0 AS Csh, 0 As Chq, Count(IntPrefPa y) As Crd
FROM tblBookings
WHERE (((IntPrefPay)= 3))
GROUP BY FinYear
UNION ALL SELECT FinYear, 0 AS Csh, 0 As Chq, Count(FinPrefPa y) As Crd
FROM tblBookings
WHERE (((FinPrefPay)= 3))
GROUP BY FinYear;

Then create a 2nd query based on this saved query -

SELECT FinYear, Sum(Csh) AS Cash, Sum(Chq) AS Cheq, Sum(Crd) AS Card
FROM qryMyQuery
GROUP BY FinYear;
Wayne Gillespie
Gosford NSW Australia
Jan 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3576
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.
14
1809
by: Salad | last post by:
A97. Situation: I have 3 tables with a text field in each and a date field in the first 2 tables: Table1 Text1, Date1 Table2 Text2, Date2 Table3 Text3 (no date field) The following makes up a saved query called Query1
5
3297
by: Lyn | last post by:
This one is difficult to explain, so I will cut it down to the basics. I have a major table 'tblA' which has an autonum field 'ID-A' as primary key (of no significance to users). 'tblA' contains many fields including picture and memo fields. The main user-selectable field is 'NameA'. There is also a crossreference table (let's call it 'tblB') which provides a secondary method of accessing records in 'tblA'. The main fields in 'tblB'...
2
4343
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
9782
by: marco | last post by:
Dear List, as it seems, MS SQL as used in Access does not allow a select INTO within a UNION query. Also, it seems that a UNION query can not be used as a subquery. Maybe my (simplified) problem can avoid these technicalities: the original table has columns A1, A2, B1, B2, C1, C2.
3
2224
by: mikes | last post by:
I have 2 separate queries, which effectively are the same except they draw data from separate tables. Both tables are (design-wise) identical, only the data is different. for each query, there are 2 tables with a standard LEFT JOIN. One field of the query is calculated, looking for a NULL in one table, and then using a field from the second table in that case. One query looks like this: PARAMETERS Text ( 255 ); SELECT...
5
2281
by: BillCo | last post by:
I'm having a problem with a union query, two simple queries joined with a union statement. It's created in code based on parameters. Users were noticing some inconsistant data and when I analysed the query produced and opened it from a MS Query it started giving strange results. The first query when run alone returns 22 records, some of which have identical values in all fields. This is 100% correct. The second query returns nothing....
7
3318
by: KoliPoki | last post by:
Hello every body. I have a small issue. Problem: I have a table with 4 descriptor columns (type). I need to formulate a query to retrieve a count for each type so I can group by...etc. The view I have works, but doesn't work when I supplement the query with some functions... they just don't like the UNION. The real problem is I can't change any of the udf's or queries, just the view. The view is inner joined back on to the primary...
5
3847
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big table, we try separate this big table into twelve tables and create a view
27
13815
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
0
9684
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9530
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10182
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
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7552
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
5445
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...
1
4120
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
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.