[Last Call] Learn how to a build a cloud-first strategyRegister Now


Combining Rows

Posted on 2011-05-10
Medium Priority
Last Modified: 2012-06-27
I have a query which, through joins and such, produces sort of the correct results.  The problem is that it produces two rows.

The results are something list this
Customer  Sell_Date  Parts  Labour  Pay_Date
12345678  05-30-11   12.34  50.00
12345678             24.56  12.50   06-02-11
98765432  05-22-11   18.23  12.50
98765432             16.19  32.00   05-26-11

Open in new window

I want to merge th rows so that I have an output like this:
Customer  Sell_Date  Parts  Labour  Pay_Date
12345678  05-30-11   36.90  62.50   06-02-11
98765432  05-22-11   34.42  44.50   05-26-11

Open in new window

I know how to do it with the numbers, but how do I get the dates to merge?
Question by:Clif
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 2000 total points
ID: 35732472
something like this could do:

select Customer, Min(Sell_Date), sum(Parts), sum(Labor), may(pay_date) from yourtable group by Customer

Open in new window

LVL 16

Expert Comment

ID: 35732475

Try the Below Query.. so use this as an Outer query to get the desired results..Replace the Table Name with your query like

Select .... FROM ()X
Group By ...

SELECT Customer, MAX(Sell_date) as Sell_Date, SUM(Parts) as Parts , SUM(Labour) as Labour, Max(Pay_Date) as Pay_Date
FROM TableName
GROUP BY Customer
LVL 10

Author Closing Comment

ID: 35732544
Perfect, thanks.

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

829 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