SQL, One id with different values in one column - mysql

Here is my query:
SELECT t.id, t.phone
FROM tablename t
It results in duplicate IDs because in one column there are two or more different values in it.
ID Phone
1 540-500-5000
1 540-888-8888
2 340-600-6000
2 340-777-7777
3 210-200-2000
4 950-600-6000
4 950-444-4444
I want to select just the first phone for each ID, in order to avoid duplicated rows just because there are two or more phones under the same ID.
Desired output:
ID Phone
1 540-500-5000
2 340-600-6000
3 210-200-2000
4 950-600-6000

SQL Fiddle:
SELECT t.id, MIN(t.phone)
FROM tablename t
GROUP BY t.id

SELECT ID, MIN(phone) MinIsTheFirst
FROM tableName
GROUP BY ID
Just having fun with the word "FIRST"

Try this:
select ID, MIN(Phone)
from tablename
group by ID
This will give you what you want if you don't care which phone is returned. If you have a way of determining the first phone, we can adjust.

Related

Need a query to do the table from two table with common and not common columns

I have two tables with multiples row. Table 1 has a few rows which Table 2 no has. And the opposite.
I need a query which will make a follow table from Table 1 and 2 and sort by ID. Rows Opt3 and Opt4 can be ignored. As below:
How to do a query with SELECT right?
Something like this should work:
(SELECT Id, Question, Answer, Descr, Opt1, NULL AS Opt2
FROM Table_1)
UNION
(SELECT Id, Question, Answer, Descr, NULL, Opt2
FROM Table_2)
ORDER BY Id

Find duplicates where entries within id are duplicate

id class count day
1 2 5 5
2 2 4 5
3 2 4 5
3 2 4 5
4 2 5 3
4 1 5 3
4 2 5 3
So I have a query for finding all duplicates based on multiple columns, however, this also returns id's where not all entries are duplicate.
In above example, the query should only show/count id 3, as all the entries with id 3 are duplicate. Even though id 4 also has a duplicate, it should not show as it has another entry that is unique.
Any idea how to do this?
If you need rows with id where there is no row with the same id and unique row values then use NOT IN and HAVING
select *
from your_table t1
where t1.id not in(
select id
from your_table
group by id, class, count, day
having count(*) = 1
)
You can use this query : http://sqlfiddle.com/#!9/1a2536/8
select id
from test
group by id
having count(distinct id,class,count,day) = 1 and count(*)>1
you group each rows by id and count how many different row the group has, if the distinct total is 1 and the total row is > 1 , there is only duplicate rows for this id.
It's quite easy, a quick note it's a very bad idea to name a column count :
SELECT id, class, `count`,day, COUNT(*)
FROM myTable
GROUP BY id, class, `count`,day
HAVING COUNT(*) > 1
edit : I misread the question so here is my solution :
SELECT test.id, test.class, test.count, test.day , count(*), t.countID
FROM (SELECT id, Count(id) AS countID FROM test GROUP BY id ) t
INNER JOIN test on t.id = test.id
GROUP BY test.id, test.class, test.count, test.day
HAVING Count(*) > 1 AND t.countID = count(*)
I came up with this :
SELECT *
FROM (SELECT id,
Count(id) AS matched
FROM test
GROUP BY id,
class,
count,
day) t
GROUP BY id , matched
HAVING Count(id) = 1
AND matched >= 2
There is maybe a more efficient way to do it but it is easier to understand this way, first we group by every column to find duplicate data. Then the first part of the having eliminates the entries that actually have different variants by id and then we keep only the lines that actually only have duplicates.
Edit : compatible with "only_full_group_by" mode

Mysql select ONLY one row FROM table WHERE columns having the same value occurs more than once

id user_id animal
1 12 Cat
2 5 Lion
3 12 Snake
how do I select any one or the first one of user_id = 12. Please note: user_id 12 appears more than once in the table, I just need to select any one of them.
Since you say first or any, you can use limit.
select * from table where user_id=12 limit 1;
you need one for each user and other and you don't matter the othres row for the same user_id you can use
select *
from my_table
where (id, user_id) in (select min(id), user_id
from my_table
group by user_id)

