Results 1 to 19 of 19

Thread: Help with SQL statement

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2005
    Posts
    138

    Help with SQL statement

    I need help writing a sql stmt. Here’s what I’m trying to do:

    See attachement pic for tables

    I need to get the sum of sales and cost between two week # (say between week 48 of 2005 and week 2 of 2006), grouped by dept.
    I know I have to look up the dates in the calendar table, in order to use a bigger than and lesser than ..date.
    I didn’t put all the weeks in this example, but the table has 80,000 lines, so they’re all there. Same with the calendar, I have all dates.

    Can some of you sql guru give me a hand? Can’t be that difficult but I’m stuck with this and nothing I’ve tried works.

    Many thanks.
    Attached Images Attached Images  

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    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
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2005
    Posts
    138

    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.

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    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
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Help with SQL statement

    good solution...! (this is off the top of my head.. it should 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
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2005
    Posts
    138

    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.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2005
    Posts
    138

    Re: Help with SQL statement

    Quote Originally Posted by Static
    good solution...! (this is off the top of my head.. it should 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.

  8. #8
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Help with SQL statement

    Do you have the ability/Authority to create a VIEW on the Oracle DB?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2005
    Posts
    138

    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.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jan 2005
    Posts
    138

    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.

  11. #11
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    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
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  12. #12
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    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
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  13. #13
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Help with SQL statement

    CAST!! forgot about that...
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jan 2005
    Posts
    138

    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

  15. #15
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    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.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Jan 2005
    Posts
    138

    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.

  17. #17
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    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.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Jan 2005
    Posts
    138

    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.

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Jan 2005
    Posts
    138

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width