0

I am trying to create a view in SQL Server 2012 that excludes columns where the entry is null (represent by 0 here so it's easier to read). My base data is this

╔════╦══════╦══════╦══════╦══════╦══════╗
║ ID ║ Col1 ║ Col2 ║ Col3 ║ Col4 ║ Col5 ║
╠════╬══════╬══════╬══════╬══════╬══════╣
║  1 ║    1 ║    0 ║    0 ║    0 ║    5 ║
║  2 ║    1 ║    2 ║    3 ║    0 ║    5 ║
║  3 ║    0 ║    0 ║    0 ║    0 ║    0 ║
║  4 ║    0 ║    2 ║    3 ║    0 ║    0 ║
╚════╩══════╩══════╩══════╩══════╩══════╝

What I would like to return would be

╔════╦══════╦══════╦══════╦══════╗
║ ID ║ Res1 ║ Res2 ║ Res3 ║ Res4 ║
╠════╬══════╬══════╬══════╬══════╣
║  1 ║    1 ║    5 ║    0 ║    0 ║
║  2 ║    1 ║    2 ║    3 ║    5 ║
║  3 ║    0 ║    0 ║    0 ║    0 ║
║  4 ║    2 ║    3 ║    0 ║    0 ║
╚════╩══════╩══════╩══════╩══════╝

In this case, since column 4 had value 0 in all entries it was not included as a result. ID1 returned 1 and 5 skipping the 0s, and had 0s to fill since ID2 had 4 columns. If all values were 0, this would either just return Res1 with 0 or no columns and just the IDs.

Hope this is clear. I'm having trouble explaining it.

2
  • Why is there a 6 in row ID 4 col Res3? Commented Nov 16, 2015 at 19:57
  • Because typing hurts. Commented Nov 16, 2015 at 20:02

4 Answers 4

2

You cannot really do what you want to do. Queries, views, and user-defined functions returns a specific set of columns. The set of columns is defined in advance. So, you cannot remove them.

You could create a dynamic query that only included columns that are currently not NULL. Or, you could create an XML data structure with the columns you want. But, the columns in a view are fixed when the view is created, and cannot be added and removed when the view is run.

Sign up to request clarification or add additional context in comments.

Comments

0

As Gordon Linoff told, you could potentially do this using Dynamic SQL, however - I wouldn't.

Handle with this logic in your application code, not database. SQL Server is not supposed to do these taks well. Code's going to be hard to maintain.

Comments

0

Well, it's possible, but the query gets some really complicated expressions.

The first column is quite easy:

Res1 = coalesce(Col1, Col2, Col3, Col4, Col5, 0),

The second column gets more complicated, as it has to determine where the first used value were to get the second value:

Res2 = case
    when Col1 is not null then coalesce(Col2, Col3, Col4, Col5, 0)
    when Col2 is not null then coalesce(Col3, Col4, Col5, 0)
    when Col3 is not null then coalesce(Col4, Col5, 0)
    when Col4 is not null then coalesce(Col5, 0)
    else 0
  end,

From there it gets even more complicated. The next value has to determine where the first two used values were. I'm not even going to try to write that.

Comments

0

If you drop the requirement where the result excludes columns where it is all null, you can do this with an UNPIVOT and then a PIVOT.

1 Comment

Yea, I looked into that but I end up having 60ish rows and only 2 have something that needs to be seen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.