SQL select syntax with embedded logic

Posted on 2007-07-30
Last Modified: 2010-03-19
i need to select from two tables. both tables can be joined on id (  =

              if t2.columnA = 1 then (
                                                       1 as abc-column-name1
                                                        t2.columnX1 as abc-column1
               if t2.columnA = 2 then (
                                                         2 as abc-column-name2
                                                         t2.columnX2 as abc-column2
from table1 t1, table2 t2
where =
Question by:jgordin
    LVL 92

    Accepted Solution

    SELECT t2.ColumnA AS [abc-column-mame1],
        (CASE WHEN t2.ColumnA = 1 THEN t2.columnX1 ELSE t2.columnX2 END) AS  [abc-column-mame2],
        t1.ColumnA, t2.ColumnB
    from table1 t1, table2 t2
    where =
    LVL 58

    Expert Comment

    In your example, you attempt to create columns with variable names. SQL isn't meant for that and only allows it in cross-tabs (this doesn't fit your scenario, however).

    Instead, manipulate only your data, for example as Patrick showed above, into fixed column names. You can then rename or hide/show these columns further down in the interface. But not directly in the query.


    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Need help creating a stored procedure 4 48
    multiple SELECT statements in a function 4 31
    SQL Update Query 23 68
    report c# 9 59
    As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
    In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    754 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

    19 Experts available now in Live!

    Get 1:1 Help Now