add Total to the duplicate values - reporting-services

I have requirement like
claimnumber -- amount -- category
----------- ------ --------
123 -- 10 -- A
123 -- 10 -- B
456 -- 10 -- C
456 -- 10 -- B
I want report to group data like below,
distinct claim nos- 2
total charge for them is 20
Total Count -- Total Amount
----------- ------------
2 -- 20
How is it possible with ssrs.?

This should work:
select
count(claimnumber) as [total count],
sum(amount) as [total amount]
from tablename
group by claimnumber

Related

How to substrate number from the sum of columns of a table based on the id

I have a table named deposit, below
dep_id
deposit_amount
comp_id
1
100
1
2
100
1
3
100
1
When I run the query below I get the next updated table which is not what I want :
query = em.createNativeQuery("UPDATE deposit SET deposit_amount = (SELECT SUM(deposit_amount) - 50) WHERE comp_id = :comp_id");
query.setParameter("comp_id", comp_id);
The updated table after the above query
dep_id
deposit_amount
comp_id
1
50
1
2
50
1
3
50
1
But I want when I substract 50 or any amount it should get the sum of the columns and minus the amount from the sum not from each column. Below
dep_id
deposit_amount
comp_id
1
83.3
1
2
83.3
1
3
83.3
1
Because the sum is 300, and 300-50 = 250
Please how should I do this?
Using a common table expression, you can use this query. Get the total deposit amount per comp_id. Then join this new table (called total) to deposit on comp_id. Subtract 50 from the sum.
WITH total as(
select comp_id,
sum(deposit_amount) as total
from
deposit
group by
comp_id
)
select dep.dep_id,
ttl.total - 50 as deposit_amount,
dep.comp_id
from
deposit dep
inner join
total ttl
on
dep.comp_id = ttl.comp_id
Sample:
dep_id
deposit_amount
comp_id
1
250
1
2
250
1
3
250
1
You should compute deposit amount in a separate query, then join back your two tables on matching "comp_id" value
WITH cte AS (
SELECT DISTINCT comp_id,
SUM(deposit_amount) OVER(PARTITION BY comp_id) AS amount
FROM deposit
)
UPDATE deposit
INNER JOIN cte
ON deposit.comp_id = cte.comp_id
SET deposit_amount = cte.amount - 50
WHERE deposit.comp_id = :comp_id;
In your final query it should look like:
query = em.createNativeQuery("WITH cte AS (SELECT DISTINCT comp_id, SUM(deposit_amount) OVER(PARTITION BY comp_id) AS amount FROM deposit) UPDATE deposit INNER JOIN cte ON deposit.comp_id = cte.comp_id SET deposit_amount = cte.amount - 50 WHERE deposit.comp_id = :comp_id");
query.setParameter("comp_id", comp_id);
Check the demo here.

How to calculate price average while grouping by date in MySQL?

In MySQL 5.7 I need to calculate the average discount per merchant and day.
There are 2 tables:
produts_manufacturers:
PROD_ID | MANUFACTURER_ID | PRICE_RECOM
1 1 10
2 1 20
merchants_prices
PROD_ID | MERCHANT_ID | PRICE_ACTUAL | DATE
1 10 9.00 21-01-20
1 11 8.80 21-01-20
1 11 9.00 22-01-19
My goal is a chart that gets its metric value from group by DATE, merchant_id
Now how is it possible to calculate the average discount of all products from a manufacturer at a particular merchant without also grouping by PROD_ID?
SELECT
date,
merchant_id,
1- (t.PRICE_ACTUAL / p.PRICE_RECOM)*100 AS discount2
-- (1 - ROUND(AVG(PRICE_ACTUAL),2) / p.PRICE_RECOM)*100 AS discount
FROM
merchants_prices t
INNER JOIN produts_manufacturers p ON t.PROD_ID = p.PROD_ID
WHERE
p.MANUFACTURER_ID = 1
AND PRICE_ACTUAL IS NOT NULL
GROUP by
date,
MERCHANT_ID
ORDER BY
date desc, merchant_id
To calculate average discount per merchant and day:
SELECT `DATE`, MERCHANT_ID, ROUND(AVG(discount), 2) as discount
FROM (
SELECT mp.`DATE`, mp.MERCHANT_ID, 1- (mp.PRICE_ACTUAL / pm.PRICE_RECOM)*100 AS discount
FROM produts_manufacturers pm
JOIN merchants_prices mp ON mp.PROD_ID = pm.PROD_ID
-- WHERE pm.MANUFACTURER_ID = 1 -- If you want to restrict to a specific manufacturer
) as x
GROUP BY `DATE`, MERCHANT_ID;
With your dataset ->
DATE MERCHANT_ID discount
2021-01-20 10 -89
2021-01-20 11 -87
2022-01-19 11 -89
To help you I made a fiddle here

