SSRS non-grouped data in the middle of grouped data in table - reporting-services

I'm not sure if I worded this properly. It's a lot easier to show. So without doing a subreport, is there any way to have the non-grouped data inside of grouped data.
╔════╦══════════════╦══════════════╦════════════╗
║ ID ║ Cars ║ Name ║ Date ║
╠════╬══════════════╬══════════════╬════════════╣
║ 1 ║ 2011 Chevy ║ John D ║ 1/1/2013 ║
║ ║ 2012 Ford ║ ║ ║
║ 2 ║ 2010 Pontiac ║ Jane D ║ 1/1/2012 ║
║ 3 ║ 1969 Chevy ║ Bob H ║ 12/1/2011 ║
║ ║ 1930 Ford ║ ║ ║
╚════╩══════════════╩══════════════╩════════════╝
Cars is not grouped. So there is 1:N relationship there with all the other data.
What I know you can do and have done is have the "Cars" all the way to the right and have it be ID, Name, Date, Cars but I cant seem how to get the cars in the middle of all of the grouped data columns. I know a subreport is "possible" but wondered if I am just missing the obvious

It's a hack, but I do it all the time:
You can embed a table in the Cars cell. You just need 1 Details cell on your embedded table (get rid of the headers and the extra columns that are auto added), and have it list your cars. It will only list the cars that are related to the value in column 1 on each row of the parent table, and should probably end up looking like your example above.

Related

Query to get IDs related to certain foreign keys but not others

I have two sites with separate Wordpress instances. One is in the process of getting WooCommerce, the other has existing users with set groups to access courses. I the future access to the courses is supposed to be sold via the shop. The answer I'm looking for has nothing to do with Wordpress per se but with SQL. Most of the data I know how to get from either one table or by slight joining but not the one for the last step. Consider the following:
User table (superfluous but for clarification):
╔═════╦══════╗
║ UID ║ NAME ║
╠═════╬══════╣
║ 1 ║ Bob ║
║ 2 ║ Tom ║
║ 3 ║ Joe ║
║ … ║ … ║
╚═════╩══════╝
Course table:
╔═════╦═════════════════════╗
║ CID ║ COURSE ║
╠═════╬═════════════════════╣
║ 1 ║ Introductory Course ║
║ 2 ║ Trial Course ║
║ 3 ║ Actual Course 1 ║
║ … ║ … ║
╚═════╩═════════════════════╝
Junction table to connect users to courses n-to-n:
╔═════╦═════╗
║ UID ║ CID ║
╠═════╬═════╣
║ 1 ║ 1 ║
║ 2 ║ 1 ║
║ 2 ║ 2 ║
║ 3 ║ 1 ║
║ 3 ║ 2 ║
║ 3 ║ 3 ║
║ … ║ … ║
╚═════╩═════╝
Not gonna make you look up the relationships, so:
Bob (1) had the Introductory Course
Tom (2) had the Introductory and the Trial Course
Joe (3) had the Introductory, the Trial and at least one "actual" course
The Introductory Course grants nothing, the Trial Course grants a discount for any actual course. If somebody already has an actual course the discount has already been granted in the past. So in the result of the query I'm only looking for the user IDs of those who have the Trial Course, optionally also the Introductory course, but none of any others:
╔════╗
║ ID ║
╠════╣
║ 1 ║
║ 2 ║
╚════╝
I can't just exclude rows from the result that don't match the introductory or the trial course, obviously, as that would still give me user IDs that may have other courses attached to them.
Can't help but think I'm missing an obviously simple solution.
As ffar as i understand you want a UID that where not in 3 Actual Course 1
AND I ASSUME THAT 4 is Actual Course 2 amd so on.
but usually there different introduction coirses and trial courses for dofferent categories, depending on University.
so i think you must rethink your strategy, or give us a glance how you manage this
SELECT
DISTINCT UID
FROM junction
WHERE
UID NOT IN (SELECT UID FROM junction WHERE CID >= 3)
| UID |
| --: |
| 1 |
| 2 |
db<>fiddle here
I think you want:
select j.uid
from junction j join
course c
on j.cid = c.cid
group by j.uid
having sum( c.course = 'Trial Course' ) > 0 and
sum( c.course not in ('Trial Course', 'Introductory Course') ) = 0;
The first having condition checks that the user has the trial course. The second that they have no non-trial or introductory courses.

What's best practice for normalisation of DB where a domain table has an "Other" option for free text?

I'm normalising a database for my company and one pattern I'm seeing a lot is using a domain lookup table for a value but also allowing "Other" and storing the results in a separate column.
Is there a cleaner way of representing this?
I'm following the normal forms up to 5NF and domain key form. In some tables this pattern is repeated more than once so we have a table like the following:
╔══════════════╦══════════════════╦═════════════════════╦═══════════════════╦══════════════════════╗
║ appliance_id ║ location_type_id ║ other_location_type ║ appliance_type_id ║ other_appliance_type ║
╠══════════════╬══════════════════╬═════════════════════╬═══════════════════╬══════════════════════╣
║ 123 ║ 1 ║ {null} ║ 13 ║ Freestanding Boiler ║
║ 124 ║ 13 ║ Annex ║ 1 ║ {null} ║
╚══════════════╩══════════════════╩═════════════════════╩═══════════════════╩══════════════════════╝
Where for example, a location_type_id & appliance_type_id of 13 is "Other" in the relevant lookup tables.
So for example, the location type table looks something like this:
╔═════╦═══════════════╗
║ id ║ location_type ║
╠═════╬═══════════════╣
║ 1 ║ Living Room ║
║ 2 ║ Kitchen ║
║ ... ║ ... ║
║ 13 ║ Other ║
╚═════╩═══════════════╝
This might very well be the best possible solution (although I would probably separate the location and appliance type into different tables).
After talking it over, we're gonna do as suggested and do a text scan and use that to populate our lookups, then going forward we'll try to discourage the use of free-text fields for lookups, storing the values in a separate table for the time being so we don't clutter our main tables.

mysql - how to sum the count of 2 different elements from the same table and column and show it as one?

I have a library with different types of Literary genres, i gotta show it as one, some very similar to the others, so ive been asked to reduce the table size by summing the amount and porcentage from some genres to the similar ones, for example:
AutoBiography must be summarized with Biography, Adventure with Action, Romance with romantic commedy.
Check The fiddle example: http://sqlfiddle.com/#!9/df7c87/9
i need to display it as the example below:
╔════════════════╦════════════╦══════════╦══════════════════╦════════════════╦══════════════╦════════════╗
║ Literary_Genre ║ Day_Amount ║ Day_Porc ║ Yesterday_Amount ║ Yesterday_Porc ║ Month_Amount ║ Month_Porc ║
╠════════════════╬════════════╬══════════╬══════════════════╬════════════════╬══════════════╬════════════╣
║ Biography ║ 4 ║ 28,56 ║ 2 ║ 31 ║ 27 ║ 35.9 ║
╠════════════════╬════════════╬══════════╬══════════════════╬════════════════╬══════════════╬════════════╣
║ romance ║ 6 ║ 42,84 ║ 1 ║ 22,6 ║ 56 ║ 61,1 ║
╠════════════════╬════════════╬══════════╬══════════════════╬════════════════╬══════════════╬════════════╣
║ Action ║ 1 ║ 7,14 ║ 4 ║ 28,56 ║ 38 ║ 45.1 ║
╚════════════════╩════════════╩══════════╩══════════════════╩════════════════╩══════════════╩════════════╝
Basically i need to sum the amount of Action+adventure and display it as Adventure Considering the time intervals, and do the same thing for Autobiography+biography and romantic comedy+romance.
ps: The porcentage numbers are random as i generated them without calculating.
You can use aggregation and a case expression:
select
case Literary_Genre
when 'Autobiography' then 'Biography'
when 'romantic comedy' then 'romance'
when 'Adventure' then 'Action'
else Literary_Genre
end Real_Literary_Genre,
sum(Day_Amount) Day_Amount,
sum(Day_Porc) Day_Porc,
sum(Yesterday_Amount) Yesterday_Amount,
sum(Yesterday_Porc) Yesterday_Porc,
sum(Month_Amount) Month_Amount
sum(Month_Porc) Month_Porc
from mytable t
group by Real_Literary_Genre
If you have a large number of categories to map, you would be better off creating a table to store this information:
original_genre | real_genre
----------------- | ----------------
Autobiography | Biography
romantic comedy | romance
Adventure | Action
You can then bring it in with a left join:
select
coalesce(m.real_genre, t.Literary_Genre) New_Literary_Genre,
sum(Day_Amount) Day_Amount,
sum(Day_Porc) Day_Porc,
sum(Yesterday_Amount) Yesterday_Amount,
sum(Yesterday_Porc) Yesterday_Porc,
sum(Month_Amount) Month_Amount
sum(Month_Porc) Month_Porc
from mytable t
left join mymapping m on m.original_genre = t.Literary_Genre
group by New_Literary_Genre

