Showing posts with label uncomment. Show all posts
Showing posts with label uncomment. Show all posts

Tuesday, March 20, 2012

Comparing CASE statement products

The query I have written below works fine. However, I now want to uncomment the WHERE clause below to find entries where the PatientAge does not fall between the PAGBeginningAge and PAGEndingAge. However, when I uncomment the WHERE clause line I receive the following error message:

Msg 156, Level 15, State 1, Line 28

Incorrect syntax near the keyword 'FROM'.

SELECT ampfm.rpt_AdtVisit.PatientFullName, ampfm.rpt_AdtVisit.AdmitPriorityCode, ampfm.dct_AdmitPriorityType.AdmitPriorityTypeName,

ampfm.rpt_AdtVisit.AccountNumber, ampfm.rpt_PatientDemographics.PatientAge, ampfm.rpt_PatientDemographics.PatientAgeGroup,

ampfm.rpt_PatientDemographics.PatientSex,

CASE WHEN PatientAgeGroup = '0 - 14' THEN '0' ELSE

CASE WHEN PatientAgeGroup = '15 - 24' THEN '15' ELSE

CASE WHEN PatientAgeGroup = '25 - 34' THEN '25' ELSE

CASE WHEN PatientAgeGroup = '35 - 44' THEN '35' ELSE

CASE WHEN PatientAgeGroup = '45 - 54' THEN '45' ELSE

CASE WHEN PatientAgeGroup = '55 - 64' THEN '55' ELSE

CASE WHEN PatientAgeGroup = '65 - 74' THEN '65' ELSE

CASE WHEN PatientAgeGroup = '75 - 79' THEN '75' ELSE

CASE WHEN PatientAgeGroup = '80 - OVER' THEN '80'

END END END END END END END END END AS PAGBeginningAge,

CASE WHEN PatientAgeGroup = '0 - 14' THEN '14' ELSE

CASE WHEN PatientAgeGroup = '15 - 24' THEN '24' ELSE

CASE WHEN PatientAgeGroup = '25 - 34' THEN '34' ELSE

CASE WHEN PatientAgeGroup = '35 - 44' THEN '44' ELSE

CASE WHEN PatientAgeGroup = '45 - 54' THEN '54' ELSE

CASE WHEN PatientAgeGroup = '55 - 64' THEN '64' ELSE

CASE WHEN PatientAgeGroup = '65 - 74' THEN '74' ELSE

CASE WHEN PatientAgeGroup = '75 - 79' THEN '79' ELSE

CASE WHEN PatientAgeGroup = '80 - OVER' THEN '200'

END END END END END END END END END AS PAGEndingAge

--WHERE PatientAge NOT BETWEEN PAGBeginningAge AND PAGEndingAge

FROM ampfm.dct_AdmitPriorityType INNER JOIN

ampfm.rpt_AdtVisit ON ampfm.dct_AdmitPriorityType.AdmitPriorityTypeCode = ampfm.rpt_AdtVisit.AdmitPriorityCode INNER JOIN

ampfm.rpt_PatientDemographics ON ampfm.rpt_AdtVisit.RegNum = ampfm.rpt_PatientDemographics.RegNum

Two things:

First, your WHERE statment needs to come after the last ON condition.

second, you are not allowed to alias the "column/expression list" of the SELECT clause like this in MS SQL Server as you can do from DB2; you will need to include the entire case statement in your WHERE clause.

You might be able to create an inline TVF to make this complicated CASE statement a little easier to read.

Maybe something like:

Code Snippet

alter function dbo.ageRange
( @.arg_ageGroup varchar(12)
)
returns table
as
return
( select cast(parsename(replace(@.arg_ageGroup,' - ','.'),2)
as int)
as lowPart,
cast( case when parsename(replace(@.arg_ageGroup,' - ','.'),1)
= 'over'
then '200'
else parsename(replace(@.arg_ageGroup,' - ','.'),1)
end as int)
as highPart
)

go

declare @.ageGroup table ( ageGroup varchar(9))
insert into @.ageGroup
select '0 - 14' union all
select '15 - 24' union all
select '25 - 34' union all
select '35 - 44' union all
select '45 - 54' union all
select '55 - 64' union all
select '65 - 74' union all
select '75 - OVER'

select ageGroup,
lowPart,
highPart
from @.ageGroup
cross apply ageRange(ageGroup)

