Put Null results of SSRS column group to the last - reporting-services

I am struggling with a challenge in SSRS and your help would be appreciated.
I have a dataset of hours and minutes in two separate columns as shown below.
---------------------------------
|A_Service_Hours| Service_Minute|
| 5 | 52 |
| 5 | 54 |
| 5 | 56 |
| 6 | 20 |
| 6 | 22 |
| 6 | 27 |
| 6 | 29 |
| 6 | 46 |
| 6 | 51 |
| 6 | 58 |
|-------------------------------|
I want to group all hours in the SSRS row group and minutes in the column group. and I want the result to be sorted like the below table. how can I achieve this in SSRS?
---------------------------------------------------
| 5 |52 | 54 | 56 | | | | |
| 6 |20 | 22 | 27 | 29 | 46 | 51 | 58 |
|--------------------------------------------------

If you add a row number to each record you can do this easily.
Make your dataset query something like
SELECT
A_Service_Hours, Service_Minute
, ROW_NUMBER() OVER(PARTITION BY A_Service_Hours ORDER BY Service_Minute) AS RowN
FROM myTable
This will give you the following results
----------------------------------------
|A_Service_Hours| Service_Minute| RowN|
| 5 | 52 | 1|
| 5 | 54 | 2|
| 5 | 56 | 3|
| 6 | 20 | 1|
| 6 | 22 | 2|
| 6 | 27 | 3|
| 6 | 29 | 4|
| 6 | 46 | 5|
| 6 | 51 | 6|
| 6 | 58 | 7|
|--------------------------------------|
You can now add a matrix control to your report, use A_Service_Hours as the RowGroup and RowN as the ColumnGroup.
This will give you exactly what you want.
This is done from memory so if it does not work, add a comment and I'll edit the answer when I get time.

I could come up with 2 slightly different representation of your req. If this works for you, let me know I will update my answer with how to achieve it.
Below Screenshot for help. Left had is original data and other two are represented results

Related

MYSQL adding columns value of rows

I have a problem in which I have a STUDENT table as
+-------------+-------------+-------------+-------------+---------------+
| roll_number | name | subject_one | subject_two | subject_three |
+-------------+-------------+-------------+-------------+---------------+
| 1 | Sheila | 32 | 48 | 64 |
| 2 | Rachel | 24 | 21 | 25 |
| 3 | Christopher | 55 | 12 | 10 |
+-------------+-------------+-------------+-------------+---------------+
I want the print the output as
+-------------+-------------+-------------+
| roll_number | name | total |
+-------------+-------------+-------------+
| 1 | Sheila | 144|
| 2 | Rachel | 70 |
| 3 | Christopher | 77 |
+-------------+-------------+-------------+
and select all student having marks greater than 75 ??
How can I achieve this using MYSQL ??
I think you just need the aggregate functions and using them is enough. I am not sure if it can help you or not.
SELECT roll_number , name , (subject_one + subject_two + subject_three) AS total FROM STUDENT HAVING total > 75 ;

MySQL pivot from 3 tables

I'm trying to build a pivot table from three tables. I've been reading for example MySQL pivot with 3 tables but seem to get nowhere. Or have I been wasting my time and should have built the output in the programming language from the results of three queries instead of doing it in a single SQL query?
My tables are:
Table 1:
id | code | description etc.
1 | paraA1 | ...
2 | paraA2 | ...
3 | paraB1 | ...
4 | paraB2 | ...
5 | paraB3 | ...
....
n | paraZn | ...
Table 2:
id | tr | set | etc.
1234 | 11 | 86 | ...
1235 | 13 | 86 | ...
1236 | 14 | 86 | ...
1237 | 18 | 86 | ...
1238 | 11 | 87 | ...
1239 | 12 | 87 | ...
1240 | 13 | 87 | ...
Table 3:
id | table1_id | table2_id | value
345 | 1 | 1234 | 20
346 | 3 | 1237 | 17
347 | 2 | 1235 | 42
348 | 5 | 1235 | 33
And the output table I want is built with these restrictions:
table1.code as columns, more rows could be added in this table so it should be done dynamically
table2.tr values as rows WHERE set = x (i.e. for one set at the time)
table3.value values as cell contents
also blank rows and columns are included (in this example row 14 and column paraB2)
So the output from the above tables for set 86 would be
id | paraA1 | paraA2 | paraB1 | paraB2 | paraB3 | ... paraZn | set
11 | 20 | | | | | | 86
13 | | 42 | | | 33 | | 86
14 | | | | | | | 86
18 | | | 17 | | | | 86
Database structure for source tables unfortunately cannot be modified.

MySQL get multiple rows into columns

