I am trying to make what I think is a very simple report, however I can't seem to get the result I want and I don't even know if it is possible in SSRS.
It's a list of student applicants and the courses they have applied for. Where student apply for more than 1 course, for example 3 A Levels (the maximum they apply for is 5), I want the subjects to appear in different columns rather than a list in one column. A simple table shows the report like this: 3 Columns: student ref, applications status, course(s)
I want it to look like this:
7 columns: Student ref, Application status, course1, course2, course3 etc
I have tried to use a Matrix but the best I can get is where each subject has its own column, so I have a lot of blank text boxes and a very long report as we have 50 plus courses on offer.50+ Columns: Student ref, Application status, English, Maths, Biology, sport, French etc
Assuming this will work with your database, then this should be a pretty simple solution.
A matrix is the way to go I think as as you said, if you have lots of courses with at least one application then the report will be very wide and in manageable.
So, I recreated your sample and then queried it, adding a column that calculated a 'choice number'. this 'choice number' will only ever be between 1 and 5 (based on you statement that no student applies for more than 5 subjects)
Here's the sample data and simple query beneath
DECLARE #t TABLE([Student] varchar(10), [Status] varchar(10), [Subject] varchar(20))
INSERT INTO #t
SELECT 'Stu001', 'OA', 'English' UNION
SELECT 'Stu001', 'OA', 'Maths' UNION
SELECT 'Stu002', 'OA', 'English' UNION
SELECT 'Stu002', 'OA', 'Biology' UNION
SELECT 'Stu002', 'OA', 'Sport' UNION
SELECT 'Stu003', 'OA', 'French' UNION
SELECT 'Stu003', 'OA', 'Chemistry' UNION
SELECT 'Stu003', 'OA', 'English' UNION
SELECT 'Stu004', 'OA', 'BTEC'
SELECT *
, SubjectChoiceNumber = ROW_NUMBER() OVER(PARTITION BY Student, Status ORDER BY Subject)
FROM #t
The SubjectChoiceNumber just assigns a sequential number. The sequence is sorted by the subject name.
This gives us the following output...
Now all you need to do is use a matrix control, set the column grouping to group by SubjectChoiceNumber and set the data cell expression to something like =FIRST(Fields!Subject.Value)
The report design looks like this
An the final output looks like this
Related
Requirement:
(Related to: MySQL)
I have set of unique names in column A, then column B,.. with each column containing a unique product.
Eg.
enter image description here
I expect an output, that considers the total of all Columns, under 1 single header and outputs something like this:
enter image description here
Is this possible? How do I go about that?
Thank you!
Not really sure how to go about this. Would concat/ Union work here?
select person, sum(item) from
(SELECT person_1 as person, shoes as item from YOURTABLE
union SELECT person_2 as person, watches as item from YOURTABLE
union SELECT person_3 as person, clothes as item from YOURTABLE)
temp group by person
I have a mySQL table with 100 rows and 6 columns namely ; full_name, name, score, city, gender, rating. I want the output as one column containing distinct city values (there are only 5 distinct cities initially & the user input value of his/her city will be added, namely; Delhi, Mumbai, Patna, Chennai ,Pune) and the second column having their respective avg score.
The database is linked to the python code which I am working on & use takes input which is stored in the above 6 columns. Then according to the user request, the output as analysis is showed as graphs using matplotlib. But I am really stuck at this part where I need to show a graph having X-VALUES as city names and Y-VALUES as respective avg score for that, I need to know the query to get such an output in mySQL where we get 2 columns storing the above.
How do I do it ?
SELECT city AS X,AVG(score) AS Y
FROM yourtable GROUP BY city
Is this, what you ment? Or if you want the result as one row, you add GROUP_CONCAT:
SELECT GROUP_CONCAT(X) AS gX,GROUP_CONCAT(Y) AS gY FROM
(SELECT city AS X,AVG(score) AS Y
FROM yourtable GROUP BY city) g
ok, redbull helps...
correct syntax :
select city, avg(score) from data group by city;
wrong syntax (what I was trying to do earlier) :
select data.distinct(city), data.avg(score) , check.check_city from data, (select name, distinct(city) as check_city from data) check where data.name = check.name and data.city in check.check_city;
Thanks Anyway !
I'm working in MS Access 2010 doing a series of UNIONs inside of a SELECT statement. After all of these UNIONs are complete, I need to add a couple of new columns using IIF statements (i.e., based on the value of a ch column). However, I can't seem to find out how or where to properly squeeze in this syntax so that the IIFs check against an entire column in the final 'unioned' table. Simply put, after the below syntax (which is trimmed and simplified for this question) runs, I want to then append a new variable column (e.g., 'asterisk') in this final table based on whether an existing column cell (e.g., 'grades') contains an asterisk. Help and patience is greatly appreciated -- I am just becoming familiar with the syntax principles of SQL.
SELECT * FROM(
SELECT class, teacher, student
grades2010 AS grades
WHERE NOT(grades2010 IS NULL OR grades2010="")
UNION
SELECT class, teacher, student
grades2011 AS grades
WHERE NOT(grades2011 IS NULL OR grades2011="")
UNION
SELECT class, teacher, student
grades2012 AS grades
WHERE NOT(grades2012 IS NULL OR grades2010="")
)
Here's an example of the type of IIF I'd want to run -- see if 'grades' contains an asterisk and create an indicator:
IIF(InStr([grades],"*")>0,"YES","NO") AS asterisk
So... You are on the right track. It would look something like:
SELECT
IIF(InStr([grades],"*")>0,"YES","NO") AS asterisk,
class,
teacher,
student
FROM(
SELECT class, teacher, student
grades2010 AS grades
WHERE NOT(grades2010 IS NULL OR grades2010="")
UNION
SELECT class, teacher, student
grades2011 AS grades
WHERE NOT(grades2011 IS NULL OR grades2011="")
UNION
SELECT class, teacher, student
grades2012 AS grades
WHERE NOT(grades2012 IS NULL OR grades2010="")
) as mysubquery
But... grades is not a column in your UNION subquery, so this won't work. You'll need to make sure that grade column is added to each SELECT statement in your subquery to do anything with it in your main SELECT.
Also worth mentioning is that your schema isn't the best. Instead of having a table for each year, you should really just have a single table with year as a column. This will get you out of having to do these nasty, and often times slow Union queries. You could fix that up pretty quick by making a table and using that UNION query there to do an INSERT from each of your year based tables.
Im building a c# program and am currently stuck at fetching data from MYSQL database and bind them to a grid view. I had been researching for a few days now but to no avail.
I have 4 table in the database.
table 1 - alpha
table 2 - bravo
table 3 - charlie
table 4 - delta
attributes of alpha (id, type, user, role )
attributes of bravo (id, type, date, user)
attributes of charlie (id,type, cat, doneby, comment)
atttibutes of delta (id,type, cat, doneby)
* the pk of alpha and bravo is (id)
* the pk of charlie and delta is (id, type)
i did a query1 before by inner joinning alpha, bravo and charlie which leads to the sucessful result of
(id, type, date, user, role, cat, doneby, comment)
and
i also did a query2 before by inner joinning alpha, bravo and delta which leads to the sucessful result of
(id, type, date, user, role, cat, doneby)
Right now, im trying to built a query3 which will merge the result from query1 and query2 together.
the result of my attempts leads to
(id, type, date, user, role, cat, doneby, comment,id, type, date, user, role, cat, doneby)
As i do not want the repeated columns, I would like to seek advice on how to get the result to become like the one below by placing the records as a new tuple in the result table.
(id, type, date, user, role, cat, doneby, comment)
Thanks!
P.S: the PK would not pose a problem due to (id, type)
If you want to have separate tuples, in fact union-ing the two sets, then do the following:
(select id, type, date, user, role, cat, doneby, '' as comment from yourtable1)
union
(select id, type, date, user, role, cat, doneby, comment from yourtable2)
Read more about union here.
Based in your last comment you could use the UNION then as Lajos already posted, like:
Query 1
UNION
Query 2
And for missing columns or column that could be found in either query you have to fill it with an empty one with the same column name.
See example here
I'm new to MySQL so I really need some help with an issue I'm facing:
I have 7 tables in same database with some datas from tests:
The 7 tables have different columns but they all have these columns:
name.
second_name.
status.
In status are added current status of each student (accepted or rejected) and I want to display using select the name, second_name from the 7 tables where status = accepted.
I managed to display from 1 table
SELECT name, second_name FROM test1 WHERE status="accepted";
But I can not figure out how to display from all 7.
It will be a real help for me if somebody could give me a hint.
If you do not mind duplicate student names with multiple accepted tests, you can try doing it with UNION ALL:
(SELECT name, second_name FROM test1 WHERE status='accepted')
UNION ALL
(SELECT name, second_name FROM test2 WHERE status='accepted')
UNION ALL
(SELECT name, second_name FROM test3 WHERE status='accepted')
-- ...and so on
IMHO it's better to normalize database to have all the names, secondnames and statuses in the separate table and do the only select instead of UNION to improve performace.