My table have data like this
id from
1 1|Chinmoy Panda|chinmoy|mfsi_chinmoyp
1 532|Narendra Mallik|narendram
1 595|Bhagirathi Panda|bhagirathi
2 1|Chinmoy Panda|chinmoy|mfsi_chinmoyp
2 532|Narendra Mallik|narendram
2 595|Bhagirathi Panda|bhagirathi
2 13|Hemendra Singh|hemendras
3 1|Chinmoy Panda|chinmoy|mfsi_chinmoyp
3 595|Bhagirathi Panda|bhagirathi
3 13|Hemendra Singh|hemendras
4 1|Chinmoy Panda|chinmoy|mfsi_chinmoyp
4 595|Bhagirathi Panda|bhagirathi
5 595|Bhagirathi Panda|bhagirathi
i am trying to this
Ignore the 1st row of every id
Count w.r.t. from
Ignore if the id has one row.
Means
Count from
4 595|Bhagirathi Panda|bhagirathi
2 532|Narendra Mallik|narendram
2 13|Hemendra Singh|hemendras
In 1,2,3,4 id 1st row contains chinmay panda.So i ignore that one
Bhagirathi Panda occured 5 times but id 5 having only one row so count is 4.
similarly for others
i tried but unable to find the result
please help me to write the query
(i didn't get what should the title so i write this one. )
thanks in advance.
You want a query something like this:
select count(*), from
from t
where left(from, 2) <> '1|' and
t.id in (select id from t group by id having COUNT(*) > 1)
group by from
However, because the column names are poorly named (using SQL reserved words), you need to properly quote them.
Also, I'm assuming that by "first" you mean the ones that start with '1|'.
this query will do
SELECT
COUNT(*) `count`,
`from`
FROM (
SELECT
`from`,
IF( COALESCE( #id, 0 ) = (#id := id) , #curRow := #curRow + 1, #curRow := 1 ) curRow
FROM
Table1 ) tmp
WHERE curRow > 1
GROUP BY `from`
ORDER BY `count` desc
SQL Fiddle DEMO
Related
I have a database as follows:
drinks_id ingredients_master_id
1 2
1 3
1 4
2 2
2 4
3 5
And I'm looking for a query where I can give it a list of ingredients_master_id such as 2,4 and it returns all of the drinks_id's that have exactly 2,4.
So in this case if I gave it ingredients_master_id 2,4,5 it would return drinks_id 2 and 3. And if I gave it 5 it would return drinks_id 3.
This is what I have so far but it's currently not displaying the correct info.
SELECT DISTINCT drinks.id
FROM drinks
WHERE drinks.id NOT IN
(
SELECT drinks.id
FROM ingredients
JOIN drinks ON ingredients.drinks_id = drinks.id
WHERE ingredients.ingredients_master_id NOT IN
(
2,3,4,5,6
)
);
You probably achieve the desired result using not exists as follows:
Select t.*
From your_table t
Where t.ingredients_master_id in (2,4,5)
And not exists
(Select 1 from your_table tt
Where tt.drinks_id = t.drinks_id
And tt.ingredients_master_id not in (2,4,5))
And I'm looking for a query where I can give it a list of ingredients_master_id such as 2,4 and it returns all of the drinks_id's that have exactly 2,4.
You can use group by and having:
select drinks_id
from t
group by drinks_id
having sum( ingredients_master_id in (2, 4) ) = 2;
The "= 2" is the size of the list, so you need to adjust that for different lists.
You can use as below:
select drink_id
from (
select drink_id,listagg(ingredients_master_id,',') within group( order by ingredients_master_id) val from <Table> group by drink_id)
where case
when instr('**2,4,5**',val)> 0
Then 'Y'
else 'N'
end = 'Y';
I'm trying to run an UPDATE query that uses the same table and I'm getting an error saying "1093 - Table 'queues_monitor_times' is specified twice, both as a target for 'UPDATE' and as a separate source for data".
UPDATE queues_monitor_times
SET queue_id = IF((
SELECT id
FROM queues_monitor_times
INNER JOIN(
SELECT pcc_group, pcc, gds, queue, category, `name`
FROM queues_monitor_times
GROUP BY pcc_group, pcc, gds, queue, category, `name`
HAVING COUNT(id) > 1
)temp ON queues_monitor_times.pcc_group = temp.pcc_group AND
queues_monitor_times.pcc = temp.pcc AND
queues_monitor_times.gds = temp.gds AND
queues_monitor_times.queue = temp.queue AND
queues_monitor_times.category = temp.category AND
queues_monitor_times.`name` = temp.`name`), 1, id)
WHERE
id NOT IN (SELECT MIN(id) FROM queues_old GROUP BY pcc_group, pcc, gds, queue, category, `name`);
I ran the select query by itself and it showed all the rows that were duplicates, which is what I wanted. I want queue_id to be set with the lowest duplicate row's id if the row is a duplicate or the row id if it is not.
Example of what the query should do:
id dup_id name value
1 1 John 13
2 2 John 13
3 3 Sally 6
4 4 Frank 4
5 5 Sally 6
And after running the query it will turn into
id dup_id name value
1 1 John 13
2 1 John 13
3 3 Sally 6
4 4 Frank 4
5 3 Sally 6
Please advise and thank you for your help.
I was able to solve my problem. Thanks for all your help!
UPDATE queues_monitor_times
SET queue_id = (
SELECT
id
FROM
queues_old
WHERE
queues_old.pcc_group = queues_monitor_times.pcc_group
AND queues_old.pcc = queues_monitor_times.pcc
AND queues_old.gds = queues_monitor_times.gds
AND queues_old.queue = queues_monitor_times.queue
AND queues_old.category = queues_monitor_times.category
AND queues_old.`name` = queues_monitor_times.`name`
GROUP BY pcc_group, pcc, gds, queue, category, `name`
HAVING COUNT(id) > 1)
WHERE
id NOT IN (SELECT MIN(id) FROM queues_old GROUP BY pcc_group, pcc, gds, queue, category, `name`);
For those that will want to use this in the future, queues_monitor_times table and queues_old table have the exact same data.
This seems like such a simple question and I terrified that I might be bashed with the duplicate question hammer, but here's what I have:
ID Date
1 1/11/01
1 3/3/03
1 2/22/02
2 1/11/01
2 2/22/02
All I need to do is enumerate the records, based on the date, and grouped by ID! As such:
ID Date Num
1 1/11/01 1
1 3/3/03 3
1 2/22/02 2
2 1/11/01 1
2 2/22/02 2
This is very similar to this question, but it's not working for me. This would be great but it's not MySQL.
I've tried to use group by but it doesn't work, as in
SELECT ta.*, count(*) as Num
FROM temp_a ta
GROUP BY `ID` ORDER BY `ID`;
which clearly doesn't run since the GROUP BY always results to one value.
Any advice greatly appreciated.
Let's assume the table to be as follows:
CREATE TABLE q43381823(id INT, dt DATE);
INSERT INTO q43381823 VALUES
(1, '2001-01-11'),
(1, '2003-03-03'),
(1, '2002-02-22'),
(2, '2001-01-11'),
(2, '2002-02-22');
Then, one of the ways in which the query to get the desired output could be written is:
SELECT q.*,
CASE WHEN (
IF(#id != q.id, #rank := 0, #rank := #rank + 1)
) >=1 THEN #rank
ELSE #rank := 1
END as rank,
#id := q.id AS buffer_id
FROM q43381823 q
CROSS JOIN (
SELECT #rank:= 0,
#id := (SELECT q2.id FROM q43381823 AS q2 ORDER BY q2.id LIMIT 1)
) x
ORDER BY q.id, q.dt
Output:
id | dt | rank | buffer_id
-------------------------------------------------
1 | 2001-01-11 | 1 | 1
1 | 2002-02-22 | 2 | 1
1 | 2003-03-03 | 3 | 1
2 | 2001-01-11 | 1 | 2
2 | 2002-02-22 | 2 | 2
You may please ignore the buffer_id column from the output - it's irrelevant to the result, but required for the resetting of rank.
SQL Fiddle Demo
Explanation:
#id variable keeps track of every id in the row, based on the sorted order of the output. In the initial iteration, we set it to id of the first record that may be obtained in the final result. See sub-query SELECT q2.id FROM q43381823 AS q2 ORDER BY q2.id LIMIT 1
#rank is set to 0 initially and is by default incremented for every subsequent row in the result set. However, when the id changes, we reset it back to 1. Please see the CASE - WHEN - ELSE construct in the query for this.
The final output is sorted first by id and then by dt. This ensures that #rank is set incrementally for every subsequent dt field within the same id, but gets reset to 1 whenever a new id group begins to show up in the result set.
I'm trying to write a MySQL query that does the following:
I have a table that looks like this
Id t
-----------
1 1
2 4
1 6
2 9
1 12
2 14
I need to find the sum of the t column for each Id of 2, and subtract from it the sum of the t column for each Id of 1.
So for this example, the sum of Id 1 is 19, and the sum of Id 2 is 27.
I would want the output to then be 8.
I would imagine the statement would look similar to:
SELECT sum(t) WHERE Id = 2 - sum(t) WHERE Id = 1;
But this obviously isn't proper syntax.
And I apologize for the poorly drawn table, I'm still new to stackoverflow.
You could use a CASE statement:
SELECT
SUM(CASE
WHEN Id = 2 THEN t
WHEN Id = 1 THEN 0 - t
ELSE 0
END) AS mySum
FROM myTable
Hopefully that works as-is... I only have SQL Server to test on, but the syntax should be the same for MySQL.
SELECT SUM(IF(`id` = 2, t, 0)) - SUM(IF(`id` = 1, t, 0)) as `result` FROM `table`
Depends on how big is your table. If it is small or no indexes you can do:
select sum(if( Id=2,t,if(Id=1,-t,0)))
from data;
If you have plenty of rows and have an index in column Id:
select sum(id2)-sum(id1)
from (
select 0 as 'id1', sum(t) as 'id2'
from data
where id=2
union
select sum(t) as 'id1', 0 as 'id2'
from data
where id=1
) as d;
I have a Table that tracks followers
FollowerUserId, FollowingUserId
1 2
2 1
3 1
4 1
1 5
I want to get all user that given Id follows and is followed by or Both.
for example for UserId 1,I want result to be: (FG: Following, FD: Followed, B: Both ways)
2,B
5,FG
3,FD
4,FD
i can easily get FG and FD by doing union
Select FollowerUserId, 'FD' From Table Where FollowingUserId =1
Union
Select FollowingUserId, 'FG' From Table Where FollowerUserId =1;
with above i get user 2 as
2,FG
2,FD
from above but I really need 2,B without UserId 2 duplicated.
How can this be done efficiently?
You can use aggregation on your basic query:
SELECT UserId,
(CASE WHEN COUNT(DISTINCT which) = 1 THEN MIN(which)
ELSE 'B'
END)
FROM (Select FollowerUserId as UserId, 'FD' as which From Table Where FollowingUserId = 1
Union ALL
Select FollowingUserId, 'FG' From Table Where FollowerUserId = 1
) f
GROUP BY UserId;