Get Number of A's in Result Table - MySQL - mysql

This is the case. In my school all classes prepare excel sheet for each class with marks for each subject in term end test. There are 17 classes. I combine them in to access table. Then again export all data in to excel. make csv file . And import to Mysql Database using phpmyadmin. now I have result table as follow.
| ID | Name | Religion | Sinhala | science | english | maths | History | Categery 1 | Categery 2 | Categery 3 | Total | Average | Rank | |
|---- |------- |---------- |--------- |--------- |--------- |------- |--------- |------------ |------------ |------------ |------- |--------- |------ |--- |
| 1 | manoj | 45 | 65 | 78 | 98 | 67 | 67 | 63 | 76 | 64 | 654 | 62 | 12 | |
Sectional Head Need to get number of students who got >75 for all Subject.
And Number of Student Who got >75 for 8 subject out of 9.
I need to retrieve number of A s, B s (marks >=75) from this table.
Ex. Student names and Number of A s
Total Number of A for all 9 subject - 45
Total Number of A for all 8 subject (any 8 subject ) - 45
Total Number of A for all 7 subject (any 7 subject ) - 45
I Tried following SQL Statement
SELECT COUNT(SELECT COUNT()
FROM result
WHERE religion >=75
AND Math >=75)
FROM result
I read about same scenario in stack overflow.
Access 2010
this one get some point. but I cant solve it for my scenario.

Use GROUP BY studentName and SUM(grade = 'A') AS numberOfAs.
[Quick answer bc question is quickly formatted]

Related

Data output in a single row by order of importance

I'm trying to get data that is on multiple rows into a single row by order of importance.
I was working with multiple tables and was able to pull all the data I need into one table - so currently I'm working with one table where the data I need exists in multiple rows. Example a person can have more than one role. However, the roles have an order of importance - I added an order of importance column to the file I'm working with.
The file I'm working with looks like this:
ID | FIRST |LAST | ROLE | ORDER OF IMPORTANCE
116 | Jamie | Ansto | PARAL | 5
116 | Jamie | Ansto | FMREMP | 11
153 | Alan | Rond | PAR | 3
153 | Alan | Rond | PARAL | 5
155 | Maureen | Aron | GP | 4
155 | Maureen | Aron | PARAL | 5
38 | William | Dry | STU | 8
175 | Nathan |Gong | OTH | 10
175 |Nathan |Gong | FMRSTU | 13
175 |Nathan | Gon | FR | 14
308 | Bridget | Abad | PAR | 3
308 | Bridget | Abad | EMP | 7
370 | Matt | Bodie | BD | 1
370 | Matt | Bodie | AL | 2
What I need is a file that has all the codes associated with one person on the same row in the order of their importance.
I want to end up with something that looks like this:
ID |FIRST |LAST |CODE1 |CODE2 |CODE3 |CODE4
116 |Jamie |Ansto |PARAL |FMREMP
153 |Alan |Rond |PAR |PARAL
155 |Maureen |Aron | GP | PARAL
381 |William |Dry |STU
175 |Nathan |Gong |OTH |FMRSTU |FR
308 | Bridget |Abad |PAR |EMP
370 | Matt |Bodie |BD | AL
I tried using Group_Concat but it didn't give me the results in the order I wanted. Any help would be appreciated.
Thanks,
MG
You can do something like this:
SELECT *,GROUP_CONCAT(`ROLE` ORDER BY `ORDER_OF_IMPORTANCE` SEPARATOR ' ' )
FROM `table1` GROUP BY `ID`;
The SEPARATOR ' ' function will give you result like this OTH FMRSTU FR. If you remove it and only do GROUP_CONCAT(ROLE ORDER BY ORDER_OF_IMPORTANCE), the result will look like this OTH,FMRSTU,FR instead.

MySQL - How to subtract two datetime from the same table

