Hybris Delete Duplicate Product Data - duplicates

Anyone knows how to create a code to delete duplicate product data in Hybris? either using an impex script or modifying the code?

Question: How to delete Hybris Products having a duplicate name?
You can run SQL / flexible query to find duplicate products and then delete those list of pk using SQL query.
List of pks to be removed
select MIN({p.pk}) as pks
from {Product! as p
JOIN CatalogVersion as CV on {p.catalogversion}={CV:PK} and {CV:version} = 'Online'
JOIN Catalog as C on {CV:catalog}={C:PK} and {C:id}='myProductCatalog'
}
group by {p:name}
having
count(*) > 1
Run the remove query
Remove all pks get from above query. Repeat this for the Online version as well.
You can find detail steps here

First of all, find the duplicated CMSItems using following flexiblesearch
SELECT {UID} as uid,{CatalogVersion} as CatalogVersion , count(1) as cnt from {CMSItem} GROUP BY {UID},{CatalogVersion} HAVING COUNT(1) > 1
then delete the duplicated ones.

Related

Removing Duplicating Issue in MySQL views

I have two tables called transactions (TransactionID, HotelID, service..) and services (id, userid, HotelID, type) and I need to create view from there. In here 1st table has 15 tuples and 2nd one has 8 tuples.
When I wrote this SQL query:
CREATE VIEW summary
AS
SELECT TransactionID, userid, HotelID, service
FROM transactions, services
WHERE transactions.HotelID = services.name
I got 105 results in summary view. How I get rid of this duplication issue.
As far as i understands your question, i think you need to use group concate and group by using join query.
using group by and group concate duplication of record will be solve.
and using join you can get common data from both table.
try below query.
currently i am not having knowledge of your table structure that's why i take assumption of your query and created my own tables and as per them created query.
Eg:
select
c.country_id,c.country_name,group_concat(s.state_id),group_concat(s.state_name) from country c inner join state s on c.country_id =s.country_id group
by c.country_id;
if you have any query feel free to ask.

SQL not exists to find some issues

