Grouping data in a SSRS report - reporting-services

In developing an SSRS 2008 R2 report, I'll like to show some data grouped by values, while merging others, I've run into a problem doing this on report builder.
| Parent Group |
|_______________________________________|
|Group A|Group B|Group C|Group D|Group E|
|_______|_______|_______|______|________|
| 5 | 2 | 1 | 1 | 5 |
| 4 | 2 | 4 | 2 | 2 |
| 1 | 3 | 1 | 3 | 2 |
Can I create a filter or grouping to combine Group C, D, E together while leaving A and B alone?
Like such,
| Parent Group |
|_____________________________|
|Group A |Group B|Other Groups|
|________|_______|____________|
| 5 | 2 | 7 |
| 4 | 2 | 8 |
| 1 | 3 | 6 |

There are two ways you can achieve this:
SQL query
Usually the best way to get the result you want is to let SQL do the heavy lifting:
SELECT GroupA, GroupB, (GroupC + GroupD + GroupE) AS OtherGroups
FROM MyTable
In the report
If you can't change your query result (for example, it is a stored procedure) then you can do the same thing in SSRS VB code.
Right-click the cell and choose Expression... and enter something like the following:
=Fields!GroupC.Value + Fields!GroupD.Value + Fields!GroupE.Value
and SUM in the same way:
=SUM(Fields!GroupC.Value) + SUM(Fields!GroupD.Value) + SUM(Fields!GroupE.Value)

Related

MySQL query to get value information from another table based on id with 3 different attribute

I'm trying to query data with MySQL database.
I have 2 tables.
tbl_alarm
id | alarm | file
1 | Alarm1| Alarm1.mp3
2 | Alarm2| Alarm2.mp3
3 | Alarm3| Alarm3.mp3
4 | Alarm4| Alarm4.mp3
tbl_profile
alarmFront | alarmLeft | alarmRight
1 | 3 | 2
Now I need to get alarm file based on id in tbl_profile. So the view will be like this.
alarmFront | alarmLeft | alarmRight
Alarm1.mp3 | Alarm3.mp3| Alarm2.mp3
SQL Fiddle
How can I do it on sql query code?

One to Many Count with one query?

I haven't touched the backend in a while.. so forgive me if this is super simple. I'm working with Lumen v.5.6.1.
| table.sets | | table.indexed_items |
|----------------| |---------------------------------|
| ID | SET | | ID | setId | itemId | have |
|----|-----------| |----|-------|--------|-----------|
| 1 | set name 1| | 1 | 3 | 1 | 2 |
| 2 | set name 2| | 2 | 3 | 2 | 1 |
| 3 | set name 3| | 3 | 3 | 3 | 4 |
| 4 | 2 | 4 | 1 |
| 5 | 2 | 5 | 3 |
| 6 | 2 | 6 | 1 |
How would I return in one query, groupedBy/distinct by setId (with set name as a left join?) to have a return like this:
[
setId: 2,
name: 'set name 2',
haveTotal: 5,
],
[
setId: 3,
name: 'set name 3',
haveTotal: 7,
]
Here is a raw MySQL query which should work. To convert this to Laravel should not be too much work, though you might need to use DB::raw once or twice.
SELECT
s.ID AS setId,
s.`SET` AS name,
COALESCE(SUM(ii.have), 0) AS haveTotal
FROM sets s
LEFT JOIN indexed_items ii
ON s.ID = ii.setId
GROUP BY
s.ID;
Demo
If you don't want to return sets having no entries in the indexed_items table, then you may remove the call to COALESCE, and you may also use an inner join instead of a left join.
Note that using SET to name your tables and columns is not a good idea because it is a MySQL keyword.
If you are using or want to use eloquent, you can do something like:
$sets = App\Sets::withCount('indexed_items')->get();
This will return a collection with a column name indexed_items_count
Obviously you will need to change depending on your model names.
Here are the docs
I always use in my project for count relation ship record.
$sets->indexed_items->count();

MySQL: optimize query for scoring calculation

I have a data table that I use to do some calculations. The resulting data set after calculations looks like:
+------------+-----------+------+----------+
| id_process | id_region | type | result |
+------------+-----------+------+----------+
| 1 | 4 | 1 | 65.2174 |
| 1 | 5 | 1 | 78.7419 |
| 1 | 6 | 1 | 95.2308 |
| 1 | 4 | 1 | 25.0000 |
| 1 | 7 | 1 | 100.0000 |
+------------+-----------+------+----------+
By other hand I have other table that contains a set of ranges that are used to classify the calculations results. The range tables looks like:
+----------+--------------+---------+
| id_level | start | end | status |
+----------+--------------+---------+
| 1 | 0 | 75 | Danger |
| 2 | 76 | 90 | Alert |
| 3 | 91 | 100 | Good |
+----------+--------------+---------+
I need to do a query that add the corresponding 'status' column to each value when do calculations. Currently, I can do that adding the following field to calculation query:
select
...,
...,
[math formula] as result,
(select status
from ranges r
where result between r.start and r.end) status
from ...
where ...
It works ok. But when I have a lot of rows (more than 200K), calculation query become slow.
My question is: there is some way to find that 'status' value without do that subquery?
Some one have worked on something similar before?
Thanks
Yes, you are looking for a subquery and join:
select s.*, r.status
from (select s.*
from <your query here>
) s left outer join
ranges r
on s.result between r.start and r.end
Explicit joins often optimize better than nested select. In this case, though, the ranges table seems pretty small, so this may not be the performance issue.

