SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need help with SQL select query.


Need help with SQL select query.

Author
Message
john_sidney
john_sidney
Valued Member
Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)

Group: General Forum Members
Points: 54 Visits: 14
Hi,

I'm a bit stuck with how I best approach this so thought I'd run it by the experts.

I have a table where I'm expecting around 1 or 2 million rows. In that table, there are 20 columns of interest. Within those 20 columns, their value will be either a decimal value or a null.

I'm using Python to access the data and within Python, there's a function that gets a list of the returned columns. So, if you did 'select * from table', you could say get_returned_columns.

So what I want to do is return only the columns that have a decimal value (or not NULL). A subset of my table could look like this:

col1, col2, col3, col4, col5
--------------------------------------------------
1.1, NULL, 2.4, 1.2, NULL
NULL, NULL, 22.0, NULL, NULL
NULL, NULL, NULL, 1.1, NULL

I don't even know where to begin so I'm going to try this in plain english:

"Select only the columns where the values have at least one decimal value (or NOT NULL)"

My results would look like this (col2 and col5 contain only NULL):

col1, col3, col4
----------------------------
1.1, 2.4, 1.2
NULL, 22.0, NULL
NULL, NULL, 1.1

Hope that makes sense? I'm not very good with SQL so please ask if you need further info.

Cheers,
J
John Mitchell-245523
John Mitchell-245523
SSC Guru
SSC Guru (131K reputation)SSC Guru (131K reputation)SSC Guru (131K reputation)SSC Guru (131K reputation)SSC Guru (131K reputation)SSC Guru (131K reputation)SSC Guru (131K reputation)SSC Guru (131K reputation)

Group: General Forum Members
Points: 131911 Visits: 19201
You can't do that easily. You'd need one query to determine which columns contain values, and a second query to select only those columns. Maybe some sort of dynamic pivot would work, but I haven't really thought that through (it would be cumbersome). Or you could just return everything and have your presentation layer choose which ones to display.

John
Thom A
Thom A
SSC Guru
SSC Guru (81K reputation)SSC Guru (81K reputation)SSC Guru (81K reputation)SSC Guru (81K reputation)SSC Guru (81K reputation)SSC Guru (81K reputation)SSC Guru (81K reputation)SSC Guru (81K reputation)

Group: General Forum Members
Points: 81986 Visits: 20952
This would much more easily achieved using something like SSRS. You can put an expression on the column then, and hide it if the count of the values in the column are 0. Would be something like:
=Iif(Count(Col2.Value) = 0, TRUE, FALSE)




Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does :-P

Please always remember to encapsulate your code in IFCode Markup. For example [code=sql] [/code].
Click here to read Jeffs Guide on how to post SQL questions, and get swift and helpful answers from the community
john_sidney
john_sidney
Valued Member
Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)

Group: General Forum Members
Points: 54 Visits: 14
Thanks to you both!

@John - I was afraid of that. I was hoping there was a one-query solution but not the end of the world. Was trying to be flash Smile

@Thom - I'm going to look into that as it does sound like a very good solution. Suspected I'd have to revert back to design but all for the good.

Thanks both! Appreciate the quick reply.

Cheers,
J
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search