How to create multiple rows from a initial row

I use mysql db engine, I wonder is it possible that the data in the table one row transferred to another table, this table would consist of two columns, id and value
each of the transferred value would go into one row and row would look like ID, value, and for as long as it has a value that is transferred to new row maintains the id as long as it has a value that belonged to the id of a row from which it transferred
Initial table looks like
id |country_name |city_1 |city_2 |city_3 |city_4
------------------------------------------------------------------------
1 |Some_country |some_city1 |some_city2 |some_city3 |some_city4
Wanted table looks like
id | city_name
1 | some_city1
1 | some_city2
1 | some_city3
1 | some_city4
Use this for one particular ID
select id, city_name from(
select id, city_1 as city_name from yourTable
union all
select id, city_2 from yourTable
union all
select id, city_3 from yourTable
union all
select id, city_4 from yourTable
) as t where id= yourID
http://sqlfiddle.com/#!9/7ee1f/1
Use this for whole table
select id, city_name from(
select id, city_1 as city_name from yourTable
union all
select id, city_2 from yourTable
union all
select id, city_3 from yourTable
union all
select id, city_4 from yourTable
) as t
order by id
What you are looking for is often referred to as vertical pivoting: you want to pivot something like an array of four city names - hard-wired into the table definition - into four vertical rows.
The solution is a cross join with a temporary table with as many consecutive integers, starting from 1, as you have columns to pivot, in conjunction with a CASE-WHEN expression that makes use of that series of integers.
See here:
WITH foo(id,country_name,city_1,city_2,city_3,city_4) AS (
SELECT 1,'Some_country','some_city1','some_city2','some_city3','some_city4'
)
, four_indexes(idx) AS (
SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
)
SELECT
id AS country_id
, idx AS city_id
, CASE idx
WHEN 1 THEN city_1
WHEN 2 THEN city_2
WHEN 3 THEN city_3
WHEN 4 THEN city_4
ELSE ''
END AS city_name
FROM foo CROSS JOIN four_indexes
;
country_id|city_id|city_name
1| 1|some_city1
1| 3|some_city3
1| 2|some_city2
1| 4|some_city4
Only the other day, I answered a question that was looking for reversing the operation we are performing here: horizontal pivoting.
See here if you're curious ...
How to go about a column with different values in a same row in sql?
Happy Playing -
Marco the Sane

Mysql select distinct but limit the distinct values

This might be confusing but what I want to do is a basic select distinct columnx limit 3, the problem I have is it returns 3 rows, but I only want to get all values from one distinct columnx.
id|columnx
1 |here
2 |here
3 |Idontwant
4 |Apple
so I want a query that will return 1 and 2. The problem is columnx could be anything and I cant just say where columnx = 'here'
The limit 3 comes into play because it's hard coded into my C# app. The issue is i also set a hashset based on columnx, i have to have that columnx static for all records. However, with every query i put together there is the possibility that with my limit i'll return 2 values in columnx, which are distinct values however, i can only have distinct columnx value per query.
No what I want is all values that belong to columnx, in this example columnx ='here', when i do a distinct all I want back is the first distinct result, not 'Idontwant' or 'apple' regardless of the limit.
Here is the query and it works.
SELECT id,columnx
FROM sites where columnx= (select distinct columnx from sites limit 1) limit 3
It sounds like you want the id values for three distinct columnx values. Is this right? Try this:
SELECT id, columnx
FROM table AS t1
JOIN (
SELECT DISTINCT(columnx) AS columnx
LIMIT 1
) AS t2 ON (t1.columnx = t2.columnx);
I also wonder if you need some ORDER BY in there somewhere, but your question doesn't mention that, so I've omitted it--essentially meaning that you'll get three semi-random columnx values.
To be honest I'm not sure what you are asking either but it sounds like you want to return the ids for those rows where there are duplicate values in columnx (limit the result to 3).
SELECT id, columnx
FROM tablename
GROUP BY columnx
HAVING ( COUNT(columnx) > 1 )
LIMIT 3