If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > select statement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-04-04, 04:18
sagarh sagarh is offline
Registered User
 
Join Date: Feb 2004
Posts: 10
select statement

hi I have the following select statement:-

select customer,warehouse,sum(CASE DATEPART(yyyy, dated)
WHEN 1999 THEN val ELSE null END) as y1999,
sum(CASE DATEPART(yyyy, dated) when 2000 THEN val
ELSE null END) as y2000 from sales table where warehouse='E' or warehouse='EC'
group by customer,warehouse
order by customer

this works fine however I need to refine the datepart section to tell sql that y1999 is actually between 1st november 1998 and 31 october 1999 and so on.

Can this statement be amended to do this?

Thanks for any help!
Reply With Quote
  #2 (permalink)  
Old 03-04-04, 07:45
cvandemaele cvandemaele is offline
Registered User
 
Join Date: Oct 2003
Location: Switzerland
Posts: 140
Try adding 2 months to column DATED before analyzing the datepart.

select
customer,
warehouse,
sum(CASE DATEPART(yyyy, dateadd(month,2,dated))
WHEN 1999 THEN val ELSE null END) as y1999,

sum(CASE DATEPART(yyyy, dateadd(month,2,dated)) when 2000 THEN val
ELSE null END) as y2000


from sales table where warehouse='E' or warehouse='EC'
group by customer,warehouse
order by customer
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On