MySQL update the column with conditions - mysql

I am trying to update (middle name - mname) a database table based on a certain condition. SQLfiddle. http://www.sqlfiddle.com/#!9/3c022/2
I would like to know HENRY {null} FORD is belonging to one of other HENRY {A,B} FORD based on the coauthors and update the table.
The author with null middle name is updated with the middle name of the author with the same first and last name and with whom he has more number of common co-authors.
For example, based on the data the results is:
HENRY FORD HENRY FORD ---> this should be updated 'B' due to more common authors
HENRY A FORD HENRY A FORD
HENRY B FORD HENRY B FORD
However,
JACK SMITH JACK SMITH ---> this shouldn't be updated due to no common authors
JACK A SMITH JACK A SMITH
JACK B SMITH JACK B SMITH
Any suggestions are appreciate.

Related

MySQL Split String Into Separate Rows Using RegEx

I have some text data in a column similar to this in MySQL 8.0.30:
Id
Weights
1
James Brown 10-10 John Doe 12-9 Sue Smith 9-9 Dick Turpin 9-12
2
Some Other 9-1 Example Name 8-13
There could be any number of name/weight combinations in the column. I want to pull these out into separate rows like so
Id
Weights
1
James Brown 10-10
1
John Doe 12-9
1
Sue Smith 9-9
1
Dick Turpin 9-12
2
Some Other 9-1
2
Example Name 8-13
I've tried using the regex_substr to no avail as I could only get it to return a single match. How could I go about doing this?

Moving consecutive results to different columns

I have the following table:
Transaction Id
Authorizer
1
john
1
robert
2
Jane
2
Paul
And the result table I need is like this:
Transaction Id
Authorizer_1
Authorizer_2
1
john
Robert
2
Jane
Paul
How can i make this in Teradata SQL assistant?
Thanks in advance!

How to select value if only all criteria are met?

First time Poster here so I appoligize about the formatting and am really novice at sql, but this has me stumped. That and I am using 2016 MS Access's SQL as well.
I have a table and I want to select only the names of the people who have fulfilled all the requirements.
Table Chore
ID Name Chore Done
1 Joe Sweep Yes
2 Joe Cook Yes
3 Joe Dust Yes
4 Bill Vacuum No
5 Bill Dust Yes
6 Carrie Bathroom Yes
7 John Cook No
8 John Beds No
9 John Laundry Yes
10 Mary Laundry No
11 Mary Sweep No
12 Cindy Car Yes
13 Cindy Garden Yes
In this case, only Joe, Carrie and Cindy's names should be returned because under their name, they finished all their chores.
Help please and thanks in advance!
You can use not in
select name from my_table
where name not in (select name from my_table where chore_done ='No');
You could check the value of max(done), like
select
name
from
my_table
group by name
having max(done) = -1
In Access, Yes/True is -1, No/False is 0, so max(done) is Yes

prediction on rowwise data or progressive data

I am working on employee attrition analysis with a table having rowwise data for a (employee like Id, name, Date_Join Date_Relieving Dept Role etc)
eID eName Joining Releiving Dept Married Experience
123 John Doe 10Oct15 12Oct16 HR No 12
234 Jen Doee 01jan16 -NA- HR No 11 (ie she is available)
I can run regression on this data to find the beta coefficient
eID eName Joining Releiving Dept Married Experience
123 John Doe 10Oct15 12Oct16 HR No 12
234 Jen Doee 01jan16 -NA- HR No 11
But I've seen other approach too.. where employee have multiple entries depending on their difference between joining date and current month or relieving month(say Employee A joined in Jan and Left in Dec so he'll have 12 entries updating corresponding columns like experience and marriage etc)
eID eName Dept Married Experience
123 John Doe HR No 0
123 John Doe HR No 1
123 John Doe HR Yes 2
123 John Doe HR Yes 3
can someone tell what differentiate two approaches.. and what is the outcome of this second approach.

SQL Server Reporting Services: Cell Value Not Repeating when using table/matrix wizard

