Advertisement

09.16.2008 at 04:20AM PDT, ID: 23734683 | Points: 250
[x]
Attachment Details

Getting date based information with an SQL query

Asked by g-spot in SQL Query Syntax

I want to create a table that shows sales for a given week both this year and last year (and maybe also the previous week for both this year and last year.

However I'm unsure how to have mutiple conditions in one query (i.e. one column is this year, one column would be last year)

I have created a query that shows the figures for the current week this year. How do I change the query to create a column that shows this week last year.

The query gives a result like this:

ProductCode TotalThisWeek
----------- -------------
001011      1
001012      15
001021      5
001022      5
001041      4
001042      1
001043      2

(7 row(s) affected)

Start Free Trial
1:
2:
3:
4:
SELECT        ProductCode, COUNT(*) AS TotalThisWeek
FROM            Policies
WHERE        (ProductCode LIKE '001%') AND (Cancelled = 0) AND (DATEPART(yy, EntryDate) = 2008) AND (DATEPART(wk, EntryDate) = DATEPART(wk, GETDATE()))
GROUP BY ProductCode, DATEPART(wk, EntryDate)
 
 
Loading Advertisement...
 
[+][-]09.16.2008 at 04:35AM PDT, ID: 22487211

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.16.2008 at 04:51AM PDT, ID: 22487301

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.16.2008 at 04:54AM PDT, ID: 22487319

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.16.2008 at 05:04AM PDT, ID: 22487383

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.16.2008 at 09:15AM PDT, ID: 22490123

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.16.2008 at 12:32PM PDT, ID: 22492352

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.17.2008 at 01:55AM PDT, ID: 22496652

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.14.2008 at 04:27PM PST, ID: 22965084

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628