Find Average Mark For Each User - Mysql - mysql

First table :
UserId UserName
1 User1
2 User2
3 User3
4 User4
Second Table
Userid Mark Aptitude English Technical Status
1 40 1 0 0 S
1 30 0 1 0 F
2 60 0 0 1 S
2 75 0 1 0 F
2 25 0 1 0 F
3 45 1 0 0 F
3 45 1 0 0 D
3 50 0 0 1 F
3 50 0 0 1 F
I have this two table. I need a query to get the each user average mark in English, Aptitude and Technical. The average should be calculated only for status F. The result should be like this
UserId AptitudeAverage EnglishAverage TechnicalAverage
1 0 30 0
2 0 50 0
3 45 0 50
4 0 0 0

Try this:-
SELECT userID, IFNULL(AVG(case when Aptitude = 1 then Mark * Aptitude end), 0) AS AptitudeAverage,
IFNULL(AVG(case when English = 1 then Mark * English end), 0) AS EnglishAverage,
IFNULL(AVG(case when Technical = 1 then Mark * Technical end), 0) AS TechnicalAverage
FROM YOUR_TAB
WHERE Status = 'F'
GROUP BY userID;
This might help you.
Here is the fiddle.
http://sqlfiddle.com/#!9/e449f/21

Related

Bar chart from many variables where varx = in Stata

