Case Select Sum

  • I have the following sql:

    SELECT Id, Name,

    CASE

    (

    (SELECT SUM(Revenue)

    FROM myTable

    WHERE [Start Date] = '2008-08-01' AND Name = 'myName')

    )

    WHEN > 0 THEN 'Has Revenue'

    ELSE 'NO Revenue'

    END

    FROM myTable

    Any pointers getting the case statement working?

  • Haven't tested it because no data... but this should work SELECT ID, Name,

    CASE SUM(Revenue)

    WHEN 0 THEN 'NO Revenue'

    ELSE 'Has Revenue'

    END

    FROM MyTable

    WHERE [Start Date] = '2008-08-01' AND Name = 'myName'...

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Worked like a charm, thanks

  • Thanks for the feedback, David.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • I'm having trouble with the following bit. I have a fairly complex set of case statments but the following is what I'm having trouble with (most inside portion)

    ELSE

    CASE

    WHEN SELECT WDF_SiteURL FROM dbo.vw_BASE_WebData_FacultyWebs_Classes_Join WHERE CLASSID = dbo.vw_BASE_CCSSM_REP_Stu_Class_D.CLASS_ID is NULL THEN SELECT WDF_SiteURL FROM dbo.vw_BASE_WebData_FacultyWebs_Classes_Join AS vw_BASE_WebData_FacultyWebs_Classes_Join_2 WHERE InstructorName = dbo.vw_BASE_CCSSM_REP_Class_D.INSTR_NAME AND WDFC_CI_DeptDiv = dbo.vw_BASE_CCSSM_REP_Class_D.CI_DEPT_DIV AND WDFC_CI_Course_Num = dbo.vw_BASE_CCSSM_REP_Class_D.CI_COURSE_NUM AND WDFC_Class_YRQ = dbo.vw_BASE_CCSSM_REP_Class_D.CLASS_YRQ

    ELSE SELECT WDF_SiteURL FROM dbo.vw_BASE_WebData_FacultyWebs_Classes_Join WHERE CLASSID = dbo.vw_BASE_CCSSM_REP_Stu_Class_D.CLASS_ID

    END

    what's the hang up or am I trying something that shouldn't/can't be done this way? All I'm trying to do is see which condition exists in this extra table, and fill the data based on that, essentially returning a URL that goes to a specific class for a student.

  • richard.noordam (6/2/2010)


    I'm having trouble with the following bit. I have a fairly complex set of case statments but the following is what I'm having trouble with (most inside portion)

    ELSE

    CASE

    WHEN SELECT WDF_SiteURL FROM dbo.vw_BASE_WebData_FacultyWebs_Classes_Join WHERE CLASSID = dbo.vw_BASE_CCSSM_REP_Stu_Class_D.CLASS_ID is NULL THEN SELECT WDF_SiteURL FROM dbo.vw_BASE_WebData_FacultyWebs_Classes_Join AS vw_BASE_WebData_FacultyWebs_Classes_Join_2 WHERE InstructorName = dbo.vw_BASE_CCSSM_REP_Class_D.INSTR_NAME AND WDFC_CI_DeptDiv = dbo.vw_BASE_CCSSM_REP_Class_D.CI_DEPT_DIV AND WDFC_CI_Course_Num = dbo.vw_BASE_CCSSM_REP_Class_D.CI_COURSE_NUM AND WDFC_Class_YRQ = dbo.vw_BASE_CCSSM_REP_Class_D.CLASS_YRQ

    ELSE SELECT WDF_SiteURL FROM dbo.vw_BASE_WebData_FacultyWebs_Classes_Join WHERE CLASSID = dbo.vw_BASE_CCSSM_REP_Stu_Class_D.CLASS_ID

    END

    what's the hang up or am I trying something that shouldn't/can't be done this way? All I'm trying to do is see which condition exists in this extra table, and fill the data based on that, essentially returning a URL that goes to a specific class for a student.

    It's a little hard to tell without seeing the rest of the query or having any test data or table definitions ...

    But, it seems to me this would be much easier to support and troubleshoot and would have superior performance if instead of doing multiple selects within a CASE statement you handle it in a more set-based fashion. Establish the correct joins in your query and then do a CASE based on the values of the records in the joined tables.

    └> bt



    Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • specifically in the ELSE Case I get

    Error in list of function arguments: 'SELECT' not recognized.

    Error in list of function arguments: ')' is not recognized.

    Unable to parse query.

    I would normally do as you suggested but in this case it isn't as easy as that, and it's a special situation. Back to the point.

    Here is the whole case statement. I'm trying to return a specific URL depending on several conditions.

    CASE (SBCTC_MISC_1)

    WHEN '85' THEN 'Campus Meetings; See angelccs.spokane.edu'

    WHEN '30' THEN

    CASE (SECT)

    WHEN 'TD' THEN 'See angelccs.spokane.edu'

    ELSE 'See http://angel.spokanefalls.edu/'

    END

    ELSE

    CASE

    WHEN SELECT WDF_SiteURL FROM dbo.vw_BASE_WebData_FacultyWebs_Classes_Join WHERE CLASSID = dbo.vw_BASE_CCSSM_REP_Stu_Class_D.CLASS_ID is NULL THEN SELECT WDF_SiteURL FROM dbo.vw_BASE_WebData_FacultyWebs_Classes_Join AS vw_BASE_WebData_FacultyWebs_Classes_Join_2 WHERE InstructorName = dbo.vw_BASE_CCSSM_REP_Class_D.INSTR_NAME AND WDFC_CI_DeptDiv = dbo.vw_BASE_CCSSM_REP_Class_D.CI_DEPT_DIV AND WDFC_CI_Course_Num = dbo.vw_BASE_CCSSM_REP_Class_D.CI_COURSE_NUM AND WDFC_Class_YRQ = dbo.vw_BASE_CCSSM_REP_Class_D.CLASS_YRQ

    ELSE SELECT WDF_SiteURL FROM dbo.vw_BASE_WebData_FacultyWebs_Classes_Join WHERE CLASSID = dbo.vw_BASE_CCSSM_REP_Stu_Class_D.CLASS_ID

    END

    END AS URL

    the ELSE CASE in the innermost case is the issue, specifically this statement (below) in a WHEN (X) is null, WHERE X is a select statement.

    I went down the set based, and couldn't get it to work the way I wanted, (and I'm not an SQL novice, or intermediate and have been doing this, for right about 15 yrs, starting with Oracle and then MSSQL).

    In the end I want 1 URL based on the Following logic

    CASE (SBCTC_MISC_1) -- SOURCE 1

    WHEN '85' THEN URL = angelccs.spokane.edu

    WHEN '30' THEN

    CASE (SECT)

    WHEN 'TD' THEN URL = angelccs.spokane.edu

    ELSE URL = http://angel.spokanefalls.edu

    END

    ELSE

    CASE

    WHEN (SELECT STATEMENT TO DETERMINE first URL condition) is null

    Then SELECT SQL URL from source_2

    ELSE

    Then Select SQL URL from source_3

    ENDEND

    the color 'tagged' section is the only section I'm having issue with. Not sure why the color didn't show up. I'm SOOOOO close to the solution on this I can just feel it. :hehe:

    Note: Even after all has hooked up properly most of the classes STILL won't have a URL and that is ok, and correct.

  • it was a minor syntax issue:

    correct parens fixed. Der....

    CASE

    WHEN (SELECT WDF_SiteURL FROM dbo.vw_BASE_WebData_FacultyWebs_Classes_Join WHERE CLASSID = dbo.vw_BASE_CCSSM_REP_Stu_Class_D.CLASS_ID) is NULL THEN (SELECT WDF_SiteURL FROM dbo.vw_BASE_WebData_FacultyWebs_Classes_Join AS vw_BASE_WebData_FacultyWebs_Classes_Join_2 WHERE InstructorName = dbo.vw_BASE_CCSSM_REP_Class_D.INSTR_NAME AND WDFC_CI_DeptDiv = dbo.vw_BASE_CCSSM_REP_Class_D.CI_DEPT_DIV AND WDFC_CI_Course_Num = dbo.vw_BASE_CCSSM_REP_Class_D.CI_COURSE_NUM AND WDFC_Class_YRQ = dbo.vw_BASE_CCSSM_REP_Class_D.CLASS_YRQ)

    ELSE (SELECT WDF_SiteURL FROM dbo.vw_BASE_WebData_FacultyWebs_Classes_Join WHERE CLASSID = dbo.vw_BASE_CCSSM_REP_Stu_Class_D.CLASS_ID)

    END

    I think I was tired at the end of last week....

  • SELECT EMPLOYEE_ID, Last_Name,

    CASE SUM(Salary)

    WHEN 0 THEN 'NO Revenue'

    ELSE 'Has Revenue'

    END as "Revenue"

    FROM employees

    WHERE employee_id = '100'

    group by employee_id,last_name

    /

    YEE_ID LAST_NAME Revenue

    ------ ------------------------- -----------

    100 King Has Revenue

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply