I have still problem with select date_of_highest_points:
$qry1 = mysql_query("INSERT INTO bbdnes_hraci (nick, sumfrags, sumpoints,
sumhours, lastdate1, highest_points, date_of_highest_points)
SELECT DISTINCT nick, SUM(frags), SUM(points), SUM(hours),
MAX(lastdate), MAX(points), ??????
FROM hraci
GROUP BY nick ");
I asked about that here https://stackoverflow.com/questions/18817900/select-in-select , but i have not solved it, despite council. Can somebody help me with code?
TABLE: hraci
nick frags points hours lastdate
Gamer1 20 100 1 2013-09-17 22:16:08
Gamer1 30 150 3 2013-09-18 20:17:15
Gamer1 25 125 0.5 2013-09-18 23:16:06
Gamer2 50 250 4 2013-09-17 21:11:30
Gamer2 5 25 2 2013-09-17 23:13:59
Need get:
TABLE: bbdnes_hraci
nick sumfrags sumpoints sumhours lastdate1 highest_points date_of_highest_points
Gamer1 75 375 4.5 2013-09-18 23:16:06 150 2013-09-18 20:17:15 ??
Gamer2 55 275 6 2013-09-17 23:13:59 250 2013-09-17 21:11:30 ??
You can do something like this:
select nick, SUM(frags) sumfrags, SUM(points) sumpoints, SUM([hours]) sumhours, max(lastdate) lastdate, max(points) highest_points
into #t1
from hraci
group by Nick
select t1.*, x.lastdate date_of_highest_points
from #t1 t1
outer apply
(select top 1 lastdate
from hraci t
where t.Nick = t1.Nick and t.points = t1.highest_points
order by t.lastdate desc) x
try this
select * from table_name where highest_points = (select max(highest_points) from table_name)
Untested:
SELECT DISTINCT nick, SUM(frags), SUM(points), SUM(hours),
MAX(lastdate), MAX(highest_points),
(
SELECT lastdate As date_of_highest_points
FROM hraci
WHERE points =(SELECT MAX(points) FROM hraci)
)
(rest of query. i.e. FROM and ORDER By's)
Related
id
reg_No
Subj_id
sub_title
score
Class_id
1
98
23
MATHEMATICS
90
2
2
98
21
ENGLISH LANG
60
2
3
98
24
PHYSICS
78
2
4
98
23
CHEMISTRY
100
2
5
98
21
BIOLOGY
81
2
6
98
24
AGRICULTURE
87
2
I want to select the best SUM(score) of the four(4) subjects including English and mathematics.
It suppose to sum 90+60+100+87 = 337
But, it's summing the entire column
Here is my query
SELECT SUM(score)
FROM table1
WHERE reg_no = 98
AND class_id=2
ORDER BY CASE WHEN sub_title IN ('English Language','Mathematics')
THEN 0
ELSE 1 END, score DESC LIMIT 4
The priority of SELECT is higher than LIMIT, therefore, you have to use a subquery
SELECT sum(score)
FROM
(
SELECT *
FROM tab
WHERE reg_no = 98 AND class_id=2
ORDER BY CASE WHEN sub_title IN ('English Language','Mathematics')
THEN 0
ELSE 1 END, score DESC
LIMIT 4
) t
DEMO
An easy way to split into two queries. The first one gets the Math and English score values, and the second one gets the two highest scores from the remaining values.
SQL Server:
With CTE As (
Select Top 2 Score From table1 Where reg_no = 98 And class_id=2 And sub_title Not In ('MATHEMATICS','ENGLISH LANG')
Order by Score Desc
Union All
Select Score From table1 Where reg_no = 98 And class_id=2 And sub_title In ('MATHEMATICS','ENGLISH LANG')
)
Select Sum(Score)
From CTE
MySQL:
With CTE As (
Select Score
From
(Select Score From table1 Where reg_no = 98 And class_id=2 And sub_title Not In ('MATHEMATICS','ENGLISH LANG')
Order by Score Desc
Limit 2) As S
Union All
Select Score From table1 Where reg_no = 98 And class_id=2 And sub_title In ('MATHEMATICS','ENGLISH LANG')
)
Select Sum(Score)
From CTE
An example is if I want to find the driver_ssn of each driver in this table who's exam scores get consecutively lower when they take more exams.
Sample data below:
Driver_ssn Branch_id Exam_date Exam_type Exam_score
11111111 20 2017-05-25 D 79
11111111 20 2017-12-02 L 67
22222222 30 2016-05-06 L 25
22222222 40 2016-06-10 L 51
22222222 40 2016-08-29 D 81
33333333 10 2017-07-07 L 45
33333333 20 2017-06-27 L 49
33333333 20 2017-07-27 L 61
44444444 10 2017-07-27 L 71
44444444 20 2017-08-30 L 65
44444444 40 2017-09-01 L 62
I don't have a query available to post as I am very new to SQL and don't know where to begin here.
I am on version 8.0.18
Yes, there is. you can use ORDER BY in your query see the example below:
SELECT * FROM your-table ORDER BY column-name DESC
Hope this helps!
Haven't tested but give this a try; returns 1111 and 4444 for the sample data:
WITH cte AS (
SELECT driver_ssn
, ROW_NUMBER() OVER (PARTITION BY driver_ssn ORDER BY exam_date) AS rn1
, ROW_NUMBER() OVER (PARTITION BY driver_ssn ORDER BY exam_score DESC, exam_date) AS rn2
FROM t
)
SELECT driver_ssn
FROM cte
GROUP BY driver_ssn
HAVING COUNT(CASE WHEN rn1 = rn2 THEN 1 END) = COUNT(*)
Basically you number the rows for each driver by date and by score separately. If there is a pattern of increasing dates and decreasing scores then row numbers will be identical for every row.
Compare the score with the previous score using LAG. Show all ssn for which not exist an exam that scored higher than the previous one using NOT EXISTS or NOT IN.
select distinct driver_ssn from exams
where driver_ssn not in
(
select driver_ssn
from
(
select
driver_ssn,
exam_score,
lag(exam_score) over (partition by driver_ssn order by exam_date) as prev_exam_score
from exams
) to_compare
where exam_score >= prev_exam_score
);
Use lag() to see where the previous score is bigger. Then use aggregation to be sure this never happens:
select driver_ssn
from (select e.*,
lag(exam_score) over (partition by driver_ssn order by exam_date) as prev_exam_score
from exams e
) e
group by Driver_ssn
having sum(prev_exam_score >= exam_score) = 0;
That is, there are no cases where the previous exam score is greater than or equal to the next one.
TABLE ONE DATA - po_header
PO_ID PROJ_ID SUP_ID TOT_SUM SUBM_DATE
32 5555 AccAYOU 99.00 2016-11-29
33 5555 AccAYOU 990.00 2016-11-29
34 25412 AccAYOU 248778595.08 2016-11-30
TABLE TWO DATA - po_details
PO_ID amount
32 110.00
33 1500000.00
34 565079266.00
34 1.00
How can I Run the following SQL ?
INSERT INTO po_header (TOT_SUM) VALUES (SELECT SUM(amount)
FROM po_details WHERE PO_ID = '34') WHERE PO_ID ='34';
Even it's un correct syntax but this is what you want:
UPDATE po_header h
SET TOT_SUM = (Select sum(amount) From po_details d Where d.po_id = h.po_id)
UPDATE po_header SET TOT_SUM = (SELECT SUM(amount) FROM po_details WHERE PO_ID='34') WHERE PO_ID='34';
Here is my table structure with data
id actor_id created_at updated_at
729 80 2012-09-10 17:05:59 2012-09-10 17:05:59
731 80 2012-09-10 17:04:02 2012-09-10 17:04:02
725 139 2012-09-06 13:59:08 2012-09-06 13:59:08
724 76 2012-09-06 11:31:30 2012-09-06 11:31:30
723 29 2012-09-06 09:40:22 2012-09-06 09:40:22
719 29 2012-09-06 09:24:02 2012-09-06 09:24:02
811 80 2012-09-02 17:05:59 2012-09-10 17:05:59
812 80 2012-09-01 17:04:02 2012-09-10 17:04:02
This is the result of
SELECT `te`.`id`, te.actor_id, te.created_at, te.created_at
FROM `timeline_events` AS te
ORDER BY
te.created_at DESC
LIMIT 10
I need group it by actor_id and created_at
Here is what i need in the end
id actor_id created_at updated_at count
729 80 2012-09-10 17:05:59 2012-09-10 17:05:59 2
725 139 2012-09-06 13:59:08 2012-09-06 13:59:08 1
724 76 2012-09-06 11:31:30 2012-09-06 11:31:30 1
723 29 2012-09-06 09:40:22 2012-09-06 09:40:22 2
812 80 2012-09-10 17:04:02 2012-09-10 17:04:02 2
Can someone guide me how to do this?
Many thanks in advance
UPD To simplify i will put another example
So say i have next rows
1 1 (count: 2)
1
3 3 (count: 1)
4
4 => after magic function it should be 4 (count: 2)
1
1 1 (count: 3)
1
6 6 (count: 2)
6
4 4 (count 3)
4
4
So it should split by groups.
UPD 2
I need this query for rendering timeline. Right now it show all info what user did, but i need group it.
Before
user1 upload photo
user1 changed information
user2 updated bio
user3 uploaded photo
user3 updated bio
user1 update bio
After
user1 uploadd photo and changed infomarion
user2 updated bio
user3 uploaded photo and updated bio
user1 updated bio
I think this might be what you are trying to do:
select t1.id,
t1.actor_id,
max(created_at) created_at,
updated_at,
t2.total
from yourtable t1
left join
(
select count(*) total, date(created_at) dt, actor_id
from yourtable
group by actor_id, date(created_at)
) t2
on t1.actor_id = t2.actor_id
and date(t1.created_at) = t2.dt
group by t1.actor_id, date(t1.created_at)
order by t1.created_at desc
see SQL Fiddle with demo
I am grouping by actor_id and the date using the DATE() function
I find solution by myself
Here is the query
SET #i = 0;
SELECT
COUNT(`wrapper`.`id`) AS 'count',
GROUP_CONCAT(`wrapper`.`type` SEPARATOR ',') as 'types'
FROM (
SELECT
#prev := (
SELECT prev_te.actor_id
FROM `timeline_events` AS prev_te
WHERE prev_te.created_at > te.created_at
ORDER BY prev_te.created_at ASC
LIMIT 1
) AS 'prev_actor_id',
IF(
#prev = te.`actor_id` OR #prev IS NULL,
#i,
#i := #i + 1
) AS 'actor_id_group',
`te`.*
FROM `timeline_events` AS te
ORDER BY
te.created_at DESC,
te.id DESC
) AS `wrapper`
GROUP BY `wrapper`.`actor_id_group`
LIMIT 10
And here is the proof link ;-)
This website really helped me
I am using wrapper for grouping purpose, because mysql didn't group by variables, if someone know better solution, please let me know
SELECT te.id, te.actor_id, te.created_at, te.updated_at ,
COUNT(*) FROM timeline_events AS te GROUP BY te.actor_id,
te.created_at ORDER BY
te.created_at DESC
LIMIT 10
Add GROUP BY actor_id before order by clause.
Latest edit:
select distinct actor_id, count(actor_id), created_at from actors group by actor_id
order by created_at desc;
Select * from Namelist;
Name Age
Sathish 25
Sathish 65
Sathish 55
Sathish 45
Sathish 35
Jana 55
Jana 25
Jana 10
Bala 55
Bala 26
How to get Percentage value for given format;
Name Count Percentege
Sathish 5 50%
Jana 3 30%
Bala 2 20%
Kindly share sql query?
This is a slightly sexier version of some of the other answers - note the use of sum(100) to avoid the longer (and more mundane) count(*) * 100 :)
select name, count(*) as count, sum(100) / total as percentage
from namelist
cross join (select count(*) as total from namelist) x
group by 1
This query(not tested) should work :
SELECT Name,
COUNT(*) AS Count,
(COUNT(*) / _total ) * 100 AS Percentege
FROM Namelist,
(SELECT COUNT(*) AS _total
FROM Namelist) AS myTotal
GROUP BY Name;
select
name,
count(name) as `count`,
count(name)/(select count(*) from namelist)*100 as pct
from namelist
group by name
replace column name and try this:
SELECT iName,
COUNT(iName) AS `Count`,
concat(FORMAT(((COUNT(iName) * 100) / NewPeople.iCount),2),'%') AS `Percentage`
FROM people, (SELECT COUNT(iName) AS iCount FROM people) NewPeople
GROUP BY iName;
Output:
Name Count Percentage
Sathish 5 50.00%
Jana 3 30.00%
Bala 2 20.00%