Link to home
Start Free TrialLog in
Avatar of bgpro_no
bgpro_no

asked on

MSSQL, combining LIMIT override and join

Hi experts,

I have the following query wich is working well as an ovverride for the LIMIT used in MySQL:

SELECT * FROM (SELECT row_number() OVER (ORDER BY ID) AS rownum, Name, ID FROM TABLE) AS A WHERE A.rownum BETWEEN $offset AND ($offset+$limit) ORDER BY A.rownum ORDER BY $order_by

I need to join this query with another table (TABLE2) on TABLE.ID/TABLE2.ID and still keep the BETWEEN statement working for limit replacement.

The query needs to be valid for MSSQL2005.

Any ideas?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bgpro_no
bgpro_no

ASKER

Hi,

the query returns each result as many times as it occurs in the other table.

Tried a GROUP BY a.ID, but it throws the following error:

Column 'A.rownum' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

Any ideas?
>as many times as it occurs in the other table.
well, if there are many rows in the "other" table, which is the condition to determine which one of those rows to take ...

check out this article to see how to solve it, eventually:
https://www.experts-exchange.com/A_3203.html