How to do arithmetic group by quarter mySQL?

I have 2 tables which have not relation both of them.
Table of income
Id Category Nominal Description Date
---- -------- -------- -------- --------
1 ADD 10000 Q1 2020-03-05
2 DD 15000 Q2 2020-05-11
3 PAD 5000 Q3 2020-08-10
Table of outcome
Id Category Nominal Description Date
---- -------- -------- -------- --------
1 ADD 7000 Q1 2020-03-20
2 DD 10000 Q2 2020-06-02
3 PAD 2000 Q3 2020-08-28
So, I want to do subtraction of nominal from income with nominal from outcome group by quarter.
Here is my query :
CREATE view Total AS
SELECT QUARTER(outcome.date) AS Qperiod, income.nominal-outcome.nominal AS remain
FROM income, outcome
GROUP BY YEAR(outcome.date), QUARTER(outcome.date)
this result shown below, it describe that first row in income table subtraction by all outcome nominal.
Qperiod remain
---- --------
1 3000
2 0
3 8000
Can anyone help me to solve this?
You have no JOIN condition in your query, so each row of income gets matched to every row of outcome. Since you have no aggregation function, this effectively means that a random row from outcome is subtracted from each row of income. You should use modern, explicit JOIN syntax and put in the appropriate JOIN condition, which is that the year and quarter are the same in both tables:
CREATE view Total AS
SELECT QUARTER(outcome.date) AS Qperiod, income.nominal-outcome.nominal AS remain
FROM income
JOIN outcome ON YEAR(outcome.date) = YEAR(income.date)
AND QUARTER(outcome.date) = QUARTER(income.date)
GROUP BY YEAR(outcome.date), QUARTER(outcome.date)
Output:
Qperiod remain
1 3000
2 5000
3 3000
Demo on SQLFiddle

SQL query for full outer join

