SSRS access fields in a dataset - reporting-services

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)

Related

Find min date from multiple fields in ssrs

Hello I am trying to find min date value between multiple columns in ssrs.
For example If date value in column A is less then in column B and column B is not nothing and column A is less then column C and column C is not nothing then chose field A
If Not Isnothing(A) and Not Isnothing(B) and A<B and A<C then
A
else if Not Isnothing(B) and Not Isnothing(C) and b<A and B<C
B
...
end if
I will be very grateful if you help me.
I answered a question recently that had a similar answer here..
How to set dynamic axis unit in SSRS Reports??
However, if possible I would so this in your dataset query it will be much easier as you can simply do..
SELECT MIN(myField) FROM
(SELECT A as myField FROM myTable
UNION
SELECT B FROM myTable
UNION
SELECT C FROM myTable
) x

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

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.

Divide SELECT value by first entry found

I am fairly inexperienced with SQL, and I have a table that looks like this (simplified version):
ID | Dataset | date | value
I'm trying to divide each value by a baseline, which would be the first entry in the database for that particular dataset.
For example, for dataset1, if the value at 05/05/2018 is 28, and the first value in the database is 4 at 01/01/2018, then I want the result to be 28/4.
thing is that not every dataset was added to the database at the same time, so they have different dates for their baseline. So if dataset1 has its first entry at 01/01/2018, dataset2 might have its first entry at 02/02/2018.
How would I go about this query? I tried a simple div, but it seems like I can only divide by a single number, and not a value-by-value table.
I tried something like this:
SELECT DATE(date) as time, value, dataset / (
SELECT min(DATE(date)) as time, value, dataset FROM table GROUP BY dataset)
FROM table GROUP BY time, dataset
but I think SQL expects the denominator to be a single value in this case, not a table.
Check this out:
SELECT T1.Date
,T1.Value/T2.Value Value
FROM TempTable T1
INNER JOIN TempTable T2
ON T2.Id
=
(SELECT T2.Id
FROM TempTable T2
WHERE T1.Dataset=T2.Dataset
ORDER BY T2.Date
LIMIT 1
)
"I think SQL expects the denominator to be a single value in this case, not a table." You are on the right track, so the sub select needs to return a single value, below is an example:
SELECT
value, (select value from table b where a.dataset = b.dataset and b.dt = (select min(dt) from table c where b.dataset = c.dataset))
FROM
table a
In MySQL 8+, you would simply do:
select t.*,
t.value / first_value(t.value) over (partition by t.dataset order by t.date) as ratio
from t;

Sort columns based on data in tuples mysql

Say, I have a table
A B C D E F
1 2 4 3 6 5
4 2 3 1 6 5
4 5 3 6 1 2
How can one get an output based on rearranging based on its data. For example,
ABDCFE
DBCAFE
EFCABD
is it possible?
EDIT:
The question seems to be asking: How can I get the list of column names in order by value?
I got it. You want to sort the values in each row and show the names of the columns in order.
Let me assume that you have a row id, so you can identify each row. Then:
select id, group_concat(which order by val) as ordered_column_names
from ((select id, a as val, 'A' as which from t) union all
(select id, b, 'B' as which from t) union all
(select id, c, 'C' as which from t) union all
(select id, d, 'D' as which from t) union all
(select id, e, 'E' as which from t) union all
(select id, f, 'F' as which from t)
) t
group by id
order by id;
SQL is fundamentally not the tool to do the operation you describe, because it violates the concept of a relation. I don't mean the common use of "relation" meaning a relationship, I mean the mathematical definition of relation.
There is no order of columns in a relation. The columns are a set, which is by definition unordered. Columns are identified by their name, not their position left-to-right.
All the entries in rows under each respective named column must be part of the same data domain. If you mix them around on a row-by-row basis, you're violating this.
I guess all your columns A through F are actually using values in the same data domain, or else reordering them wouldn't make any sense. If this is true, then you're violating First Normal Form by defining a table with repeating groups of columns. You should instead have all six columns be in one column of a second table. Then it becomes very easy to sort them by value.
Basically, what you're trying to do is better solved by formatting the data results in some application code.
There is a way to do it ,get coulmn name by column ordinal order and print it.
For each value in coulmn iterate this and get the column name for the ordinal specified in cloumn data. Here ordinal position is value in each coulmn data. Iterate for each row and each column and your problem is solved.
select column_name
from information_schema.columns
where table_name = 'my_table_name' and ordinal_position = 2;
It appears that you are asking for output where each row in the output is just a specification of the order of the data values in the columns.
Then, if the values are always integers between 1 and 5, you can do it by outputting a character value of 'A' where the data value is 1, a 'B' where the data value is 2, etc. This SQL will do that.
Select Char(A+64)A, Char(B+64) B,
Char(C+64) C, Char(D+64) D,
Char(E+64) E, Char(F+64) F
From table
if the want the column sort order in one output column, you could also do this:
Select Char(A+64) + Char(B+64) +
Char(C+64) + Char(D+64) +
Char(E+64) + Char(F+64) SortOrder
From table

How to rotate single row to key-value table in SSRS 2012?

Using SSRS 2012 with SQL Server 2012. I have a result set in SQL Server 2012 which has the following fields as headers:
sumDeposits,
sumWithdrawals,
sumFees,
etc
This has been mapped to a Dataset in SSRS.
I would like a SSRS table or matrix where I can have the column header as column one and the data row (there is only one row in the result set) as column two in a table. Is this possible? Been struggling with it.
I should add that the way I am doing it now is to drag a Field from the Dataset onto the SSRS page, and then put a text box next to it. The number of fields is growing so I'd rather have a table.
It sounds like you want to unpivot the data from columns into rows. If that is the case, then you should be able to use the UNPIVOT function in SQL.
The basic structure of the query will be:
select col, value
from yourtable
unpivot
(
value
for col in (sumDeposits, sumWithdrawals, sumFees) -- your columns here
)unpiv;
If you have data that is not the same datatype, then you will have to convert the data for all columns to be the same type:
select col, value
from
(
select sumDeposits,
cast(sumWithdrawals as numeric(10, 2)) sumWithdrawals,
sumFees
from yourtable
) d
unpivot
(
value
for col in (sumDeposits, sumWithdrawals, sumFees)
) unpiv;
This type of query is exactly the same as using a UNION ALL with multiple calls to the same table:
select 'sumDeposits' col, sumDeposits as value
from yourtable
union all
select 'sumWithdrawals' col, sumWithdrawals as value
from yourtable
union all
select 'sumFees' col, sumFees as value
from yourtable