Solved

Condense query to one

Posted on 2014-04-24
3
169 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
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.

743 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

11 Experts available now in Live!

Get 1:1 Help Now