I have a table called visits where concat(s_id, c_id) is unique and id is the primary key. s_id is the ID number of a website and c_id is a campaign ID number. I want to show all the hits each campaign is getting and group by the site. I want each site on a single row
+-----+------+------+------+
| id | s_id | c_id | hits |
+-----+------+------+------+
| 1 | 13 | 8 | 245 |
| 2 | 13 | 8 | 458 |
| 3 | 13 | 3 | 27 |
| 4 | 13 | 4 | 193 |
| 5 | 14 | 1 | 320 |
| 6 | 14 | 1 | 183 |
| 7 | 14 | 3 | 783 |
| 8 | 14 | 4 | 226 |
| 9 | 5 | 8 | 671 |
| 10 | 5 | 8 | 914 |
| 11 | 5 | 3 | 548 |
| 12 | 5 | 4 | 832 |
| 13 | 22 | 8 | 84 |
| 14 | 22 | 1 | 7 |
| 15 | 22 | 3 | 796 |
| 16 | 22 | 4 | 0 |
+----+------+------+-------+
I would like to have the following result set:
s_id | hits | hits | hits| hits
13 | 245 | 458 | 27 | 193
14 | 320 | 183 | 783 | 226
5 | 671 | 914 | 548 | 832
22 | 84 | 7 | 796 | 0
Here is what I have tried which does not pull all the hits columns back.
SELECT v.*, v2.* FROM visits v
INNER JOIN visits v2 on v.s_id = v2.s_id
GROUP BY s_id
How can I get multiple rows into columns?
If your'e data set is not crazy huge and you are just trying to get the multiple rows as a single row.... one way to do this...
SELECT
s_id,
GROUP_CONCAT(hits SEPARATOR ',') as hits_list
FROM
visits
GROUP BY s_id
Since it doesn't use any joins or subqueries etc, i find this way to be quite fast.
you can later split/explode the data based on the ',' separator in PHP or whatever language you are using.
$hits = explode($hits_list, ','); //get them in an array

mysql: If a value appears more than once, get values from the others similar rows and create new columns

Here is an example of my table patients:
id | case_id | patient_id | syndrome | age |
------------------------------------------------------
1 | 23 | 24 | stable | 45 |
2 | 24 | 25 | stable | 64 |
3 | 25 | 24 | coronary | 46 |
4 | 26 | 27 | stable | 73 |
5 | 27 | 24 | stable | 48 |
6 | 28 | 25 | coronary | 67 |
7 | 29 | 40 | coronary | 68 |
If patient_id appears more than once, i only want to show the first row and add extra columns to this row containing the values of the others similar patient_ids.
For example
id | case_id | patient_id | syndrome | age | syndrome2 | age2 | syndrome3 | age3
--------------------------------------------------------------------------------------------
1 | 23 | 24 | stable | 45 | coronary | 46 | stable | 48
2 | 24 | 25 | stable | 64 | coronary | 67 | |
7 | 29 | 40 | coronary | 68 |
Is this feasible using mysql code?
Currently, I have this code:
SELECT *, count(patients.patient_id) as PatientsNo FROM patients
GROUP BY patient_id
HAVING COUNT(patient_id) > 1
Any suggestions please?
Thank you in advance,
Zinon

MySQL Query for averages

good morning. I have this table:
mysql> select * from Data;
+---------------------------+--------+-------+
| affyId | exptId | level |
+---------------------------+--------+-------+
| 31315_at | 3 | 250 |
| 31324_at | 3 | 91 |
| 31325_at | 1 | 191 |
| 31325_at | 2 | 101 |
| 31325_at | 4 | 51 |
| 31325_at | 5 | 71 |
| 31325_at | 6 | 31 |
| 31356_at | 3 | 91 |
| 31362_at | 3 | 260 |
| 31510_s_at | 3 | 257 |
| 5321_at | 4 | 90 |
| 5322_at | 4 | 90 |
| 5323_at | 4 | 90 |
| 5324_at | 3 | 57 |
| 5324_at | 4 | 90 |
| 5325_at | 4 | 90 |
| AFFX-BioB-3_at | 3 | 97 |
| AFFX-BioB-5_at | 3 | 20 |
| AFFX-BioB-M_at | 3 | 20 |
| AFFX-BioB-M_at | 5 | 214 |
| AFFX-BioB-M_at | 7 | 20 |
| AFFX-BioB-M_at | 8 | 40 |
| AFFX-BioB-M_at | 9 | 20 |
| AFFX-HSAC07/X00351_M_at | 3 | 86 |
| AFFX-HUMBAPDH/M33197_3_st | 3 | 277 |
| AFFX-HUMTFFR/M11507_at | 3 | 90 |
| AFFX-M27830_3_at | 3 | 271 |
| AFFX-MurIL10_at | 3 | 8 |
| AFFX-MurIL10_at | 5 | 8 |
| AFFX-MurIL10_at | 6 | 4 |
| AFFX-MurIL2_at | 3 | 20 |
| AFFX-MurIL4_at | 5 | 78 |
| AFFX-MurIL4_at | 6 | 20 |
| U95-32123_at | 1 | 128 |
| U95-32123_at | 2 | 128 |
| U98-40474_at | 1 | 57 |
| U98-40474_at | 2 | 57 |
+---------------------------+--------+-------+
37 rows in set (0.00 sec)
If I wanna look for the average expression level (level) of each array probe (affyId) across all experiments, I do SELECT affyId, AVG(level) AS average FROM Data GROUP BY affyId;
However, I can't figure out how to look for the average expression level of each array probe (affyId) for each experiment... It must be something similar to the last query, but I don't obtain good results... any help?
PD: someone told me I should give some reputation or click to some green button if somebody solves my question... Is it right? How do I do it? I'm pretty new on this website...
This shows the average for every affyId:
SELECT affyId, AVG(level) AS average FROM Data GROUP BY affyId
This the average for every exptId:
SELECT exptId, AVG(level) AS average FROM Data GROUP BY exptId
and this the average for every exptId in every affyId:
SELECT affyId, exptId, AVG(level) AS average FROM Data GROUP BY exptId, affyId
Just add that to the group by clause
SELECT affyId, exptId, AVG(level) AS average
FROM Data
GROUP BY affyId, exptId;