MySQL Nested Select Querys using data from main query - mysql

I have edited the question to make the problem more simple and added the suggested changes in the coments, Thx a lot
I have a table matches with data and I need:
1- To get all the columns for matches before a date.
http://sqlfiddle.com/#!9/4ff02f/18
SELECT m1.* FROM matches AS m1 WHERE m1.date < '2019-02-23 00:00:00'
2- I need to add two columns with the “streak” for home and away teams in each match (or row). The “streak” for a team is the count of matches before the date of the current match in same competition, until the team lost a game (being home or away indistintly). The result would be kind of this:
results desired
I need to get all the info in only one query in MySql…and I am getting crazy with JOINS, SUBQUERIES,….They don´t get to do what I mean. Really appreciate some help
Thanks!

It sounds like you want to only populate specific fields for your streaks and that you want cumulative data. You may need to use fields that don't show (invisible, or just at the end) for "win/lose" and cumulative data to provide the count.
I don't have time to write a full solution right now, but this will be too long for a "comment" so bear with me:
Add columns for "Win" in home and away. Place a 1 or 0 accordingly.
Add columns for streak and conditionally include a cumulative count of Win IF the Win is 1. But if the win is Zero, set the cumulative back to Zero.
Example of CASE: https://www.w3schools.com/mysql/func_mysql_case.asp
Example of Cumulative: https://popsql.com/learn-sql/mysql/how-to-calculate-cumulative-sum-running-total-in-mysql
Once you have it written, you could likely combine these into a single function/field. But ... baby steps. Like the rest of us. :)

Related

How to create a query that is able to sort data into 2 groups?

so I'm very new to Access - just started learning it this week. I have data comprised of a bunch of policy numbers with corresponding ratings and premium values.
What I'm trying to do is create a query table that aggregates this data by rating (=1 or >1), this is the part I can't figure out. In design mode, I have put criteria =1 or >1, but it's not doing anything; when I switch to datasheet view, it just lists all of the ratings instead of two boxes that say =1 and >1. If anyone could give me insight on how to do this, I'd appreciate it!
From what I understand after reading your question, you want a query that shows two columns, one that counts the records with a rating of 1 and one that counts the records with a rating of greater than 1.
The best way to do this is to set up two expression columns, both wrapped in a count as you can see below:
SELECT Count(IIf([Rating]=1,[Rating],Null)) AS [=1], Count(IIf([Rating]>1,[Rating],Null)) AS [>1]
FROM tblSortRating;
If you have any questions or I've misunderstood your question leave a comment and I'll get back to you.

Report builder 3.0 Using Reportitems!TexboxXX.Value sometimes creates multiple boxes. Why?

I have 6 Datasets each one is the same query but has a different WHERE clause based on employee type. They generate 6 tables. At the top of the report there is a summary table which uses reportitems!textboxXX.value to grab the totals for 2 particular fields from all the tables. I'm also using a StartDate and EndDate parameter. Each reportitems! expression in the table comes from a different dataset in the report if that is relevant.
When I run the report using dates from yesterday back to the 9th of May I get the desired output.
But when I go to the 8th I get multiple rows all the same.
and as I go even further back I get even more rows of the same information. To my knowledge nothing interesting or different happened on the 8th of May and the tables further down the report look exactly the same. Any idea what might be causing this? The design view looks like this if that helps. There's no grouping or filters on the table.
Still not certain about the mechanics behind this but I found a 'solution' so I figured I'd post it. I just made a new dataset for my summary tables where the query was simply SELECT 1. Now I get a single row every time.

Can't figure how to use lookup() in SSRS 2008 R2

