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
Related
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)
I have a query to result in the below data from DB
But actually, I want to get the data as
Is there any way we can do this the SSRS?
How can I do this? really stuck
You can do this easily in SSRS without changing your dataset.
Add a Matrix control to your report
Drag the Assignee field to the Rows placeholder
Drag the Priority_n field to the Columns placeholder
Drag the Count_C field to the Data placeholder
that's it. If this does not help, let me know and I will update with sample images.
Please recheck the column names;
SELECT * FROM (
SELECT
[Assignee],
[Priority_n],
[Count_C]
FROM dataset
) Results
PIVOT (
SUM([Count_C])
FOR [Priority_n]
IN (
[High],
[Medium],
[Low]
)
) AS PivotTable
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.
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
I have an ssrs report, in which the user can choose the type of the chart he needs(pie,line,.., table).if the user chooses "table", another parameter must be chosen (#ListOfColumns). if I choose "table", I can see the list of fields and generate the report. When I choose another type of chart, the list of fields is empty(as it should be),but cannot generate the report due to "#ListOfColumns" parameter needs a value.I tried to set this parameter to blank and null, but didn't work. before adding "table" and cascading parameter, everything was working fine.
detail:
#ChooseChartType parameter is integer, with "specify values":1,2,3,4(4 is table).
#ListOfColumns dataset is:
if(#ChooseChartType=4)
SELECT 1 ID, 'TypeOfTest' AS ColumnName UNION
SELECT 2 ID, 'NoOfTest' AS ColumnName UNION
SELECT 3 ID, 'Priority' AS ColumnName UNION
SELECT 4 ID, 'Purpose' AS ColumnName UNION
SELECT 5 ID, 'TypeOfRoute' AS ColumnName UNION
SELECT 6 ID, 'CreatedBy' AS ColumnName
Add IF conditions to your dataset then just remove #ListOfColumns from your dataset script if it's not needed.