Solved

Strange select.

Posted on 2013-06-06
7
214 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 92

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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 

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 500 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 eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
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.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

690 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