Solved

sql query modify the layout

Posted on 2011-02-26
7
257 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
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…

760 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

20 Experts available now in Live!

Get 1:1 Help Now