473,656 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need some help fixing my UNION statement

Hello everyone based on the data, I created a union query which
produces this.

SELECT [MonthYear],[CheckWeek],[CheckCount], 0 As ClosedCount
FROM [OpenByDate]
UNION SELECT
[MonthYear],[CheckWeek], 0 AS OpenedCount,[CheckCount]
FROM [ClosedByDate]
ORDER BY [MonthYear], [CheckWeek];

MonthYear CheckWeek CheckCount ClosedCount
Apr 2003 Week: 3 1 0
Apr 2003 Week: 4 1 0
Apr 2004 Week: 1 1 0
Apr 2004 Week: 4 1 0
Apr 2004 Week: 5 1 0
Feb 2003 Week: 5 1 0
Jul 2003 Week: 2 0 1
Jul 2004 Week: 1 0 1
Jul 2004 Week: 2 0 3
Jul 2004 Week: 2 1 0
Jul 2004 Week: 3 1 0
Jun 2004 Week: 1 0 3
Jun 2004 Week: 1 6 0
Jun 2004 Week: 2 0 3
Jun 2004 Week: 2 6 0
Jun 2004 Week: 3 0 3
Jun 2004 Week: 4 0 7
Jun 2004 Week: 4 3 0
Jun 2004 Week: 5 4 0
May 2004 Week: 3 0 1
May 2004 Week: 3 1 0
May 2004 Week: 4 0 1
May 2004 Week: 4 1 0
May 2004 Week: 5 0 1
May 2004 Week: 5 5 0
Oct 2003 Week: 3 1 0

The data displayed above is correct but the weeks appear twice if they
occur in the same week. It doesn't put both opencount/closedcount
beside each other evenly. Also the Months are not in order. Do you
know what I could add for this. Thanks.

The two queries look like this:

ClosedByDate Query

SELECT Format(([Closure Date]),"mmm") & " " & Format(([Closure
Date]),"yyyy") AS MonthYear, "Week: " & GetWeekNumberIn Month([Closure
Date]) AS CheckWeek, Count(Closeddat es.[Closure Date]) AS CheckCount
FROM Closeddates
GROUP BY Year([Closure Date]), Month([Closure Date]), Format(([Closure
Date]),"mmm") & " " & Format(([Closure Date]),"yyyy"), "Week: " &
GetWeekNumberIn Month([Closure Date])
ORDER BY Year([Closure Date]), Month([Closure Date]);

OpenByDate Query

SELECT Format(([Opened Date]),"mmm") & " " & Format(([Opened
Date]),"yyyy") AS MonthYear, "Week: " & GetWeekNumberIn Month([Opened
Date]) AS CheckWeek, Count(Openeddat es.[Opened Date]) AS CheckCount
FROM Openeddates
GROUP BY Year([Opened Date]), Month([Opened Date]), Format(([Opened
Date]),"mmm") & " " & Format(([Opened Date]),"yyyy"), "Week: " &
GetWeekNumberIn Month([Opened Date])
ORDER BY Year([Opened Date]), Month([Opened Date]);
Nov 13 '05 #1
5 1912
al******@hotmai l.com (Alicia) wrote in message news:<d3******* *************** ****@posting.go ogle.com>...
Hello everyone based on the data, I created a union query which
produces this.

SELECT [MonthYear],[CheckWeek],[CheckCount], 0 As ClosedCount
FROM [OpenByDate]
UNION SELECT
[MonthYear],[CheckWeek], 0 AS OpenedCount,[CheckCount]
FROM [ClosedByDate]
ORDER BY [MonthYear], [CheckWeek];

MonthYear CheckWeek CheckCount ClosedCount
Apr 2003 Week: 3 1 0
Apr 2003 Week: 4 1 0
Apr 2004 Week: 1 1 0
Apr 2004 Week: 4 1 0
Apr 2004 Week: 5 1 0
Feb 2003 Week: 5 1 0
Jul 2003 Week: 2 0 1
Jul 2004 Week: 1 0 1
Jul 2004 Week: 2 0 3
Jul 2004 Week: 2 1 0
Jul 2004 Week: 3 1 0

The data displayed above is correct but the weeks appear twice if they
occur in the same week. It doesn't put both opencount/closedcount
beside each other evenly.


Sounds like you want a Group By clause in your query and you don't
have one.

Also the Months are not in order.
Order by the Date field, not the alphabetic stuff, and then it will
work. Just don't show the date field in your output.
Nov 13 '05 #2
al******@hotmai l.com (Alicia) wrote in message news:<d3******* *************** ****@posting.go ogle.com>...
Hello everyone based on the data, I created a union query which
produces this.

SELECT [MonthYear],[CheckWeek],[CheckCount], 0 As ClosedCount
FROM [OpenByDate]
UNION SELECT
[MonthYear],[CheckWeek], 0 AS OpenedCount,[CheckCount]
FROM [ClosedByDate]
ORDER BY [MonthYear], [CheckWeek];

MonthYear CheckWeek CheckCount ClosedCount
Apr 2003 Week: 3 1 0
Apr 2003 Week: 4 1 0
Apr 2004 Week: 1 1 0
Apr 2004 Week: 4 1 0
Apr 2004 Week: 5 1 0
Feb 2003 Week: 5 1 0
Jul 2003 Week: 2 0 1
Jul 2004 Week: 1 0 1
Jul 2004 Week: 2 0 3
Jul 2004 Week: 2 1 0
Jul 2004 Week: 3 1 0
Jun 2004 Week: 1 0 3
Jun 2004 Week: 1 6 0
Jun 2004 Week: 2 0 3
Jun 2004 Week: 2 6 0
Jun 2004 Week: 3 0 3
Jun 2004 Week: 4 0 7
Jun 2004 Week: 4 3 0
Jun 2004 Week: 5 4 0
May 2004 Week: 3 0 1
May 2004 Week: 3 1 0
May 2004 Week: 4 0 1
May 2004 Week: 4 1 0
May 2004 Week: 5 0 1
May 2004 Week: 5 5 0
Oct 2003 Week: 3 1 0

The data displayed above is correct but the weeks appear twice if they
occur in the same week. It doesn't put both opencount/closedcount
beside each other evenly. Also the Months are not in order. Do you
know what I could add for this. Thanks.

The two queries look like this:

ClosedByDate Query

SELECT Format(([Closure Date]),"mmm") & " " & Format(([Closure
Date]),"yyyy") AS MonthYear, "Week: " & GetWeekNumberIn Month([Closure
Date]) AS CheckWeek, Count(Closeddat es.[Closure Date]) AS CheckCount
FROM Closeddates
GROUP BY Year([Closure Date]), Month([Closure Date]), Format(([Closure
Date]),"mmm") & " " & Format(([Closure Date]),"yyyy"), "Week: " &
GetWeekNumberIn Month([Closure Date])
ORDER BY Year([Closure Date]), Month([Closure Date]);

OpenByDate Query

SELECT Format(([Opened Date]),"mmm") & " " & Format(([Opened
Date]),"yyyy") AS MonthYear, "Week: " & GetWeekNumberIn Month([Opened
Date]) AS CheckWeek, Count(Openeddat es.[Opened Date]) AS CheckCount
FROM Openeddates
GROUP BY Year([Opened Date]), Month([Opened Date]), Format(([Opened
Date]),"mmm") & " " & Format(([Opened Date]),"yyyy"), "Week: " &
GetWeekNumberIn Month([Opened Date])
ORDER BY Year([Opened Date]), Month([Opened Date]);


You could add theYear and theMonth {e.g., Year([Opened Date]) As
theYear} to your ClosedByDate and OpenByDate queries. Then you could
use those to help sort your results. The weeks appearing twice can be
handled by creating an intermediate table consisting of what you show
currently as results. I think you would 'Group By' theYear, theMonth
and CInt(Right([CheckWeek],1)) and SUM your two counts to combine
values from your auxiliary table. To get everything to print out
evenly, use an Access Report or use the Space() and Len() functions
together to pad spaces in the output where needed. The string 'week:'
can even be added at the 'output to file' step if required. Note that
if you have theYear and theMonth you can create a string that looks
like Oct 2003 by Format$([theMonth] & '/1/' & [theYear], 'mmm yyyy')
so your MonthYear field would not be needed until the final totals
query.

