How to sum duplicated values in a Group(SSRS 2005) - reporting-services

How to sum duplicated values in a Group*(SSRS 2005).*
eg.
My query returns values like the following:
CusID Discount Amount
1 20 1000
1 20 2000
1 5 700
2 15 1500
2 15 3000
But,when I sum Discount amount in Group Footer, I cannot get the total values like below. I get 45 for CusID 1 instead of 25. Please help me to solve this problem.Thanks.
CusID Discount Amount
1 20 1000
1 20 2000
1 5 700
------------------------
Total 25 3700
2 15 1500
2 15 3000
------------------------
Total 15 4500

well without what your actual data looks like, i can only provide you a code example based off of the data you provided.
declare #table table (CustID int, Discount int, Amount int)
insert into #table (CustID,Discount,Amount)
select 1 as CusID,20 as Discount,1000 as Amount
union all
select 1,20,2000
union all
select 1,5,700
union all
select 2,15,1500
union all
select 2,15,3000
select
CustID,
sum(Discount) as Discount,
sum(Amount) as Amount
from
(
select
CustID,
Discount,
SUM(Amount) as Amount
from #table
group by CustID, Discount
) a
group by CustID

One slight simplification to DForcek42's answer by using the sum(distinct x)
declare #table table (CustID int, Discount int, Amount int)
insert into #table (CustID,Discount,Amount)
select 1 as CusID,20 as Discount,1000 as Amount
union all
select 1,20,2000
union all
select 1,5,700
union all
select 2,15,1500
union all
select 2,15,3000
select
CustID,
sum(distinct Discount),
SUM(Amount) as Amount
from #table
group by CustID

Related

How to select sum of column values using from and to time values? [duplicate]

I have a table with the following structure and sample data:
STORE_ID | INS_TIME | TOTAL_AMOUNT
2 07:46:01 20
3 19:20:05 100
4 12:40:21 87
5 09:05:08 5
6 11:30:00 12
6 14:22:07 100
I need to get the hourly sum of TOTAL_AMOUNT for each STORE_ID.
I tried the following query but i don't know if it's correct.
SELECT STORE_ID, SUM(TOTAL_AMOUNT) , HOUR(INS_TIME) as HOUR FROM VENDAS201302
WHERE MINUTE(INS_TIME) <=59
GROUP BY HOUR,STORE_ID
ORDER BY INS_TIME;
Not sure why you are not considering different days here. You could get the hourly sum using Datepart() function as below in Sql-Server:
DEMO
SELECT STORE_ID, SUM(TOTAL_AMOUNT) HOURLY_SUM
FROM t1
GROUP BY STORE_ID, datepart(hour,convert(datetime,INS_TIME))
ORDER BY STORE_ID
SELECT STORE_ID,
HOUR(INS_TIME) as HOUR_OF_TIME,
SUM(TOTAL_AMOUNT) as AMOUNT_SUM
FROM VENDAS201302
GROUP BY STORE_ID, HOUR_OF_TIME
ORDER BY INS_TIME;

How to group the data properly by multiple columns?

I have a database table that represents concert hall and it looks as following:
Columns: ActionID, RegionName, RowNumber, Price, Quantity
The sample data:
ActionID RegionName RowNumber Price Quantity
1 Region1 22 8000 7
1 Region1 - 8000 1
1 Region2 10 5000 2
1 Region2 10 5000 2
1 Region2 10 5000 1
I should display particular regions with overall quantity per region grouped by ActionID, RegionName, Price and Quantity.
1 Region1 22 8000 8
1 Region2 10 5000 5
As you can see the RowNumber should be displayed, but not taken into account in grouping.
How can I do that?
You could use a CTE and ranking function like ROW_NUMBER:
WITH CTE AS
(
SELECT t.*,
rn = ROW_NUMBER()OVER(Partition By ActionID, RegionName, Price
Order By RowNumber),
OverallQuantity = SUM(Quantity) OVER (Partition By ActionID, RegionName, Price)
FROM dbo.TableName t
)
SELECT ActionID, RegionName, RowNumber, Price, Quantity = OverallQuantity
FROM CTE
WHERE RN = 1
Result:
ActionID RegionName RowNumber Price Quantity
1 Region1 - 8000 8
1 Region2 10 5000 5
Demo
You could use something like this:
SELECT ActionId,
RegionName,
MAX(RowNumber),
MAX(Price),
SUM(Quantity)
FROM #tblTest
GROUP BY ActionId, RegionName
But if there are more RowNumber or Price, it should be modified the query
You can apply a MAX or MIN to collapse the rows:
SELECT MAX(ActionID), RegionName, MAX(RowNumber), MAX(Price), SUM(Quantity)
FROM my_table
GROUP BY RegionName
This obviously gets more complicated if you have multiple ActionID or RowNumber or Price per region. In that case, only use this if you don't care which one of these multiple values you get back.

