Anoop Bansal

Anoop Bansal

  • NA
  • 58
  • 5.8k

Get data from last to first with join and distinct rows

Mar 10 2017 8:33 AM
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)
  1. select distinct p.InvoiceNo, p.Date, p.NetAmt as Amount, vd.VendorName from Purchase p    
  2. 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) 
  1. select p.InvoiceNo, p.Date, p.NetAmt as Amount, vd.VendorName from Purchase p  
  2. inner join VendorDetails vd on p.VendorID = vd.VendorId  
  3. order by p.Id desc offset 0 rows fetch next 10 rows only  
 But I want distinct p.InvoiceNo here with pagination functionality)
 
  1. select distinct p.InvoiceNo, p.Date, p.NetAmt as Amount, vd.VendorName from Purchase p    
  2. inner join VendorDetails vd on p.VendorID = vd.VendorId    
  3. 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. 
 
 
 

 
 
 

Answers (5)