SQL Query to find records belongs to two states - mysql

cust_id
state
1
CA
2
IL
3
SC
1
PA
3
IA
4
MO
5
VA
4
NY
Can any one please advise on SQL Query that return the cust_id that belong to two states as below:
The output should be
cust_id
state
1
CA
1
PA
3
SC
3
IA
4
NY
4
MO

Try the following query
Solution 1
Select * From yourtable
Where Cust_Id In (Select cust_id From yourtable Group By cust_id Having
Count(*) = 2) Order By cust_id,state
Solution 2
With T1 As
(Select cust_id From yourtable Group By cust_id Having Count(*) = 2)
Select T2.* From yourtable T2 Join T1 On T1.cust_id = T2.cust_id O
Order By T2.cust_id,T2.state

SELECT tmp.*
FROM tmp
INNER JOIN (
SELECT cust_id
,COUNT(STATE) s_count
FROM tmp
GROUP BY [cust_id]
) sub
ON tmp.cust_id = sub.cust_id
WHERE sub.s_count = 2
ORDER BY cust_id
,STATE

One simple approach would use a COUNT window function, that will assign the amount of times each "cust_id" occurs in your table. Once you get this value, you can filter out rows whose count is smaller than 2.
WITH cte AS (
SELECT *, COUNT(cust_id) OVER(PARTITION BY cust_id) AS cnt
FROM tab
)
SELECT cust_id,
state
FROM cte
WHERE cnt > 1
Check the demo here.

Related

Filter out records based on condition SQL

I am trying to filter out some records based on condition but couldn't get the proper results.
Data:
GID OID SID Z
1 1 1 A
1 2 2 B
1 3 3 C
1 2 4 B
Expected Result:
GID OID SID Z
1 1 1 A
1 3 3 C
Here GID, OID can be repeated but not SID.
Need to filter out all records where Z contains 'A' & 'C'
What I have tried:
select distinct GID, OID, SID, Z
from table
where Z ilike ('A') or Z ilike ('C')
but this query will include all record of sample GID records.
Moreover I have also thought of self join but could not frame the query around that.
I think this is the query you need
WITH cte AS (
SELECT
gid, oid, sid, z,
ROW_NUMBER() OVER () AS rn
FROM data
WHERE LOWER(z) LIKE '%a%'
OR LOWER(Z) LIKE '%c%'
)
SELECT gid, oid, sid, z FROM cte c
WHERE NOT EXISTS (
SELECT sid FROM cte t
WHERE t.z = c.z
AND t.sid = c.sid
AND t.rn < c.rn
)
I use ROW_NUMBER to be able to check if a sid value repeats.
Demo

SQL add same contents

How can I delete all columns that are duplicate and don't have the biggest "amount". I have the following table:
ID TIME AMOUNT
-----------------------------------
1 x 5
2 y 1
2 y 3
3 z 1
3 z 2
3 z 3
But I want it to be like this, so that only the column which has the biggest number "survives":
ID TIME AMOUNT
------------------------------------
1 x 5
2 y 3
3 z 3
How can I do this?
You can get the max amount per id and time and then get the rows matching:
select t.Id, t.Time, t.amount
from myTable t
inner join
(select Id, time, max(amount) as amt
from myTable
group by Id, Time) tmp on t.id = tmp.id and
t.time = tmp.time and
t.amount = tmp.amt
DbFiddle demo
EDIT: You may want to add DISTINCT depending on your needs.
One other approach using a CTE
with del as (
select *,
First_Value(amount) over(partition by id order by amount desc) maxamount
from t
)
delete from t
using t join del on t.id = del.id and t.amount < maxamount;
WITH cte AS
(
SELECT
ID,
ROW_NUMBER() OVER (PARTITION BY TIME ORDER BY AMOUNT DESC) AS ROWNUM
FROM
MyTable
)
DELETE MyTable
FROM MyTable
JOIN cte USING (ID)
WHERE ROWNUM > 1;
WITH syntax requires MySQL 8.0.
I think some of the answers here are overly complicated.
delete t
from yourtable t
join yourtable t2 on t.id = t2.id
and t.time = t2.time
and t2.amount > t.amount

Get last and most values in mysql [duplicate]

