Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Calculated column based on case statement result

Author  Topic 

wembleybear
Yak Posting Veteran

93 Posts

Posted - 2013-12-10 : 12:13:00
I'm new to SQL Server and would like to add a calculated column to this query from the report writer in our ERP system based on the NextFreq case statement result.

Basically, I want to create a column called service with result as follows:

If IV.meter > NextFreq then the result should be 'OVERDUE'
If (NextFreq - IV.meter) <50 then the result should be 'DUE SOON'
Otherwise the result should be 'NOT DUE'


This is the code from the current report writer query:


Select IV.item, IV.meter, isnull(wt.name,0)as name,  case when whh.meterstop is null then 0 end meterstop, whh.rejected, Case when cast(meterstop as int)  > 0 then cast(meterstop as int) when meterstop is null then isnull(IV.meter,0) else isnull(IV.meter,0) end EndMeter, ISNULL(CAST(SUBSTRING(wt.name,1,4)as int),0) as LastFreq,
case when whh.rejected = 1 then ISNULL(CAST(SUBSTRING(wt.name,1,4)as int),0) when ISNULL(CAST(SUBSTRING(wt.name,1,4)as int),0) = 0 then 100 when ISNULL(CAST(SUBSTRING(wt.name,1,4)as int),0) = 100 then 500 else ISNULL(CAST(SUBSTRING(wt.name,1,4)as int),0)+500 end NextFreq

From INVENTORY IV Left Outer Join
(Select worksorderhdr.itemcode, Max(worksorderhdr.worktype) WorkType,
Max(worksorderhdr.date_created) LastService, Max(worksorderhdr.worknumber)
DN
From worksorderhdr
Where worksorderhdr.worktype In (10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 32)
Group By worksorderhdr.itemcode) whi On whi.itemcode = IV.item
Left Outer Join
worksordertypes wt On whi.WorkType = wt.id Left Outer Join
worksorderhdr whh On whi.DN = whh.worknumber Left Outer Join
sched_maintenance crt On crt.code = IV.item And crt.description Like 'CERT%'
And crt.next_calendar_date Is Not Null Left Outer Join
(Select sm.code, Max(Case
When sm.last_maint Like '[0-9]%' Then Abs(sm.last_maint) Else 0
End) LastM, Max(sm.description) Descript
From sched_maintenance sm
Where sm.description Like 'Z%'
Group By sm.code
Having Max(sm.last_maint) Is Not Null And Max(sm.last_maint) Not Like 'DN%')
lm On lm.code = IV.item Left Outer Join
(Select Max(dh.sname) Sname, Max(dh.saddr1) Saddr1, Max(dh.saddr2) Saddr2,
Max(dh.scity) Town, Max(dh.szip) SPost, di.item, dh.dticket Contract
From deltickitem di Inner Join
deltickhdr dh On di.dticket = dh.dticket
Where di.stage = 4
Group By di.item, dh.dticket) sd On sd.item = IV.item And
IV.itemised_status In (32, 64) Left Join
inventory IR On IV.item = IR.mfg
Where IV.itemised_status > 15 and (IR.descr is null or IR.descr like 'Appell%')
Order By IV.item


Any help at all would be appreciated.

Many thanks
Martyn

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2013-12-10 : 12:19:37
CASE WHEN IV.METER > NextFreq THEN 'OVERDUE' WHEN NextFreq-IV.meter <50 THEN 'DUE SOON' ELSE 'NOT DUE' END

--------------------------
http://connectsql.blogspot.com/
Go to Top of Page

wembleybear
Yak Posting Veteran

93 Posts

Posted - 2013-12-10 : 15:14:23
Thanks, I tried your suggestion but I get an error as it doesn't recognize NextFreq:

Msg 207, Level 16, State 1, Line 3
Invalid column name 'NextFreq'.
Msg 207, Level 16, State 1, Line 3
Invalid column name 'NextFreq'.


Not sure how it needs modifying to take account of that?

Many thanks
Martyn
Go to Top of Page

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2013-12-10 : 15:17:31
I thought its a column from one of your tables used in query. If its calculated, then go for subquery or CTE.

--------------------------
http://connectsql.blogspot.com/
Go to Top of Page

wembleybear
Yak Posting Veteran

93 Posts

Posted - 2013-12-11 : 05:15:54
Thanks - I put in a CTE and it works perfectly.

Many thanks for your help.
Martyn
Go to Top of Page
   

- Advertisement -