SSRS Group per N distinct - reporting-services

I'm trying to solve a problem.
As an example, let’s say I have a table containing 3 columns (Name, Timestamp, Value) with 30 unique or distinct Names and over + 1,000 rows
I have created a line chart which plots Value(Y) vs. Time(X) for each Name (series group), therefore there are 30 lines in this chart.
However, having 30 series in a chart is too cluttered and illegible for the most part. So I have placed the chart in a table (although a "List" would probably be more appropriate). I then grouped the table by "Name" which gives me one Name (series) per chart, which is great but still not ideal as this generates lots of charts and takes up many pages.
I would now like to keep the name grouping but have 5 Names (series) per chart.
I believe this is something simple but I’ve battled with this by searching and trying numerous grouping expressions for several days without any success.
Also , the order or ranking is not important.
Here is a grouping example that comes close to what I need but is not suitable as it groups per 5 lines rather than 5 per distinct name.
This expression groups per 5 lines… =Cieling(RowNumber(Nothing)/5)
Thanks
John.

The easiest way to solve your problem is to add that information on your query. If you're using SQL Server you can add to your query:
SELECT Name, Timestamp, Value,
ROW_NUMBER() OVER(PARTITION BY Name) as rownum
....
And then your grouping expression on the table can be =Fields!rownum.Value Mod 5

Related

MySQL UNION ALL Multiple usage optimization

