Solved

Return only one record from a Grouped Table Join

Posted on 2014-09-15
2
187 Views
Last Modified: 2014-09-15
Hello

I have an orders table that has duplicate products on the same order.
I Need to ADD the duplicate Qtys together then return one of the Order Line ID's

I have Grouped the Table by the Order ID and the Stock Item but how do I get the Line ID of One of the Lines?

SELECT     SOL.SEQNO,SOL.HDR_SEQNO, SOL.STOCKCODE, SUM(SOL.ORD_QUANT)
FROM         dbo.SALESORD_LINES AS SOL
WHERE SOL.HDR_SEQNO = 205474
GROUP BY SOL.SEQNO,SOL.HDR_SEQNO,SOL.STOCKCODE

This Returns

SEQNO      HDR_SEQNO      STOCKCODE      ORDER_QTY
86616      205474      A101487              1
86617      205474      A101488       1
86618      205474      A101489       1
86619      205474      A103866       10
86620      205474      A103867       10
86621      205474      X100246       1
86622      205474      X100246       1

What I need is

SEQNO      HDR_SEQNO      STOCKCODE      ORDER_QTY
86616      205474      A101487              1
86617      205474      A101488       1
86618      205474      A101489       1
86619      205474      A103866       10
86620      205474      A103867       10
86621      205474      X100246       2

(The last two lines have the Order_qty added and one of the seqno records)

Note the "WHERE SOL.HDR_SEQNO = 205474" is only to reduce the records to one order for a sample - The code is to go in a view with thousands of orders
0
Comment
Question by:p-plater
[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
2 Comments
 
LVL 25

Accepted Solution

by:
chaau earned 500 total points
ID: 40324446
It looks like you are interested in the MIN(SEQNO). Use this query:
SELECT     MIN(SOL.SEQNO) AS SEQNO, SOL.HDR_SEQNO, SOL.STOCKCODE, SUM(SOL.ORD_QUANT)
FROM         dbo.SALESORD_LINES AS SOL
WHERE SOL.HDR_SEQNO = 205474
GROUP BY SOL.HDR_SEQNO,SOL.STOCKCODE

Open in new window

0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40324458
Looks like chaau has directly answered your question, so I won't propose another answer, but as an aside if you want to read some articles related to what you're doing here, check out SQL Server GROUP BY Solutions and SQL Server Delete Duplicate Rows Solutions.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

691 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