TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Anoop Bansal
NA
58
6.2k
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)
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.
Reply
Answers (
5
)
how to call a web page through stored procedure
Creating a table in SQL using values of two table with some