With reference to my previous question
http://www.c-sharpcorner.com/Forums/get-sql-data-last-to-first
I want to use features like pagination in sql server (as offset and fetch does in my previous question)
I have 2 tables
1. Purchase Table (contains id (identity column), invoiceNo, Date, NetAmt, vendorID, vendorName) (m mentioning some columns, as there are more columns)
2. Vendor Table (id (identity column), vendorID, vendorName)
Want to get distinct invoiceNo from purchase table
data to be fetch based on vendorID
I wrote my basic query like (which works fine)
- select distinct p.InvoiceNo, p.Date, p.NetAmt as Amount, vd.VendorName from Purchase p
- inner join VendorDetails vd on p.VendorID = vd.VendorId
Now adding pagination like functionality (getting rows from last to first) using offset and fetch
(till now it works fine)
- select p.InvoiceNo, p.Date, p.NetAmt as Amount, vd.VendorName from Purchase p
- inner join VendorDetails vd on p.VendorID = vd.VendorId
- order by p.Id desc offset 0 rows fetch next 10 rows only
But I want distinct p.InvoiceNo here with pagination functionality)
- select distinct p.InvoiceNo, p.Date, p.NetAmt as Amount, vd.VendorName from Purchase p
- inner join VendorDetails vd on p.VendorID = vd.VendorId
- order by p.Id desc offset 0 rows fetch next 10 rows only
Now the error comes
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
Please suggest something.