Solved

Dynamicallyfilled column in select statement based on one of three fields

Posted on 2011-09-13
2
283 Views
Last Modified: 2012-06-21
I have a query that is almost exactly what I need, however I need to add a Column to the end where I have the dummy field of active account to display the account the has '_Sys' from which ever of the OACT (T1, T2, or T3) AcctCode fields that matches that criteria.

Any help would be greatly appreciated.

* Only 1 of the fields will ever contain the '_Sys' code.

Best of luck,
John-Michael
(Select T0.AcctCode, T0.AcctName, T0.Levels, T0.GrpLine, T0.FatherNum, T0.GroupMask, 
T1.AcctCode, T1.GroupMask, 
T2.AcctCode, T2.GroupMask,
T3.AcctCode, T3.GroupMask, T3.FormatCode,
'Active Account' as 'Active Account'

from OACT T0 
Left Outer Join OACT T1 on T1.FatherNum = T0.AcctCode
Left Outer Join OACT T2 on T2.FatherNum = T1.AcctCode
Left Outer Join OACT T3 on T3.FatherNum = T2.AcctCode
Where T0.Levels = 1)



Order By T0.GroupMask

Open in new window

0
Comment
Question by:John-Michael_Davis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 250 total points
ID: 36533193
use case
(Select T0.AcctCode, T0.AcctName, T0.Levels, T0.GrpLine, T0.FatherNum, T0.GroupMask, 
T1.AcctCode, T1.GroupMask, 
T2.AcctCode, T2.GroupMask,
T3.AcctCode, T3.GroupMask, T3.FormatCode,
case 
  when T1.AcctCode = '_SYS' then
    T1.AcctCode
  when T2.AcctCode = '_SYS' then
    T2.AcctCode  
  when T3.AcctCode = '_SYS' then
    T3.AcctCode  
  else
    ''
 end [Active Account]

from OACT T0 
Left Outer Join OACT T1 on T1.FatherNum = T0.AcctCode
Left Outer Join OACT T2 on T2.FatherNum = T1.AcctCode
Left Outer Join OACT T3 on T3.FatherNum = T2.AcctCode
Where T0.Levels = 1)



Order By T0.GroupMask

Open in new window

0
 
LVL 1

Author Closing Comment

by:John-Michael_Davis
ID: 36533271
Excellent solution, worked like a charm.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question