mysql to select records where values are in sequence - mysql

How can I select records where ID1 values are in sequence (of two or more) where TOWN matches
My table
TOWN ID1
town1 1
town1 2
town1 4
town2 1
town2 5
town2 8
town3 1
town3 2
town3 3
required result
TOWN ID1
town1 1
town1 2
town3 1
town3 2
town3 3
sql fiddle
http://sqlfiddle.com/#!2/b409f/26

You can use an EXISTS clause to check for the next value in the sequence. This code will only match "sequences" of length >= 2, which seems to be what you want from your example.
SELECT *
FROM Table1 a
WHERE EXISTS (SELECT *
FROM Table1 b
WHERE b.TOWN=a.TOWN
AND b.ID1 IN (a.ID1 - 1, a.ID1 + 1))
ORDER BY TOWN, ID1

If you question is "give me all rows that have an adjacent id1 field for the town", then simply:
select distinct t1.*
from Table1 t1
join Table1 t2 on t2.town = t1.town and ABS(t1.ID1 - t2.ID1) = 1
order by 1, 2
See SQLFiddle for this.
To also match on another column, add the condition to the join, eg:
select distinct t1.*
from Table1 t1
join Table1 t2
on t2.town = t1.town
and t2.state = t1.state
and ABS(t1.ID1 - t2.ID1) = 1
order by 1, 2

Related

Select MAX of each corresponding row [duplicate]

This question already has answers here:
Retrieving the last record in each group - MySQL
(33 answers)
Closed 11 months ago.
new to SQL but I'll try to be clear,
I have 3 table, which have corresponding key columns. I need to select the row the MAX Value of a set column, for EACH of it's corresponding column.
Table1
ID1 Value1
1 Marie
2 Max
3 John
Table2
ID2 Value2
1 First
2 Second
3 Third
Table3
ID1 ID2
1 1
1 2
2 1
2 2
2 3
3 1
So far I have something like so;
SELECT T1.Value1, T2.Value2 FROM Table1 T1
INNER JOIN Table3 T3 ON T1.ID1 = T3.ID1
INNER JOIN Table2 T2 ON T3.ID2 = T2.ID1
WHERE (That's where I can't formulate correctly)
So far my tables are correctly joined, but I want to output only the rows where ID2 is at it's max value for the corresponding ID1.
So we'd have
ID1 ID2
1 2
2 3
3 3
And so, from correspondence of value we'd finally have.
Value1 Value2
Marie Second
Max Third
John First
Any help? Also, group by is prohibited.
You must use MAX in WHERE condition:
SELECT
T1.Value1,
T2.Value2
FROM
Table1 T1
INNER JOIN Table3 T3 ON T1.ID1 = T3.ID1
INNER JOIN Table2 T2 ON T3.ID2 = T2.ID1
WHERE
T3.ID2 = (
SELECT
MAX(T3_alias.ID2)
FROM
Table3 AS T3_alias
WHERE
T3_alias.ID1 = T1.ID1
)

Joining two tables on interval basis

In SQL, suppose that I have table A
ID
--
1
3
5
and table B
ID2
---
1
2
3
4
5
6
To get the result similar to:
ID | ID2
----------
1 | 1
1 | 2
3 | 3
3 | 4
5 | 5
5 | 6
For an explanation, an element in column ID2 will be mapped to the highest value in the column ID that is less than or equal to the said element in ID2.
For example, 4 in column ID2 is mapped to 3 from column ID, because 3 is the largest value in column ID which is less than or equal to 4.
Is it possible at all to do this in sql?
What I would do is start by joining the two tables on the condition that the id in the first table is less than or equal to that in the second table, like this:
SELECT t1.id, t2.id AS id2
FROM t1
JOIN t2 ON t2.id >= t1.id;
Once you have that, you can select the maximum id from the first table, and group by the id from the second table to get the largest pairs:
SELECT MAX(t1.id) AS id, t2.id AS id2
FROM t1
JOIN t2 ON t2.id >= t1.id
GROUP BY t2.id;
SQL Fiddle seems to be down but I will update with a link as soon as I can.
SELECT MAX(A.ID) ID, B.ID2
FROM A
INNER JOIN B ON B.ID2 >= A.ID
GROUP BY B.ID2
If you only need the matching ID column:
select b.*,
(select max(ID) from a where a.ID <= b.ID2) as a_Id
from b
If you need more columns:
select *
from a
join
(
select b.*,
(select max(ID) from a where a.ID <= b.ID2) as a_Id
from b
) as b
on a.Id = b.a_Id

