Total of sum when group by name then do total of Amount - sql

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')

Related

select total sales per day

i have query like this and i wanted to have sum of sales per day
SELECT DATEPART(day,deduction_timestamp) as [day],
[fare_deduction]
FROM [dbfastsprocess].[dbo].[vClosingTransitLog]
WHERE bus_id in ('JEAST', 'MKV004', 'NWTN01')
and YEAR(deduction_timestamp) = ISNULL(2016, YEAR(deduction_timestamp))
and MONTH(deduction_timestamp) = ISNULL(10, MONTH(deduction_timestamp))
GROUP BY DATEPART(day,deduction_timestamp), fare_deduction
with result :
day fare_deduction
--------------------------------
1 10.00
3 15.00
3 2.00
4 10.00
10 20.00
31 12.00
and i wanted the result to be like this..
day fare_deduction
--------------------------------
1 10.00
3 17.00
4 10.00
10 20.00
31 12.00
and take note that not all day have values and it only display the affected
day only. Can help me on these? Thanks!
SELECT DATEPART(day,deduction_timestamp) as [day],
sum(fare_deduction) as fare_deduction
FROM [dbfastsprocess].[dbo].[vClosingTransitLog]
WHERE bus_id in ('JEAST', 'MKV004', 'NWTN01')
and YEAR(deduction_timestamp) = ISNULL(2016, YEAR(deduction_timestamp))
and MONTH(deduction_timestamp) = ISNULL(10, MONTH(deduction_timestamp))
GROUP BY DATEPART(day,deduction_timestamp)
Simply use sum, it will get your required result
SELECT DATEPART(day,deduction_timestamp) as [day],
SUM([fare_deduction]) [fare_deduction]
FROM [dbfastsprocess].[dbo].[vClosingTransitLog]
WHERE bus_id in ('JEAST', 'MKV004', 'NWTN01')
and YEAR(deduction_timestamp) = ISNULL(2016, YEAR(deduction_timestamp))
and MONTH(deduction_timestamp) = ISNULL(10, MONTH(deduction_timestamp))
GROUP BY DATEPART(day,deduction_timestamp)

Sum Data in SQL by a column

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

How do I build a SQL Statement that selects multiple rows and sums the amount?

I'm recently working with DB/2 for an AS/400 and I ran across something that I am not really strong at. I have written this SQL Statement to bring back all of the fields that will bring back all the results dated from 4/30/2012 to 4/30/2013 I've hard-coded the values for now:
SELECT PPOLNO, PPRMPD FROM PFNTLPYMTH WHERE (PYEAR >=2012 AND PMONTH <=4 AND PDAY >=1)
The table has multiple PPOLNO values that are the same, and the same with the PPRMPD field.
I'd like to be able to return a single PPOLNO and a sum of all of the dollar amounts. For example:
PPOLNO | PPRMPD
1 | 500.00
1 | 500.00
2 | 250.00
1 | 100.00
3 | 5000.00
I want to write a sql query that will bring back just:
PPOLNO | PPRMPD
1 | 1100.00
2 | 250.00
3 | 5000.00
But I am not sure what to add on to the SQL Statement to make it so. I can get a distinct list of PPOLNO, but I'm not exactly sure how to go about getting the sum in the same query (if it's even possible). Any help would be greatly appreciated.
Josh
Please Try:
SELECT PPOLNO, SUM(PPRMPD) PPRMPD FROM PFNTLPYMTH GROUP BY PPOLNO
and you can add necessary where conditions for the query.
SELECT PPOLNO, SUM(PPRMPD) AS SUM FROM PFNTLPYMTH WHERE (PYEAR >=2012 AND PMONTH <=4 AND PDAY >=1) GROUP BY PPOLNO
Should do it
This should be pretty straight-forward.
SELECT PPOLNO, SUM(PPRMPD) AS TotalAmount
FROM TableName
-- WHERE ..add conditions here...
GROUP BY PPOLNO

Displaying unique attributes of table and the total value in postgresql

I have one table with columns stamp_type and amount as follows
stamp_type | amount
--------------------------------------------------------------+--------
GENERAL STAMP | 11000
GENERAL STAMP | 25000
COURT FEE STAMP | 9800
SPECIAL ADHESIVE | 721000
GENERAL STAMP | 125000
COURT FEE STAMP | 21000
Now I want to display as follows:
stamp_type | amount
GENERAL STAMP 161000
COURT FEE STAMP 30800
SPECIAL ADHESIVE 721000
TOTAL:912800
I am unable to display the unique values. can any one give suggest me the query. I tried to use Distinct but dint work.
select * from
(
select 0 as srt, stamp_type, sum(amount) as SumAmount from t group by stamp_type
union
select 1 as srt, 'Total' as stamp_type, sum(amount) as SumAmount from t
) b order by srt
try this code::
select stamp_type, sum(amount) amount
from tbl
group by stamp_type
union
select 'Total:' stamp_type,sum(amount) amount
from tbl;

Get max of column using sum

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.