Solved

t-sql query

Posted on 2014-02-28
5
305 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 VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

749 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