0

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?

3
  • Even better if the code didnt require static entries like HOME1 <> HOME2
    – dahund
    Commented Jun 21, 2016 at 19:52
  • What is your rdbms? Sql Server, Postgres, Oracle? Commented Jun 21, 2016 at 20:00
  • Should this question belong on code-golf? codegolf.stackexchange.com
    – ebyrob
    Commented Jun 21, 2016 at 20:07

4 Answers 4

2

Easy way to have random data.

;with tblA as (
select name,data,
row_number() over(partition by data order by newid()) rn
from A
)
select name,data
from tblA
where rn = 1
8
  • Wouldn't it be where rn <= 2 as the OP wants 2 rows (or IN (1,2))?
    – Igor
    Commented Jun 21, 2016 at 19:58
  • @Igor No. See partition by Commented Jun 21, 2016 at 19:59
  • This assume OP has Sql Server. Commented Jun 21, 2016 at 20:01
  • Brilliant, thanks. @JuanCarlosOropeza - IMO you have to assume something as the OP gives very little information. If the OP wanted a server specific solution they should have included that in their question.
    – Igor
    Commented Jun 21, 2016 at 20:03
  • @Igor I may think the other way, general rule if you assume you are probably wrong. So dont waste your time asking the wrong question. Is better ask for details. Is obvious OP is new user so teach him how make a better question instead. Commented Jun 21, 2016 at 20:07
0

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] 
);
17
  • how does this return 2 random rows? Commented Jun 21, 2016 at 19:53
  • @devlincarnate correct .. i have update the answer with random data
    – ScaisEdge
    Commented Jun 21, 2016 at 19:56
  • This returns one row with 2 sets of values in it. The question asks for the values in two separate rows.
    – ebyrob
    Commented Jun 21, 2016 at 19:59
  • BY "inner join table b" you mean "inner join table a" as it is same table=
    – dahund
    Commented Jun 21, 2016 at 19:59
  • by inner join b i mean the inner join with the same table .. exactly .. in sql the tables are tables .. same or not same are tables .. and you can use them like you explicit in the condition ..
    – ScaisEdge
    Commented Jun 21, 2016 at 20:02
0

If the question is really this simple, you can use an aggregate such as MAX() or MIN() to grab one row for each different DATA:

SELECT   MAX(NAME), DATA
FROM     TABLE_A
GROUP BY DATA

Of course, if any other variables are introduced to the requirements, this may no longer work.

4
  • 1
    how is this random? Commented Jun 21, 2016 at 19:51
  • @devlincarnate Good point, I read the top part with a focus on simply having different DATA values. If it has to be truly random then this won't work. Commented Jun 21, 2016 at 19:52
  • Column 'DATA' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. you know what this is?
    – dahund
    Commented Jun 21, 2016 at 20:15
  • @dahund DATA is right there in: GROUP BY DATA
    – ebyrob
    Commented Jun 21, 2016 at 20:16
0
;WITH cteA AS (
    SELECT
       name
       ,data
       ,ROW_NUMBER() OVER (PARTITION BY data ORDER BY NEWID()) as DataRowNumber
       ,ROW_NUMBER() OVER (PARTITION BY 1 ORDER BY NEWID()) as RandomRowNumber
    FROM
       A
)

SELECT *
FROM
    cteA
WHERe
    DataRowNumber = 1
    AND RandomRowNumber <= 2

This Expands on @AlexKudryashev 's answer a little.

;with tblA as (
select name,data,
row_number() over(partition by data order by newid()) rn
from A
)
select name,data
from tblA
where rn = 1

The only issue with what he had Is that the number of Rows where rn = 1 will be depended on the COUNT(DISTINCT data) so it could lead to more than 2 results. To fix one could add a SELECT TOP 2 clause but it might not be fully random as results at that point as it will be dependent on the ordinal results of how SQL optimizes the query which is likely to be consistent. To get truly random add a second random row number and limit the results to the top 2 of those.

Not the answer you're looking for? Browse other questions tagged or ask your own question.