I am looking to add a common value.
My table has the following data:
Customer: $ Value:
123 100.00
123 100.00
abc 100.00
abc 100.00
I want it to display as:
Customer: $ Value:
123 200.00
abc 200.00
There are a number of other columns too that contain various different dates etc but they are not relevant here.
SELECT Customer, SUM(Value)
FROM myTable
GROUP BY Customer
Use GROUP BY Clause and the aggregate function SUM().
For more info about GROUP BY refer here
SELECT Customer, SUM(Value) AS Value FROm table GROUP BY Customer
You could do this:
SELECT
Customer,
SUM(Value) AS Value
FROM
Table1
GROUP BY
Customer
Related
So I have a query select that outputs something like this:
(the actual results I'm working with is much more complicated but these are the important parts)
id trans
123 5.00
124 6.00
124 7.00
125 8.00
125 9.00
I want to create a result like this:
id trans total
123 5.00 5.00
124 6.00 13.00
124 7.00 13.00
125 8.00 17.00
125 9.00 17.00
Basically I want to add a column that contains a total of all the transactions for each id, while still showing all the transactions. I think the solution will have something to do with group by, nested selects and the sum function but I can't get it right.
A windowed function works well for this scenario:
select
*
,sum(trans) over(partition by id) as total
from
myTable
SqlFiddle Example
Generally speaking, you want:
SUM(value) OVER (PARTITION BY group)
If the first output is from this:
SELECT id, SUM(col) AS trans
FROM table
GROUP BY id;
Then you need this:
SELECT id, SUM(col) AS trans, SUM(SUM(col)) OVER (PARTITION BY id) AS id_total
FROM table
GROUP BY id;
If the first output is from this:
SELECT id, trans
FROM table;
Then you need this:
SELECT id, trans, SUM(trans) OVER (PARTITION BY id) AS id_total
FROM table;
Table:1
Date Customer Amount
12-Dec ABC 200
15-Dec ABC 300
Output:
I need to group the data by Customer and need to take the latest date for that unique record.
Date Customer Amount
15-Dec ABC 500
You seems to want aggregation :
select max(to_date(date, 'DD-MON-YYYY')), cust, sum(amount)
from table t
group by cust;
I have the following table called Orders
Order | Date | Total
------------------------------------
34564 | 03/05/2015| 15.00
77456 | 01/01/2001| 3.00
25252 | 02/02/2008| 4.00
34564 | 03/04/2015| 7.00
I am trying to select the distinct order sum the total and group by order #, the problem is that it shows two records for 34564 because they are different dates.. How can I sum if they are repeated orders and pick only the max(date) - But sill sum the total of the two instances?
I.E result
Order | Date | Total
------------------------------------
34564 | 03/05/2015| 22.00
77456 | 01/01/2001| 3.00
25252 | 02/02/2008| 4.00
Tried:
SELECT DISTINCT Order, Date, SUM(Total)
FROM Orders
GROUP BY Order, Date
Of couse the above won't work as you can see but i am not sure how to achieve what i intend.
SELECT [order], MAX(date) AS date, SUM(total) AS total
FROM Orders o
GROUP BY [order]
You can use the MAX aggregate function to choose the latest Date to appear from each Order group:
SELECT Order, MAX(Date) AS Date, SUM(Total) AS Total
FROM Orders
GROUP BY Order
Simplest query should be:
SELECT MAX(Order), MAX(Date), SUM(Total)
FROM Orders
You can use SUM and MAX together:
SELECT
[Order],
[Date] = MAX([Date]),
Total = SUM(Total)
FROM tbl
GROUP BY [Order]
A word of advice, please refrain from using reserved words like Order and Date for your columns and table names.
Just add MAX(Date) to your SELECT clause.
Try this :
SELECT DISTINCT Order, MAX(Date), SUM(Total)
FROM Orders
GROUP BY Order, Date
select CandidateName, SUM(Amount) as Amount
from MaseHisab
where Member = 'ABC+DEF+GHI'
group by CandidateName
CandidateName Amount
jain 72.00
rakesh 30.00
shams 110.00
If you are using SQL-Server then
use with rollup.that will give you sum of amount column at the end of that
select isnull(CandidateName,'Total'),SUM(Amount) as Amount
from MaseHisab
where Member='ABC+DEF+GHI' group by CandidateName
with rollup
o/p
CandidateName Amount
jain 72.00
rakesh 30.00
shams 110.00
NULL 212.00
EDIT
use isnull for that. see the above query
isnull(CandidateName,'Total')
If you are using other databases
COALESCE(CandidateName,'Total')
I have one table with following data..
saleId amount date
-------------------------
1 2000 10/10/2012
2 3000 12/10/2012
3 2000 11/12/2012
2 3000 12/10/2012
1 4000 11/10/2012
4 6000 10/10/2012
From my table I want result with max of sum amount between dates 10/10/2012 and 12/10/2012 which for the data above will be:
saleId amount
---------------
1 6000
2 6000
4 6000
Here 6000 is the max of the sums (by saleId) so I want ids 1, 2 and 4.
You have to use Sub-queries like this:
SELECT saleId , SUM(amount) AS Amount
FROM Table1
GROUP BY saleId
HAVING SUM(amount) =
(
SELECT MAX(AMOUNT) FROM
(
SELECT SUM(amount) AS AMOUNT FROM Table1
WHERE date BETWEEN '10/10/2012' AND '12/10/2012'
GROUP BY saleId
) AS A
)
See this SQLFiddle
This query goes through the table only once and is fairly optimised.
select top(1) with ties saleid, amount
from (
select saleid, sum(amount) amount
from tbl
where date between '20121010' and '20121210'
group by saleid
) x
order by amount desc;
You can produce the SUM with the WHERE clause as a derived table, then SELECT TOP(1) in the query using WITH TIES to show all the ones with the same (MAX) amount.
When presenting dates to SQL Server, try to always use the format YYYYMMDD for robustness.