Solved

Condense query to one

Posted on 2014-04-24
3
173 Views
Last Modified: 2014-04-25
I currently am using 4 queries that I would like to condense to one query. The first query collects data from the current day, the 2nd is Current Week, the third is Last week, and the 4th is Current Year.

Right now the 4 queries are identical with a different label and where clause. I have their individual codes below. Is there a way to do this on one query. I believe that query would have columns titled "Day", "CurrentWeek", "LastWeek", and "Year". If the date falls into the appropriate category, that row would say "Day" in the day column. If not, it would be blank.

Query for Day:
SELECT qryBookingTotals.DateRecorded, Sum(qryBookingTotals.ExtendedPrice) AS SumOfExtendedPrice, Sum(qryBookingTotals.CountOfDAY) AS SumOfCountOfDAY, "ADAY" AS DateSpec
FROM qryBookingTotals
WHERE (((DateValue(Nz([qryBookingTotals].[DateRecorded],#1/1/1950#)))=DateValue(Now())))
GROUP BY qryBookingTotals.DateRecorded, "ADAY";

Open in new window


Query for CurrentWeek:
SELECT qryBookingTotals.DateRecorded, Sum(qryBookingTotals.ExtendedPrice) AS SumOfExtendedPrice, Sum(qryBookingTotals.CountOfDAY) AS SumOfCountOfDAY, "CURRENTWEEK" AS DateSpec
FROM qryBookingTotals
WHERE (((Format(Nz([qryBookingTotals].[DateRecorded],#1/1/1950#),"wwyy"))=Format(Now(),"wwyy")))
GROUP BY qryBookingTotals.DateRecorded, "CURRENTWEEK";

Open in new window


Query for Last week:
SELECT qryBookingTotals.DateRecorded, Sum(qryBookingTotals.ExtendedPrice) AS SumOfExtendedPrice, Sum(qryBookingTotals.CountOfDAY) AS SumOfCountOfDAY, "LASTWEEK" AS DateSpec
FROM qryBookingTotals
WHERE (((Format(Nz([qryBookingTotals].[DateRecorded],#1/1/1950#),"wwyy"))=Format(Date()-7,"wwyy")))
GROUP BY qryBookingTotals.DateRecorded, "LASTWEEK";

Open in new window


Query for Year:
SELECT qryBookingTotals.DateRecorded, Sum(qryBookingTotals.ExtendedPrice) AS SumOfExtendedPrice, Sum(qryBookingTotals.CountOfDAY) AS SumOfCountOfDAY, "YEAR" AS DateSpec
FROM qryBookingTotals
WHERE (((Format(Nz([qryBookingTotals].[DateRecorded],#1/1/1950#),"yyyy"))=Format(Now(),"yyyy")))
GROUP BY qryBookingTotals.DateRecorded, "YEAR";

Open in new window

0
Comment
Question by:cansevin
  • 2
3 Comments
 
LVL 22

Accepted Solution

by:
rspahitz earned 500 total points
ID: 40021352
One possibility, if all the displayed fields are the same, is to use the UNION command.

I forget how to do this in the Access interface, but if you go to the SQL window, just paste each query and put the word UNION between them:

select query1...
UNION
select query2...
UNION
select query3...
UNION
select query4

In some cases, this may not work, so try it in a new blank query.  Also, the Access query window will typically not show the 4 queries is you go to design view.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40022355
in non-access SQL this could be achieved by placing a case expression inside a SUM() function e.g. (sort of like this)

, SUM(case when DateRecorded = today*       then somevalue end) as this_day
, SUM(case when DateRecorded in thisweek* then somevalue end) as this_week

My Access skills are rubbish, but the same effect can be achieved using IIF() inside the SUM()

So if you really want to do this in one pass over the data (instead of 4) that is how I would go about doing it - pity is I can't offer to whole query


*today & "in thisweek" don't have to really exist, there are concepts not functions or system variables
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40022368
something along theses lines (I think) (!untested!)
SELECT qryBookingTotals.DateRecorded
, SUM( IIF((DateValue(Nz([qryBookingTotals].[DateRecorded],#1/1/1950#)))=DateValue(Now()),qryBookingTotals.ExtendedPrice,0) ) AS SumOfExtendedPrice_Day
, SUM( IIF((DateValue(Nz([qryBookingTotals].[DateRecorded],#1/1/1950#)))=DateValue(Now()),qryBookingTotals.CountOfDAY,0) )    AS SumOfCountOfDAY_Day

, SUM( IIF((Format(Nz([qryBookingTotals].[DateRecorded],#1/1/1950#),"wwyy"))=Format(Now(),"wwyy"),qryBookingTotals.ExtendedPrice,0) ) AS SumOfExtendedPrice_week
, SUM( IIF((Format(Nz([qryBookingTotals].[DateRecorded],#1/1/1950#),"wwyy"))=Format(Now(),"wwyy"),qryBookingTotals.CountOfDAY,0) )    AS SumOfCountOfDAY_week

WHERE (((Format(Nz([qryBookingTotals].[DateRecorded],#1/1/1950#),"yyyy"))=Format(Now(),"yyyy")))
GROUP BY qryBookingTotals.DateRecorded

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now