Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Below is Sql ResultSet Table

Dates         Col1       Col2       Col3

17-2-2017      0          1          0  
18-2-2017      1          1          0
19-2-2017      1          1          0



I have get result like this below

Dates        col1     col2

17-2-2017     0         1
18-2-2017     1         1
19-2-2017     1         1


What I have tried:

SQL
SELECT p.*
FROM Data p 
INNER JOIN (SELECT A.Date 
            FROM (SELECT p.Date,p.Col1,p.Col2 
 ) As A
            GROUP BY A.Date 
            HAVING COUNT(*) > 1
Posted
Updated 17-Feb-17 19:04pm
v2
Comments
OriginalGriff 17-Feb-17 8:36am    
And?
What did you want to get?
Tomas Takac 17-Feb-17 9:20am    
So you want to exclude Col3 because there are all zeroes? Why? I'm asking that SQL is good at filtering data row-wise but not column-wise. I guess it can be done - just make sure it's worth the effort. Seems like a very odd requirement to me.
Bryian Tan 17-Feb-17 10:54am    
Can you provide some sample output that you would expect to see?
Karthik_Mahalingam 17-Feb-17 23:05pm    
he just wanted to remove the col3, since it has all zeros
Member 12085957 18-Feb-17 0:22am    
Yes

1 solution

This is not a perfect solution, but it works

Create a temp table and copy the actual data to the temp table, check if the column has all zeros, if so then drop the column from temp table

if exists (     select  * from tempdb.dbo.sysobjects o    where o.xtype in ('U')    and o.id = object_id(N'tempdb..#tbltemp'))
DROP TABLE #tbltemp; 
CREATE TABLE #tbltemp( Dates varchar(9),col1 int, col2 int, col3 int)
insert   into #tbltemp (dates,col1,col2,col3     )   select  dates,col1,col2,col3 from Table1 ;
 
 
 if not exists ( select * from #tbltemp where col3 <> 0 )
 ALTER TABLE #tbltemp DROP COLUMN col3
 if not exists ( select * from #tbltemp where col2 <> 0 )
 ALTER TABLE #tbltemp DROP COLUMN col2
 if not exists ( select * from #tbltemp where col1 <> 0 )
 ALTER TABLE #tbltemp DROP COLUMN col1

 

  select * from #tbltemp
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900