i have a table t_points in mySql like example in below.
Name Surname Point
Joe Arnold 120
Michale Black 250
Masha Petrova 300
Natalie Jackson 120
John Turo 200
Bona Meseda 250
Zeyda Nura 150
Zanura Bohara 60
Shaheen Boz 360
Abbas Murat 160
Keira Black 230
Tom Robinson 480
Fred Balka 490
Semia Hudovi 90
Sona Bahari 60
i want to write a query which will display the count of point ranges. Point ranges are like this: point between 0 and 100, 101 and 200, 201 and 300, 301 and 400.
Result must be like below
0_100 101_200 201_300 301_400
3 5 4 3
i think u understand what i want to say. So which query i have to use for this result?
Thanks.
select
count(CASE WHEN point BETWEEN 0 AND 100 THEN 1 END) as count0_100,
count(CASE WHEN point BETWEEN 101 AND 200 THEN 1 END) as count101_200,
count(CASE WHEN point BETWEEN 201 AND 300 THEN 1 END) as count201_300,
...
from
t_poits
Something like that:
select count(*) as count, abs(point/100) as range
from t_poits
group by abs(point/100)
set #range = 500;
select floor(field1/#range)*#range as `from`,(ceil(field1/#range)+if(mod(field1,#range)=0,1,0))*#range as `to`, count(field1)
from table1
group by 1,2
order by 1,2;
You can group points column and do some math operation to specify range. Based on that you can count number of records.
Like..
SELECT concat( 101 * round( Point /101 ) , '-', 101 * round( Point /101 ) +100 ) AS `range` , count( * ) AS `result`
FROM t_points
GROUP BY 1
ORDER BY Point
I hope it will work for you.
select
concat(floor(Point/100),'01_',ceil(Point/100),'00') as ind,
count(*) as cnt
from t group by ind order by ind asc;
Related
I have query below as:
SELECT
age_quartile,
MAX(age) AS quartile_break
from
(SELECT
full_name,
age,
NTILE(4) OVER (ORDER BY age) AS age_quartile
FROM friends) AS quartiles
WHERE age_quartile IN (1, 3)
GROUP BY age_quartile)
This gives me output that looks like:
age_quartile | quantile_break
1 31
3 35
Desired Output:
outlier range
25
41
where 25 = 31-6 and 41 = 35 + 6
How can I add to my query above where I can my final desired output? My query currently gives me what the numbers are where I need to do one additional step to solve for the outlier range. thanks!
table data looks like:
friends
full_name | age
Ameila Lara 1
Evangeline Griffin 21
Kiara Atkinson 31
Isobel Nieslen 31
Genevuve Miles 32
Jane Jenkins 99
Marie Acevedo null
Dont now ntile is the right function to use here. But one way is to define the age quartiles in a temp table and join with age table and find the results. Just a try. There may be better way. Interested to see other answers.
Sample Query:
with friends as
(
select 'user1' as full_name, 31 as age union all
select 'user2' as full_name, 55 as age union all
select 'user3' as full_name, 75 as age
),
quartiles_age as
(
select 1 as quartile, 0 as st_range, 25 as end_range union all
select 2 as quartile, 26 as st_range, 50 as end_range union all
select 3 as quartile, 51 as st_range, 75 as end_range union all
select 4 as quartile, 76 as st_range, 100 as end_range
)
SELECT
fr.full_name,
fr.age,
qrtl_age.quartile,
qrtl_age.end_range - fr.age as diff_age
FROM
friends fr
join quartiles_age qrtl_age on fr.age between qrtl_age.st_range and qrtl_age.end_range
Fiddle URL : (https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=c48d209264e90d276cea6ae03f2a7af6)
You can calculate the range using:
MAX(age) - MIN(age) AS age_range
That answers the question you asked. Your sample data has an arbitrary 6 for the calculation, which the question does not explain.
I have a data set with name and their transaction ,how to get average and count of transactions grater than that average and less than that average..
Name Transaction
John 12
John 34
John 45
John 66
John 32
chris 26
chris 54
chris 56
chris 99
chris 13
chris 4
kim 22
kim 34
kim 7
kim 11
kim 34
O/P will be
Name Avg Count_greater_than_Avg Count_Less_than_Avg
John 37.8 2 3
chris 42 3 3
kim 21.6 3 2
Thanks in advance..
Try this:
SELECT t1.Name, t2.Aver,
COUNT(CASE WHEN Transaction < Aver THEN 1 END) Count_Less_than_Avg,
COUNT(CASE WHEN Transaction > Aver THEN 1 END) Count_greater_than_Avg
FROM mytable AS t1
JOIN (
SELECT Name, AVG(Transaction * 1.0) AS Aver
FROM mytable
GROUP BY Name
) AS t2 ON t1.Name = t2.Name
GROUP By Name
You need a derived table in order to calculate the average value per Name. You can then JOIN the original table to this derived table and use conditional aggregation in order to calculate less than, greater than number of transactions.
Demo here
This basically first add a column Your_Avg using a correlated query, and then wrap it with another select to select the count of the occurrences of times smaller then avg and times larger.
SELECT tt.name,tt.Your_Avg,
count(CASE WHEN tt.Your_Avg > tt.Transaction then 1 end) as Greater_Then_Avg,
count(CASE WHEN tt.Your_Avg > tt.Transaction then 1 end) as Smaller_Then_Avg
FROM(
SELECT t.name,
(SELECT avg(s.transaction) FROM YourTable s WHERE s.name = t.name) as Your_Avg,
t.transaction
FROM YourTable) tt
GROUP BY tt.name
Trying to set amount range like 0 to 9 10 to 19 ... 50 - 99 but when done individually i.e. a.Amount >50 returns data rows and similarly a.Amount >100 returns data rows but the follwing returns null rows. Please help deadline is near! The Amount is a varchar data type.
SELECT DATE_FORMAT((STR_TO_DATE(a.TRANSACTION_DATE,'%d.%m.%Y')), '%Y%m') mnt,
COUNT(DISTINCT a.CUSTOMER_ID) totalNum
FROM credittx a
WHERE a.COUNTRY = 'Germany'
AND a.AMOUNT BETWEEN 100 AND 50
GROUP BY DATE_FORMAT((STR_TO_DATE(a.TRANSACTION_DATE,'%d.%m.%Y')),'%Y%m')
The range is from-to
do BETWEEN 50 AND 100
Seems to me that you must have between 50 AND 100. Not 100 AND 50.
SELECT
DATE_FORMAT((STR_TO_DATE(a.TRANSACTION_DATE,'%d.%m.%Y')), '%Y%m') mnt,
COUNT(DISTINCT a.CUSTOMER_ID) totalNum
FROM credittx a
WHERE a.COUNTRY = 'Germany'
AND a.AMOUNT BETWEEN 50 AND 100
GROUP BY DATE_FORMAT((STR_TO_DATE(a.TRANSACTION_DATE,'%d.%m.%Y')),'%Y%m')
As mentioned in the comment that 50 is less than 100 so please change:
AND a.AMOUNT BETWEEN 100 AND 50
to:
AND a.AMOUNT BETWEEN 50 AND 100
I want to find number of repeated value in group using single select query.
I can't use the column in group by for which I want to find number of occurrence in some group.
e.g.
obsid Name Value
1 Ronak 120
2 Ronak 125
3 Ronak 120
4 Pankti 130
5 Pankti 130
6 Sanket 140
7 Aakash 140
8 Unnat 120
Now I want to develop select query in mysql that give me below result
obsid Name Value Value_occurrence
1 Ronak 120 2
2 Ronak 125 1
4 Pankti 130 2
6 Sanket 140 1
7 Aakash 140 1
8 Unnat 120 1
SELECT min(obsid) AS obsid, Name, Value, COUNT(*) AS Value_occurence
FROM yourTable
GROUP BY Name, Value
Group by the desired columns and use COUNT to count the rows in each group:
SELECT MIN(`obsid`) AS `obsid`, `Name`, `Value`, COUNT(*) AS Value_occurrence
FROM yourtable
GROUP BY `Name`, `Value`
Try this online demo which shows your example data and the results for the above query.
I am trying to get a query to show all the data for event prizes on one line. For example this is an example of my table.
Event_id Place Money
101 1 120
101 2 60
101 3 30
102 1 100
102 2 50
102 3 25
What I want is for the results to look like the following:
Event_id First Second Third
101 120 60 30
102 100 50 25
Any help to accomplish this would be greatly appreciated.
Hope this helps.
SELECT Event_ID,
GROUP_CONCAT(IF(place = 1, `money`, NULL)) `First`,
GROUP_CONCAT(IF(place = 2, `money`, NULL)) `Second`,
GROUP_CONCAT(IF(place = 3, `money`, NULL)) `Third`
FROM tableName
GROUP BY event_id
If you want learn more about sql tricks, visit this:
Common but Useful MySQL Queries
SEE on SQLFiddle
This also works for me
SELECT DISTINCT Event_id, (SELECT Money FROM PRIZE WHERE Place=1 AND Event_id=t.Event_id) AS First,
(SELECT Money FROM PRIZE WHERE Place=2 AND Event_id=t.Event_id) AS Second,
(SELECT Money FROM PRIZE WHERE Place=3 AND Event_id=t.Event_id) AS Third
FROM PRIZE t;