James A. Fortune
Nov 13 '05 #3
Hi Alicia,

Well . I'm not very familiar with UNION queries either, so I couldn't get
the *exact* results you wanted.

Now everything works as it should, with the small (but irritating) exception
of not being able to combine the "OpenedCoun t" and "ClosedCoun t on a single
line. I even tried using the "Star Trek" . 2004.06 J date method to get the
union query to group on identical values. (i.e. Jun 2004, Week 1)

I also designed a couple of "Test" queries to see if the tally of
"OpenedCoun t / ClosedCount" matched the number of records returned by
criteria, and they do. 27 and 19, respectively.

So anyway . like I said . it's not perfect, but it does accomplish what (I
think) you want/need.

qryUnion

MonthYear
WeekNo
OpenedCount
ClosedCount
FirstDate

Apr 2003
Week: 3
1

2003.04

Jul 2003
Week: 2

1
2003.07

Oct 2003
Week: 3
1

2003.1

Apr 2004
Week: 4
1

2004.04

May 2004
Week: 4

1
2004.05

May 2004
Week: 4
1

2004.05

May 2004
Week: 5
4

2004.05

Jun 2004
Week: 1

1
2004.06

Jun 2004
Week: 1
5

2004.06

Jun 2004
Week: 2

3
2004.06

Jun 2004
Week: 2
5

2004.06

Jun 2004
Week: 3

3
2004.06

Jun 2004
Week: 4

6
2004.06

Jun 2004
Week: 4
3

2004.06

Jun 2004
Week: 5
4

2004.06

Jul 2004
Week: 1

1
2004.07

Jul 2004
Week: 2

3
2004.07

Jul 2004
Week: 2
1

2004.07

Jul 2004
Week: 3
1

2004.07


I began by deleting about 600 blank (junk) records from the sample data
table that you sent me. I was also getting confused by the "Queries based on
Queries" concept, so I deleted your existing ones and designed 2 new ones of
my own. (qryOpenedCount and qryClosureCount ) Both of these queries ignore
the Nulls, and calculate the "MonthYear" , "WeekNo" etc. I used "First" of
the dates involved, because "GroupBy" causes one record for each date.

These 2 queries seem to return the desired results individually, so I tried
to get the UNION thing to work using that.

Here is the SQL from these 2 queries, followed by the SQL for the UNION
query:

--------------------------------------------------

** qryClosureCount **

SELECT Format([Closure Date],"mmm yyyy") AS MonthYear, "Week: " &
GetWeekNumberIn Month([Closure Date]) AS WeekNo, Count(newAPARda ta.[Closure
Date]) AS ClosedCount, First(CDbl(Form at([Closure Date],"yyyy\.mm") )) AS
FirstDate

FROM newAPARdata

WHERE (((newAPARdata.[Closure Date]) Is Not Null))

GROUP BY Format([Closure Date],"mmm yyyy"), "Week: " &
GetWeekNumberIn Month([Closure Date])

ORDER BY First(CDbl(Form at([Closure Date],"yyyy\.mm") ));

--------------------------------------------------

** qryOpenedCount **

SELECT Format([Opened Date],"mmm yyyy") AS MonthYear, "Week: " &
GetWeekNumberIn Month([Opened Date]) AS WeekNo, Count(newAPARda ta.[Opened
Date]) AS OpenedCount, First(CDbl(Form at([Opened Date],"yyyy\.mm") )) AS
FirstDate

FROM newAPARdata

WHERE (((newAPARdata.[Opened Date]) Is Not Null))

GROUP BY Format([Opened Date],"mmm yyyy"), "Week: " &
GetWeekNumberIn Month([Opened Date])