I have 3 tables a, b, and c.
Table a contains the ids of the stores, their earning date, and count of sale (flight tickets).
Table b contains the id, sale date, and count of clothing orders.
Table c contains the id, date and total count.
SQL> select * from a;
STOREID EARNINGDATE COUNT_FLIGHT_TICKETS
-------------------- ----------- ----------------
store01 14980000 10
store01 14980001 32
store02 14980000 134
SQL> select * from b;
STOREID EARNINGDATE CLOTHES_SALE_COUNT
-------------------- ----------- ---------------
store01 14980000 6
store02 14980000 6
SQL> select * from c;
STOREID EARNINGDATE TOTAL_SALE_COUNT
-------------------- ----------- -------------
store01 14980001 32
store01 14980000 16
store02 14980000 134
Given above the tables, I have to print all the stores ids, with their date of earning for total sale, flight sale, and clothing sale.
|StoreId | EarningDate | FlightCount | ClothingCount | TotalCount |
I have used below query, but failing to get the above.
select b.storeId , sum(a.COUNT_FLIGHT_TICKETS),
sum(b.CLOTHES_SALE_COUNT), sum (c.TOTAL_SALE_COUNT)
from a
full outer join b on a.storeId = b.storeId
and a.EarningDate = b.earningdate
full outer join c on a.storeId = c.storeId
and a.earningDate = b.earningDate group by a.storeId;
This query does not give all the rows and having some bug.
STOREID flight clothing total
------ --------- --------- --------------------
store02 134 6 134
store01 52 12 48
Can someone help me to correct this query to get the expected output?
One option would be to take the UNION of the thee tables and then aggregate by store:
SELECT
t.STOREID,
t.EARNINGDATE,
SUM(t.COUNT_FLIGHT_TICKETS) AS FlightCount,
SUM(t.CLOTHES_SALE_COUNT) AS ClothingCount,
SUM(t.TOTAL_SALE_COUNT) AS TotalCount
FROM
(
SELECT
STOREID,
EARNINGDATE,
COUNT_FLIGHT_TICKETS,
0 AS CLOTHES_SALE_COUNT,
0 AS TOTAL_SALE_COUNT
FROM a
UNION ALL
SELECT STOREID, EARNINGDATE, 0, CLOTHES_SALE_COUNT, 0
FROM b
UNION ALL
SELECT STOREID, EARNINGDATE, 0, 0, TOTAL_SALE_COUNT
FROM c
) t
GROUP BY
t.STOREID,
t.EARNINGDATE
This gets around the join problem you correctly pointed out, which might require a full outer join. Full outer join in MySQL is a hassle, and in any case it usually should not be necessary with good design.
Demo here:
Rextester
Assuming that for each table, (storeId, earningdate) is unique or compound keys, group by will be unnecessary.
You can try this query.
select
IF(isnull(a.STOREID), IF(isnull(b.STOREID), c.STOREID, b.STOREID),a.STOREID) as StoreId,
IF(isnull(a.EARNINGDATE), IF(isnull(b.EARNINGDATE), c.EARNINGDATE, b.EARNINGDATE),a.EARNINGDATE) as EarningDate,
IF(isnull(COUNT_FLIGHT_TICKETS),0,COUNT_FLIGHT_TICKETS) as FlightCount,
IF(isnull(CLOTHES_SALE_COUNT),0,CLOTHES_SALE_COUNT) as ClothingCount,
IF(isnull(TOTAL_SALE_COUNT),0,TOTAL_SALE_COUNT) as TotalCount
from a full outer join b
on a.storeId = b.storeId and a.EarningDate = b.earningdate
full outer join c
on a.storeId = c.storeId and a.earningDate = c.earningDate;
Result was:
STOREID EarningDate flight clothing total
------ ------------- --------- --------- --------------------
store01 14980000 10 6 16
store02 14980000 134 6 134
store01 14980001 32 0 32
Is this your expected result?
I think you forgot the earning date.
Given above the tables, I have to print all the stores ids, with their
date of earning for total sale, flight sale, and clothing sale.
|StoreId | EarningDate | FlightCount | ClothingCount | TotalCount |

How to calculate the difference between purchase and sale quantity?

How can I calculate the difference between the purchase and sale quantity in one query using Ms Access database?
My data, for example, looks like this:
ProductId Type Quantity
1 Purchase 24
1 Sale 1
How would I get the difference of (24-1=23) in one query?
I suppose you have the database name [DB-NAME].
and Columns and rows are something like.
[Table1]
ProductID Quantity Purchase Sale
----------- --------- --------- --------
1 1 24 1
2 100 50 10
If you want to calculate the [Purchase] - [Sale] for a specific Product id Use:
( Select (Purchase - Sale) AS MyNumber FROM[DB-Name].[Table1] WHERE (ProductID=1))
//where 1 is your product id
The result table will be
MyNumber
--------
23
if you want to calculate the totals for all [ProductID] Use:
(Select (SUM(Purchase) - Sum(Sale)) AS MyNumber FROM[DB-Name].[Table1] )
The result table will be
MyNumber
--------
63
You can self-join the table:
SELECT p.productId, (p.quanity - COALESCE(s.quantity, 0)) difference
FROM table p
LEFT JOIN table s
ON p.type = 'Purchase' AND s.type = 'Sale' AND p.productId = s.productId