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 2008 Forums
 Transact-SQL (2008)
 Greater than output as TRUE with Value from 2 SQL

Author  Topic 

best_boy26
Starting Member

42 Posts

Posted - 2011-03-07 : 12:49:11
I have Two table's called O3 and o5 and contains s.no and O3 & O5 in the Table (i.e..in O3 & O5). and s.no will be matches in both the tables. Now i want to run a query to get if the O3_Value (O3) value is more than O5_Value (O5) then i should get True as output in one column. And also in another coloumn the O5 Value should be displayed.


I am able to match the SQL Query however the Greater than O3 logic iam missing...:( need you help here...


SELECT dbo.O3.[s.no] AS [O3_Sn.NO], dbo.O5.[s.no] AS [O5_Sn.NO], dbo.O3.O3, dbo.O5.O5
FROM dbo.O3 INNER JOIN
dbo.O5 ON dbo.O3.[s.no] = dbo.O5.[s.no]


----o3 Table has below data
s.no o3
1 10
2 12
3 13
4 15
5 16

----05 Table has below Data

s.no o5
1 11
2 11
3 9
4 12
5 7

---------My Actual query should display as below output



s.no Flag O5 Value
1 TRUE 11
2 TRUE 11
3 FALSE 9
4 FALSE 12
5 FALSE 7



My SQL query putput should be like as below

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-03-07 : 13:12:43
Should just be able to add a CASE statement to do that logic:
SELECT 
O3.[s.no]
,CASE WHEN O3.o3 > O5.o5 THEN 'TRUE' ELSE 'FASLE' END AS Flag
,O5.o5 AS Value
FROM
O3
INNER JOIN
O5
ON O3.[s.no] = O5.[s.no]
Go to Top of Page
   

- Advertisement -