I have a dataset which shows a worker’s scores on various skills using four test types along with their supervisor and the director above the supervisor. To save space, the dataset example below is for just one worker. This is what I start with:
Director Supervisor Worker Test Skill Score
Doris Smith Jane Awe Lorina Marc Overall 1: Identifying Support 1
Doris Smith Jane Awe Lorina Marc Test A 1: Identifying Support 4
Doris Smith Jane Awe Lorina Marc Test B 1: Identifying Support 1
Doris Smith Jane Awe Lorina Marc Test C 1: Identifying Support 5
Doris Smith Jane Awe Lorina Marc Overall 2: Tracking the Sequence 3
Doris Smith Jane Awe Lorina Marc Test A 2: Tracking the Sequence 2
Doris Smith Jane Awe Lorina Marc Test B 2: Tracking the Sequence 5
Doris Smith Jane Awe Lorina Marc Test C 2: Tracking the Sequence 5
Doris Smith Jane Awe Lorina Marc Overall 3: Searching for Exceptions 3
Doris Smith Jane Awe Lorina Marc Test A 3: Searching for Exceptions 3
Doris Smith Jane Awe Lorina Marc Test B 3: Searching for Exceptions 3
Doris Smith Jane Awe Lorina Marc Test C 3: Searching for Exceptions 3
I feed this into SQL Server Reporting Services using either table wizard or matrix wizard. I have to move Skill column over the Score column so the skills are now columns.
Row Groups: Director, Supervisor, Worker Test
Column Group: Skill
Value: Score
I get this:
Director Suprviser Worker Test 1: Identifying Support 2: Tracking the Sequence 3: Searching for Exceptions
Doris Smith Jane Awe Lorina Marc Overal 1 3 3
Test A 4 2 3
Test B 1 5 3
Test C 5 5 3
Al Vega Overal 5 5 3
Test A 3 3 2
Test B 2 4 4
Test C 5 2 5
David Osorio Overal 1 1 3
Test A 2 4 2
Test B 4 5 1
Test C 2 3 2
Katie Lewis Ally McIntosh Overal 1 2 3
Test A 5 3 4
Test B 3 3 2
Test C 1 3 2
Christina Gooderd Overal 2 2 1
Test A 4 4 1
Test B 5 5 4
Test C 2 5 4
I need to have a value in each cell, so the values need to repeat for each group. So, what I want should look like this:
Director Suprviser Worker Test 1: Identifying Support 2: Tracking the Sequence 3: Searching for Exceptions
Doris Smith Jane Awe Lorina Marc Overal 1 3 3
Doris Smith Jane Awe Lorina Marc Test A 4 2 3
Doris Smith Jane Awe Lorina Marc Test B 1 5 3
Doris Smith Jane Awe Lorina Marc Test C 5 5 3
Doris Smith Jane Awe Al Vega Overal 5 5 3
Doris Smith Jane Awe Al Vega Test A 3 3 2
Doris Smith Jane Awe Al Vega Test B 2 4 4
Doris Smith Jane Awe Al Vega Test C 5 2 5
Doris Smith Jane Awe David Osorio Overal 1 1 3
Doris Smith Jane Awe David Osorio Test A 2 4 2
Doris Smith Jane Awe David Osorio Test B 4 5 1
Doris Smith Jane Awe David Osorio Test C 2 3 2
Doris Smith Katie Lewis Ally McIntosh Overal 1 2 3
Doris Smith Katie Lewis Ally McIntosh Test A 5 3 4
Doris Smith Katie Lewis Ally McIntosh Test B 3 3 2
Doris Smith Katie Lewis Ally McIntosh Test C 1 3 2
Doris Smith Katie Lewis Christina Gooderd Overal 2 2 1
Doris Smith Katie Lewis Christina Gooderd Test A 4 4 1
Doris Smith Katie Lewis Christina Gooderd Test B 5 5 4
Doris Smith Katie Lewis Christina Gooderd Test C 2 5 4
What do I fix/change/modify so I can have a value in each cell filled in?
As you've seen, SSRS treats row header cells differently, i.e. stretching them over any child groups.
Your report probably looks a bit like this:
I've highlighted the dotted lines that separate the report areas. This example report has the same issue as your example:
To get around this, the various row group values need to be moved from the row header area to the main report area.
First, delete the left four rows - when prompted choose Delete columns only.
You should only have the Skill column remaining.
Right click and Insert Column -> Outside Group - Left.
Keep adding columns using Insert Column - Left.
Once you have enough new columns, add the various grouping values. The report should look something like this:
Note that there are now no dotted lines between the Skill column and the Test column.
Now the group values are repeated for each row as required:
I had a similar issue, but in a matrix, needed to repeat prior value at the details group level when there is only one rowgroup. To do this, I used custom code.
For example, I have a column in my matrix named "Clusters". The row group field is a simple date field. In my data set, I have Date, Clusters, ResourceType as fields. I have different resource type values and so I can see the dates that clusters associated with a given resource type have data. My challenge came from the fact that my data set is sparse - for a given ResourceType value, not all dates have values. In my matrix, I ended up with rows that have blank values in cells that there is no corresponding row in the underlying data set. The use of Previous doesn't work well (lots of other examples).
To solve the problem, I used custom code and a hashtable as follows:
Private LastSeenValue as System.Collections.HashTable = New System.Collections.HashTable
' GetRowValue is used to fill in blank cells in the dynamic matrix with the nearest value above them in the same column.
' The data can contain multiple sets according to ResourceType field, and not all dates are present in all of these sets.
' This has the effect on the screen of having blank cells for each given date where there is no corresponding resource type.
' The requirement this function enables is that it allows filling in the blank cells with the nearest real value above.
' The SSRS PreviousRow function does not do this.
' Author: DanRo, 1/8/2016
'
' Some behavior notes for developers who follow and seek to alter the function.
' The prototype for the GetRowValue function performs "null to zero" coercion as a result of return type. This was done purposefully.
' The Object type for the FieldVal inpute parameter allows null rows to be processed with the same type of coercion
' on the incoming side.
' This is report specific logic that takes advantage of the fact that all of data requiring this function is numeric.
Function GetRowValue(ByVal FieldName as String, ByVal FieldVal as Object, ByVal ResourceType as String) As Double
' TheKey variable allows this function to be used for any number of columns for any number of resource types.
Dim TheKey as String
TheKey = "[" & FieldName & "][" & ResourceType & "]"
' See if a value was passed. In SSRS, when the cell tries to render in the matrix, there
' is no underlying data row for the column region, so a null (Nothing) gets passed by the runtime environment.
If FieldVal is Nothing Then
' Coercion on the return type happens when the HashTable Item property returns Null if the lookup fails.
' If the lookup succeeds, the last value encountered (top to bottom) will be present.
Return LastSeenValue(TheKey)
End If
' now we know that a value was passed
If (Not LastSeenValue.ContainsKey(TheKey)) Then
LastSeenValue.Add(TheKey, FieldVal)
Return FieldVal
End If
' A value was passed and we have an old value. Update it
LastSeenValue(TheKey) = FieldVal
Return FieldVal
End Function
Finally, in the Matrix Cell for Clusters column I set the expression to:
=Code.GetRowValue("Clusters", Fields!Clusters.Value, ReportItems!ResourceType.Value)
And that solved the problem. An added benefit is now my rows at the start of the table that were blank now correctly contain zeroes (which was correct for me). One tricky thing was Typing the FieldVal argument to Object instead of Integer (which was my Clusters data type) because you cannot check for existence on value types. Another was referring to the ReportItem!ResourceType.Value instead of Fields!ResourceType.Value because ResourceType was my column grouping. Finally, the return type choice for the function will impact whether your data has decimal points - so the choice of Double lets you handle both integers and real numbers. This would have to be modified to make this function handle strings correctly.
Before, Original Experience:
After, Values are now repeating where previously blank: