+---------+----------------+--------+
| aid | fn | col_no |
+---------+----------------+--------+
| 2011768 | ABDUL | 5 |
| 2011499 | ABDULLA | 4 |
| 2011198 | ADNAN | 3 |
| 2011590 | AKSHAYA PRAISY | 2 |
| 2011749 | AMIR | 1 |
| 2011213 | AMOGHA | 5 |
| 2011027 | ANU | 4 |
| 2011046 | ANUDEV D | 3 |
| 2011435 | B S SAHANA | 2 |
| 2011112 | BENAKA | 1 |
+---------+----------------+--------+
How to sort the number like col_no as 1 2 3 4 5 and again repeat as 1 2 3 4 5?
i need output like this
+---------+----------------+--------+
| aid | fn | col_no |
+---------+----------------+--------+
| 2011749 | AMIR | 1 |
| 2011590 | AKSHAYA PRAISY | 2 |
| 2011198 | ADNAN | 3 |
| 2011499 | ABDULLA | 4 |
| 2011768 | ABDUL | 5 |
| 2011112 | BENAKA | 1 |
| 2011435 | B S SAHANA | 2 |
| 2011046 | ANUDEV D | 3 |
| 2011027 | ANU | 4 |
| 2011213 | AMOGHA | 5 |
+---------+----------------+--------+
You can use row_number() partition by col_no:
select t.*
from t
order by row_number() over (partition by col_no order by fn),
col_no;
Here is a db<>fiddle.
Related
I have a query_table Table and wants to join with match_table Table with nearest matching string. If it was vice-versa then 'like' would have worked but have no idea how to do this.
query_table
+----+------------------+
| id | string |
+----+------------------+
| 1 | fcc9e8796feb |
| 2 | fcdbd7ebcf89 |
| 3 | fccc87896feb |
| 4 | fcc7c7896fef |
| 5 | fcced777aaaf |
+----+------------------+
match_table
+----+-----------+
| id | match_code|
+----+-----------+
| 1 | fcff |
| 2 | fcccc |
| 3 | fccc8 |
| 4 | fccc9 |
| 5 | fccdb |
| 6 | fccdc |
| 7 | fccd8 |
| 8 | fcce |
| 9 | fcced |
| 10 | fccee |
| 11 | fcce6 |
| 12 | fcc7b |
| 13 | fcc7c |
| 14 | fcc8e |
| 15 | fcc87 |
| 16 | fcc88 |
| 17 | fcc9e |
| 18 | fcdbb |
| 19 | fcdbc |
| 20 | fcdbd |
+----+-----------+
I expect
result
+----+------------------+----+----------------+
| id | string | id | match_code |
+----+------------------+----+----------------
| 1 | fcc9e8796feb | 17 | fcc9e |
| 2 | fcdbd7ebcf89 | 20 | fcdbd |
| 3 | fccc87896feb | 3 | fccc8 |
| 4 | fcc7c7896fef | 13 | fcc7c |
| 5 | fcced777aaaf | 9 | fcced |
+----+------------------+----+----------------+
May be this is really a simple question, thanks in advance.
What I currently have:
+-----+---+---+---+---+
| sid | a | b | c | d |
+-----+---+---+---+---+
| 123 | | | | 4 |
| 123 | | 2 | | |
| 123 | | | 3 | |
| 123 | 1 | | | |
| 456 | | 5 | | |
| 456 | | | 6 | |
| 789 | | | | 8 |
| 789 | 7 | | | |
+-----+---+---+---+---+
What I am trying to get:
+-----+------+------+------+------+
| sid | a | b | c | d |
+-----+------+------+------+------+
| 123 | 1 | 2 | 3 | 4 |
| 456 | | 5 | 6 | |
| 789 | 7 | | | 8 |
+-----+------+------+------+------+
How such "rows concatenation" could be done in MySQL?
You can do this with the MAX() aggregation function with a GROUP BY clause in your query.
SELECT sid, MAX(a), MAX(b), MAX(c), MAX(d)
FROM table
GROUP BY sid
I used MAX() because it will filter the NULL values with others values.
More explanation here : MySQL Documentation
I have a question very similar to this one, but different (aka, I couldn't extend the answer to that one to fit my purposes. Due to the second WHERE condition, specifically).
I have a table which tracks the visit number for customers. There are two types of visits:
| ID | InStoreVisit | InStoreDate | OnlineVisit | OnlineDate |
|----|--------------|-------------|-------------|------------|
| 1 | 1 | 1/1/11 | | |
| 1 | 2 | 1/2/11 | | |
| 1 | | | 1 | 1/3/11 |
| 1 | 3 | 1/4/11 | | |
| 2 | | | 1 | 2/2/12 |
| 2 | 1 | 2/3/12 | | |
| 2 | | | 2 | 2/4/12 |
I need to create a new column which has a sort of 'global visit number' as such:
| ID | InStoreVisit | InStoreDate | OnlineVisit | OnlineDate | GobalVisit |
|----|--------------|-------------|-------------|------------|------------|
| 1 | 1 | 1/1/11 | | | 1 |
| 1 | 2 | 1/2/11 | | | 2 |
| 1 | | | 1 | 1/3/11 | 3 |
| 1 | 3 | 1/4/11 | | | 4 |
| 2 | | | 1 | 2/2/12 | 1 |
| 2 | 1 | 2/3/12 | | | 2 |
| 2 | | | 2 | 2/4/12 | 3 |
I'm getting mixed up on the WHERE condition with which I can do the self-join. Any advice greatly appreciated.
i'm pretty new to sql and i have a problem that i coundn't really describe to google
SELECT name AS state
FROM state
WHERE id IN
(
SELECT state_id
FROM city
WHERE id=(SELECT city_id FROM zipcode)
)
;
This lists the states of all zipcodes I have in my database, I also want to list the zipcodes (zipcode.zipcode) in a additional column corresponding with the state
Thank you very much in advance for your help
my tables look like this:
city
+----+----------+-----------+---------------------+---------+---------+
| id | state_id | county_id | name | lat | lng |
+----+----------+-----------+---------------------+---------+---------+
| 1 | 1 | 1 | Prem, Oberbayern | 47.6833 | 10.8 |
| 2 | 2 | 2 | Pfullendorf (Baden) | 47.9249 | 9.25718 |
| 3 | 3 | 3 | Wissen, Sieg | 50.7833 | 7.75 |
| 4 | 1 | 4 | Miltenberg | 49.7039 | 9.26444 |
| 5 | 1 | 5 | Moosthenning | 48.6833 | 12.5 |
| 6 | 1 | 1 | Bernbeuren | 47.7333 | 10.7833 |
| 7 | 4 | 6 | Demmin, Hansestadt | 53.9 | 13.0333 |
| 8 | 2 | 7 | Konstanz, Universit | 47.6667 | 9.18333 |
| 9 | 5 | 8 | Teutschenthal | 51.45 | 11.8 |
| 10 | 6 | 9 | Vierlinden | 52.515 | 14.3141 |
+----+----------+-----------+---------------------+---------+---------+
zipcode
+----+---------+-------------+---------+
| id | city_id | district_id | zipcode |
+----+---------+-------------+---------+
| 1 | 1 | NULL | 86984 |
| 2 | 2 | NULL | 88630 |
| 3 | 3 | NULL | 57537 |
| 4 | 4 | NULL | 63897 |
| 5 | 4312 | 502 | 84164 |
| 6 | 6 | NULL | 86975 |
| 7 | 7 | 778 | 17109 |
| 8 | 8 | NULL | 78462 |
| 9 | 8 | NULL | 78464 |
| 10 | 8 | NULL | 78465 |
+----+---------+-------------+---------+
state
+----+------------------------+
| id | name |
+----+------------------------+
| 1 | Bayern |
| 2 | Baden-W?rttemberg |
| 3 | Rheinland-Pfalz |
| 4 | Mecklenburg-Vorpommern |
| 5 | Sachsen-Anhalt |
| 6 | Brandenburg |
| 7 | Niedersachsen |
| 8 | Schleswig-Holstein |
| 9 | Nordrhein-Westfalen |
| 10 | Th?ringen |
+----+------------------------+
Use explicit JOINs:
SELECT z.*, s.name AS state
FROM zipcode z JOIN
city c
ON z.city_id = c.id JOIN
state s
ON c.state_id = s.id;
If you are learning SQL, focus on learning JOIN rather than IN.
I have three table
td_product
|--------------|------------------------------------------------------------|
| product_id | product_title | compatible_model | |
|---------------------------------------------------------------------------|
| 1 | prod-1 | 1,4,5,6,8 | |
|---------------------------------------------------------------------------|
| 2 | prod-2 | 1,5,6 | |
|---------------------------------------------------------------------------|
| 3 | prod-3 | 4,6 | |
|---------------------------------------------------------------------------|
the i have
td_model
|--------------|----------------------------------------------------|
| model_id | model_title | brand_id | model_slug |
|-------------------------------------------------------------------|
| 1 | mode-1 | 1 | 1-mode-1 |
|-------------------------------------------------------------------|
| 2 | mode-2 | 2 | 2-mode-2 |
|-------------------------------------------------------------------|
| 3 | mode-3 | 4 | 3-mode-3 |
|-------------------------------------------------------------------|
| 4 | mode-4 | 4 | 4-mode-4 |
|-------------------------------------------------------------------|
| 5 | mode-5 | 2 | 5-mode-5 |
|-------------------------------------------------------------------|
| 6 | mode-6 | 4 | 6-mode-6 |
|-------------------------------------------------------------------|
| 7 | mode-7 | 1 | 7-mode-7 |
|-------------------------------------------------------------------|
| 8 | mode-8 | 2 | 8-mode-8 |
|-------------------------------------------------------------------|
and finally the brand
|--------------|------------------------------------------------------------|
| brand_id | brand_title | |
|---------------------------------------------------------------------------|
| 1 | brnd-1 | |
|---------------------------------------------------------------------------|
| 2 | brnd-2 | |
|---------------------------------------------------------------------------|
| 3 | brnd-3 | |
|---------------------------------------------------------------------------|
| 4 | brnd-3 | |
|---------------------------------------------------------------------------|
| 5 | brnd-3 | |
|---------------------------------------------------------------------------|
| 6 | prod-3 | |
|---------------------------------------------------------------------------|
Now i have a query which runs like this
SELECT * FROM td_product,td_model,td_brand
WHERE
td_product.product_id > 0
AND td_model.model_id IN (td_product.compatible_model)
AND td_model.brand_id = td_brand.brand_id
AND td_model.model_slug = '1-mode-1'
Which displays prod-1 and prod-2
but this query return null value.
SELECT * FROM td_product,td_model,td_brand
WHERE
td_product.product_id > 0
AND td_model.model_id IN (td_product.compatible_model)
AND td_model.brand_id = td_brand.brand_id
AND td_model.model_slug = '5-mode-5'
What am i doing wrong?
Instead of IN use FIND_IN_SET(...); change:
AND td_model.model_id IN (td_product.compatible_model)
to:
AND FIND_IN_SET(td_model.model_id, td_product.compatible_model) > 0