I'm practicing some SQL (new to this),
I have the next tables:
screening_occapancy(idscreening,row,col,idclient)
screening(screeningid,idmovie,idtheater,screening_time)
Im trying to creating a query to search which clients watched all the movies in the "screening" table and show their ID(idclient).
this is what I written(which doesn't work):
select idclient from screening_occapancy p where not exists
(select screeningid from screening where screeningid=p.idscreening)
I know it's probably not that good so please try to explain also what am I doing wrong.
P.S My mission is to use not/exists while doing it...
Thanks!
Your query is basically fine, although the select distinct is unnecessary in the subquery:
select p.idclient
from screening_occapancy p
where not exists (select 1
from screening s
where s.screeningid = p.idscreening
);
Notes:
You can select anything in the exists subquery. Selecting a column is misleading.
Use table aliases and use them for all column references, particularly in a correlated subquery.
If you are designing the tables, I would advise you to give the primary key and foreign key the same name (screeningid or idscreening, but not both).
EDIT:
If you want clients who watched all movies, then I would approach this as:
select p.idclient
from screening_occapancy p
group by p.idclient
having count(distinct p.screening_occapancy p) = (select count(*) from screening);
Why don't you count the number of movies in the screening_table, load it into a variable and check the results of your query results against the variable?
load number of movies into variable (identified by idmovie):
SELECT count(DISTINCT(idmovie)) FROM screening INTO #number_of_movies;
check the results of your query against the variable:
SELECT A.idclient,
count(DISTINCT(idmovie)) AS number_of_movies_watched,
FROM screening_occapancy A
INNER JOIN screening B
ON(A.idscreening = B.screeningid)
GROUP BY A.idclient
HAVING number_of_movies_watched = #number_of_movies ;
If you want to find all clients, that attended all screenings, replace idmovie with screeningid.
Even someone relatively new to MySQL can get his head around this query. The "not exists"-approach is more difficult to understand.

Query to join 3 tables

I need to write a query to join 3 tables.
My tables are:
ucommerce_customer
ucommerce_order
ucommerce_order_line
All 3 tables have a column called order_id.
The table ucommerce_order has a column called order_status.
When the order_status is set to "open" I want to display the order details.
ResultSet myRs = myStmt.executeQuery
("SELECT * FROM ucommerce_customer
INNER JOIN ucommerce_order
INNER JOIN ucommerce_order_line
WHERE ucommerce_order.order_status = 'open'");
My query ignores the order status and displays all orders, open and closed.
Also I have several products so ucommerce_order_line has several entries for the same order_id, my query displays duplicate entries and it duplicates the entire list as well.
How can I write a query that will show only open orders without duplicating everything?
In MySQL, the on/using clause is optional. This is very sad because someone can make mistakes like you did. Your question only mentions one column, so perhaps that is all that is needed for the join:
SELECT *
FROM ucommerce_customer INNER JOIN
ucommerce_order
USING (orderId) INNER JOIN
ucommerce_order_line
USING (OrderId)
WHERE ucommerce_order.order_status = 'open';
I would be surprised if the customer table really had a column called OrderId (seems like a bad idea in most situations), so the first USING clause might want to use CustomerId.
I would recommend to use a natural join instead. Maybe that's where the errors are coming from.
The duplicates can be removed by running SELECT DISTINCT * ...

mysql query "GROUP BY" giving random results

I was here once before and was assisted tremendously:
mysql group by returning incorrect result
I am now stuck with a similar Query which has an added table:
here is the Fiddle http://sqlfiddle.com/#!2/7fe40/5
There are 3 tables: Project, Task and Timesheet
The Task table has a foreign key (project_id) which links back to the Parent Project, it also holds a value for "assigned hours".
The timesheet table holds the "actual_hours" and the users name.
what I am trying to do is SUM how many actual hours have been spent on each project.
this query returns incorrect and random results:
SELECT DISTINCT
`timesheet`.`name`,
SUM(`timesheet`.`hours`) AS `total_hours`,
`project`.`project_name`,
`task`.`task_name`,
SUM(`task`.`hrs`) AS `assigned_hours`
FROM
`task`
INNER JOIN `project` ON (`task`.`project_id` = `project`.`project_id`)
INNER JOIN `timesheet` ON (`task`.`task_id` = `timesheet`.`task_id`)
GROUP BY
`project`.`project_name`
Any help greatly appreciated.
Thanks
Mysql has support for non-standard grouping. The fix is to group by all non-aggregate selected columns:
...
GROUP BY `timesheet`.`name`, `project`.`project`, `task`.`task_name`
You should also remove the DISTINCT keyword.
In mysql only (all other databases will raise an exception), grouping by fewer than all non-aggregated columns results in a random row for each unique combination of those columns that are grouped by (although in practice, it isn't a random row but the first row encountered - YMMV is you rely on this).
If you just want the sum for the projects, why are you including the tasks?
SELECT SUM(ts.`hours`) AS `total_hours`,
p.`project_name`,
SUM(t.`hrs`) AS `assigned_hours`
FROM `task` t INNER JOIN
`project` p
ON t.`project_id` = p.`project_id`) INNER JOIN
`timesheet` ts
ON t.`task_id` = ts.`task_id`
GROUP BY p.`project_name`;
I have a feeling though that this might give inaccurate results because there may be multiple timesheet records for each task. So you can try:
SELECT SUM(ts.total_hours) AS `total_hours`,
p.`project_name`,
SUM(t.`hrs`) AS `assigned_hours`
FROM `task` t INNER JOIN
`project` p
ON t.`project_id` = p.`project_id` INNER JOIN
(select ts.task_id, sum(ts.hours) as total_hours
from `timesheet` ts
group by ts.task_id
) ts
ON t.`task_id` = ts.`task_id`
GROUP BY p.`project_name`;
Here is a SQL Fiddle.

SQL Select - Some Rows Won't Display

I have two tables. One of them named files and there is al list of all files. the second table called payments, and there is in there a list of payments for some files.
Payments:
id | fileid | {...}
1 2
2 3
3 2
Files:
id | {...}
1
2
3
I want to select all files, and join the table payments to order by count of this table.
In this case, the first row will be file #2, because it repeats the most in the payments table.
I tried to do it, but when I do it - not all of the rows are shown!
I think it happens because not all of the files are in the payments table. So in this case, I think that it won't display the first row.
Thanks, and sorry for my English
P.S: I use mysql engine
** UPDATE **
My Code:
SELECT `id`,`name`,`size`,`downloads`,`upload_date`,`server_ip`,COUNT(`uploadid`) AS numProfits
FROM `uploads`
JOIN `profits`
ON `uploads`.`id` = `profits`.`uploadid`
WHERE `uploads`.`userid` = 1
AND `removed` = 0
ORDER BY numProfits
As others have noted you need to use LEFT JOIN. - This tells MySQL that entries from the tables to the left should be included even if no corresponding entries exists in the table on the right.
Also you should use GROUP BY to indicate how the COUNT should be deliminated.
So the SQL should be something like;
SELECT Files.ID, count(Payments.FileID) as numpays FROM
Files
LEFT OUTER JOIN
Payments
ON Files.id=Payments.FileID
GROUP BY files.ID
ORDER BY numpays desc
SQL Fiddle
Try this:
select B.fileid,A.{}.....
from
(select id,.....
from files A
inner join
(select count(*),fileid,.....
from payments
group by fileid) B
on files.id=payments.fileid)
I hope this helps. I'm assuming that all ID in files table are unique. In this answer, you can apply an order by clause as per your wish. I've left the select statement to you to select whatever data you want to fetch.
As far as your problem is described, I think this should work. If any problems, do post a comment.
Try LEFT JOIN - in MySQL, the default JOIN is actually an INNER JOIN. In an INNER JOIN, you will only get results back that are in both sides of the join.
See: Difference in MySQL JOIN vs LEFT JOIN
And, as noted in the comments, you may need a GROUP BY with your COUNT as well, to prevent it from just counting all the rows that come back.