MySQL: Get total hours, then multiply with rate, then rounding in one SELECT statement

My data looks like this:
UserID Hours BillRate
1 1.50 2.25
1 2.50 3.25
1 3.50 3.25
2 5.50 4.25
2 6.50 5.25
2 7.50 5.25
Is there anyway that I can get total spend for each person in one SELECT statement?
This is what I have. It returns what I want, but I have to do 2 SELECT statements.
SELECT UserID, SUM(OneSpend) AS TotalSpend
FROM (
SELECT UserID, ROUND(SUM(Hours)*BillRate,2) AS OneSpend
FROM mytable
GROUP BY UserID, BillRate
) a
GROUP BY UserID
SELECT UserID, ROUND(SUM(Hours*BillRate), 2) AS TotalSpend
FROM mytable
GROUP BY UserID
According to the distributive property of multiplication over addition, SUM(Hours)*BillRate is the same as SUM(Hours * BillRate) when all the BillRate values are the same, as they are when you group by BillRate.

How to (MySQL) multiply columns and then the sum rows?

I've got this table:
id | payment_id | quantity | unit cost
1 633 1 750
2 633 1 750
3 632 2 750
4 632 2 750
What I need is :
payemnt_id | total
633 1500
632 3000
You can also think of this as Countries and then you are trying to find the total for each Country. I was sure there were some tutorials like this but could not find by STFW.
You can simply put the formula (expression) inside SUM:
SELECT payment_id, SUM(`unit cost` * quantity) AS total
FROM myTable
GROUP BY payment_id
SELECT
payment_id,
SUM(subtotal) AS total
FROM(
SELECT
payment_id,
(unit_cost * quantity) AS subtotal
) AS t1
GROUP BY
payment_id
SELECT payemnt_id, SUM(`unit cost` * quantity) as total
FROM Table1
GROUP BY payemnt_id
SELECT COALESCE (SUM(cost_field * quantity_field),0) AS tot FROM Table_Name
I Think This One Is Good, Why ?
Because If Their is No Result It Will Give You Zero

Removing duplicate values from a dataset

I am developing an SSRS report with the following dataset (Table-1). I am grouping by Account and Period. My goal is to get the Total Expense and the Budget within a group. Because the Budget data is duplicated per group, I cannot use SUM() function for Budget. How do I remove the duplicates so the new dataset looks like this? (Table-2) Please advice. Thank you for your time.
Table-1
ID Account Period Expense Budget
1 100 201301 20 100
2 100 201301 30 100
3 100 201302 10 150
4 100 201302 40 150
5 200 ...................
Table-2
ID Account Period Expense Budget
1 100 201301 20 100
2 100 201301 30 NULL
3 100 201302 10 150
4 100 201302 40 NULL
5 200 ...................
If you really want to make duplicate budgets null try this update command
please check sqlfiddle http://sqlfiddle.com/#!3/1e619/11
Update table1
set budget = null
where id in
(
select aa.id from
(
select id,row_number()
over(partition by Budget order by Period) as rno
from table1
) aa
where rno > 1
);
select * from table1;
good luck.
I would use a windowed function if you have to do that grouping in SQL. If you can do it in SSRS just add a 'Row Grouping Parent' it would be better.
For SQL I would do this:
declare #Temp table ( ID int identity, Account int, period int, expense int, budget int);
insert into #Temp values (100, 201301, 20, 100),(100, 201301, 30, 100),(100, 201302, 10, 150),(100, 201302, 40, 150)
select *
from #Temp
select
ID
, Account
, Period
, Expense
, case when (row_number() over(partition by Budget order by Period) = 1) then Budget end as Budget-- only shows first occurrence of an order amount ordering by person
from #Temp