I need help on a small problem with a subtraction in the same table and column
Well, iam creating a view, but the aplication generated the results of used time in tha same table and column.
My table have the following columns: id,field_id,object_id and value_date.
| ID | FIELD_ID | OBJECT_ID | VALUE_DATE |
| 55 | 4 | 33 | 2016-12-18 19:02:00 |
| 56 | 5 | 33 | 2016-12-18 19:12:00 |
| 57 | 4 | 35 | 2016-12-18 19:30:00 |
| 58 | 5 | 35 | 2016-12-18 20:00:00 |
I do not have much knowledge in sql, but i have tried some functions like timestampdiff, period_siff and others examples in stackoverflow.com.
Someone help me to subtract ID 56 with field_id 5 by line with ID 55 and field_id 4 in object_id 33 in SQL to bring the result in minutes. Ex: 10 or 00:10:00
An article about this problem would already help me. Thank you very much!
Lets assume that you want result to be in day format then query will be :
SELECT DATEDIFF(day,startDate,endDate) AS 'Day'
FROM table1;
Find complete example here
The soluction is below:
select TIMESTAMPDIFF(MINUTE,F1.value_date,F2.value_date) as minutes, F1.value_date,F2.value_date,F1.object_id,F2.object_id,F1.field_id,F2.field_id
from otrs_tst.dynamic_field_value F1
join otrs_tst.dynamic_field_value F2 on F1.object_id = F2.object_id
where F1.field_id in ('4','5')
and F2.field_id in ('4','5')
and F2.field_id <> F1.field_id
and F1.field_id < F2.field_id
group by F1.object_id,F2.field_id

SQL Count Distinct Using Multiple Unique Identifiers

My company ran a series of TV ads and we're measuring the impact by changes in our website traffic. I would like to determine the cost per session we saw generated, based on the cost of each ad.
The trouble is, the table this is referencing has duplicate data, so my currently cost_per_session isn't counting right.
What I have so far:
client_net_cleared = cost of ad
ad_time, media_outlet, & program = combined are a unique identifier for each ad
diff = assumed sessions generated by ad
.
SELECT DISTINCT tadm.timestamp AS ad_time
, tadm.media_outlet AS media_outlet
, tadm.program AS program
, tadm.client_net_cleared AS client_net_cleared
, SUM(tadm.before_ad_sum) AS before_ad_sessions
, SUM(tadm.after_ad_sum) AS after_ad_sessions
, (SUM(tadm.after_ad_sum) - SUM(tadm.before_ad_sum)) AS diff
, CASE WHEN tadm.client_net_cleared = 0 THEN null
WHEN (SUM(tadm.after_ad_sum) - SUM(tadm.before_ad_sum)) <1 THEN null
ELSE (tadm.client_net_cleared/(SUM(tadm.after_ad_sum) - SUM(tadm.before_ad_sum)))
END AS cost_per_session
FROM tableau.km_tv_ad_data_merged tadm
GROUP BY ad_time,media_outlet,program,client_net_cleared
Sample data:
ad_time | media_outlet | program | client_net_cleared | before_ad_sessions | after_add_sessions | diff | cost_per_session
---------------------|---------------|----------------|--------------------|--------------------|--------------------|------|-----------------
2016-12-09 22:55:00 | DIY | | 970 | 55 | 72 | 17 | 57.05
2016-12-11 02:22:00 | E! | E! News | 388 | 25 | 31 | 6 | 64.66
2016-12-19 21:15:00 | Cooking | The Best Thing | 428 | 70 | 97 | 27 | 15.85
2016-12-22 14:01:00 | Oxygen | Next Top Model | 285 | 95 | 148 | 53 | 5.37
2016-12-09 22:55:00 | DIY | | 970 | 55 | 72 | 17 | 57.05
2016-12-04 16:13:00 | Headline News | United Shades | 1698 | 95 | 137 | 42 | 40.42
What I need:
Only count one instance of each ad when calculating cost_per_session.
EDIT: Fixed the query, had a half completed row where I was failing at doing this before asking the question. :)
Get rid of the DISTINCT in SELECT DISTINCT in the first line of your query. It makes no sense in a GROUP BY query.
If your rows are entirely duplicate, try deduplicating the table before you put it into the GROUP BY grinder by replacing
FROM tableau.km_tv_ad_data_merged tadm
with
FROM ( SELECT DISTINCT timestamp, media_outlet, program,
client_net_cleared,
before_ad_sum, after_ad_sum
FROM tableau.km_tv_ad_data_merged
) tadm

MySQL, Determine value associated with MAX() of another value using GROUP BY [duplicate]

