简体   繁体   中英

Total Sum SQL Server

I have a query that collects many different columns, and I want to include a column that sums the price of every component in an order. Right now, I already have a column that simply shows the price of every component of an order, but I am not sure how to create this new column.

I would think that the code would go something like this, but I am not really clear on what an aggregate function is or why I get an error regarding the aggregate function when I try to run this code.

SELECT ID, Location, Price, (SUM(PriceDescription) FROM table GROUP BY ID WHERE PriceDescription LIKE 'Cost.%' AS Summary) FROM table

When I say each component, I mean that every ID I have has many different items that make up the general price. I only want to find out how much money I spend on my supplies that I need for my pressure washers which is why I said `Where PriceDescription LIKE 'Cost.%'

To further explain, I have receipts of every customer I've worked with and in these receipts I write down my cost for the soap that I use and the tools for the pressure washer that I rent. I label all of these with 'Cost.' so it looks like (Cost.Water), (Cost.Soap), (Cost.Gas), (Cost.Tools) and I would like it so for Order 1 it there's a column that sums all the Cost. _ prices for the order and for Order 2 it sums all the Cost._ prices for that order. I should also mention that each Order does not have the same number of Costs (sometimes when I use my power washer I might not have to buy gas and occasionally soap).

I hope this makes sense, if not please let me know how I can explain further.

`ID    Location    Price     PriceDescription
 1     Park        10        Cost.Water
 1     Park        8         Cost.Gas
 1     Park        11        Cost.Soap
 2     Tom         20        Cost.Water
 2     Tom         6         Cost.Soap
 3     Matt        15        Cost.Tools
 3     Matt        15        Cost.Gas
 3     Matt        21        Cost.Tools
 4     College     32        Cost.Gas
 4     College     22        Cost.Water
 4     College     11        Cost.Tools`

I would like for my query to create a column like such

`ID    Location    Price    Summary
 1     Park        10       29
 1     Park        8        
 1     Park        11       
 2     Tom         20       26
 2     Tom         6        
 3     Matt        15       51
 3     Matt        15       
 3     Matt        21       
 4     College     32       65
 4     College     22       
 4     College     11        `

But if the 'Summary' was printed on every line instead of just at the top one, that would be okay too.

You just require sum(Price) over(Partition by Location) will give total sum as below:

SELECT ID, Location, Price, SUM(Price) over(Partition by Location) AS Summed_Price
   FROM yourtable
WHERE PriceDescription LIKE 'Cost.%' 

First, if your Price column really contains values that match 'Cost.%' , then you can not apply SUM() over it. SUM() expects a number (eg INT, FLOAT, REAL or DECIMAL). If it is text then you need to explicitly convert it to a number by adding a CAST or CONVERT clause inside the SUM() call.

Second, your query syntax is wrong: you need GROUP BY, and the SELECT fields are not specified correctly. And you want to SUM() the Price field, not the PriceDescription field (which you can't even sum as I explained)

Assuming that Price is numeric (see my first remark), then this is how it can be done:

SELECT ID
     , Location
     , Price
     , (SELECT SUM(Price)
        FROM table
        WHERE ID = T1.ID AND Location = T1.Location
       ) AS Summed_Price
FROM table AS T1

to get exact result like posted in question

    Select  
                T.ID,
                T.Location,
                T.Price,
                CASE WHEN (R) = 1 then RN ELSE NULL  END Summary 
            from  (
    select 
                ID, 
                Location, 
                Price ,
                SUM(Price)OVER(PARTITION BY Location)RN,
                ROW_number()OVER(PARTITION BY Location ORDER BY ID )R 
            from  Table
     )T
order by T.ID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM