I have a report in RDLC. I try to sum up without duplicates. So how to sum ignoring duplicates??
№ sale Date_Sales Amount Date_payment Amount
01-01 01/07/2013 1000 15/07/2013 500
01-01 01/07/2013 1000 18/07/2013 500
Total 2000 1000
and should be:
№ sale Date_Sales Amount Date_payment Amount
01-01 01/07/2013 1000 15/07/2013 500
01-01 01/07/2013 1000 18/07/2013 500
Total 1000 1000
I hide duplicates in the second line but I can't avoid the sum of this duplicate fields. I need to see the sum of sales and the sum of payments in the same report.
In SQL it would be
SELECT SUM(columnname) AS iSum FROM(
SELECT DISTINCT columnname FROM tablename) AS foo
Related
I have a table that stores scheduled charges within a date range. I am looking to sum the values per month, but am unsure how to structure the query to handle the range logic. A simplified version of the table structure is below where dates are in MM/DD/YYYY format:
Start End Amount
01/15/2020 04/30/2020 200
02/05/2020 06/30/2020 300
03/01/2020 12/15/2020 400
04/02/2020 10/25/2020 500
The output would display a sum for every month there are records for based on an entry existing in the Start or End column for that month. So the output would look like this:
Month Amount
01/2020 200
02/2020 500
03/2020 900
04/2020 1400
05/2020 1200
06/2020 1200
07/2020 900
08/2020 900
09/2020 900
10/2020 900
11/2020 500
12/2020 500
Any ideas of how to handle this without a separate table storing each month/year to check if it is within the range?
You can use a recursive CTE and then aggregate:
with cte as (
select datefromparts(year(start), month(start), 1) as dte, end, amount
from t
union all
select dateadd(month, 1, dte), end, amount
from cte
where eomonth(dte) < eomonth(end)
)
select dte, sum(amount)
from cte
group by dte;
Here is a db<>fiddle.
I need to remove duplicate total values from the results of a complicated SQL query that produces a table of invoice details.
I've looked for examples, but i can't find anything that allows me to remove duplicates in just one column, based on the values in one other column.
This is a basic example of the data i have. I need to maintain this table format:
Project Section Reference Section_Amount Total
a 1 inv1 1500 1500
a 1 inv2 1000 3000
a 2 inv2 1000 3000
a 3 inv2 1000 3000
a 4 inv3 1700 1700
I need to have:
Project Section Reference Section_Amount Total
a 1 inv1 1500 1500
a 1 inv2 1000 3000
a 2 inv2 1000 NULL
a 3 inv2 1000 NULL
a 4 inv3 1700 1700
As you can see the total for invoice 2 is only shown once.
Is this possible?
Thanks in advance for any help!
You can use row_number():
select Project, Section, Reference, Section_Amount,
(case when row_number() over (partition by reference order by section) = 1
then total
end) as total
from t;
I have two similar tables as follows
Table 1
Date Amount Tax
4/1/2016 1000 100
4/1/2016 2000 200
5/3/2016 1500 150
5/6/2016 1000 100
5/6/2016 3000 300
7/9/2016 2500 250
Table 2
Date Amount Tax
4/1/2016 1000 100
4/2/2016 3000 300
5/3/2016 1500 150
5/9/2016 4000 400
8/11/2016 3000 300
10/9/2016 2000 200
dates can be similar or different in both tables.
I want two queries.
First, a query which gives me sum of amount and tax from each date from both tables between required dates. Eg: Table 1 have 2 entries and table 2 have 1 entry for 4/1/2016. so the result should be as below (summing up all three entries)
Date Amount Tax
4/1/2016 4000 400
4/2/2016 3000 300
5/3/2016 3000 300
5/6/2016 4000 400
5/9/2016 4000 400
7/9/2016 2500 250
8/11/2016 3000 300
10/9/2016 2000 200
Second,a query which gives of sum of amount and tax for each month from both tables between required dates. Eg output as below
Date Amount Tax
4/2016 4000 400
5/2016 11000 1100
7/2016 2500 250
8/2016 3000 300
10/2016 2000 200
Query that have I have written till now( not working )
SELECT date, sum(Amount),sum(Tax)
From Table1
WHERE Date BETWEEN #04/01/2016# AND #12/31/2016#
UNION ALL
SELECT date, sum(Amount),sum(Tax)
From Table2
WHERE Date BETWEEN #04/01/2016# AND #12/31/2016#
GROUP BY Date
For first query, consider a union query derived table with outer query aggregation:
SELECT q1.[Date], SUM(q1.Amount) AS DayTotalAmt, SUM(q1.Tax) AS DayTotalTax
FROM
(SELECT [Date], Amount, Tax
FROM Table1
UNION ALL
SELECT [Date], Amount, Tax
FROM Table2
) AS q1
GROUP BY q1.[Date]
For second query, consider using first query as a source with another outer query layer that runs a WHERE filter with month/year aggregation:
SELECT Format(q2.Date, "M/YYYY"), SUM(q2.DayTotalAmt) AS MonthTotalAmt,
SUM(q2.DayTotalTax) AS MonthTotalTax
FROM
(SELECT q1.[Date], SUM(q1.Amount) AS DayTotalAmt, SUM(q1.Tax) AS DayTotalTax
FROM
(SELECT [Date], Amount, Tax
FROM Table1
UNION ALL
SELECT [Date], Amount, Tax
FROM Table2) AS q1
GROUP BY q1.[Date]
) AS q2
WHERE q2.Date BETWEEN CDate("4/1/2016") AND CDate("12/31/2016")
GROUP BY Format(q2.Date, "M/YYYY")
Or if you save first query:
SELECT Format(q.Date, "M/YYYY"), SUM(q.DayTotalAmt) AS MonthTotalAmt,
SUM(q.DayTotalTax) AS MonthTotalTax
FROM Query1 q
WHERE q.Date BETWEEN CDate("4/1/2016") AND CDate("12/31/2016")
GROUP BY Format(q.Date, "M/YYYY")
I'm trying to acive the following with power pivot. Still couldn't figure out how. Is it possible?
Example from access
Gourped by “MatNr”. “Qty cases” are summed. From the “tbl_Weight” show the “Cases on trolley” per material. Divide “summed Qty cases” by “Cases on trolley” and show the results in the fourth column. Most importantly total the “summed Qty cases” column and the “Nr of trolleys per mat” column.
Two tables:
tbl_Sales
MatNr Qty cases
4564 100
4654 565
4564 100
4654 50
tbl_Weight
MatNr Cases on trolley
4564 10
4654 20
Query:
SELECT tbl_Sales.MatNr, Sum(tbl_Sales.[Qty cases]) AS [SummevonQty cases], tbl_Weight.[Cases on trolley], Sum([Qty cases]/[Cases on trolley]) AS [Nr of trolleys per mat]
FROM tbl_Sales INNER JOIN tbl_Weight ON tbl_Sales.MatNr = tbl_Weight.MatNr
GROUP BY tbl_Sales.MatNr, tbl_Weight.[Cases on trolley];
Expected results:
MatNr SummevonQty cases Cases on trolley Nr of trolleys per mat
4564 200 10 20
4654 615 20 30.75
Totals 815 50.75
Date - Amount
12-nov-2016 200
12-nov-2016 100
13- nov -2016 400
13 -nov-2016 200
result show like-
Date Amount
12- nov-2016 300
13- nov-2016 600
as whole month
select [Date], sum(Amount) as Amount from yourTable group by [Date]