mySql - Select but don't show duplicates of specific row - mysql

I need to SELECT from a db and don't show rows that are identical in SOME rows (or actually hide the rows that are identical to eachother except 1 item...)
Let's give an example:
ID C1 C2 C3
1 3 3 4
1 5 5 4
1 2 3 4
1 6 5 4
1 2 3 4
After SELECT i want:
ID C1 C2 C3
1 X 3 4
1 X 5 4
where "X" has no importance...i have to show that column but i don't care which one is shown.
Is this possible with a simple SELECT query?
To sum up, if i ask the question regarding this specific example, What can i do to SELECT from that table and show only one of the rows if it has identical duplicates in ID, C2 and C3?
SIDENOTE: this MYSQL: SELECT Method - but don't show duplicates / GROUP or DISTINCT? doesn't help.

Have you tried GROUP BY?
SELECT Id, MAX(C1), C2, C3
FROM SomeTable
GROUP BY Id, C2, C3
Since you don't care about C1, in this query, I get the largest C1 value.

Related

Split N-Values to N-rows using MySQL Query [duplicate]

This question already has an answer here:
Split Comma separated Values in Column MYSQL
(1 answer)
Closed 5 years ago.
I have the table training, I want to split Training_name Column values to multiple Rows:
SLNO Category Training_name
1 A 1,5,9,15,12,16
2 B 2,6,10,17
3 C 1,3,7,19,14,18
I used below Query but using this Query i can only split to 2 Rows only?
SELECT training.SLNO,training.CATEGORY, SubString_Index(training.TRAINING_NAME, ',', 1) AS TRAINING_NAME FROM training UNION ALL SELECT training.SLNO,training.CATEGORY, SubString_Index(training.TRAINING_NAME, ',', -1) FROM training
i am trying to get the table as given below,Please help me out
SLNO Category Training_name
1 A 1
1 A 5
1 A 9
1 A 15
1 A 12
1 A 16
2 B 2
2 B 6
2 B 10
2 B 17
3 C 1
3 C 3
3 C 7
3 C 19
3 C 14
3 C 18
Here is an option which will work well if every training name entry has two values separated by a single comma.
SELECT
PARENT_SLNO,
RNO,
TRAINING_CATEGORY,
SUBSTRING_INDEX(TRAINING_NAME, ',', 1) AS TRAINING_NAME
FROM yourTable
UNION ALL
SELECT
PARENT_SLNO,
RNO,
TRAINING_CATEGORY,
SUBSTRING_INDEX(TRAINING_NAME, ',', -1)
FROM yourTable
If you want to convert your entire table, then you could select the above query into a new table, delete the old one, then rename the new one. If your CSV data could have a varying number of commas then my query would need to be modified, but the general approach could remain the same.

mysql row numbering reset every different record values

My question is pretty similar to this one Auto number and reset count for each different column value
except that I can't make it work.
I have the table record:
ID(autoINC) plate_number
1 A
2 A
3 A
4 B
5 B
6 C
7 C
I want to display something like this adding additional field cc:
I have the table record:
ID(autoINC) plate_number count
1 A 1
2 A 2
3 A 3
4 B 1
5 B 2
6 C 1
7 C 2
You can have a correlated subquery which sequentially count the row which can be used as a rownumber.
SELECT A.ID,
A.plate_number,
(
SELECT COUNT(*)
FROM tableName c
WHERE c.plate_number = a.plate_number AND
c.ID <= a.ID) AS RowNumber
FROM TableName a
SQLFiddle Demo

Get N number of records from child table for each parent record in a MySQL View

I tried finding answer to this question in SO , but could not find any. Any links will be of great help.
I have a parent table and a child table with one to many relationship between the parent and child table. The child table contains around 1 million records and I want to create a view with 1st 10 records in child table for each parent record.
Example-
Parent_Table - Fields -- ID, Name
ID Name
---- -----
1 A
2 B
3 C
Child_Table - Fields -- ID, ParentID, Date, Data
ID ParentID Date Data
--------------------------
1 1 04/10 A1
2 1 04/11 A2
3 1 04/11 A3
4 1 04/12 A4
5 1 04/12 A5
6 2 04/10 B1
7 2 04/11 B2
8 2 04/12 B3
9 2 04/12 B4
10 2 04/13 B5
11 2 04/13 B6
Now, I want to create a view with 1st 4 records for each parent record sorted by date.
Output Expected
ID ParentID Date Data
--------------------------
1 1 04/10 A1
2 1 04/11 A2
3 1 04/11 A3
4 1 04/12 A4
6 2 04/10 B1
7 2 04/11 B2
8 2 04/12 B3
9 2 04/12 B4
Any links or guide to the solution will be appreciated. Thanks in advance!
In case you need any clarification, please post a comment.
If you need to create a VIEW, you could use something like this:
CREATE VIEW First_Four AS
SELECT c1.*
FROM
Child_Table c1 LEFT JOIN Child_Table c2
ON c1.ParentID = c2.ParentID
AND (STR_TO_DATE(c1.`date`, '%m/%Y')>STR_TO_DATE(c2.`date`, '%m/%Y')
OR (STR_TO_DATE(c1.`date`, '%m/%Y')=STR_TO_DATE(c2.`date`, '%m/%Y')
AND c1.ID>c2.ID)
)
GROUP BY
c1.ID, c1.ParentID, c1.`Date`, c1.Data
HAVING
COUNT(c2.ID)<4
I'm considering the field data as a VARCHAR column, so we need to use STR_TO_DATE, if it is not we can just compare c1.date with c2.date directly.
Please see fiddle here.
I tried this one my computer and it displayed based on your requirements using your own data. I changed some field name though like ID of Child to ChildID, Date to ChildDate, Data to ChildData. Here it is:
SELECT * FROM
(SELECT ParentID, ChildID, ChildDate, ChildData, #ChildRank:= CASE WHEN #Parent <> ParentID THEN 1 ELSE #ChildRank+1 END as ChildRanking, #Parent := ParentID as Parent FROM
(SELECT #ChildRank:=0) CR, (SELECT #Parent:=1) P, (SELECT * FROM Child_Table ORDER BY
ParentID, ChildID) MainTable) AllTable WHERE ChildRanking <=4;
I use only the Child Table only but anyway you could INNER JOIN this with Parent_Table if you like.
A little explanation:
1) ChildRank will starts with Rank 0 (i.e. SELECT #ChildRank:0) but because of #ChildRank+1 it will start with Rank 1
2) When new ParentID (i.e. #Parent<> ParentID) then starts with Rank 1 right away.
3) AllTable is the alias for everything so that you could now reference the ChildRanking field.
If you don't want to display the ChildRanking field then you have to specify the fields you want to dispaly.