ORDER BY First(CDbl(Form at([Opened Date],"yyyy\.mm") ));

--------------------------------------------------

** qryUnion **

SELECT MonthYear, WeekNo, OpenedCount, "" As ClosedCount, FirstDate

FROM qryOpenedCount

UNION SELECT ALL MonthYear, WeekNo, "" As OpenedCount, ClosedCount,
FirstDate

FROM qryClosureCount

GROUP BY MonthYear, WeekNo, ClosedCount, FirstDate

ORDER BY FirstDate;

--------------------------------------------------

I am also attaching a revised Alicia2.MDB (to the e-mail only)

Don

P.S.

CC'd to the newsgroup, in case someone else can either learn from, and/or
improve on this.

Hopefully I won't be chastised too hard for posting in HTML, which is
necessary in order to display the table.

-----Original Message-----
From: Alicia Keller LetsDeleteThis@ SpamMeNot

Sent: Sunday, July 18, 2004 9:44AM
To: LetsDeleteThis@ SpamMeNot
Subject: RE: Trying to combine Two queries into One Query.. Need Help!

[I wanted] both

APAROPENDATES and APARCLOSEDDATES to combine their data together into one

query with 4 columns.

Such as

APARCLOSEDQUERY

MonthYear CheckWeek CheckCount

Jul 2003 Week: 2 1

May 2004 Week: 4 1

Jun 2004 Week: 1 1

Jun 2004 Week: 2 2

Jun 2004 Week: 3 3

Jun 2004 Week: 4 6

Jul 2004 Week: 1 1

Jul 2004 Week: 2 2

and

APAROPENQUERY

MonthYear CheckWeek CheckCount

Apr 2003 Week: 3 1

Oct 2003 Week: 3 1

Apr 2004 Week: 4 1

May 2004 Week: 4 1

May 2004 Week: 5 3

Jun 2004 Week: 1 5

Jun 2004 Week: 2 4

Jun 2004 Week: 4 3

Jun 2004 Week: 5 3

Jul 2004 Week: 2 1

Jul 2004 Week: 3 1

to combine their data side by side. I guess that is where the UNION thought

came in.

like

MonthYear CheckWeek OpenCount ClosedCount

Jun 2004 Week: 1 5 1

Jun 2004 Week: 2 4 2

Jun 2004 Week: 3 blank or 0 3

Jun 2004 Week: 4 3 6

Jun 2004 Week: 5 3 a blank or 0

etc by month...

Thanks

<snip>
Nov 13 '05 #4
> Hopefully I won't be chastised too hard for posting in HTML, which is
necessary in order to display the table.


Oh well, I guess I don't have to worry about that. I guess I have OE set up
to post as plain text. Hopefully everyone will be able to envision the table
anyway. :)
Nov 13 '05 #5
"Don Leverton" <le************ ****@telusplane t.net> wrote in
news:e21Lc.9958 1$eO.95853@edtn ps89:
Hi Alicia,

Well . I'm not very familiar with UNION queries either, so I
couldn't get the *exact* results you wanted.


Alicia and Don, you're sorta on the right track.
What you want to do is first create a union query with the date
information, then left join on summary queries to add the
numerical data.

qryMyUnion
SELECT Format(([Closure Date]),"mmm") & " "
& Format(([Closure Date]),"yyyy") AS MonthYear,
"Week: " & GetWeekNumberIn Month([Closure Date]) AS CheckWeek,
year([closure date]) & month([closure date]) &
GetWeekNumberIn Month([Closure Date]) AS SortKey
FROM Closeddates
UNION
SELECT Format(([Opened Date]),"mmm") & " "
& Format(([Opened Date]),"yyyy") AS MonthYear,
"Week: " & GetWeekNumberIn Month([Opened Date]) AS CheckWeek,
year([opened date]) & month([opened date]) & GetWeekNumberIn Month
([opened Date]) AS SortKey
FROM Openeddates;

then