I am having issues in MySQL query optimization.
Situation is like below.
There are over 200000 rows with multi columns in a SQL table.
And I am making filter options in frontend for these data.
For instance, two columns "Year" and "Make".
And there are many values like "2021", 2022, 2019, 2010 in Year columns, while "Ford", "Chevrolet" and so on in Make.
Example Link:
https://www.autobidmaster.com/en/carfinder-online-auto-auctions/?make=Chevrolet
These values are not unique in each columns. and
I am gonna make filter options(unique value : it's count in each column) based on unique values for these two columns.
I thought I can use data grouped by unique values in each and merge them using UNION ALL in single query.
For instance: for two columns Year and Make
$sql1 = "
(SELECT 'Make' as filter_option_name ,Make as filter_options_key_name, COUNT(*) as filter_option_count
FROM dbcopart.wprdb_copartdata ". $where_str ."
GROUP BY filter_options_key_name
ORDER BY filter_options_key_name)
UNION ALL
(SELECT 'Year' as filter_option_name ,Year as filter_options_key_name, COUNT(*) as filter_option_count
FROM dbcopart.wprdb_copartdata ". $where_str ."
GROUP BY filter_options_key_name
ORDER BY filter_options_key_name) "
WIth two columns, it was okay. worked properly.
But there are another columns: over 20 columns to be used as filter option.
20 time UNION ALL for over 200000 rows was slow.
How can I improve my SQL query?
I think there should be another effective way instead of my stupid 'multiple UNION ALL'.
Thanks for your attention.
Your UNION ALL is probably optimal for gathering all 20 sets of counts at one time. But consider running that once an hour and storing it into another table -- then use fetch from that table. (The data will be a little stale, but perhaps good enough for the use case.)
Yes, once they picked 'Lamborghini', you will have to go back to the table to get the revised values for all the counts (minus make). If there is an index starting with make, then this second big UNION will be faster than the first.
Two layers might be worth caching; more than that will take a lot of space for minimal benefit.
Consider keeping the entire dataset in memory and using app code to do the necessary counts; it will probably be faster than using SQL. (But a lot more code.)

mysql group_concat alternative or multiple rows as columns

Before i start my question i cover briefly what the problem is:
I have a table that stores around 4 million 'parameter' values. These values have an id, simulation id and parameter id.
The parameter id maps to a parameter table that basically just maps the id to a text like representation of the parameter x,y, etc etc
The simulation table has around 170k entries that map parameter values to a job.
There is also a score table which stores the score of each simulation , simulations have varying scores for example one might have one score another might have three. The scores tables has a simulation_id column for selecting this.
Each job has an id and an objective.
Currently im trying to select all the parameter_values who's parameter is 'x' and where the job id is 17 and fetch the score of it. The variables of the select will change but in princible its only really these things im interested in.
Currently im using this statement:
SELECT simulation.id , value , name , ( SELECT GROUP_CONCAT(score) FROM score WHERE score.simulation_id = simulation.id ) AS score FROM simulation,parameter_value,parameter WHERE simulation.id=parameter_value.simulation_id AND simulation.job_id = 17 AND parameter_value.parameter_id=parameter.id AND parameter.name = "$x1"
This works nicley except its taking around 3 seconds to execute. Can this be done any faster?
I don't know if it would be faster doing a query before this a pre-calculating the parameter_ids im searching for and doing an WHERE parameter_id IN (1,2,3,4) etc.
But i was under the impression SQL would optimize this anyway?
I have created index's where ever possible but cant get faster than the 2.7 seconds mark.
So my question would be:
Should i pre-calculate some values and avoid the joins,
Is there another other than group_concat to get the scores
and is there any other optimizations i could make to this?
I should also add that the scores must be in the same row or at least return sorted so i can easily read them from the result set.
Thanks,
Lewis

Generate a query that show the times that questions get wrong

I have a table named countwronganswer with columns cwa_id, question_num. How can I generate a table with query that shows two columns, one column lists all the question_num and second column lists the number of times that cwa_id that related to the question_num.
Question Number |Total # of Mistake |
1 12
2 22
..etc
ATTENTION: This question was asked without the awareness of the existence of count or Groupby method because of the knowledge level at that state. Count() or Groupby() were the key to generate the 2nd column of total # values which I did not aware of completely, therefore, any attempt, at that point of time, to write the code for the data will be close to meaningless. Vote up if possible if you think its useful or resolved your issue.
Probably something like this
SELECT question_num, COUNT(cwa_id) total_mistakes
FROM countwronganswer
GROUP BY question_num
select question_num , count(cwa_id)
from tableName group by question_num

MySQL: How to fetch by "New and Bestselling" like amazon?

In amazon.com, for instant, users can sort products by "New and Bestselling".
I have an "items" table which contains "uploaddate" column and "views" column for each item. How can I sort items by "New and Bestselling", combining uploaddate & views?
How can I modify this query to combine uploaddate & views to fetch items as "New & Bestselling"?
select * from items order by uploaddate desc, views desc
There are three ways I can think of off the top of my head that could all be valid approaches.
1) Restrict the query to the last 7 days (or however you define "New") and just sort by views. This will ONLY show you new items, but it's the simplest.
2) Add a field that corresponds to how many weeks ago the item was added, and determine that by using a CASE statement. Like SELECT CASE(WHEN DATEDIFF(CURDATE()-uploaddate) < 7 THEN 1 ELSE 0 END AS relative week then sort by weeksort.
3) Like you suggested, as part of the select statement, use a formula to generate a ranking number based on the views and uploaddate fields, and sort by that. That's more difficult because ideally you want your rank to scale based on the maximum number of views and to heavily penalize older items so you don't have an amazingly popular item from 2 years ago at the top of the "New and Bestselling" chart.
I would do two SELECT statements, one sorted by uploaddate and one sorted by views, then use UNION to merge them, then wrap them with another SELECT statement and group them to prevent duplicates.

Difficult MS Access Query - how to combine them?

I am trying to build an access report based on data from multiple different tables within the database.
I have 3 columns which perform calculations, and I am wondering how to put this query together. All 3 columns deal with dates, but calculate them differently.
The first column retrieves the most recent date of action for a userid if the type of action is "B":
select pid, Max(date) as most_recent
from actions
where ref = 'B'
group by pid;
The second column performs a calculation based on 2 fields, one is a date and one is a number in months. I am unsure how to add these two fields so that the number is added to the date as a number of months.
what i have so far is:
select nummonths,Max(lastvisit) from users
the third column I need to select the first date thats in the future for each user (next appointment date), there will be dates before and after this date so its a little difficult:
select uid,date from visits
The code for the last 2 queries needs to be slightly modified, and I was wondering what the best approach would be to join these all together? A type of join?
If you need to build a report with data from the 3 queries, you will need related data to join them. In that case, please send the structure of the tables.
If you need to show 3 lists in one report, you can use subreports: create a new empty report. In design mode, you can add 3 subreports from the toolbox bar. To each of the subreport assign the record source property to the corresponding sql.
regards
I am unsure how to add these two fields so that the number is added to the date as a number of months.
Use the DateAdd() function:
SELECT DateAdd("m", 2, LastVisit) FROM ...
Results in a date two months from the LastVisit date.