Another idea would be to put your ranges in a table that included (1) the range, (2) the low and (3) the high of the range. Then you could use a CROSS APPLY to fetch the desired numeric LOW and HIGH of the range.

|||

I can't believe I had the WHERE statement before the FROM statement; boy this has been a long day.

I placed the entire case statement in the WHERE clause and everything is working fine now. Thanks!

BTW, I am not familiar with inline TVF. What is it or where could I read up on it?

|||

( I edited my previous response to include it; please take a look. )

|||

Thanks! I may try something like that on my next project.

sqlsql

Thursday, February 16, 2012

comment/uncomment keyboard shortcuts changed .. grrrrr!!!!

Call me old-school, but I happened to like the one-key Shift-Ctrl-C and Shift-Ctrl-R methods of commenting and uncommenting T-SQL code. I've tried to see how I can set up my own macro's to give me those key combos back instead of the double-the-work ctrl-K + ctrl-C and ctrl-K + ctrl-U keyboard short cuts.

The k-c one is the worst because it takes two hands to type it, but the two fingered k-u isn't much better.

Can someone tell me how I can override or augment the keyboard shortcuts so that I can get these two familiar short-cuts back. I don't care if I have to override something .. whatever it is, it's not nearly as imporant as this to me.In Management Studio goto Tools>Options and select Environment>Keyboard.

Change the Keyboard scheme to SQL Server 2000

--

HTH

Jasper Smith (SQL Server MVP)

http://www.sqldbatips.com

I support PASS - the definitive, global

community for SQL Server professionals -

http://www.sqlpass.org

wrote in message

news:c3b85d6a-9303-480e-877d-7bb4caf9cab6@.discussions.microsoft.com...

> Call me old-school, but I happened to like the one-key Shift-Ctrl-C and

> Shift-Ctrl-R methods of commenting and uncommenting T-SQL code. I've

> tried to see how I can set up my own macro's to give me those key combos

> back instead of the double-the-work ctrl-K + ctrl-C and ctrl-K + ctrl-U

> keyboard short cuts.

>

> The k-c one is the worst because it takes two hands to type it, but the

> two fingered k-u isn't much better.

>

> Can someone tell me how I can override or augment the keyboard shortcuts

> so that I can get these two familiar short-cuts back. I don't care if I

> have to override something .. whatever it is, it's not nearly as

> imporant as this to me.

>|||In Management Studio goto Tools>Options and select Environment>Keyboard. Change the Keyboard scheme to SQL Server 2000

Comment/Uncomment keyboard shortcut changed .. grrrrr!!!!

Call me old-school, but I happened to like the one-key Shift-Ctrl-C and Shift-Ctrl-R methods of commenting and uncommenting T-SQL code. I've tried to see how I can set up my own macro's to give me those key combos back instead of the double-the-work ctrl-K + ctrl-C and ctrl-K + ctrl-U keyboard short cuts.

The k-c one is the worst because it takes two hands to type it, but the two fingered k-u isn't much better.

Can someone tell me how I can override or augment these so that I can get these two familiar short-cuts back. I don't care if I have to override something .. whatever it is, it's not nearly as imporant as this to me.I'm getting used to just using the toolbar comment/un-comment.

Have to agree, there are a few 'little' things that are quite irritating...

I miss the line/column count in the status bar at the bottom. Made finding your errors fairly easy most of the time.

I miss the spinning globe on the dropdown Window list to see if a long running process is still running.

And the ctrl-b to move the resultpane was nice too. It's pretty hit or miss to get the mouse just right to move it now...

Nor can you scroll with a mouse wheel in the Execution Plan Results anymore.

Progress eh? :)|||Actually for Visual Studio developers the change is welcome/necessary because that is consistent with the VS environment. I have to say you have a point about the k-c combination though Smile|||In Management Studio you should be able to go to

Tools -> Customize -> Commands -> Keyboard, then pick Keyboard in Environment and change Keyboard Scheme to SQL Server 2000. You should get your shortcuts back.

HTH,
Boris.

p.s. I should add that you will NOT get ALL of your shortcuts back. Sorry!|||That worked.

You get a Big kiss(tm) for that little tid-bit!

X X
X
X X

:-)|||Yeap, ctrl-b still doesn't work, bummer. Thanks for the tidbit though.

It's nice to have the comment/uncomment back. :)|||

As in the VS2005 IDEs you can use Ctrl-K,C Ctrl-K,U to comment/uncomment source, that's an option, or not ? Smile

Sascha