SELECT qryMyUnion.[MonthYear],
qryMyUnion.[CheckWeek],
opencount+close dcount as [CheckCount],
Opencount,
ClosedCount
FROM qryMyUnion
LEFT JOIN [OpenByDate]
ON qryMyUnion.mont hyear = openbydate.mont hyear
AND qryMyUnion.week = openbydate.week
LEFT JOIN [ClosedByDate]
ON qryMyUnion.mont hyear = Closedbydate.mo nthyear
AND qryMyUnion.week = Closedbydate.we ek
ORDER BY SortKey;

--
Bob Quintal

PA is y I've altered my email address.
Nov 13 '05 #6

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

Similar topics

4
1909
by: Bung | last post by:
Hi, I have a tricky sql statment I have to write (tricky for me) and I am stuck. I'm having trouble with the following problem. Table1 (Column a, Column b, Column c) Table2 (Column a, Column b, Column c) Table3 (Column a, Column b, Column c) Table1 contains a row of value (1, 2, 3)
3
5371
by: Edward | last post by:
I am having conceptual trouble with the following query: select r.ServiceID,r.ContractID,sum(Total) from ( select csc.ServiceID,c.ContractID, sum(csc.ContainerMovement) as Total from iwms_tbl_CustomerSiteContainers csc, iwms_tbl_ContractLines cl, iwms_tbl_Contracts c,
2
2529
by: Jeff Blee | last post by:
I am hoping someone can help me. I am making a Access 97 app for a person and have run up against a problem to do with MS Graph. There is a table that has a number of data elements and a date field and entries are entered each month. A graph is required that has three of the data elements represented basically on the y axis and time as months along the x axis. So for each month there is a group of three columns representing the data...
8
17218
by: Bill | last post by:
Hello out there; This may be a challenge but I'm certain it's possible but I can't seem to figure out how. I have a table that has several date fields, e.g., Date1, Date2, Date3, Date4 ... etc. I wish to find out either of two things for each row. 1) What is the maximun date in any of the date fields in the row.
48
3850
by: phillip.s.powell | last post by:
MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't ask) I have database Spring with table Students I have database Summer with table Students I am tasked to produce a query of all students in both tables with no duplicates. No clue whatsoever.
4
2454
by: rdemyan via AccessMonster.com | last post by:
I have the following SQL statement in code that is set to the RowSource of a combobox. The combobox has two columns. SELECT '(ALL)' As Site, '' As , 0 As SortFirst FROM GROUP BY SITE_COMPLEX UNION SELECT SITE_COMPLEX As Site, Count(BLDG_ID) As , 1 As SortFirst FROM
1
2800
by: Randy Volkart | last post by:
I'm trying to fix a glitch in a complex access database, and have a fairly complex problem... unless there's some obscure easy fix I don't know being fairly new with Access. Basically, the area I'm trying to fix includes a form which takes entered data, concatenates it into a VB string to form an SQL query, then launches a report with information from the query. Several tables are linked in the query, but the key ones for this problem...
1
1439
by: Cyprus106 | last post by:
I've got a statement as follows that grabs matching records out two tables: "SELECT * FROM BKRETAIL WHERE BKRETAIL.MATK='THISTHE' UNION SELECT * FROM BKMASTER WHERE BKMASTER.MATK='THISTHE'; My problem is that when I try to place something like "ORDER BY BKRETAIL.MEDSTA ASC" after the statement, it throws an error. I need these ordered in a certain arrangement. To be honest, what I really need is to order them by when MEDSTA's field is...
0
2771
by: LanaR | last post by:
Hello, one sql statement is causing severe performance issue. The problem occurs only in UDB environment, the same statemnt on the mainframe is running fine. I have an explain output from the sql. The statement itself is not that complicated, it is 3 selects and union all. Explain output is pretty big, but I could not find anything unusual. I'm new to db2 and I could be missing stuff. I am posting the explain output below and I really...
0
8382
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
8717
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
8498
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
8600
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...
0
7311
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
6162
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
5629
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
4150
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
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.