I have a bar chart question here. Given that for all the variables in the dataset 1 = yes and 0 = No. I would like to plot a bar graph with the percentages (where var=1) on the y-axis and the variables on the x axis. Thanks in advance.
Dataset
Water
Ice
Fire
Vapor
1
1
0
1
1
0
0
1
0
1
1
1
1
1
1
1
1
1
0
1
1
1
1
0
0
1
1
1
0
1
0
1
0
1
1
1
1
0
1
1
0
1
0
0
0
1
1
0
1
0
1
0
1
0
1
0
1
1
1
1
0
1
0
1
1
0
1
1
1
0
1
0
1
1
0
1
1
0
0
1
0
1
1
1
1
1
0
1
1
0
0
1
0
1
1
1
The percent of 1s in a (0, 1) variable is just the mean multiplied by 100. As you probably want to see the percent as text on the graph, one method is to clone the variables and multiply each by 100.
You could then use graph bar directly as it defaults to showing means. I don't like its default in this case and the code instead uses statplot, which must be installed before you can use it.
* Example generated by -dataex-. For more info, type help dataex
clear
input byte(water ice fire vapor)
1 1 0 1
1 0 0 1
0 1 1 1
1 1 1 1
1 1 0 1
1 1 1 0
0 1 1 1
0 1 0 1
0 1 1 1
1 0 1 1
0 1 0 0
0 1 1 0
1 0 1 0
1 0 1 0
1 1 1 1
0 1 0 1
1 0 1 1
1 0 1 0
1 1 0 1
1 0 0 1
0 1 1 1
1 1 0 1
1 0 0 1
0 1 1 1
end
quietly foreach v of var water-vapor {
clonevar `v'2 = `v'
label var `v'2 "`v'"
replace `v'2 = 100 * `v'
}
* ssc install statplot
statplot *2 , recast(bar) ytitle(%) blabel(bar, format(%2.1f))
Try
. ssc install mylabels
checking mylabels consistency and verifying not already installed...
all files already exist and are up to date.
. sysuse nlsw88, clear
(NLSW, 1988 extract)
. mylabels 0(10)70, myscale(#/100) local(labels)
0 "0" .1 "10" .2 "20" .3 "30" .4 "40" .5 "50" .6 "60" .7 "70"
. graph bar (mean) married collgrad south union, showyvars legend(off) nolabel bargap(20) ylabel(`labels')
. table, statistic(mean married collgrad south union)
------------------------------
Married | .6420303
College graduate | .2368655
Lives in the south | .4194123
Union worker | .2454739
------------------------------
This relies on mylabels, and implements the bar gap (which I also like).

MySql: Count occurrences of values by date

I'm trying to count the number of occurences based severity level (1-5) on distinct dates. Note I have another table but severity levels are words (High, Medium and Low...not 1 to 5).
Example of DB:
DATE LEVEL COUNT
---- ----- -----
05/11/2018 3 14
05/11/2018 5 11
05/11/2018 5 11
05/12/2018 3 14
05/12/2018 2 14
05/13/2018 2 11
05/13/2018 1 12
Expected output
Date 1 2 3 4 5
--------- -- -- -- -- --
05/11/2018 0 0 14 0 22
05/12/2018 0 14 14 0 0
05/13/2018 12 11 0 0 0
Expected output 2
Level 05/11/2018 05/12/2018 05/13/2018
--------- ---------- ---------- ----------
1 0 0 12
2 0 14 11
3 14 14 0
4 0 0 0
5 22 0 0
I tried
SELECT CONCAT(DAY(`DATE`) ,MONTH(`DATE`) , YEAR(`DATE`)) AS DDMMYYYY ,
COUNT(DISTINCT LEVEL) as NumCount
FROM `myDatabase`
GROUP BY CONCAT(DAY(`DATE`),MONTH(`DATE`), YEAR(`DATE`) )
but I'm getting the number of different counts..
Any guidance would be appreciated! Thx!
You can't really do pivot tables in MySQL. However with a fixed number of columns (such as expected output #1) you can simulate them with CASE statements e.g.
select date_format(date, '%d%m%Y') as Date,
sum(case when level=1 then count else 0 end) as `1`,
sum(case when level=2 then count else 0 end) as `2`,
sum(case when level=3 then count else 0 end) as `3`,
sum(case when level=4 then count else 0 end) as `4`,
sum(case when level=5 then count else 0 end) as `5`
from table1
group by Date
Output:
Date 1 2 3 4 5
11052018 0 0 14 0 22
12052018 0 14 14 0 0
13052018 12 11 0 0 0

Sort a table based on one condition and the list remaining rows accordingly

I had a table with following details
cID sID Name pID childrenCount
1 1 Site 1 5
2 1 Safty 2 4
3 1 Archit 3 3
4 1 Civil 1 0
5 1 Concs 1 0
6 1 Pavm 1 0
7 1 Paint 3 0
8 1 Alum 3 0
9 1 Doors 3 0
10 1 Highw 1 0
11 1 Road 1 0
12 1 Alarm 2 0
13 1 Safty 2 0
14 1 Fence 2 0
15 1 Beaco 2 0
What I want is to write a select query to order the above table first by their childrenCount values in descending order and the list the corresponding rows according as below
cID sID Name pid childrenCount
1 1 Site 1 5
4 1 Civil 1 0
5 1 Conc 1 0
6 1 Pavm 1 0
10 1 Highw 1 0
11 1 Road 1 0
2 1 Safty 2 4
12 1 Alarm 2 0
13 1 Safty 2 0
14 1 Fence 2 0
15 1 Beacon 2 0
3 1 A WRK 3 3
7 1 Paint 3 0
8 1 Alumin 3 0
9 1 Doors 3 0
Thanks In Advance
Try this...first order by pid then childrenCount desc.
SELECT * FROM TABLENAME order by pid, childrenCount desc
Try this:
SELECT * FROM tableA ORDER BY pid, childrenCount DESC, cid

Mysql Between Clause inside Case when statement and count number of items in column

Here i am trying to get the count of products by using case when in mysql .
My sample data is
ID | Proname | led | lcd | hd | fullhd | 3d | displaysize (inches) | brandID
1 tv1 1 0 0 1 0 22 3
2 tv2 0 1 1 0 0 26 3
3 tv3 1 0 1 0 0 32 3
4 tv4 1 0 0 1 1 55 3
5 tv5 1 0 0 1 0 42 3
Now my expected out put
lcdcnt | ledcnt | hdcnt | fullhdcnt | 3dcnt | dispcntlessthan32 | displaycntbetwwen32and42 | displaycntabove42
1 4 2 3 1
Here is my Query . but i am not getting the correct output as i expected
select
sum(lcdtv) lcdcnt,
sum(ledtv) ledcnt,
sum(3dtv) 3dcnt,
sum(plasmatv) plasmacnt,
sum(smarttv) smatcnt,
sum(hdtv) hdnt,
sum(fullhdtv) fullhdcnt,
sum(ultrahdtv) ultrahdcnt,
sum(4ktv) 4kcnt,
sum(8ktv) 8kcnt,
sum(oledtv) oledcnt,
case
when (displayinches between 1 and 32) then count(displayinches)
end as dispcntlessthan32
case
when (displayinches between 32 and 42) then count(displayinches)
end as displaycntbetwwen32and42
from
tv
where
brandID = 3 and (ledtv = 1) and price != 0
Here is the SQLFiddel Demo
Below is the Approach :
select
sum(lcd) lcdcnt,
sum(led) ledcnt,
sum(3d) 3dcnt,
sum(hd) hdnt,
sum(fullhd) fullhdcnt,
sum(3d) 3dcnt,
sum(case when displaysize between 1 and 32 then 1 else 0 end) as dispcntlessthan32,
sum(case when displaysize between 33 and 42 then 1 else 0 end) as displaycntbetween32and42
from table1
where brandID = 3

Join table in order to create field with data from different tables

I need to update a field named "tipos" in a table named azz_properties with values from other tables named azz_locality (field name is "name") and azz_category (field name is also "name"), using a word to separate those values, the word "in". Substantially I need to create a mini-description phrase, like "Property Category in Property Locality", Ex. House in Rome.
Also, I need to update the value only if it is empty.
I tried the following code but i receive "0 lines affected"
update azz_properties p join
azz_locality l
on p.id = l.id join
azz_category c
on p.id = c.id
set p.tipos = concat(c.name, ' in ', l.name);
Anyone can help me please? What am I doing wrong?
below are some lines from each table, I tried to make this visible in a good way but this is the best I could do, sorry for it...:
Table azz_category
id name alias parent published ordering
17 Apartamentos apartamentos 0 1 0
18 Casas casas 0 1 1
19 Casas em condominios casas-em-condominios 0 1 2
20 Coberturas coberturas 0 1 3
Table azz_locality
id parent mid zipcode name alias published ordering checked_out checked_out_time
1 1 0 0 Abraão abraao 1 0 0 0000-00-00 00:00:00
2 1 0 0 Armação armacao 1 0 0 0000-00-00 00:00:00
3 1 0 0 Agronômica agronomica 1 0 0 0000-00-00 00:00:00
5 1 0 0 Bairro de Fatima bairro-de-fatima 1 0 0 0000-00-00 00:00:00
6 1 0 0 Balneário Estreito balneario-estreito 1 0 0 0000-00-00 00:00:00
7 1 0 0 Barra da Lagoa barra-da-lagoa 1 0 0 0000-00-00 00:00:00
9 1 0 0 Beira Mar beira-mar 1 0 0 0000-00-00 00:00:00
10 1 0 0 Bela Vista bela-vista 1 0 0 0000-00-00 00:00:00
168 19 0 0 Siriú siriu 0 0 0 0000-00-00 00:00:00
This is azz_properties, where category id is "cid" field and locality id is "lid"
id name name_tipos name_barrios alias parent agent_id agent ref type cid lid sid cyid postcode address description text text_es text_en text_barrios tipos price published use_booking ordering panoramic video lat lng available featured years bedrooms bathrooms garage area covered_area hits listdate refresh_time checked_out checked_out_time
2920 Vendo Apartamento... Vendo Apartamento... vendo-apartamento... 0 62 A3044 62 17 3 1 1 Rua Silveira Agenciamento... <p>Apartamento ... 360000.00 1 0 0 NULL 0.000000 0.000000 0 0 2012.01.01.05110 3 2 1 105 90 231 2013-05-03 2013-05-03 00:00:00 0 0000-00-00 00:00:00
Seems like what you need, given the data, is:
update azz_properties p join
azz_locality l
on p.lid = l.id join
azz_category c
on p.cid = c.id
set p.tipos = concat(c.name, ' in ', l.name);