Solved

Problem getting distinct value along with sums SQL Server

Posted on 2016-09-22
12
63 Views
Last Modified: 2016-10-07
Let's say I run the following query:

SELECT        TOP (100) PERCENT Job, Total_Price, Invoice_Amount AS pri, hrs2 AS hrs, material3 AS mat, service2 AS ser, labor2 AS lab
FROM            dbo.V_Cust_Cost_Det_Inv_Date_B
WHERE        (Invoice_Date BETWEEN '9/1/16' AND '9/21/16')
ORDER BY Total_Price

Open in new window


It returns something like this:

Job / Total_Price / pri / hrs / mat / ser / lab
104591      5249.9900      4749.9900      26.589133      2078.7292      0.0000      439.6842
104591      5249.9900      500.0000      2.797928      218.7410      0.0000      46.2672

What I want is to be able to query the view and get sums of pri / hrs / mat / ser / lab but only the distinct value of Total_Price. I don't want them to sum. So far I have this which does not include the Total_Price:

SELECT        SUM(Invoice_Amount) AS pri, SUM(hrs2) AS hrs, SUM(material3) AS mat, SUM(service2) AS ser, SUM(labor2) AS lab
FROM            dbo.V_Cust_Cost_Det_Inv_Date_B
WHERE        (Invoice_Date BETWEEN '9/1/16' AND '9/21/16')

Open in new window


If somehow I could incorporate something like this into the query:

SELECT SUM(tpri) AS tpri FROM (SELECT DISTINCT Job, Total_Price AS tpri FROM V_Cust_Cost_Det_Inv_Date_B WHERE (Invoice_Date BETWEEN '9/1/16' AND '9/21/16')) AS derivedtbl_1

Open in new window


In the end this is what I want to see:

Job / Total_Price / pri / hrs / mat / ser / lab
104591      5249.9900      5249.9900      29.38706      2297.4702      0.0000      485.9514

Thanks in advance for your help.
0
Comment
Question by:princeservice
[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
  • 6
  • 5
12 Comments
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 41810569
Would this help?
SELECT  Job, SUM(Total_Price), SUM(Invoice_Amount) AS pri, SUM(hrs2) AS hrs, SUM(material3) AS mat, SUM(service2) AS ser, SUM(labor2) AS lab
FROM  dbo.V_Cust_Cost_Det_Inv_Date_B
WHERE  (Invoice_Date BETWEEN '9/1/16' AND '9/21/16')
GROUP BY Job
ORDER BY 2

Open in new window

0
 

Author Comment

by:princeservice
ID: 41810651
Vitor - You are summing the total price. That is what I am trying to avoid.
0
 

Author Comment

by:princeservice
ID: 41810654
I think I have solved it myself. I can just take the average of the total price.
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 41810662
It returns something like this:

 Job / Total_Price / pri / hrs / mat / ser / lab
 104591      5249.9900      4749.9900      26.589133      2078.7292      0.0000      439.6842
 104591      5249.9900      500.0000      2.797928      218.7410      0.0000      46.2672

In the end this is what I want to see:

 Job / Total_Price / pri / hrs / mat / ser / lab
 104591      5249.9900      5249.9900      29.38706      2297.4702      0.0000      485.9514

Looks to me a SUM of the above lines.
Can you share your solution so I can understand what was really the requirement?
0
 

Author Comment

by:princeservice
ID: 41810664
Vitor, The end result was summing the pri column, not the total_price column.
0
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 41810679
Mostly all columns but the total_price column, right?
0
 

Author Comment

by:princeservice
ID: 41810683
Nevermind, I do not have it solved
0
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 41810729
So, what do still missing?
0
 

Author Comment

by:princeservice
ID: 41810957
I probably need to expand on the question. I did not give you enough information. Let's start over:

Let's say I run the following query:

SELECT        TOP (100) PERCENT Job, Total_Price, Invoice_Amount AS pri, hrs2 AS hrs, material3 AS mat, service2 AS ser, labor2 AS lab
FROM            dbo.V_Cust_Cost_Det_Inv_Date_B
WHERE        (Invoice_Date BETWEEN '9/1/16' AND '9/21/16')
ORDER BY Total_Price

It returns something like this:

Job / Total_Price / pri / hrs / mat / ser / lab
104589      5698.2564      5698.2564      124.5633      1255.3665      0.0000      655.5556
104590      12345.0000      12345.0000      3232.23     111.1255      0.0000     96.6600
104591      5249.9900      4749.9900      26.589133      2078.7292      0.0000      439.6842
104591      5249.9900      500.0000      2.797928      218.7410      0.0000      46.2672

What I want is to be able to query the view and get sums of pri / hrs / mat / ser / lab but only the distinct value of Total_Price GROUPED BY JOB. I don't want the Total_Price to sum because that number is the same for every similar job. So far I have this which does not include the Total_Price:

SELECT        SUM(Invoice_Amount) AS pri, SUM(hrs2) AS hrs, SUM(material3) AS mat, SUM(service2) AS ser, SUM(labor2) AS lab
FROM            dbo.V_Cust_Cost_Det_Inv_Date_B
WHERE        (Invoice_Date BETWEEN '9/1/16' AND '9/21/16')

If somehow I could incorporate something like this into the query (Please don't let this throw you off from a good solution. This might be the absolute wrong way to do it):

SELECT SUM(tpri) AS tpri FROM (SELECT DISTINCT Job, Total_Price AS tpri FROM V_Cust_Cost_Det_Inv_Date_B WHERE (Invoice_Date BETWEEN '9/1/16' AND '9/21/16')) AS derivedtbl_1

In the end this is what I want to see:

Job / Total_Price / pri / hrs / mat / ser / lab
104589      5698.2564      5698.2564      124.5633      1255.3665      0.0000      655.5556
104590      12345.0000      12345.0000      3232.23     111.1255      0.0000     96.6600
104591      5249.9900      5249.9900      29.38706      2297.4702      0.0000      485.9514

Notice that job 104589 and 104590 are fine because I never had to sum anything but job 104591 had two lines so I summed up everything from pri over but only took the distinct value of the Total_Price.

Hope this helps.

Thanks in advance for your help.
0
 
LVL 50

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 41812086
So lets try a little variation of my first solution:
SELECT  Job, Total_Price, SUM(Invoice_Amount) AS pri, SUM(hrs2) AS hrs, SUM(material3) AS mat, SUM(service2) AS ser, SUM(labor2) AS lab
FROM  dbo.V_Cust_Cost_Det_Inv_Date_B
WHERE (Invoice_Date BETWEEN '9/1/16' AND '9/21/16'
GROUP BY Job, Total_Price
ORDER BY Job

Open in new window

0
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 41833248
princeservice, any feedback on this question?
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

734 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