How to substract group by total in SSRS? - reporting-services

In this Image I have three groups, the grand child group is pp
and this will always have two values 1 and 2 between these group there will be multiple rows for pp=1 and pp=2 now i have to substract pp=2 QTY total from pp=1 Total Please help me. on the last of group as grand total.

Use this expression:
=SUM(IIF(Fields!pp.Value = 1,Fields!IssueQty.Value,0) -
SUM(IIF(Fields!pp.Value = 2,Fields!IssueQty.Value,0)
The expression must be in the scope of the details or pp group.
Let me know if this helps.

Related

SSRS 2012 MATRIX TOTAL DYNAMIC COLUMNS BY COLUMN HEADING

Simple Q:
In SSRS 2012: require a matrix with column totals derived from the dynamic column headings;
e.g.
Period 1 2 3 Total Total
Type Act Bud Act Bud Act Bud Act Bud
Total 10 9 10 9 10 9 30 27
is this possible in SSRS?
Can easily get the total of the sum of columns -(57) but not split by type.
Thank You.
data is grouped ;
Business Unit - Row Group
Account Type - Row Group
Month Period - Column Group
Amount_Type (Act, Bud) - Column Group
The Aggregate is Amount
Tks
Without knowing the structure of your dataset I think you can add two columns outside the columns groups at the right side and use these expression to calculate the respective total:
For Actual total:
=SUM(IIF(Fields!Type.Value = "Act",Fields!Amount.Value,0))
For Budget total:
=SUM(IIF(Fields!Type.Value = "Bud",Fields!Amount.Value,0))
UPDATE: Try setting the scope of the SUM function.
=SUM(IIF(Fields!Type.Value = "Act",Fields!Amount.Value,0),"DataSetName")
Replace DataSetName by the actual name of your dataset.
Let me know if this helps.

Average value for top n records?

i have this SQL Schema: http://sqlfiddle.com/#!9/eb34d
In particular these are the relevant columns for this question:
ut_id,ob_punti
I need to get the average of the TOP n (where n is 4) values of "ob_punti" for each user (ut_id)
This query returns the AVG of all values of ob_punti grouped by ut_id:
SELECT ut_id, SUM(ob_punti), AVG(ob_punti) as coefficiente
FROM vw_obiettivi_2015
GROUP BY ut_id ORDER BY ob_punti DESC
But i can't figure out how to get the AVG for only the TOP 4 values.
Can you please help?
It will give SUM and AVG of top 4. You may replace 4 by n to get top n.
select ut_id,SUM(ob_punti), AVG(ob_punti) from (
select #rank:=if(#prev_cat=ut_id,#rank+1,1) as rank,ut_id,ob_punti,#prev_cat:=ut_id
from Table1,(select #rank:=0, #prev_cat:="")t
order by ut_id, ob_punti desc
) temp
where temp.rank<=4
group by ut_id;
This is not exactly related to the question asked, I am placing this because some one might get benefited.
I got the hackerearth problem to write mysql query to fetch top 10 records based on average of product quantity in stock available.
SELECT productName, avg(quantityInStock) from products
group by quantityInStock
order by quantityInStock desc
limit 10
Note: If someone can make better the above query, please welcome to modify.

How to use SQL MAX(SUM()) function

Hi this is driving me crazy consultation, such as selecting the maximum of this consultation, I made a temporary table to which the maximum then sack him, but quiciera know if the same query can be removed without temporary tables.
I have 2 tables N AND ACCRUED PAYROLL D
Table I occupy the payroll payroll 201314-201320
and table I need the EMPLOYEEIDNO accrued; cod_tiponomina; cod_nomina and accrued employee wages are earned in 14 days;
Good help is that I need to get the maximum of the salary calculation with the sum ();
thanks for the help.
SELECT MAX((SUM(d.devengado) / (COUNT(d.devengado)*14))*30) salario_30dias
FROM devengados d
JOIN nominas n
ON n.cod_nomina = d.cod_nomina
WHERE d.cod_empleado = 564
AND d.cod_tiponomina = 1
AND d.cod_nomina BETWEEN 201314 AND 201320
AND d.devengado > 0
GROUP
BY YEAR(n.fecha_cierre)
, MONTH(n.fecha_cierre);
'Max' and 'Sum' are always executed according to the Group By Clause.
You can nest 2 Selects to get the Maximum of Different Sums:
SELECT MAX(sum) FROM
(SELECT SUM(column) AS sum FROM table GROUP BY crit1)
GROUP BY sum
But a better Way would be to sort the sums, and pick the first one to achieve the same as a sourrounding MIN/MAX (It would not require the nesting of selects):
SELECT SUM(column) AS sum FROM table GROUP BY crit1 ORDER BY sum DESC LIMIT 0,1
(for MIN you would need to sort ASC)

Finding The Sum OF Two Values In SQL Server 2008

I have a table Usage and it contains the following columns
sl_No
usage_ID
energyItem_ID
qty
unit_ID
location_ID
p_Rate
Sometimes the same EnergyItem might be located at different locations..
During those conditions how can I get the sum of qty of an individual energyItem..
How to get the sum of the qty of energyItems?
If I've understood correctly, you're trying to find the quantity of each
energy item, regardless of its location, using information in a single table.
The following query will give you the energyItem_ID of each item followed by the total quantity of each item:
SELECT energyItem_ID,Sum(qty) as TotalQuantity
FROM Usage
GROUP BY energyItem_ID
ORDER BY energyItem_ID
If, on the other hand, you wanted the quantity of each energy item, broken down by location, you would need the following:
SELECT location_ID,energyItemID,Sum(qty) as QuantityByLocation
FROM Usage
GROUP BY location_ID,energyItemID
ORDER BY location_ID,energyItemID
The order by clauses make the result easier to follow, but are not strictly necessary.
Finally, the answer by marc_s will give you the quantity of a specific energyItem.
How about:
SELECT EnergyItem_ID, SUM(qty)
FROM dbo.Usage
WHERE EnergyItem_ID = 42 -- or whatever ....
GROUP BY EnergyItem_ID
Or what are you looking for?? The question isn't very clear on the expected output....
select a.usage_ID , b.sum(p_Rate) as total from Table_1 a
inner join Table_2 as b on a.usage_ID = b.usage_ID
group by a.usage_ID

Help with MySQL query... Need help ordering a group of rows

I can tell it best by explaining the query I have, and what I need.
I need to be able to get a group of items from the database, grouped by category, manufacturer, and year made. The groupings need to be sorted based on total amount of items within the group. This part is done with the query below.
Secondly, I need to be able to show an image of the most expensive item out of the group, which is why I use MAX(items.current_price). I thought MAX() gets the ENTIRE row corresponding to the largest column value. I was wrong, as MAX only gets the numeric value of the largest price. So the query doesnt work well for that.
SELECT
items.id,
items.year,
items.manufacturer,
COUNT(items.id) AS total,
MAX(items.current_price) AS price,
items.gallery_url,
FROM
ebay AS items
WHERE
items.primary_category_id = 213
AND
items.year <> ''
AND
items.manufacturer <> ''
AND
items.bad_item <> 1
GROUP BY
items.primary_category_id,
items.manufacturer,
items.year
ORDER BY
total DESC,
price ASC
LIMIT
10
if that doesnt explain it well, the results should be something like this
id 10548
year 1989
manufacturer bowman
total 451
price 8500.00 (The price of the most expensive item in the table/ not the price of item 10548)
gallery_url http://ebay.xxxxx (The image of item 10548)
A little help please. Thanks
I've had this same problem, and I'm fairly certain you have to do two queries (or a subquery, that's a matter of taste).
The first query is like what you have (except id isn't helping you).
The second query uses the GROUP BY fields and one (one!) MAX field to get the id and any other meta-data you need.
I believe this is the implementation, although it's hard to test:
SELECT
items.id,
items.year,
items.manufacturer,
items.gallery_url
FROM
ebay as items
NATURAL JOIN
(
SELECT
COUNT(items.id) AS total,
MAX(items.current_price) AS current_price,
items.primary_category_id,
items.manufacturer,
items.year
FROM
ebay AS items
WHERE
items.primary_category_id = 213
AND
items.year <> ''
AND
items.manufacturer <> ''
AND
items.bad_item <> 1
GROUP BY
items.primary_category_id,
items.manufacturer,
items.year
ORDER BY
total DESC,
price ASC
LIMIT
10
) as bigones
ORDER BY
bigones.total DESC,
bigones.current_price ASC
This documentation may help you understand what's going on:
http://dev.mysql.com/doc/refman/5.1/en/group-by-hidden-columns.html
... all rows in each group should have the same values for the columns that are ommitted from the GROUP BY part. The server is free to return any value from the group, so the results are indeterminate unless all values are the same.