In the report I am building I have 2 data sets: one gives me, per user, per day, the total amount in that status; the other one gives me, per user, how many days in the date range the user actually showed up at work. Each of these data sets comes from its respective stored procedure. See screenshot.
The problem I have is that I need to report not the total time in status per user, but the average per day. So in the screenshot you can see that one user has 5 entries for 5 days worked and the other one has 3 entries for 4 days worked. simply because in one of those 4 days he didn't had that status at all.
I tried adding a calculated field to my "status" data set by using the lookup() function but it kept on giving me errors, which makes me think I don't quite know how to use it.
I also tried using group variables, and I was able to define it under group properties, but it never come up as an option to be used when writing an expression.
Any ideas using lookup(), variables or otherwise?
You're on the right track. Sounds like the Lookup function is exactly what you want.
Instead of trying to add the calculated field to the dataset, try putting it directly in the report item where you want this displayed.
Something along these lines should work:
=SUM(Fields!Available.Value)
/ Lookup(Fields!UserId.Value, Fields!UserId.Value, Fields!Days.Value , "NameOfDaysDataset")
If this isn't working, please post a few more details of your data sets, field names, and where you need this to appear.

MySQL virtual fields for duplicate results

I apologize in advance that I don't know the terminology of the tools I'm trying to use.
I have a table of events with a startdate field (among others) and a related repeats table with a reference to the event id. The repeats table stores the days of the week on which the event repeats and whether it's monthly, weekly, etc. What I'm hoping to do is duplicate the repeating events within the SQL query so my final result will have the the same event in different places when ordered on start date, so I can limit the results for proper pagination.
I'm looking at creating virtual tables and cloning tables documentation, but I'm having trouble applying the examples to my situation.
Update:
Hopefully I can elaborate on this.
The basics of what I have now is SELECT * FROM 'events' WHERE 'start_date' >= TODAY() ORDER BY 'start_date LIMIT 20 which gets me every event from today on, but I'm paginating the results so only 20 are displayed at a time.
What I would like to do is create a temporary 'virtual' table with the events which have an associated repeat entry, on which I will change the start_date based on the repeat information. So if it's a weekly repeat, this second table would be filled with identical events except that each start_date would be 7 days from the last. Then I could do a join on these two tables, limit those results to the 20 pagination limit I want, and have a query result with the events in the correct place and easy to perform pagination on.
I understand that creating a function in mySQL might be on the right track as I imagine I would have to loop through some information for adding to dates. I only know the level of SQL one picks up by writing in PHP, so functions are a bit out of my scope, though it doesn't seem that it'll be too hard to pick up with a little reading. I'm more confused about how I would create a fake table, add entries to it in a loop and then use a join on it to merge it with the first query.
I'm also beginning to wonder about the overhead for doing this in mySQL and, should I be successful in getting this to work, how I might cache these results, though it's only an afterthought right now.
Thanks to those who are trying to help me, I'm having trouble getting this question into words for some reason.

MySQL: Use aggregate result in further calculation

I'm currently working on writing report generators. For one report I need to do a breakdown by a given characteristic (supplier, logging user, language, etc which for each row includes the name of the characteristic I'm interested in, the number of items that match that characterastic, and the percentage of total items this figure represents. The first two aren't a problem, the third is.
For example, to get a breakdown by language I'd be using a query like this.
SELECT lang_id,
COUNT(IF(open=TRUE,1,NULL)) AS lang_total
FROM table
GROUP BY lang_id;
This gives me the number of items per language.
I can get the total number of items in the table and store it in a variable simply enough with a plain count.
SELECT #totalOpen:=COUNT(*) FROM table WHERE open = TRUE;
Now I want to add a third column, which is the figure in lang_total divided by the value in #totalOpen multiplied by 100 (in other words, the percentage of all items that fit the criteria). Something along the lines of the following:
This is the bit I'm having trouble with, as because as far as I can tell you can't use aggregate columns in calculations.
SELECT lang_id,
COUNT(IF(open=true,1,NULL)) AS lang_total
(lang_total/#totalOpen)*100 as lang_percent
FROM table
GROUP BY lang_id;
I'm sure that there must be a way of doing this in MySQL, but I've not been able to track it down. Can anyone help out with this?
I read this question now for the first time. I know that probably it's too late to be useful for you but I would have solved in this way.
select lang_id,
sum(if(open= true,1,0)) as lang_total,
coalesce(sum(if(open= true,1,null)) / #r,0) as percentage
from table,(select #r:=count(*) from table where open = TRUE) as t
group by lang_id;