How to SUM the values from group in SSRS which are distinct - reporting-services

I have a report where I want to calculate the Grand total but ignore the values of a group which have same value. Below are my tables and the data. The Package No column is grouped and will be unique always, but every unique Package may or may not have same dimensions. I want to SUM the dimension (Length, Width and Height) and it Package No has multiple items in it then have to consider only the first row for summing up the values as the dimensions will remain the same for all the items within the same Package. Can anyone please help me to achieve this result?

Assuming the data that gets returned from your dataset query looks like the sample you show in the image then you should be able to do something like...
=SUM(IIF(Fields!RowNum.Value = 1, Fields!Length.Value, 0))
EDIT after OP update
If the RowNum column is not in your dataset then you will need to add it.
If you dataset query is just a script then it should be as easy as something like this...
SELECT *
, RownN = ROW_NUMBER() OVER(PARTITION BY PackageNo ORDER BY Item)
FROM myTable
If the dataset is the result of a stored proc then you might have to change the dataset query to something like
CREATE TABLE #t (PackageNo varchar(50), Length int, Width int, Height int, Item varchar(50))
INSERT INTO #t
EXEC myStoredProc
SELECT *
, RownN = ROW_NUMBER() OVER(PARTITION BY PackageNo ORDER BY Item)
FROM #t
Once one of the above options are been taken then you should be able to simply apply the =SUM(IIF(Fields!RowN.Value = 1, Fields!Length.Value, 0))
expression in your report totals.

Related

SSRS access fields in a dataset

