Solved

Dynamicallyfilled column in select statement based on one of three fields

Posted on 2011-09-13
2
271 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
2 Comments
 
LVL 32

Accepted Solution

by:
ewangoya 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.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL 2012 Syntax Error 5 25
SQL Backup skipping a few tables 7 29
Runtime 3044 error 14 22
Join vs where 2 11
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now