Reporting Services - show multiple values for a column horizontally rather than vertically - reporting-services

I have a report where row data can have the same data, apart from the data in the last column. Just adding the data to a table results in this:
Column 1
Column 2
Column 3
Column 4
1
abc
1111
234345
1
def
2222
435656
1
def
2222
423233
1
xyz
1234
145423
I want to show the data like this, where if a row has multiple values for Column 4 value, additional Column 4's are added horizontally:
Column 1
Column 2
Column 3
Column 4
Column 4
1
abc
1111
234345
1
def
2222
435656
423233
1
xyz
1234
145423
I've tried adding a Parent Group to Column 4, which is close to what I want, but every row is given it's own column for the Column 4 value so it ends up like this:
Column 1
Column 2
Column 3
Column 4
Column 4
Column 4
Column 4
1
abc
1111
234345
1
def
2222
435656
423233
1
xyz
1234
145423
etc...
Is there a way to achieve the layout I require?

You can do this with a small change to your dataset query.
Here I have recreated your sample data table as a table variable called `#t' . Then I query the table and add a column which gives us a unique index for each 'Column4' value within each Column1-3 group
DECLARE #t TABLE (Column1 int, Column2 varchar(10), Column3 int, Column4 int)
INSERT INTO #t VALUES
(1, 'abc', 1111, 234345) ,
(1, 'def', 2222, 435656) ,
(1, 'def', 2222, 423233) ,
(1, 'xyz', 1234, 145423)
SELECT
*
, ROW_NUMBER() OVER(PARTITION BY Column1, Column2, Column3 ORDER BY Column4) as Col4Index
FROM #t
Now in your report, add a matrix with one rowgroup. This will group on Column1, Column2 and Column3
Now add a column group that is grouped on Col4Index
Add your first 3 columns to the matrix making sure they are all in the single rowgroup (just add additional columns inside the group first and then select the correct field for each.
Drop the Column4 field into the [Data] placeholder and finally set the header for this column to an expression (optional) like this
="Column4_" & Fields!Col4Index.Value
The report design looks like this
The final output looks like this

Related

How can I copy rows from one to another table with a different colnm data

I had two tables Table 1 & Table 2 AS shown here
Table:1
ID
IMG_PATH
CAT_ID
166
hfhbf
1
164
jgj
2
162
ggd
1
160
mfnf
1
158
dbd
2
Table:2
ID
IMG_PARENT_ID
Here I want to print table 1's ID column data Example:166
Here (ID-1) Example:165
Here I want to print table 1's ID column data Example:164
Here (ID-1) Example:163
Here I want to print table 1's ID column data Example:162
Here (ID-1) Example:161
Here I want to print table 1's ID column data Example:160
Here (ID-1) Example:159
Here I want to print table 1's ID column data Example:158
Here (ID-1) Example:157
AS SHOWN IN TABLE 2 I NEED FOLLOWING VALUE...
and dont try this manually method:
INSERT INTO tabla2
SELECT * FROM tabla1
WHERE id = 1 //Here we write the condition
I want to fetch data because arround 10,000's row are inserted in this table
Lots of tries but didnt get it
based on what you provided info about your question, this is what I understand about this.
Assuming that table 1 is auto_increment with ID of 1-10,000.
Then you can use this to select the even IDs in table 1 and insert it to table 2
insert into table2 (ID) select ID from table1 group by ID having mod(ID, 2) = 0;
To select odd IDs from table 1 and insert it to table 2 you can use this
insert into table2 (IMG_PARENT_ID) select ID from table1 group by ID having mod(ID, 2) = 1;

SSRS-Problem in Comparing rows under a group and show it Matched or Unmatched

I have data looks like below, and am grouping at Column1. I need to compare column2 with in Group and highlight if the values for column2 is same or different in Column3-
Column1 Column2 Column3
123 111
123 111
1234 2222
1234 2222
1234 3333
I am using expression in Column3 as below. As you can at last "Column1" I have mentioned it considering it will group and then compare.
=IIF(Fields!Column2.Value = Previous(Fields!Column2.Value), "Same", IIF (Fields!Column2.Value <> Previous(Fields!Column2.Value), "Different")), "Column1"
My expectation is as below, i.e. column3 should populate if all value of Column2 is same under a group (column1 grouping) then Column3 should populare same else different for all rows under a group
Column1 Column2 Column3
123 111 Same
123 111 Same
1234 2222 Different
1234 2222 Different
1234 3333 Different
You can use CountDistinct for this.
If we assume your Column1 'RowGroup' is called grpCol1 then the expression in column 3 would look something liek this.
= IIF(CountDistinct(Fields!Column2.Value,"grpCol1") >1 , "Different", "Same")
Basically this says.. Count how many different Column2 values there are within the the row group grpCol1
So for the first two rows in your example it would return 1 as there is only 1 distinct value, for the next three rows, it would return 2 as there are 2 distinct values.
Note: grpCol1 or whatever your row group is called must be within qoutes and is case sensitive.
A simpler way would be:
=IIF(Fields!Column2.Value = Fields!Column1.Value, "Same", "Different")

Grouping by multiple columns with equal priority in MySQL

I have a table of data, and I want to be able to output all the data that does not repeat the contents of two columns, an illustration:
Example table:
Column1 | Column2 | Column3
1 XX pop
2 YY yif
3 ZZ pop
4 PP pop
5 XX pop
6 YY yif
7 PP Hor
8 MM tre
9 PP pop
10 XX pop
11 MM pop
What I want to do here is output all rows where the values for both Columns 2 and 3 are not repeated, so you will see that Column1 == 1 and Column1 == 5 have the same values in both Column2 and Column3, so they only need to output where Column1 == 1 and not =5 (or =10).
I would like to set the query to output all rows, except where Column1 == 5,6,9,10 because these have both Column2 and Column3 repetition.
Intended output of query: (note: all three columns are required)
Column1 | Column2 | Column3
1 XX pop
2 YY yif
3 ZZ pop
4 PP pop
7 PP Hor
8 MM tre
11 MM pop
I have tried looking into Group By but this only appears to group by one column, or at least groups them from left to right so GROUP BY (Column2, Column3) would GROUP BY all values in Column2 and then values in Column3 rather than treat both columns equally.
I found one possible solution which was to concat the columns beforehand, such as
GROUP BY CONCAT(Column2, '_', Column3)
From Is it possible to GROUP BY multiple columns using MySQL? but this has been vaguely criticised (at least, as an answer to that question), but seems the closest code I've seen to do what I want to do.
So, how should I structure my GROUP BY clause to reach this outcome?
Is the GROUP BY CONCAT I have found a good way of approaching this?
GROUP BY on multiple columns is possible. Depending on the result you want, you have to apply extra GROUP functions to the rest of your data. In your case it looks like you want the first column1 index, and unique combinations of (column2,column3)
The following query should do the trick:
SELECT MIN(column1) AS column1,column2,column3
FROM table1
GROUP BY column2,column3
ORDER BY MIN(column1) ASC;
If you don't care about Column1 at all, you don't even need GROUP BY. Just use DISTINCT
SELECT DISTINCT Column2, Column3
FROM the_table
ORDER BY however_you_want
;
Otherwise, Norbert's answer is probably more fitting.

Combine column results in SQL Server

I have a functionality where user can select table(only one at a time) and based on that table columns will appear. User can select multiple columns so what I want is,when user selects column(s) then it should return the result of selected columns. Now as per my table if user selects Colum_1 which has unique values then I'll allow user to select that but if user selects Column_2 I'll prompt the message by saying that 'Select another column or more than one', and then in background I'll create combination for those selected columns.
Example:
Table name = `TestTable`
Column_1 Column_2 Column_3
-------- -------- --------
1 1 ab
2 2 bc
3 1 bc
Expected results:
Column_1 = 1 2 3
Column_1, Column_2 = 1 1, 2 2, 3 1 (combination of two columns)
Column_1, Column_2, Column_3 = 1 1 ab, 2 2 bc, 3 1 bc (combination of three columns)
I've tried a query but it is useful for one column name only. I'm not sure if user selects multiple columns then how to handle that.
My query:
declare #colCount bigint, #uniqColCount bigint, #result nvarchar(max)
select #colCount = count(Column_1) from TestTable
select #uniqColCount = count(distinct Column_1) from TestTable
if(#colCount = #uniqColCount)
begin
set #result = (select Column_1 from TestTable)
print #result
end
else
print 'false'
I need to achieve for-each kind of logic in SQL.
You could use a While condition to solve this. Here is a reference for more details.
https://msdn.microsoft.com/en-CA/library/ms178642.aspx

MySQL query (condition)

I want to achieve one thing though I'm not sure if it's possible.
So let's say I have a table with few columns, but only two of them are of interest to me right now. Example of table:
Column 1 | Column 2
blabla | blablahhhhh
wor154 | blablahhhhh
word123 | word12435564
something | some4565
What I want to achieve, is to select all fields where first 5 or more symbols of value of Column 2 don't match with first 5 or more symbols of value of Column 1. So I don't want to select rows where 5 or more symbols of value of Column 1 match 5 or more symbols of value of Column 2. In example, query should return only 2nd and 4th rows
So, is it possible and if it's, how it can be achieved. Thank you.
I'd go with a SUBSTRING():
SELECT col1 FROM table WHERE SUBSTRING(col1, 1, 5) <> SUBSTRING(col2, 1, 5);
You can use something similar to this:
select *
from table1
where substring(column1, 1, 5) != substring(column2, 1, 5)
See SQL Fiddle with Demo