Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Case Select Sum Expand / Collapse
Author
Message
Posted Tuesday, August 12, 2008 2:52 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, September 10, 2012 10:43 AM
Points: 6, Visits: 111
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?


Post #551445
Posted Tuesday, August 12, 2008 7:40 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:42 PM
Points: 36,781, Visits: 31,237
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #551534
Posted Thursday, August 14, 2008 7:47 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, September 10, 2012 10:43 AM
Points: 6, Visits: 111
Worked like a charm, thanks
Post #552691
Posted Friday, August 15, 2008 6:05 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:42 PM
Points: 36,781, Visits: 31,237
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #553859
Posted Wednesday, June 2, 2010 11:04 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Monday, April 14, 2014 3:54 PM
Points: 727, Visits: 425
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.
Post #931516
Posted Wednesday, June 2, 2010 12:30 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, December 4, 2013 1:49 PM
Points: 1,104, Visits: 1,174
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
Post #931588
Posted Wednesday, June 2, 2010 12:57 PM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Monday, April 14, 2014 3:54 PM
Points: 727, Visits: 425
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
[color=#FF000] 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
END[/color]END


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.

Note: Even after all has hooked up properly most of the classes STILL won't have a URL and that is ok, and correct.
Post #931612
Posted Monday, June 7, 2010 1:33 PM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Monday, April 14, 2014 3:54 PM
Points: 727, Visits: 425
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....
Post #933675
Posted Tuesday, July 23, 2013 11:53 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, July 23, 2013 11:52 PM
Points: 1, Visits: 3
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
Post #1476880
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse