How to group duplicate values into separate groups? [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Data
Needed Result
Now I have to query how to get count (id) and group by the single field. This means that when I get the result, it will be as shown in image 2. When I use this query, it will only correct the first when line, and the next when it will return the missing value due to the above condition. Hope you can help. Thanks.
select count(id)
from table
GROUP BY (CASE
WHEN `fields` like '%-31-%' THEN '31'
WHEN `fields` like '%-35-%' THEN '35'
WHEN `fields` like '%-49-%' THEN '49'
ELSE '52' END)

select patterns.groupnum, count(table.id)
from table, ( SELECT '%-31-%' pattern, 31 groupnum UNION ALL
SELECT '%-35-%' , 35 UNION ALL
SELECT '%-49-%' , 49 UNION ALL
SELECT '%-52-%' , 52 ) patterns
WHERE table.`field` LIKE patterns.pattern
GROUP BY patterns.groupnum;
But I'd recommend you to normalize your data.

Related

Two different columns need criteria with SUM condition [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a table with two columns in a mysql database.
For example: Item_name, qty in table1
I need to retrieve all items by Item_name where sum(qty) <> 0
I tried the following but it didn't work
select item_name from table1 where SUM(QTY) <> 0 group by item_name
Thank you.
When you want to add a condition on an aggregate, HAVING is used rather than WHERE:
SELECT item_name
FROM table1
GROUP BY item_name
HAVING SUM(QTY) <> 0

How can I apply a rule to update records in a field based on a condition and by grouping the data [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am trying to do this task in sql. but I am not able to derive a logic that helps getting me this output.
It looks like you want to bring the name of the player whose code is "x" for each id. If so, one option uses window functions:
select id, playercode, playername,
max(case when playercode = 'x' then playername end) over(partition by id) requiredoutput
from mytable
If your database does not support window functions, one option uses a correlated subquery. Assuming no duplicates on (id, playercode):
select id, playercode, playername,
(
select t1.playername
from mytable t1
where t1.id = t.id and t1.playercode = 'x'
) requiredoutput
from mytable t
If you're looking for an update statement maybe something like this would work
with upd_cte as (
select distinct id, player_name
from tTable
where Playercode='x')
update t
set requiredoutput=u.player_name
from tTable t
join upd_cte u on t.id=u.id;

how to convert colums to rows in sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a table with the following values
and I need the output values as below.
Please help to arrive the code
It's not pretty, but this would work:
select *
from
(
select nams_contract_number, 1 as Sequence, 'Preqc' as DataType, act_preqc, est_preqc
from table
union all
select nams_contract_number, 2 as Sequence, 'abstractoin' as DataType, act_abstraction, est_abstraction
from table
union all
select nams_contract_number, 3 as Sequence, 'review' as DataType, act_review, est_review
from table
union all
select nams_contract_number, 4 as Sequence, 'postreview' as DataType, act_postreview, est_postreview
from table
) x
order by x.nams_contract_number, x.sequence

Mysql query relating to count and date time [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a table 'transaction' which contains columns transaction_id, sender_id,amount, date_time,payee_id .
I want to find transactions which are more than five made by sender_id in one day by single sender.
i tried
mysql> select * from transaction where sender_id count() =5 and datedif() =1;
I think this query will be helpful for you.
select sender_id, count(sender_id) as count
from `transaction`
group by sender_id, date(date_time)
having count>5
this will return the list of sender_ids who have more than 5 transactions
if you also need the list of that transactions than you need this
select t.*
from (
select sender_id, count(sender_id) as count
from `transaction`
group by sender_id, date(date_time)
having count>5
) as senders
inner join `transaction` as t on t.sender_id=senders.sender_id
PS. for good performance I recommend run second query on MySQL server with version 5.6.10 or higher

MySql: how to get unique id from two field in a table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
There is a table called member_info and this table have 2 rows (member_id and sponsor_id)
table looks like :
member_id (1,2,3,4,5,6,7,8);
sponsor_id (,1,1,1,2,5,5,5);
Now i need to pick those member_id's who are not in sponsor_id like
member_id(3,4,6,7,8);
Any help Appreciated.
You can use the NOT IN function with a sub-query:
SELECT `member_id`
FROM `member_info`
WHERE `member_id` NOT IN (
SELECT DISTINCT(`member_id`) FROM `sponsor_info`
)
Subqueries introduced with the keyword NOT IN also return a list of zero or more values.
SELECT member_id
FROM member_inf
WHERE member_id NOT IN (
SELECT DISTINCT(member_id) FROM sponsor_info
)