Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Selecting specific rows

Posted on 2016-11-04
3
55 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 50

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

828 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