I'm looking for the most appropriate method to retrieve a list of duplicates in my database with laravel and eloquent.
Exemple of table records :
id : 1
artist_name : Jane Doe
track_name : The doe song
id : 2
artist_name : John Doe
track_name : A love song
id : 3
artist_name : Jane Doe
track_name : The doe song
id : 4
artist_name : John Doe
track_name : Happy
id : 5
artist_name : John Doe
track_name : Happy
Result expected (a list of all duplicates):
id : 1
artist_name : Jane Doe
track_name : The doe song
id : 3
artist_name : Jane Doe
track_name : The doe song
id : 4
artist_name : John Doe
track_name : Happy
id : 5
artist_name : John Doe
track_name : Happy
I tried the following piece of code but I'm getting an error :
$tracks = Track::whereIn('id', function ( $query ) {
$query->select('id')->from('tracks')->groupBy('track_name')->havingRaw('count(*) > 1');
})->paginate(20);
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1
of SELECT list is not in GROUP BY clause and contains nonaggregated
column 'table.tracks.id' which is not functionally dependent on
columns in GROUP BY clause; this is incompatible with
sql_mode=only_full_group_by (SQL: select count() as aggregate from
tracks where id in (select id from tracks group by
track_name having count() > 1))
This is not recommended but you should try
Remove ONLY_FULL_GROUP_BY from phpmyadmin
Open phpmyadmin & select localhost
Click on menu Variables & scroll down for sql mode
Click on edit button to change the values & remove ONLY_FULL_GROUP_BY & click on save.
Or you can run following query :
mysql > SET GLOBAL sql_mode=(SELECT REPLACE(##sql_mode,'ONLY_FULL_GROUP_BY',''));
Related
I have 2 sheets : Sheet1 has large number of records with columns Firstname, Lastname and Company. Another Sheet2 has same columns but small set of records. I want only those records from sheet1 which have a matching firstname and lastname in sheet2 but company is different. How can I achieve this with help of a formula? (it has to be scale-able).
E.g.
Sheet1
Firstname Lastname Company
John Doe ABC
Jon Smith XXX
Dan S. XXX
John Davies XXX
Sheet2
Firstname Lastname Company
John Davies ABC
Jon Smith XXX
Expected output :
Firstname Lastname Company
John Davies XXX
Try below formula:
=SORT(IFERROR(ARRAYFORMULA(SPLIT(ARRAYFORMULA(VLOOKUP(ARRAYFORMULA(E2:E&" "&F2:F&" "&C2:C),ARRAYFORMULA(A2:A&" "&B2:B&" "&C2:C),1,0))," ")),""),1,false)
Let's say i have a table structure like this :
ID | Name | SCHOOLNAME | CODESCHOOL
1 DARK Kindergarten 123 1
2 DARK Kindergarten 111 1
3 Knight NY University 3
4 Knight LA Senior HS 2
5 JOHN HARVARD 3
so, how to diplay all of the data above into like this :
ID | Name | SCHOOLNAME | CODESCHOOL
1 DARK Kindergarten 123 1
3 Knight NY University 3
5 JOHN HARVARD 3
my purpose is want to display data with the max of codeschool, but when i tried with my query below :
SELECT NAME, SCHOOLNAME, MAX(CODESCHOOL) FROM TABLE GROUP BY NAME, SCHOOLNAME
but the result is just like this :
ID | Name | SCHOOLNAME | CODESCHOOL
1 DARK Kindergarten 123 1
2 DARK Kindergarten 111 1
3 Knight NY University 3
4 Knight LA Senior HS 2
5 JOHN HARVARD 3
maybe it caused by the GROUP BY SCHOOLNAME, when i tried to not select SCHOOLNAME, the data displayed just like what i expected, but i need the SCHOOLNAME field for search condition in my query
hope you guys can help me out of this problem
any help will be appreciated
thanks
Using some wacky joins you can get a functional get max rows per category query.
What you essentially need to do is to join the table to itself and make sure that the joined values only contain the top values for the CODESCHOOL column.
I've also added a :schoolname parameter because you wanted to search by schoolname
Example:
SELECT
A.*
FROM
TABLE1 A
LEFT OUTER JOIN TABLE1 B ON B.NAME = A.NAME
AND B.CODESCHOOL < A.CODESCHOOL
WHERE
B.CODESCHOOL IS NULL AND
(
(A.SCHOOLNAME = :SCHOOLNAME AND :SCHOOLNAME IS NOT NULL) OR
(:SCHOOLNAME IS NULL)
);
this should create this output, note that dark has 2 outputs because it has 2 rows with the same code school which is the max in the dark "category"/name.
ID|NAME |SCHOOLNAME |CODESCHOOL
--| -----|----------------|----------
4|Knight|LA Senior HS | 2
5|JOHN |HARVARD | 3
2|DARK |Kindergarten 111| 1
1|DARK |Kindergarten 123| 1
It's not the most effective query but it should be more than good enough as a starting point.
Sidenote: I've been blatantly stealing this logic for a while from https://www.xaprb.com/blog/2007/03/14/how-to-find-the-max-row-per-group-in-sql-without-subqueries/
I am using an analytical window function ROW_NUMBER().
This will group (or partition) by NAME then select the top 1 CODESCHOOL in DESC order.
Select NAME,
SCHOOLNAME,
CODESCHOOL
From (
Select NAME,
SCHOOLNAME,
CODESCHOOL,
ROW_NUMBER() OVER (PARTITION BY NAME ORDER BY CODESCHOOL DESC) as rn
from myTable)
Where rn = 1;
Employee
Employee Type : 0130
Unit : 4189670095711234
Basic Salary : 11.00
Joined Date : 04/12/yy 06:30:05
Country : 826-United Kingdom
(123.66) --- Endof Employee -------------
R 4567 ABCD -> Len f---- i 01/14
Employee
Employee Type : 0120
Unit : 4189670095711234
Basic Salary : 11.00
Joined Date : 04/12/yy 06:30:05
Country : 826-United Kingdom
(123.66)- --- Endof Employee ------------
R 4567 ABCD -> Len f---- i 01/14
Employee
Employee Type : 0130
Unit : 4189670095711235
Basic Salary : 11.00
Joined Date : 04/12/yy 06:30:05
Country : 826-United Kingdom
(123.66) --- Endof Employee -------------
Hi,
I would like to convert the following semi-structured data to structured data using talend.
Please let me know how can i convert the data to structured form and so that i can insert it into a relational table.
Here is a solution, thanks to tPivotToColumnsDelimited component.
tFileInputDelimilted is associated with a 2 fields schema (nammed property and value) and has a special field separator which is " : " (space-colon-space).Avanced Setting options "Trim all columns" and "Check each row structure against schema" are ticked.
tMap is here to associate a rank for each input line depending the "property" name:
As you can see, the sequence name is based on the property name, so each file record for the same employee will have the same rank value.
Finally, tPivotToColumnsDelimited move on a single line all the input records with the same rank value and, most important, values are associated to the rigth property.
Set "Pivot column" as "property", "Aggregation column" as "value", "Aggregation function" as "first" and "Group by" as "rank". Select the desired filename for the output and finally you will get the desired result:
Hope this helps.
Hi ,
I am new to crystal report. I have a problem to create formula :
Let's say :
I have two tables :
tbl_Details :
Emp_id Emp_nameEmp_Deptt
1 Ram MMM
2 Naresh NNN
3 kapil HHH
4 Namita DDD
tbl_Mapping :
Type1 Type2 Emp_Deptt
ButterSotch ButterScotch NNN
ButterScotch Strawberry DDD
Olive Starch MMM
Olive Olive HHH
Note : In the above tables, Emp_Deptt column is same.
Now i want to create a formula like as below :
if Mapping.Type1 = Mapping.Type2 then find Emp_Deptt in Details table and get the Emp_name.
for example :
Butterscotch = Butterscotch then Emp_Deptt is NNN matched with Details table and the Emp_name is Naresh.
if doesn't find any match then nothing to do.
Now, i want to create a formula to get the desired value and print place somewhere in crystal report.
From Database Menu select 'Database Expert'. Add tbl_Mapping & tbl_Details to the selected tables.. make sure tbl_Mapping & tbl_Details are linked with Emp_Deptt fields.
Write following in the formula editor:
if {tbl_Mapping.Type1} = {tbl_Mapping.Type2} then {tbl_Details.Emp_name}
I have an excel sheet with poeple, each people has a father and a mother that is in the same poeple sheet. My exel table looks like that :
poeple --- father --- mother
john -------- tony ----- jane
tony -------- jack
I would like to import the datas to an Oracle database table that look like :
id --- poeple --- father --- mother
0 -----jack
1 -----tony-------- 0
2 -----jane
what should be my workflow ?
3 ----john -------- 1-----------2
It would be easier to at least start by loading the data into a table with the surrogate ID:
people father mother
------ ------ ------
john tony jane
tony jack
Then you can add rows for the fathers and mothers not already in the "people" column:
insert into mytable (people)
( select mother from mytable
union
select father from mytable
)
minus
select people from mytable;
That will give you:
people father mother
------ ------ ------
jack
tony jack
jane
john tony jane
You can then add a surrogate ID for each row and use that instead, if you need it.