Creating a new table using multiple SELECTs - mysql

Please see the image below:
Given the original table, I need to create the derived table in MySQL on the server.
With CREATE TABLE ... AS SELECT DISTINCT... I am able to create the derived table with the Category column, but trying in vain to create the Category_count column. Can you kindly point out how to solve this?
Not very comfortable with MySQL or SQL even, hence the request.
Many thanks in advance!

Use the following if the table does not exists:
CREATE TABLE derived AS
SELECT Category, COUNT(*) AS Category_count
FROM original
GROUP BY Category
Use the following if the table already exists and without creating duplicate rows:
INSERT INTO derived (Category, Category_count)
SELECT Category, COUNT(*) AS Category_count
FROM original o
WHERE NOT EXISTS (SELECT * FROM derived d WHERE d.Category = o.Category)
GROUP BY Category
Use the following to update the rows already exists:
UPDATE derived LEFT JOIN (
SELECT Category, COUNT(*) AS Category_count
FROM original
GROUP BY Category
)x ON derived.Category = x.Category
SET derived.Category_count = x.Category_count

Select catoregy,count(id)as category_count into derived from original group by catoregy

Related

sql stored procedure with out duplicates name

How can I create a query to SELECT ALL DB WITHOUT duplicates
Like (old DB that is no longer in use c,f,g. basically if it does have eur and has an original name than it is relevant):
a
b
c
ceur
d
f
feur
g
geur
I need it to be like:
a
b
ceur
d
feur
geur
Many thanks...
SELECT DISTINCT
is what you're looking for. See more here.
For instance, let's say you have a table that contains the following rows:
name, city, address, country.
You now wish to get the countries that has been stored, without duplicates. Multiple people might come from the same country, and so the table would most likely have duplicate entries of that country.
How you achieve this is by using the SELECT DISTINCT.
Example:
SELECT DISTINCT country FROM table_name;
What this will do is retreive the country row without duplicates. That way, you can see which countries are actually stored in that table without duplicates.
If you have multiple databases (I don't know if that's what you were getting at), then you will need to perform a JOIN on the relevant tables, given you have access to them all. I would recommend doing a LEFT JOIN if you are to join more than just 1 extra table.
Example:
SELECT DISTINCT table_name.row_name, table_name.row_name2, table_name.row_name3
FROM table_name
LEFT JOIN table_name2 ON table_name.row_name = table_name2.row_name
LEFT JOIN table_name3 ON table_name2.row_name = table_name3.row_name
[...]
WHERE table.row_name = 'value';
Can you query information_schema.TABLES and distinct in the select, plus a predicate to filter out whatever you don't want?
You can do:
select t.*
from t
where name like '%eur'
union all
select t.*
from t
where not like '%eur' and
not exists (select 1 from t t2 where t2.name = concat(t.name, 'eur');

Every derived table must have its own alias #1248

I am running this query on MySQL:
CREATE TABLE DuplicateSKU
SELECT * FROM FeedsAll
INNER JOIN (
SELECT FeedsAll.SKU
FROM FeedsAll
GROUP BY FeedsAll.SKU
HAVING COUNT(*) > 1) as DuplicateSKU;
and it is giving this error:
#1248 - Every derived table must have its own alias
What is wrong?
Help, please!
You are using the name DuplicateSKU twice, once for the new table once for your subquery. These should be different names, e.g.
create table duplicatesku
select *
from feedsall
inner join
(
select sku
from feedsall
group by sku
having count(*) > 1
) as duplicates on duplicates.sku = feedsall.sku;
By the way you were cross-joining (missing ON clause, which MySQL doesn't report unfortunately). I added the appropriate ON clause.

Get biggest COUNT(DISTINCT) with condition mysql

right now I'm trying to return the biggest COUNT(DISTINCT column)-number from a mysql table.
It's hard to describe, so I'll give you an example:
My table has the following columns: s_id, k_id, p_id.
Now I want to count the different s with the condition that every entry has the same p_id, too. I need this to prepare a HTML-Table (so i know how many Columns this table will have).
Data Example:
This is what I got, so far:
SELECT COUNT(DISTINCT k_id) AS a FROM `table`
the problem with this is, that there may be 4 different k_ids but 3 of them are related to p_id = 1 and the last one is releated to p_id = 2.
a returns 4 instead of 3.
Thanks for support!
I think you want this:
select p_id, count(distinct s_id) as cnt
from table
group by p_id
order by cnt desc
limit 1;
Please consider this:
select max(count(distinct(k_id))) from table
group by p_id

Eliminating duplicates from SQL query

What would be the best way to return one item from each id instead of all of the other items within the table. Currently the query below returns all manufacturers
SELECT m.name
FROM `default_ps_products` p
INNER JOIN `default_ps_products_manufacturers` m ON p.manufacturer_id = m.id
I have solved my question by using the DISTINCT value in my query:
SELECT DISTINCT m.name, m.id
FROM `default_ps_products` p
INNER JOIN `default_ps_products_manufacturers` m ON p.manufacturer_id = m.id
ORDER BY m.name
there are 4 main ways I can think of to delete duplicate rows
method 1
delete all rows bigger than smallest or less than greatest rowid value. Example
delete from tableName a where rowid> (select min(rowid) from tableName b where a.key=b.key and a.key2=b.key2)
method 2
usually faster but you must recreate all indexes, constraints and triggers afterward..
pull all as distinct to new table then drop 1st table and rename new table to old table name
example.
create table t1 as select distinct * from t2; drop table t1; rename t2 to t1;
method 3
delete uing where exists based on rowid. example
delete from tableName a where exists(select 'x' from tableName b where a.key1=b.key1 and a.key2=b.key2 and b.rowid >a.rowid) Note if nulls are on column use nvl on column name.
method 4
collect first row for each key value and delete rows not in this set. Example
delete from tableName a where rowid not in(select min(rowid) from tableName b group by key1, key2)
note that you don't have to use nvl for method 4
Using DISTINCT often is a bad practice. It may be a sing that there is something wrong with your SELECT statement, or your data structure is not normalized.
In your case I would use this (in assumption that default_ps_products_manufacturers has unique records).
SELECT m.id, m.name
FROM default_ps_products_manufacturers m
WHERE EXISTS (SELECT 1 FROM default_ps_products p WHERE p.manufacturer_id = m.id)
Or an equivalent query with IN:
SELECT m.id, m.name
FROM default_ps_products_manufacturers m
WHERE m.id IN (SELECT p.manufacturer_id FROM default_ps_products p)
The only thing - between all possible queries it is better to select the one with the better execution plan. Which may depend on your vendor and/or physical structure, statistics, etc... of your data base.
I think in most cases EXISTS will work better.

How do I multiple select in mysql using one query?

I have an sql statement like this:
Select Id,Name From table where id = var
Select Id,Name From table where id = var
Select Id,Name From table where id = var
I was able to a multiple select in one query but it's only allows one column per select.
SELECT (
SELECT COUNT(*)
FROM user_table
) AS tot_user,
(
SELECT COUNT(*)
FROM cat_table
) AS tot_cat,
(
SELECT COUNT(*)
FROM course_table
) AS tot_course
Use join if tables relates each other else
use union.
You can use any of subselects, unions or joins - it depends on what you're trying to do:
SQL joins are used to query data from two or more tables, based on a relationship between certain columns in these tables.
For instance:
SELECT user_table.name, user_table.email, cat_table.title
FROM user_table
FULL JOIN cat_table
ON user_table.cat_id=cat_table.id
ORDER BY user_table.id
More to read at http://www.w3schools.com/sql/sql_join.asp