| Author |
Topic |
|
rahul8346
Starting Member
21 Posts |
Posted - 2006-05-04 : 06:28:18
|
| hi guys, I am having a table tblNumber(id int,Number int). I wish to get the positive and negative number from the column "Number" to be displayed as two different column as Positive and Negative number. I am trying this code1) It is giving null values in some cell SELECT CASE WHEN number > 0 THEN num END as positive, CASE WHEN number < 0 THEN num END as neagtive FROM tblNumber2) It is giving duplicate data select a.num as [positive], b.num as [negative] from tblNumber a,tblNumber b where a.num >=0 and b.num<0 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-05-04 : 07:14:13
|
[code]select (select number from tblnumber where number>0) as positive, (select number from tblnumber where number<0) as negative[/code]Go with the flow & have fun! Else fight the flow Blog thingie: [URL="http://weblogs.sqlteam.com/mladenp"] |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-05-04 : 07:16:29
|
| 1)SELECT CASE WHEN number > 0 THEN num ELSE ... some value here ...END as positive,CASE WHEN number < 0 THEN num ELSE ... some value here ...END as neagtiveFROM tblNumber2) You have no JOIN between A and B, so every row in A will be matched with every row in B !!This might do the trickselect a.num as [positive], NULL as [negative]from tblNumber awhere a.num >=0UNION ALLselect NULL as [positive], b.num as [negative]from tblNumber bwhere b.num<0Kristen |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-05-04 : 07:58:29
|
quote: Originally posted by spirit1
select (select number from tblnumber where number>0) as positive, (select number from tblnumber where number<0) as negative Go with the flow & have fun! Else fight the flow Blog thingie: [URL="http://weblogs.sqlteam.com/mladenp"]
That will lead to error if the subquery returns more than one value MadhivananFailing to plan is Planning to fail |
 |
|
|
rahul8346
Starting Member
21 Posts |
Posted - 2006-05-04 : 08:56:31
|
| hi txs for the reply,but for the first stated query it gives error1)select (select num from tblnumber where num>0) as positive,(select num from tblnumber where num<0) as negativeError:Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.2) This one works fine but the same problem remains, it fills Null values in the Cells.select a.num as [positive], NULL as [negative]from tblNumber awhere a.num >=0UNION ALLselect NULL as [positive], b.num as [negative]from tblNumber bwhere b.num<0It should give output aspositive | Negative---------------------12 -945 -5 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-05-04 : 08:59:29
|
| Can you post some sample data and the result you want?When it is NULL do you want to show them as 0?MadhivananFailing to plan is Planning to fail |
 |
|
|
jen
Master Smack Fu Yak Hacker
4110 Posts |
Posted - 2006-05-04 : 09:01:42
|
try to read coalesce in Books OnLine to avoid null values and incorporate that in your query... it's just a presentation problem --------------------keeping it simple... |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-05-04 : 09:26:30
|
| "This one works fine but the same problem remains, it fills Null values in the Cells"Then use my other one with the ELSE ... some value here ... and put an appropriate value in for rows that do not have a matching value. Would zero do?Kristen |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-04 : 09:47:23
|
quote: I am having a table tblNumber(id int,Number int)It should give output aspositive | Negative---------------------12 -945 -5
I think this is one of those strange requirement that just want 2 sets of unrelated value to appear side by side.Poster's data is probablyid Number1 122 -93 454 -5 And Poster wanted 12 and -9 on the same row. I have seen couple of questions like this lately. KH |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-05-04 : 10:00:18
|
[code]declare @numbers table (number int )insert @numbers select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9 union all select -1 union all select -2 union all select -3 union all select -4 union all select -5 union all select -6select identity(int,1,1) as id, numberinto #tempPos from @numberswhere number > 0select identity(int,1,1) as id, numberinto #tempNeg from @numberswhere number < 0select t1.number as positve, t2.number as negative from #tempPos t1 left join #tempNeg t2 on t1.id = t2.iddrop table #tempPos, #tempNeg[/code]Go with the flow & have fun! Else fight the flow Blog thingie: [URL="http://weblogs.sqlteam.com/mladenp"] |
 |