SQL 'COUNT' not returning what I expect, and somehow limiting results to one row

Some background: an 'image' is part of one 'photoshoot', and may be a part of zero or many 'galleries'. My tables:
'shoots' table:
+----+--------------+
| id | name |
+----+--------------+
| 1 | Test shoot |
| 2 | Another test |
| 3 | Final test |
+----+--------------+
'images' table:
+----+-------------------+------------------+
| id | original_filename | storage_location |
+----+-------------------+------------------+
| 1 | test.jpg | store/test.jpg |
| 2 | test.jpg | store/test.jpg |
| 3 | test.jpg | store/test.jpg |
+----+-------------------+------------------+
'shoot_images' table:
+----------+----------+
| shoot_id | image_id |
+----------+----------+
| 1 | 1 |
| 1 | 2 |
| 3 | 3 |
+----------+----------+
'gallery_images' table:
+------------+----------+
| gallery_id | image_id |
+------------+----------+
| 1 | 1 |
| 1 | 2 |
| 2 | 3 |
| 3 | 1 |
| 4 | 1 |
+------------+----------+
What I'd like to get back, so I can say 'For this photoshoot, there are X images in total, and these images are featured in Y galleries:
+----+--------------+-------------+---------------+
| id | name | image_count | gallery_count |
+----+--------------+-------------+---------------+
| 3 | Final test | 1 | 1 |
| 2 | Another test | 0 | 0 |
| 1 | Test shoot | 2 | 4 |
+----+--------------+-------------+---------------+
I'm currently trying the SQL below, which appears to work correctly but only ever returns one row. I can't work out why this is happening. Curiously, the below also returns a row even when 'shoots' is empty.
SELECT shoots.id,
shoots.name,
COUNT(DISTINCT shoot_images.image_id) AS image_count,
COUNT(DISTINCT gallery_images.gallery_id) AS gallery_count
FROM shoots
LEFT JOIN shoot_images ON shoots.id=shoot_images.shoot_id
LEFT JOIN gallery_images ON shoot_images.image_id=gallery_images.image_id
ORDER BY shoots.id DESC
Thanks for taking the time to look at this :)
You are missing the GROUP BY clause:
SELECT
shoots.id,
shoots.name,
COUNT(DISTINCT shoot_images.image_id) AS image_count,
COUNT(DISTINCT gallery_images.gallery_id) AS gallery_count
FROM shoots
LEFT JOIN shoot_images ON shoots.id=shoot_images.shoot_id
LEFT JOIN gallery_images ON shoot_images.image_id=gallery_images.image_id
GROUP BY 1, 2 -- Added this line
ORDER BY shoots.id DESC
Note: The SQL standard allows GROUP BY to be given either column names or column numbers, so GROUP BY 1, 2 is equivalent to GROUP BY shoots.id, shoots.name in this case. There are many who consider this "bad coding practice" and advocate always using the column names, but I find it makes the code a lot more readable and maintainable and I've been writing SQL since before many users on this site were born, and it's never cause me a problem using this syntax.
FYI, the reason you were getting one row before, and not getting and error, is that in mysql, unlike any other database I know, you are allowed to omit the group by clause when using aggregating functions. In such cases, instead of throwing a syntax exception, mysql returns the first row for each unique combination of non-aggregate columns.
Although at first this may seem abhorrent to SQL purists, it can be incredibly handy!
You should look into the MySQL function group by.

How can I sequence a column within a table using SQL (MySQL)?

I have a list mapped using Hibernate with an index column. The table looks like this:
mysql> select * from chart;
+----+--------+------+-----------------------+
| id | report | indx | name |
+----+--------+------+-----------------------+
| 2 | 1 | 0 | Volume |
| 3 | 2 | 0 | South Africa (Volume) |
| 5 | 2 | 2 | People |
| 6 | 2 | 3 | Platforms |
| 7 | 2 | 4 | People (Gender) |
+----+--------+------+-----------------------+
As you can see chart id=4 for report=2 with indx=1 has been deleted.
I need to eliminate the gaps so all the indx values for a given report run in sequence from 0. I could write Java code to sort this out but SQL solution would be much easier to deploy.
Anyone know how to do this? I am using MySQL.
if this technique works on your version of MYSQL you can try
SET #row := -1;
UPDATE chart
SET indx = #row := #row + 1
WHERE report = 2
ORDER BY indx
but I think better to change design.