Mysql left join fails to returns all values from first table

I've two tables named table1 and table2. table2 may have elements from table1. I'd like to show all the results from table2 with the price if available in table1 and with status 1. If no product matches in table1 and also status is 0, need to return price as 0.
table1
id pname item_code price status
1 product1 abcd 200 1
2 product2 pqrs 500 1
3 product3 wxyz 425 1
4 product5 mnop 100 0
and table2 as follows
id item_code
10 efgh
11 abcd
12 pqrs
13 mnop
I have tried following query
SELECT `t2`.`id`, `t2`.`item_code`, `t1`.`price`,`t1`.`pname`, COUNT(t2.item_code) AS sellers FROM (`table2` as t2) LEFT JOIN `table1` as t1 ON `t1`.`item_code` = `t2`.`item_code` WHERE `t1`.`status` = 1 GROUP BY `t2`.`item_code`
but it returns common values in table1 and table2 with status 1, but I need all records from table2 with price as 0 if nothing match in table1 or status 0 in table1.
Expected output
id item_code price
10 efgh 0
11 abcd 200
12 pqrs 500
13 mnop 0
Any help please.
Thanks,
Not sure about your current query which is having count and group by however you can do as below
select
t2.id,
t2.item_code,
case
when t1.status = 1 then t1.price
else 0
end as price
from table2 t2
left join table1 t1 on t1.item_code = t2.item_code
Now note that if table1 has multiple matching values from table2 in that case we may need grouping data.
Try below query:
SELECT `t2`.`id`, `t2`.`item_code`, `t1`.`price`,`t1`.`pname`, COUNT(t2.item_code) AS sellers FROM (`table2` as t2) LEFT JOIN (select * from table1 where status = 1)t1 ON `t1`.`item_code` = `t2`.`item_code` GROUP BY `t2`.`item_code`
Move the part of the WHERE clause that checks the left joined table to the ON clause
SELECT `t2`.`id`, `t2`.`item_code`, COALESCE(`t1`.`price`, 0),`t1`.`pname`, COUNT(t2.item_code) AS sellers
FROM (`table2` as t2)
LEFT JOIN `table1` as t1
ON `t1`.`item_code` = `t2`.`item_code`
AND`t1`.`status` = 1
GROUP BY `t2`.`item_code`

show duplicates from each group in temporary table using mysql

Can some one help me with a mysql query to show duplicates from each group in temporary table using mysql
sqlfiddle
http://sqlfiddle.com/#!2/d8c9c/17
Table
TOWN ID1
town1 1
town1 1
town1 4
town2 1
town2 5
town2 8
town3 1
town3 3
town3 3
Required result
TOWN ID1
town1 1
town1 1
town3 3
town3 3
I have tried SELECT * FROM Table1 group by TOWN,ID1
but this removes duplicates and also shows non-duplicate records
This is the query you're looking for
SELECT TOWN, ID1 FROM Table1 t WHERE ( SELECT COUNT(*) FROM Table1 WHERE TOWN = t.TOWN AND ID1 = t.ID1 ) > 1;
SELECT a.TOWN, a.ID1 FROM Table1 a JOIN
(SELECT TOWN, ID1 FROM Table1 GROUP BY TOWN, ID1 HAVING COUNT(*) > 1) b
ON a.TOWN = b.TOWN
AND a.ID1 = b.ID1

Sort output from multiple tables

Query1(by combining table1, table 2): returns
SELECT t1.ID1,t1.Name
FROM table1 t1, table2 t2
WHERE t1.ID1=t2.ID;
ID1 Name
4 ppp
1 pqr
2 abc
3 xyz
Query2(using table 3, which stores IDs): returns
select ID from table3;
ID
1
2
3
4
Combine Query1 & Query 2 & produce output as
ID Name
1 pqr
2 abc
3 xyz
4 ppp
ie main values are coming from 2 different values while sorted values(IDs) are stored in random order in third table.
something like:-
select t1.ID1,t1.Name from table1 t1, table2 t2
LEFT JOIN table3 t3 ON t3.ID = t1.ID where t1.ID=t2.ID;