This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 6 years ago.
I have a MySQL database that contains the table, "message_route". This table tracks the the path between hubs a message from a device takes before it finds a modem and goes out to the internet.
"message_route" contains the following columns:
id, summary_id, device_id, hub_address, hop_count, event_time
Each row in the table represents a single "hop" between two hubs. The column "device_id" gives the id of the device the message originated from. The column "hub_address" gives the id of the hub the message hop was received by, and "hop_count" counts these hops incrementally. The full route of the message is bound together by the "summary_id" key. A snippet of the table to illustrate:
+-----+------------+-----------+-------------+-----------+---------------------+
| id | summary_id | device_id | hub_address | hop_count | event_time |
+-----+------------+-----------+-------------+-----------+---------------------+
| 180 | 158 | 1099 | 31527 | 1 | 2011-10-01 04:50:53 |
| 181 | 159 | 1676 | 51778 | 1 | 2011-10-01 00:12:04 |
| 182 | 159 | 1676 | 43567 | 2 | 2011-10-01 00:12:04 |
| 183 | 159 | 1676 | 33805 | 3 | 2011-10-01 00:12:04 |
| 184 | 160 | 2326 | 37575 | 1 | 2011-10-01 00:12:07 |
| 185 | 160 | 2326 | 48024 | 2 | 2011-10-01 00:12:07 |
| 186 | 160 | 2326 | 57652 | 3 | 2011-10-01 00:12:07 |
+-----+------------+-----------+-------------+-----------+---------------------+
There are three total messages here. The message with summary_id = 158 touched only one hub before finding a modem, so row with id = 180 is the entire record of that message. Summary_ids 159 and 160 each have 3 hops, each touching 3 different hubs. There is no upward limit of the number of hops a message can have.
I need to create a MySQL query that gives me a list of the unique "hub_address" values that constitute the last hop of a message. In other words, the hub_address associated with the maximum hop_count for each summary_id. With the database snippet above, the output should be "31527, 33805, 57652".
I have been unable to figure this out. In the meantime, I am using this code as a proxy, which only gives me the unique hub_address values for messages with a single hop, such as summary_id = 158.
SELECT DISTINCT(x.hub_address)
FROM (SELECT hub_address, COUNT(summary_id) AS freq
FROM message_route GROUP BY summary_id) AS x
WHERE x.freq = 1;
I would approach this as:
select distinct mr.hub_address
from message_route mr
where mr.event_time = (select max(mr2.event_time)
from message_route mr2
where mr2.summary_id = mr.summary_id
);

SQL GROUP BY query issue

I have a table with the following columns:
DriverNumber; DriverName; CarNumber; DriverConditions; LogonTime; VehicleID
this table has an entry for each LogonTime for each DriverNumber, and a driver can logon to different vehicles.
for example:
93070495 Mehar 189 Parcel, V, Wheelchair, M50, Special, Animal, COD P... Jan 2 2014 07:40:26:197AM 1029
93070495 Mehar 189 Parcel, V, Wheelchair, M50, Special, Animal, COD P... Jan 7 2014 08:09:50:097AM 1029
25184313 Kerry 895 Parcel, Cheques, V, Wheelchair, Special, Animal, C... Jan 3 2014 05:00:26:600PM 970
what i essentially want to do is show how many times a DriverNumber logs into each car.
this is what i have done so far:
SELECT DriverNumber, DriverName, CarNumber, DriverConditions, LogonTime,
count(DriverNumber) as DriverCount
FROM SilverDrivers
WHERE DriverNumber > 0
GROUP BY CarNumber
This gives me close to what i am after, but it only shows one CarNumber per DriverNumber. eg:
DRIVER HDL | DRIVER NAME | CAR NUMBER | DRIVER CONDITIONS | NUMBER OF LOGONS
98749492 | Manpreet | 3 | Parcel | 10
32176467 | Mark | 19 | Wheelchair | 7
92173581 | Varinder | 46 | Parcel | 1
what i want it to look like is:
DRIVER HDL | DRIVER NAME | CAR NUMBER | DRIVER CONDITIONS | NUMBER OF LOGONS
98749492 | Manpreet | 3 | Parcel | 7
98749492 | Manpreet | 12 | Parcel | 3
32176467 | Mark | 19 | Wheelchair | 4
32176467 | Mark | 214 | Wheelchair | 3
92173581 | Varinder | 46 | Parcel | 1
You should also group by driver number to get the count you want. Also, add all the other columns to your GROUP BY clause and remove columns that wouldn't be unique from SELECT (I left only DriverName as I assume it's always the same for one DriverNumber).
SELECT DriverNumber, DriverName, CarNumber, count(*) as DriverCount
FROM SilverDrivers
WHERE DriverNumber > 0
GROUP BY DriverNumber, DriverName, CarNumber
You want either
GROUP BY DriverNumber, CarNumber
or
GROUP BY CarNumber, DriverNumber