Solved

sql query modify the layout

Posted on 2011-02-26
7
267 Views
Last Modified: 2012-06-27
I have a table that has the following date
rank1        rank2   rank3           ID
1      NULL      NULL      112
NULL      2      NULL      112
NULL      NULL      NULL      112

I want to write a query that returns

ID   Rank1 Rank2 Rank3
112  1         2            Null
0
Comment
Question by:Angela4eva
7 Comments
 
LVL 30

Expert Comment

by:Wayne Barron
ID: 34990252
I am not sure about how to get it all on a single row, will have to continue looking in on it.
But to get everything that is not null.

from table where rank1 is not null or rank2 is not null or rank3 is not null.

That will return all rows, but to do it as a single row, that I am unsure of?

Carrzkiss
0
 
LVL 11

Expert Comment

by:dougaug
ID: 34990342
See if this works for you:


select ID,
       (select Rank1 from table t2 where Rank1 is not null and t2.ID = t1.ID) as Rank1,
       (select Rank2 from table t3 where Rank2 is not null and t3.ID = t1.ID) as Rank2,
       (select Rank3 from table t4 where Rank3 is not null and t4.ID = t1.ID) as Rank3
  from table t1
0
 
LVL 22

Assisted Solution

by:Thomasian
Thomasian earned 250 total points
ID: 34990360
SELECT ID
      ,MAX(rank1) Rank1
      ,MAX(rank2) Rank2
      ,MAX(rank3) Rank3
FROM tablename
GROUP BY ID

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 30

Expert Comment

by:Wayne Barron
ID: 34990404
@Thomasian
Simple and easy.
Will have to remember this one!

Carrzkiss
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 34991432
did you want the max?
or something else

if you had
rank1        rank2   rank3           ID
1      NULL      NULL      112
NULL      2      NULL      112
NULL      NULL      NULL      112
3    null null 112

what would you want?

3 2 null 112  or something else
0
 
LVL 40

Accepted Solution

by:
Vadim Rapp earned 250 total points
ID: 34991635
select
nullif(0,max(isnulll(rank1,0))),
nullif(0,max(isnulll(rank2,0))),
nullif(0,max(isnulll(rank3,0)))

from mytable
group by id
0
 

Author Closing Comment

by:Angela4eva
ID: 34992451
thanks
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Restrict result set 1 34
Query Records that don't match 8 34
Using OPENQUERY in a SELECT statement 6 36
create insert script based on records in a table 4 14
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

912 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

21 Experts available now in Live!

Get 1:1 Help Now