473,388 Members | 1,423 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,388 software developers and data experts.

Union Query

G'day ppl.

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

tblBookings.FinYear
tblBookings.DepPrefPay
tblBookings.IntPrefPay
tblBookingsFinPrefPay

I tried structuring it as:
SELECT tblBookings.FinYear, tblBookings.DepPrefPay
FROM tblBookings
UNION SELECT tblBookings.IntPrefPay, tblBookings.IntFinPay; AS
PaymentPreference;

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 3669
On Thu, 19 Jan 2006 21:27:38 +1100, "NoodNutt" <no******@iprimus.com.au> wrote:
G'day ppl.

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

tblBookings.FinYear
tblBookings.DepPrefPay
tblBookings.IntPrefPay
tblBookingsFinPrefPay

I tried structuring it as:
SELECT tblBookings.FinYear, tblBookings.DepPrefPay
FROM tblBookings
UNION SELECT tblBookings.IntPrefPay, tblBookings.IntFinPay; AS
PaymentPreference;

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.FinYear, tblBookings.DepPrefPay As PrefPay
FROM tblBookings
WHERE Not (tblBookings.DepPrefPay) Is Null And FinYear = 1
UNION ALL SELECT tblBookings.FinYear, tblBookings.IntPrefPay As PrefPay
FROM tblBookings
WHERE Not (tblBookings.IntPrefPay) Is Null And FinYear = 1
UNION ALL SELECT tblBookings.FinYear, tblBookings.FinPrefPay As PrefPay
FROM tblBookings
WHERE Not (tblBookings.FinPrefPay) 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******@iprimus.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.FinYear
tblBookings.DepPrefPay
tblBookings.IntPrefPay
tblBookingsFinPrefPay

I tried structuring it as:
SELECT tblBookings.FinYear, tblBookings.DepPrefPay
FROM tblBookings
UNION SELECT tblBookings.IntPrefPay, tblBookings.IntFinPay; AS
PaymentPreference;

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******@iprimus.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(DepPrefPay) AS Csh, 0 As Chq, 0 As Crd
FROM tblBookings
WHERE (((DepPrefPay)=1))
GROUP BY FinYear
UNION ALL SELECT FinYear, Count(IntPrefPay) AS Csh, 0 As Chq, 0 As Crd
FROM tblBookings
WHERE (((IntPrefPay)=1))
GROUP BY FinYear
UNION ALL SELECT FinYear, Count(FinPrefPay) AS Csh, 0 As Chq, 0 As Crd
FROM tblBookings
WHERE (((FinPrefPay)=1))
GROUP BY FinYear
UNION ALL SELECT FinYear, 0 AS Csh, Count(DepPrefPay) As Chq, 0 As Crd
FROM tblBookings
WHERE (((DepPrefPay)=2))
GROUP BY FinYear
UNION ALL SELECT FinYear, 0 AS Csh, Count(IntPrefPay) As Chq, 0 As Crd
FROM tblBookings
WHERE (((IntPrefPay)=2))
GROUP BY FinYear
UNION ALL SELECT FinYear, 0 AS Csh, Count(FinPrefPay) As Chq, 0 As Crd
FROM tblBookings
WHERE (((FinPrefPay)=2))
GROUP BY FinYear
UNION ALL SELECT FinYear, 0 AS Csh, 0 As Chq, Count(DepPrefPay) As Crd
FROM tblBookings
WHERE (((DepPrefPay)=3))
GROUP BY FinYear
UNION ALL SELECT FinYear, 0 AS Csh, 0 As Chq, Count(IntPrefPay) As Crd
FROM tblBookings
WHERE (((IntPrefPay)=3))
GROUP BY FinYear
UNION ALL SELECT FinYear, 0 AS Csh, 0 As Chq, Count(FinPrefPay) 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
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 ...
14
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...
5
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...
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: 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)...
3
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...
5
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...
7
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...
5
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...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
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...

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.