Solved

Condense query to one

Posted on 2014-04-24
3
180 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 49

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 49

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
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, when working with VBA, learn some techniques for writing readable and easily maintained code.

729 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