I want to write a SSRS expression that will allow me to grab the value from Column B base on the max value from ColumnA.
For Example, I have the following values
ColumnA
ColumnB
1
Test
2
Tester
3
Testing
=FIRST(
iif(
Fields!ColumnA.Value= MAX(Fields!ColumnA.Value,"test"),
Fields!ColumnB.Value,0
),"test"
)
The reason I am doing this is because I am trying to combine to datasets in one table. Certain fields in the table just needs to select top N values from another dataset.
I think that easiest way is to add something like row_number into your sql query (row_number() over (order by ColumnA desc) rn and then you can have condition iif(fields!rn.value = 1, fields!ColumnB.value,0)

MYSQL error causing calculated result to be written to next row

Using MYSQL 5.7
This query creates a new table with the columns order_month and SKU plus the calculated columns qty_mth, count_mth and avg_month. The resulting table correctly reflects the first 4 columns but the final column (avg_month), while being correctly calculated is written on the next row.
CREATE TABLE tbl_temp_si_trans4
(temp_si_trans4id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY temp_trans4idkey (temp_si_trans4id),
INDEX index1 (order_month,SKU))
SELECT order_month, SKU,
#qty_mth := SUM(net_qty_after_refund) AS qty_mth,
#count_mth := COUNT(DISTINCT(order_year)) AS count_mth,
#avg_month := #qty_mth/#count_mth AS avg_month
FROM order_trans4
GROUP BY order_month, SKU
Please see below example of result:
I have tried to following modifications to the avg_month calculation line with the same result.
(#qty_mth/#count_mth) AS avg_month and
#qty_mth/#count_mth AS avg_month
From the documentation:
As a general rule, you should never assign a value to a user variable
and read the value within the same statement. You might get the
results you expect, but this is not guaranteed.
You can use subqueries without using user defined variables to achieve the effect you are looking for however. Something similar to the following
SELECT x.total_sale,
x.f1 / x.total_sale AS f1_percent
FROM (
SELECT s.f1,
s.f1 + s.f2 AS total_sale,
FROM sales s
) x

Getting Scope error on SUM(IIF()) of rows from other table

Currently getting a scope error using the code below, what we're trying to do is count the number of rows that match the conditions we have:
=
SUM(
IIF(
Fields!Defect_Category.Value = "Packaging"
& Fields!Defect_Category.Value = "Major"
& Fields!WorkOrderDisplayID.Value = Fields!Work_Order_Id.Value,
1, 0),
"dsDefects"
)
Work_Order_Id is the "key" of the dsGeneral dataset which is the current scope/dataset of the tablix where we're trying to implement this. Any way we can fix this?
My understanding is that the Scope parameter of SUM is referring to the dataset we're trying to get the sum of (or count of, in this case). When I specify "dsDefects" as the scope of SUM, I get the following error:
The Value expression for the text box 'Textbox101' refers to the field
'Work_Order_Id'. Report item expressions can only refer to fields
within the current dataset scope or, if inside an aggregate, the
specified dataset scope. Letters in the names of fields must use the
correct case.
However, if I remove the scope parameter value, I'm getting the following error:
The Value expression for the text box 'Textbox101' refers to the field
'Defect_Category'. Report item expressions can only refer to fields
within the current dataset scope or, if inside an aggregate, the
specified dataset scope. Letters in the names of fields must use the
correct case.
I think you will need something like this...
=
IIF(
Fields!Defect_Category.Value = "Packaging"
& Fields!Defect_Category.Value = "Major",
LOOKUPSET(Fields!WorkOrderDisplayID.Value, Fields!WorkOrderDisplayID.Value, Fields!WorkOrderDisplayID.Value, "dsDefects").Length,
0)
I'll give a simple example of counting matches from another dataset and that might help you put the two together to get an solution.
If I create two datasets with the following queries, called dsEmp and dsDev respectively
dsEmp
DECLARE #e table (empid int, empname varchar(10))
insert into #e values
(1, 'Bob'), (2, 'Dave')
SELECT * FROM #e
dsDev
declare #d table(empid int, device varchar(10))
insert into #d VALUES
(1, 'Phone'),
(1, 'Laptop'),
(1, 'Desktop'),
(2, 'Phone'),
(3, 'Tablet')
SELECT * FROM #d
Then in my report I add a table bound to dsEmp showing the empID and empName and then in the final column I use the following expression
=LookupSet(
Fields!empid.Value,
Fields!empid.Value,
Fields!empid.Value, "dsDev"
).Length
I get this final output
As lookupset returns a collection, the collection's length is, in fact, the number of items contained in the collection.

SQL - Get row at certain %

I am trying to work out if this is possible:
I would like to add all of the values of a row up via SUM, so for example:
120
6239
2810
123
This would be SUM() and would equal 9292.
I would then like to run a query that selects the specific row that is equal to a percentage. So say I have 79.612%, I would like to select the row that matches this percentage of the total.
Any idea?
Please use the below code. Its working fine with SQL Server 2012 .
DECLARE #Table TABLE (Value int)
INSERT INTO #Table
(Value)
VALUES
(120),(6239),(2810),(123)
Select t.Value FROM #Table t
GROUP BY t.[Value]
HAVING (100*t.value)/(Select sum(t.Value) FROM #Table t) >79.612
Took a crack at answering your question based off the information provided but as our comments suggest we need more information around where the percentage comes from.
Select * from yourTable -- The table you want to match the percentage table
where SomePercentage = select (sum(value) /
(select sum(value) from yourTable2)) * 100 -- Total value used as upper bracket of percentage
from yourTable2 where someValue like '123' -- Whatever you want to base the percentage off
Edit to match updated requirements where the percentage is passed in and we get the value closest? to the figure.
Select top 1 * from yourTable where
value <= (Select (Sum(somevalues) * #In_Percentage)/100 from yourTable2)
order by value desc

How to get the max value of a field without grouping in MySql

I have a query with many wheres and orders criterias. One of the fields of the select is 'price' (element price) but I would like to have also (in every row) the maximun price of all the selected elements.
I tried to include MAX aggregate function on select hoping that this will return desired value, but insetead of that, price and MAX(price) returns the same. Searching into MySql doc I found the reason:
If you use a group function in a
statement containing no GROUP BY
clause, it is equivalent to grouping
on all rows.
Is there a way to solve this problem?
Thanks in advance!
There's a similar question (but not resolving this): find max value without aggregate operator in mysql
You can do this:
SELECT
id,
price,
(SELECT MAX(price) FROM your_table) AS max_price
FROM your_table
I'm not sure why you'd want to return that value on every row though... I'd probably do this in two separate queries, or else use a UNION ALL:
SELECT id, price FROM your_table
UNION ALL
SELECT NULL, MAX(price) FROM your_table
Maybe you could use a stored procedure like so:
CREATE PROCEDURE QueryWithMax ([parameter list if necessary])
BEGIN
-- Obtain the maximum price
DECLARE x INT UNSIGNED; -- change datatype if appropriate
SET x = SELECT
MAX(price)
FROM
...
WHERE
...
;
-- Now do your query
SELECT
price,
[other columns],
x AS MaxPrice
FROM
...
WHERE
...
GROUP BY
...
;
END
I haven't tried this, but if you had a subquery that extracts the maximum price (so you get one row), and then do a cross join (cartesian product) with it. You should get something like what you want. I can't vouch for how fast this would be.