I have a query that returns a row of data for each country as well as the other related information. The query (with everything not relevant removed) looks like so:
SELECT a.*, ROW_NUMBER() OVER(ORDER BY a.DateSubmitted) As RowNumber, c.CountryName FROM Table1 a
inner join Table2 b
on a.id = b.id
inner join Table3 c on c.countrycode = b.countrycode
This returns the row number for each row like so:
ID User Country RowNumber
8 testtest BANGLADESH 1
14 testtest ANGOLA 2
14 testtest AUSTRALIA 3
14 testtest BANGLADESH 4
15 testera1 BELIZE 5
15 testera1 CONGO 6
However, I need to return a 0 or 1 for each unique ID and not for each row in the main statement I already have. So what I would like to have is the following:
ID User Country RowNumber
8 testtest BANGLADESH 0
14 testtest ANGOLA 1
14 testtest AUSTRALIA 1
14 testtest BANGLADESH 1
15 testera1 BELIZE 0
15 testera1 CONGO 0
I’m pretty weak at SQL and I’m finally making a conscious attempt to improve it, so thank you very much for your help.
DENSE_RANK() can be used to give each
ida different value, but with all occurances of the sameidhaving the same value. Do% 2to any number and you get0or1…