Mysql query only select rows with unique result within GROUP_CONCAT

Is there a way to select only the rows that have an other result than the row previous selected?
In one of my tables I store advertisement data, that’s one row per advertisement. I also store in an other table the prices for rental per dag, week, month, this table contain more than one row per advertisement.
I want to select al the rows from table 2 where there is a change in one of the prices (in the example row 1 and 3 in table 2) in the same query as the data selection. I know that I have to use a GROUP_CONCAT to get one row instead of a 2 row result in this case, but how to get 2 result rows from table 2 and 1 result row in total?
The outcome of the query has to be something like: tre,234,” 12345678911,12,45, 32555678911,12,67 ”
Table 1 (advertisements)
ID_adv data1 data2
1 tre 234
2 ghj 34
3 jk 098
4 jfjk 12
Table 2 (dates)
ID_dates ID_adv timestamp_day price1 price2
1 1 12345678911 12 45
2 1 22345677771 12 45
3 1 32555678911 12 67
4 2 42345671231 34 34
I tried
SELECT
t1.*,
GROUP_CONCAT(t2.date) AS dates
FROM Table1 t1
LEFT JOIN Table2 t2 ON t2.ID_adv = t1.ID_adv
WHERE t1.ID_adv = 3 GROUP BY t1.ID_adv
Can you try this one:
SELECT T3.ID_adv
, T3.data1
, T3.data2
, CAST(GROUP_CONCAT(CONCAT(T3.timestamp_day, ',', T3.price1, ',', T3.price2)) AS CHAR) AS DatePrice
FROM (
SELECT T1.*
, MIN(T2.timestamp_day) AS timestamp_day
, T2.price1
, T2.price2
FROM Table1 T1
LEFT JOIN Table2 T2 ON T2.ID_adv = T1.ID_adv
GROUP BY T1.ID_adv, T2.price1, T2.price2
) T3
GROUP BY T3.ID_adv;
I've tried it on SQL Fiddle.

Access Totals Query Not Necessarily Returning First Record

I have a table of data like this:
id user_id A B C
=====================
1 15 1 2 3
2 15 1 2 5
3 20 1 3 9
4 20 1 3 7
I need to remove duplicate user ids and keep the record that sorts lowest when sorting by A then B then C. So using the above table, I set up a temp query (qry_temp) that simply does the sort--first on user_id, then on A, then on B, then on C. It returns the following:
id user_id A B C
====================
1 15 1 2 3
2 15 1 2 5
4 20 1 3 7
3 20 1 3 9
Then I wrote a Totals Query based on qry_temp that just had user_id (Group By) and then id (First), and I assumed this would return the following:
user_id id
===========
15 1
20 4
But it doesn't seem to do that--instead it appears to be just returning the lowest id in a group of duplicate user ids (so I get 1 and 3 instead of 1 and 4). Shouldn't the Totals query use the order of the query it's based upon? Is there a property setting in the query that might impact this or another way to get what I need? If it helps, here is the SQL:
SELECT qry_temp.user_id, First(qry_temp.ID) AS FirstOfID
FROM qry_temp
GROUP BY qry_temp.user_id;
You need a different type of query, for example:
SELECT tmp.id,
tmp.user_id,
tmp.a,
tmp.b,
tmp.c
FROM tmp
WHERE (( ( tmp.id ) IN (SELECT TOP 1 id
FROM tmp t
WHERE t.user_id = tmp.user_id
ORDER BY t.a,
t.b,
t.c,
t.id) ));
Where tmp is the name of your table. First, Last, Min and Max are not dependent on a sort order. In relational databases, sort orders are quite ephemeral.