SSRS - align specific row in two tables

I'm looking to develop a SSRS report which gets data from two different datasets (unrelated). Then i have two tablix placed horizontally side by side.
The data in each table can vary on rows, and the client wants to align the specific row (which may not be last Row) in both the tables.
Is there a way to achieve this layout just using expressions? I tried to write a custom code to somewhat achieve the behavior but we are deploying this report to D365 online and there the custom code is not supported in sandbox environment.
if someone has any suggestion which gives me an idea on how to achieve such layout, it would be very much appreciated.
Table A Table B
╔═══════╦══════╗ ╔═══════╦══════╗
║ Col1 ║ Col2 ║ ║ Col1 ║ Col2 ║
╠═══════╬══════╣ ╠═══════╬══════╣
║ A1 ║ A ║ ║ B1 ║ X ║
╠═══════╬══════╣ ╠═══════╬══════╣
║ A2 ║ B ║ ║ B2 ║ Y ║
╠═══════╬══════╣ ╠═══════╬══════╣
║ A3 ║ C ║ ║ ║ ║
╠═══════╬══════╣ ╠═══════╬══════╣
║ Align ║ ABC ║ ║ Align ║ XY ║
╠═══════╬══════╣ ╠═══════╬══════╣
║ A4 ║ A ║ ║ B3 ║ B ║
╠═══════╬══════╣ ╠═══════╬══════╣
║ A5 ║ A ║ ║ B4 ║ B ║
╚═══════╩══════╝ ╚═══════╩══════╝
I think you can achieve this by using the LookupSet function in SSRS.
Add a couple of columns to the end of Table A and use the LookupSet function to source the rest of the values..
something like this: (assuming your data set for table B is called DatasetTableB
=LookupSet(Fields!Col1.Value, Fields!Col1.Value, Fields!Col2.value, "DatasetTableB")
You would do the same for the rest of the columns and get the other values.. where Column1 matches, you get the values.. the rest will be blank

MySql: Consolidating rows with matching records

My table has a list of users with Items associated to them. I hope to be able to consolidate the items field into a list or create another column if the row has a matching User/email.
I have the following structure with in the MySQL table:
╔═══════════╦═════════════╦═════════════╦═════════════╦═════════════╦════════════╗
║ ID ║ User_ID ║ Item ║ Date ║ User ║ Email ║
╠═══════════╬═════════════╬═════════════╬═════════════╬═════════════╬════════════╣
║ 1 ║ 1 ║ Laptop 1 ║ 30th Nov ║ John ║ J#test.com ║
║ 2 ║ 2 ║ Laptop 3 ║ 12th Nov ║ Emma ║ e#test.com ║
║ 3 ║ 2 ║ Camera 3 ║ 12th Nov ║ Emma ║ e#test.com ║
╚═══════════╩═════════════╩═════════════╩═════════════╩═════════════╩════════════╝
I am very new to SQL but i think i would need to use some type of transpose field to a column function?
Any help will be greatly appreciated
Thanks
I think you want group_concat(). Personally, I would use user_id, rather than user/email. or, perhaps all three together. Something like this:
select user_id, user, email, group_concat(item) as items
from table t
group by user_id, user, email;