Solved

Condense query to one

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

809 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