Solved

t-sql query

Posted on 2014-02-28
5
292 Views
Last Modified: 2014-03-01
I have a table sql server 2008 table called MyTestTable1

Here is my table and script:
http://sqlfiddle.com/#!3/78d82/2

So my table looks like this:
my table
I need to write a query that does the following:

Look at columns MyCol1, MyCol2, MyCol3 and whichever of those columns does not equal to 0, use the value for the column from the detail column.
So in this case the column called MyCol3 doesn't not equal to zero. So then MyCol3 is the column with a value , so now instead of showing 6 I want to display the value in the detail column.

So my result set should be this:
desired result set
So for example if MyCol2 had been the one with the value of 6 then my output result would have been this:

test 2 of desired result
I started to write the query like this:

SELECT OrderId
             ,Case
         When ([MyCol1] <> 0) THEN [MyCol1Detail]
         When ([MyCol2] <> 0) THEN [MyCol2Detail]
         When ([MyCol3] <> 0) THEN [MyCol3Detail]
        ELSE End
FROM [MyTestTable1]


But I'm not sure I have the syntax correct or how to say name the resulting column whatever the name of the column with a value is?

can anyone help?
0
Comment
Question by:maqskywalker
  • 3
  • 2
5 Comments
 
LVL 35

Expert Comment

by:David Todd
ID: 39896440
Hi,

That query looks like it will work - apart from the else in the case. take the word else out.

HTH
  David
0
 
LVL 1

Author Comment

by:maqskywalker
ID: 39896443
Updating my question...

So i have something close to what I need.

This is my query ( http://sqlfiddle.com/#!3/78d82/4 ):

SELECT OrderID
      ,Case
             When ([MyCol1] <> 0) THEN [MyCol1Detail]
             When ([MyCol2] <> 0) THEN [MyCol2Detail]
             When ([MyCol3] <> 0) THEN [MyCol3Detail]
       End
FROM [MyTestTable1]
 
This gives me this

my current result
How do I add give the column name , the same name as the column that had a value.
So the name will either be MyCol1, MyCol2,  or MyCol3.

Since my table has value of 6 in MyCol3 the name of my output table show be called MyCol3
0
 
LVL 35

Expert Comment

by:David Todd
ID: 39896448
Hi,

SELECT OrderID
      ,Case
             When ([MyCol1] <> 0) THEN [MyCol1Detail]
             When ([MyCol2] <> 0) THEN [MyCol2Detail]
             When ([MyCol3] <> 0) THEN [MyCol3Detail]
       End as Detail
      ,Case
             When ([MyCol1] <> 0) THEN 'MyCol1'
             When ([MyCol2] <> 0) THEN 'MyCol2'
             When ([MyCol3] <> 0) THEN 'MyCol3'
       End as WhichColumn
FROM [MyTestTable1]

Regards
  David
0
 
LVL 1

Author Comment

by:maqskywalker
ID: 39896464
David,

 in the the first case statement
Can I replace End As Detail

with End AS ( Case
             When ([MyCol1] <> 0) THEN 'MyCol1'
             When ([MyCol2] <> 0) THEN 'MyCol2'
             When ([MyCol3] <> 0) THEN 'MyCol3'
       End)

Because I think that's what I need?
0
 
LVL 35

Accepted Solution

by:
David Todd earned 500 total points
ID: 39896608
Hi,

Nope. What you are trying to do wont work.

It took me a while to think about this: What in effect you are trying to do is _rename_ the column depending on what the value on that row is. Can't do that. We can dynamically name the column for things like pivot tables if you're willing to live with some horrible looking code.

Or if you really want the columns to follow the sort of value then its really simple

select * from table

HTH
  David
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

In this article I will describe the Backup & Restore 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.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

759 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