Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Select columns based on filter? *SOLVED*

Author  Topic 

steamngn
Constraint Violating Yak Guru

306 Posts

Posted - 2005-05-11 : 11:28:33
I get asked some of the damndest things:
We have an internal website here, and one of the web pages returns table info about machinery tests. The table is pretty simple, with a clustered key on columns WRKDATE and MECHANIC, and 4 additional columns test1,test2,test3,test4. each of the testx columns get a machine id number in it when a test is done, ie. if test #1 is done on machine 50 today, then WRKDATE is set to today,MECHANIC is 'Andy', and test1 is '50'. Pretty simple stuff.
NOW what the office is asking for is this:
If we do a select on this table like:
select * from machtest
where test1='50' or test2='50' or test3='50' or test4='50'

can we get it to return WRKDATE,MECHANIC,and just test1, as this is the only column where this machine has been tested? What they are asking for it see ONLY the colums that have the filter data in it. At first I told them to go away, but now my curiosity is starting to kick in (more coffee,scotty!)...
Is this something that is possible? Perhaps through a proc or something instead of a select query? Or would/could something like this be handled at presentation

There's never enough time to type code right,
but always enough time for a hotfix...


**UPDATE**
I got it.
Created a procedure that builds the SQL query based on a subquery of each column. This way the column only appears if the requested data is actually in it.
Thanks again!
Andy
   

- Advertisement -