Solved

Selecting specific rows

Posted on 2016-11-04
3
35 Views
Last Modified: 2016-11-04
I need to select the Top row of the table and the bottom row of the table from the CUSTOMERORDER table not sure how to do it. I'm running SQL server 2012.
This is what i have so far.

select *, CompanyName, Phone, lastname, orderdate
from Customer
      inner join Employee
            on Customer.AcctRepNo = Employee.EmpNo
      inner join CustomerOrder
            on Customer.CustNo = CustomerOrder.CustNo

I need the first row (orderno 10000) and the bottom row (10032) only.
Attached is a word file of the table
Doc13.docx
0
Comment
Question by:Rob Fried
3 Comments
 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 41874051
Hi,

pls try

Select * FROM (select Top 1 *, CompanyName, Phone, lastname, orderdate
 from Customer
       inner join Employee
             on Customer.AcctRepNo = Employee.EmpNo
       inner join CustomerOrder
             on Customer.CustNo = CustomerOrder.CustNo ORDER BY OrderNo Asc
Union
select Top 1 *, CompanyName, Phone, lastname, orderdate
 from Customer
       inner join Employee
             on Customer.AcctRepNo = Employee.EmpNo
       inner join CustomerOrder
             on Customer.CustNo = CustomerOrder.CustNo ORDER BY OrderNo Desc)

Open in new window

Regards
1
 
LVL 12

Accepted Solution

by:
Dustin Saunders earned 500 total points
ID: 41874101
Alternatively, you could use MAX() MIN() for this.

Select * FROM (select *, CompanyName, Phone, lastname, orderdate
 from Customer
       inner join Employee
             on Customer.AcctRepNo = Employee.EmpNo
       inner join CustomerOrder
             on Customer.CustNo = CustomerOrder.CustNo
 WHERE OrderNo = (SELECT MIN(OrderNo) FROM CustomerOrder)
Union
select *, CompanyName, Phone, lastname, orderdate
 from Customer
       inner join Employee
             on Customer.AcctRepNo = Employee.EmpNo
       inner join CustomerOrder
             on Customer.CustNo = CustomerOrder.CustNo)
 WHERE OrderNo = (SELECT MAX(OrderNo) FROM CustomerOrder)

Open in new window

0
 

Author Closing Comment

by:Rob Fried
ID: 41874113
THANKS! worked perfectly!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Database Containment - Benefits 6 25
while loop in html mail format 5 32
Sql query 34 20
SQL Transaction logs 8 9
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now