|
|
rahul8346
Starting Member
21 Posts |
Posted - 2006-05-05 : 06:07:47
|
| Actually, KHtan have understood me right, and i wanted to perform this task in a single query without using any temporary table.AS Khtan have mentioned-------------------------------------------------------------------------I think this is one of those strange requirement that just want 2 sets of unrelated value to appear side by side.Poster's data is probablyid Number1 122 -93 454 -5And Poster wanted 12 and -9 on the same row. I have seen couple of questions like this lately. |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-05-05 : 06:12:14
|
nope ... not possible in a single query... you could use a function.and how would you tell it which number comes next to which?and this is probably a front end issue. return 2 recordsets one with positive and one with negative numbers and show them in a way you want.Go with the flow & have fun! Else fight the flow Blog thingie: [URL="http://weblogs.sqlteam.com/mladenp"] |
 |
|
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2006-05-05 : 08:29:53
|
| I think this can be done in a single query, but the poster will need to tell us how to associate two different records so they appear on the same output row. |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-05-05 : 08:40:00
|
ok if you have an association i agree... but without one... i don't think there is a way, is there?because the whole point of the temp tables and identites is to enforce an association.Go with the flow & have fun! Else fight the flow Blog thingie: [URL="http://weblogs.sqlteam.com/mladenp"] |
 |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2006-05-05 : 08:59:30
|
I think we're going to find it's something like this (SQL Server 2005):SELECT N.number, P.numberFROM ( SELECT ROWNUMBER() OVER (ORDER BY id) AS row, number FROM tblNumber WHERE number < 0 ) AS NFULL OUTER JOIN ( SELECT ROWNUMBER() OVER (ORDER BY id) AS row, number FROM tblNumber WHERE number > 0 ) AS P ON N.row = P.rowORDER BY COALESCE(N.row, P.row) |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-05-06 : 05:06:01
|
How about this??Declare @Tbl Table (ID char(1), Num int )Insert @Tbl Select 'a', 12 Union All Select 'b', -9 Union All Select 'c', 45 Union All Select 'd', -5 Select A.Num,b.Num From (Select (Select Count(1) From @Tbl a Where a.[Num] <= b.[Num] And a.Num<0 ) as RowNum ,b.Num From @Tbl b Where b.Num<0 ) as A Inner Join (Select (Select Count(1) From @Tbl a Where a.[Num] <= b.[Num] And a.Num>0) as RowNum ,b.Num From @Tbl b Where b.Num>0 ) as B On A.RowNum = b.RowNum If Debugging is the process of removing Bugs then i Guess programming should be process of Adding them. |
 |
|
|
rahul8346
Starting Member
21 Posts |
Posted - 2006-05-08 : 08:57:25
|
| Thank You...Guys. You all are certainly Awesome.Thanks for the Help.I am sure i will never get scared of SQL until this Team Exist.Txs chiragkhabaria, I just works Fine...with the only exception that it will not work for Duplicates records. Txs A Lot |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-05-08 : 13:35:16
|
| yeah it will not work for the duplicate records... there has to some some uniqure idenity in the table..If Debugging is the process of removing Bugs then i Guess programming should be process of Adding them. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-05-09 : 01:31:25
|
quote: Originally posted by chiragkhabaria How about this??Declare @Tbl Table (ID char(1), Num int )Insert @Tbl Select 'a', 12 Union All Select 'b', -9 Union All Select 'c', 45 Union All Select 'd', -5 Select A.Num,b.Num From (Select (Select Count(1) From @Tbl a Where a.[Num] <= b.[Num] And a.Num<0 ) as RowNum ,b.Num From @Tbl b Where b.Num<0 ) as A Inner Join (Select (Select Count(1) From @Tbl a Where a.[Num] <= b.[Num] And a.Num>0) as RowNum ,b.Num From @Tbl b Where b.Num>0 ) as B On A.RowNum = b.RowNum If Debugging is the process of removing Bugs then i Guess programming should be process of Adding them.
Not advisible if the table has thousands of recordsMadhivananFailing to plan is Planning to fail |
 |
|
|
|