This question already has an answer here:
how to get count of values in mysql [closed]
(1 answer)
Closed 6 years ago.
I have a table user_places like this:
user_id recorded_date place_visited
2 2016-04-05 NY
2 2016-04-07 UK
2 2016-04-08 UK
2 2016-04-08 UK
3 2016-04-08 AUS
3 2016-04-09 AUS
2 2016-04-15 NY
3 2016-04-15 CAN
I am trying to get the place visited in last recorded_date of user and name of the place that he has visited the most.
i.e
user_id recorded_date_last place_visited_most last_visited_place
2 2016-04-15 UK NY
3 2016-04-15 AUS CAN
Here's a way using a subquery:
select * ,
(select place_visited from mytable t2
where t2.user_id = t1.user_id
group by place_visited
order by count(*) desc
limit 1) place_visited_most
from mytable t1
where recorded_date = (select max(recorded_date) from mytable)
http://sqlfiddle.com/#!9/ad351/4
Here's an untested stab at it. I'm not as familiar with MySQL so there may be other approaches that work better. This is a set-based method that avoids using limit and order by.
select t.user_id, t.place_visited, modex.cnt, maxd.max_recorded_date
from
T t inner join
(
select user_id, max(recorded_date) as max_recorded_date from T
group by user_id
) maxd
on maxd.user_id = t.user_id
inner join
(
select user_id, max(cnt) as max_cnt
from
(
select user_id, count(*) as cnt from T
group by user_id, place_visited
) counts
group by user_id
) maxc
on maxc.user_id = t.user_id
inner join
(
select user_id, cnt, min(place_visited) as place_most_visited
from
(
select user_id, place_visited, count(*) as cnt from T
group by user_id, place_visited
) break_ties
group by user_id, cnt
) modex
on modex.user_id = maxd.user_id
and maxc.max_cnt = modex.cnt and modex.place_most_visited = t.place_visited
Since you could have ties for the mode of place_visited I just kept the lowest alphabetically. I did assume that there would be no ties for recorded_date though.

how to find maximum if i have more than one answer

I have a table:
ID CLUSTERID
1 56
1 24
1 24
1 35
2 13
2 24
Now, i want to get the following:
I want to count per id, which cluster id repeats most of the time.
For example, in ID=1, CLUSTERID=24 repeats most of the time
In ID=2 i have 2 CLUSTER IDs that repeats the same.
So in the output i will have:
ID CLUSTERID
1 24
2 13
2 24
The answer that i wrote (and works)
TT is my original table that have 2 columns: ID and CLUSTER ID
SELECT t3.ID,t3.ClusterID,t3.ListingAmount
FROM
(SELECT ID, ClusterID, COUNT() AS ListingAmount
FROM tt
GROUP BY ID, ClusterID) AS t3 LEFT JOIN
(SELECT ID, MAX(ListingAmount) AS amount
FROM
(SELECT ID, ClusterID, COUNT() AS ListingAmount
FROM tt
GROUP BY ID, ClusterID) AS t2
GROUP BY ID) AS BB ON BB.id=t3.id
WHERE BB.amount=t3.ListingAmount
Can't think of a more elegant solution right now (I'm sure there is), but it seems to do the job:
select t1.id,
t1.clusterid,
t1.cnt
from (
select id,
clusterid,
count(*) as cnt
from foo
group by id, clusterid
) t1
join (select id,
max(cnt) as max_count
from (
select id,
clusterid,
count(*) as cnt
from foo
group by id, clusterid
) tm
group by id
) t2 on t1.id = t2.id
and t1.cnt = t2.max_count
order by t1.id, t1.cnt;
SQLFiddle example: http://sqlfiddle.com/#!2/2cacc/3

Is it possible to do a group by count(distinct) without a join?

Table 1 : (Company)
ID Name
1 A
2 B
3 C
Each company (pk = ID) can have one or more employees.
Table 2 : (Employee) (CompanyID referencing ID)
CompanyID EmpID Name
1 1 Joe
1 2 Doe
1 3 Boe
2 4 Lou
3 5 Su
3 6 Ram
Query :
select CompanyID, count(*) from Employee group by CompanyID having count(*) > 1; # Lists companies and their counts.
CompanyID count(*)
1 3
3 2
For this query, I want just one result with the count of distinct CompanyIDs. So, '2' in this case [Companies A and C].
In short, I am looking for number of companies with 2 or more employees.
Is there anyway to get the result without a temp table or a join? I am using MySQL.
Yes:
select count(*) from
(select CompanyID from Employee group by CompanyID having count(*) > 1) v
or for ranges:
select count(*) from
(select CompanyID from Employee group by CompanyID
having count(*) >= 5 and count(*) < 10) v
Yes, it's possible with subqueries:
SELECT COUNT(*)
FROM
( SELECT 1
FROM Employee
GROUP BY CompanyID
HAVING COUNT(*) > 1
) AS grp
or:
SELECT COUNT(DISTINCT CompanyID)
FROM Employee AS e
WHERE EXISTS
( SELECT *
FROM Employee AS e2
WHERE e2.CompanyID = e.CompanyId
AND e2.EmpID <> e.EmpID
)
or perhaps if COUNT(*) is slow, you can use MIN() and MAX():
SELECT COUNT(*)
FROM
( SELECT 1
FROM Employee
GROUP BY CompanyID
HAVING MAX(EmpID) > MIN(EmpId)
) AS grp