Re: Help with SQL statement
Why do u need the dates? can u use the week numbers?
SELECT SUM(SALES) FROM tblDATA WHERE [Week] Between 48 and 52
Re: Help with SQL statement
How about between week 48 and week 2 in 2006?
Week 2 is after week 48 according to the dates. But don't forget I also
have a week 2 in the year 2005, I can't show here all the 80,000 lines.
;)
Re: Help with SQL statement
Step 1/
Create a view on top of your DATA table with a calculated column, lets call it YEARWEEK, that is a concatenation of YEAR and WEEK (including a zero before single digit months).
This field will have data as follow
200501
200502
200503
...etc...
200512
200601
..etc...
Step 2/
Your WHERE clause now becomes
Code:
WHERE YEARWEEK between 200510 AND 200602
Re: Help with SQL statement
good solution...! (this is off the top of my head.. it should :rolleyes: work in access but I dont know what DB u are using)
SELECT YM, SUM(SALES) FROM (SELECT FISC_YEAR & FORMAT([WEEK],"0#") as YM, SALES FROM tblDATA) WHERE YM Between 200548 and 200602
Re: Help with SQL statement
Interesting approach. Is there anyway I could do this in one sql statement?
Actually, I have to do this in one pass.
Can you give me an example on how I would write this?
I really s*** at sql, sorry for asking so much.
Re: Help with SQL statement
Quote:
Originally Posted by Static
good solution...! (this is off the top of my head.. it should :rolleyes: work in access but I dont know what DB u are using)
SELECT YM, SUM(SALES) FROM (SELECT FISC_YEAR & FORMAT([WEEK],"0#") as YM, SALES FROM tblDATA) WHERE YM Between 200548 and 200602
I'm using ADO to connect to an oracle database. So I guess it's pretty much generic sql.
Re: Help with SQL statement
Do you have the ability/Authority to create a VIEW on the Oracle DB?
Re: Help with SQL statement
Thanks a bunch guys for your help, I will try it out. If anyone has other syntax other than using concatenate, please post it.
Re: Help with SQL statement
Quote:
Originally Posted by DKenny
Do you have the ability/Authority to create a VIEW on the Oracle DB?
I could ask the db admin to do it, but since I know this can be done from my end in one stmt, I find that solution more attractive. This way, I wouldn't have to bug him and wait for him.
;)
Re: Help with SQL statement
Here's a 'One-pass' method
Note: I had to do this on SQLServer and I think you need a & rather than a + to concat string fields in Oracle.
Code:
SELECT DEPT
,SUM(AMOUNT)
FROM DK_TEST
WHERE CAST(CAST(MYYEAR AS CHAR(4))+ CAST([WEEK] AS CHAR(2))AS INT)
BETWEEN 200512 AND 200601
GROUP BY
DEPT
Re: Help with SQL statement
i think concat would be the way to do it...
SELECT YM, SUM(SALES) FROM (SELECT CONCAT(FISC_YEAR,WEEK) as YM, SALES FROM tblDATA) WHERE YM Between 200548 and 200602
no wait.. that wont format the weeks to 01 ... hmmm
Re: Help with SQL statement
CAST!! forgot about that... ;)
Re: Help with SQL statement
Quote:
Originally Posted by DKenny
Here's a 'One-pass' method
Note: I had to do this on SQLServer and I think you need a & rather than a + to concat string fields in Oracle.
Code:
SELECT DEPT
,SUM(AMOUNT)
FROM DK_TEST
WHERE CAST(CAST(MYYEAR AS CHAR(4))+ CAST([WEEK] AS CHAR(2))AS INT)
BETWEEN 200512 AND 200601
GROUP BY
DEPT
If I may call upon you help again, what does CAST stands for? Also would the CHAR(2) take care of the 0 in front of single digit week number?
Tx
Re: Help with SQL statement
I would want to avoide a Sub-SELECT as you cannot pass it a WHERE statement - therfore a lot more user-side processing.
Re: Help with SQL statement
Quote:
Originally Posted by DKenny
I would want to avoide a Sub-SELECT as you cannot pass it a WHERE statement - therfore a lot more user-side processing.
ahh ok, I see your point. I'm aiming for speed here, since I have to do this quite a number of time.
Re: Help with SQL statement
Quote:
Originally Posted by D-niss
If I may call upon you help again, what does CAST stands for?
CAST is use to change the Type of a value.
Synthax:
CAST( Value as Type)
So CAST(MYYEAR AS CHAR(4)) converts the values in the field MYYEAR to a fixed length string, 4 characters long.
Quote:
Originally Posted by D-niss
Also would the CHAR(2) take care of the 0 in front of single digit week number?
Tx
Yes, the CHAR datatype is fixed length, and casting an Integer as a CHAR will always add preceding zeros.
Re: Help with SQL statement
Quote:
Originally Posted by Static
CAST!! forgot about that... ;)
lol, cool. It's not to late. Any help I get, even slightly wrong, points me in the right direction.
:)
Re: Help with SQL statement
Quote:
Originally Posted by DKenny
CAST is use to change the Type of a value.
Synthax:
CAST( Value as Type)
So CAST(MYYEAR AS CHAR(4)) converts the values in the field MYYEAR to a fixed length string, 4 characters long.
Yes, the CHAR datatype is fixed length, and casting an Integer as a CHAR will always add preceding zeros.
ahhh this is beautifull, I love it. I will try it soon and let you guys know. At first sight, it seems this will do the job.