Problem getting distinct value along with sums SQL Server

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 labFROM dbo.V_Cust_Cost_Det_Inv_Date_BWHERE (Invoice_Date BETWEEN '9/1/16' AND '9/21/16')ORDER BY Total_Price

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 labFROM dbo.V_Cust_Cost_Det_Inv_Date_BWHERE (Invoice_Date BETWEEN '9/1/16' AND '9/21/16')

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

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 labFROM dbo.V_Cust_Cost_Det_Inv_Date_BWHERE (Invoice_Date BETWEEN '9/1/16' AND '9/21/16')GROUP BY JobORDER BY 2

Vitor - You are summing the total price. That is what I am trying to avoid.

0

princeserviceAuthor Commented:

I think I have solved it myself. I can just take the average of the total price.

0

Veeam is happy to provide a free NFR license for one year. It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

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

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

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.

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 labFROM dbo.V_Cust_Cost_Det_Inv_Date_BWHERE (Invoice_Date BETWEEN '9/1/16' AND '9/21/16'GROUP BY Job, Total_PriceORDER BY Job

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Open in new window