[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Strange select.

Posted on 2013-06-06
7
Medium Priority
?
217 Views
Last Modified: 2013-06-06
I have an order table and a line item table that has multiple entries Header and line items, but I only want to retrieve the first occurrence of the order header;

select o.bt_id,o.ORDER_NUMBER,o.total_charges,o.source_code
from Orders o
inner join Order_Lines ol on o.ORDER_NUMBER = ol.order_number and ol.PRODUCT_CODE = 'FIELDVALUE'
order by o.bt_id

returns multiple lines for each user, I only want the first occurence
0
Comment
Question by:Jeff_Kingston
[X]
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
7 Comments
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 39225472
"First occurence" meaning what, exactly?

It would be helpful to see some sample data showing what's in the source tables, and what result you're expecting.
0
 

Author Comment

by:Jeff_Kingston
ID: 39225486
101893      12761333.00      66.25      WEB
101893      13242872.00      -2.02      
101893      13243075.00      -2.02      
101893      13404457.00      66.25      WEB
101893      13571055.00      66.25      WEB

and I only want the first entry.
0
 
LVL 9

Expert Comment

by:sarabhai
ID: 39225518
can u try out following..

SELECT o.bt_id,o.ORDER_NUMBER,o.total_charges,o.source_code
FROM Orders o
INNER JOIN (SELECT TOP 1 * FROM Order_Lines) ol ON o.ORDER_NUMBER = ol.order_number AND ol.PRODUCT_CODE = 'FIELDVALUE'
ORDER BY o.bt_id
0
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

Author Comment

by:Jeff_Kingston
ID: 39225540
That doesn't return any rows at all?
0
 

Author Comment

by:Jeff_Kingston
ID: 39225608
All I want is the first occurence for each customer.


Cust            Order                 charges
101893      12761333.00      66.25      WEB
101893      13242872.00      -2.02      
101893      13243075.00      -2.02      
101893      13404457.00      66.25      WEB
101893      13571055.00      66.25      WEB
222222      15761333.00      78.45      WEB
222222      16404457.00      78.45      WEB
222222      16571055.00      78.45      WEB
345612      14761133.00      56.42      WEB
345612      18904457.00      56.42      WEB
345612      19071055.00      56.42      WEB
0
 
LVL 22

Accepted Solution

by:
Thomasian earned 2000 total points
ID: 39225829
SELECT *
FROM
(
select o.bt_id,o.ORDER_NUMBER,o.total_charges,o.source_code
	,rn = ROW_NUMBER() OVER (PARTITION BY o.bt_id ORDER BY o.ORDER_NUMBER)
from Orders o
inner join Order_Lines ol on o.ORDER_NUMBER = ol.order_number and ol.PRODUCT_CODE = 'FIELDVALUE'
) T
WHERE rn=1
order by bt_id

Open in new window

0
 

Author Closing Comment

by:Jeff_Kingston
ID: 39225971
That is a syntax I have never seen and it works like a charm.  You have saved my brain fom exploding.

Excellent job!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

650 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