Solved

Condense query to one

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
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…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

776 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