dahund dahund - 5 months ago 8
SQL Question

Return two rows from SQL table with a difference in values

iam trying to return 2 rows from table that have a difference in values, not being an SQL wise man i am stuck any help would be appreciated :-)

TABLE A:

NAME DATA
Oscar HOME1
Jens HOME2
Will HOME1
Jeremy HOME2
Al HOME1


Result, should be 2 random rows with a difference in DATA value

NAME DATA
Oscar HOME1
Jeremy HOME2


Anyone?

Answer

Couuld be you need

select * from my_table a
inner join my_table b on a.data !=b.data
where a.data in ( SELECT data FROM my_table ORDER BY RAND() LIMIT 1);

For your code

SELECT * 
FROM [dbo].[ComputerState] as a
INNER JOIN [dbo].[ComputerState] as b ON a.ServiceName != b.ServiceName 
WHERE a.ServiceName IN ( 
     SELECT  top 1  [ServiceName] FROM [dbo].[ComputerState] 
);