I have pieced this together from sites online and it works but not completely, what i need it to do is take the top 3 results and average them but it takes ALL results, can anyone point me in the right direction?
SELECT i.NAME,
e.comp,
Round(Avg(c.phase1 + c.phase2 + c.phase3 + c.phase4 + c.phase5
+ c.phase6), 2) AS "Average Score",
( CASE
WHEN compID = '7' THEN Concat(Round(Avg(
( (
c.phase1 + c.phase2 + c.phase3 + c.phase4 + c.phase5
+ c.phase6 ) / 400 ) * 100), 2), ' %')
WHEN compID = '5' THEN Concat(Round(Avg(
( (
c.phase1 + c.phase2 + c.phase3 + c.phase4 + c.phase5
+ c.phase6 ) / 600 ) * 100), 2), ' %')
WHEN compID = '3' THEN
Concat(Round(Avg(( ( c.phase1 + c.phase2 + c.phase3 + c.phase4 + c.phase5
+ c.phase6 ) / 600 ) * 100), 2), ' %')
ELSE 'Unspecified'
END ) AS "Average as Percent"
FROM jos_practicedetail c,
jos_comps e,
jos_practice g,
jos_members i
WHERE e.compsid = g.competition
AND g.practiceid = c.practicepid
AND i.memberid = c.competitorid
AND g.typeID = '2'
AND Year(g.pdate) = '2017'
AND (SELECT Count(*)
FROM jos_practicedetail b
WHERE b.competitorid = c.competitorid
AND b.practicepid = c.practicepid
AND ( b.phase1 + b.phase2 + b.phase3 + b.phase4 + b.phase5
+ b.phase6 ) >= (
c.phase1 + c.phase2 + c.phase3 + c.phase4 + c.phase5
+ c.phase6 )) <= 3
GROUP BY competitorid
HAVING Count(*) > 2
ORDER BY competitorid,
( Avg(c.phase1 + c.phase2 + c.phase3 + c.phase4 + c.phase5
+ c.phase6) ) DESC
Related
I have no idea how to group and count duplicates row on mysql
below is the result that I got from my query
ssn + checktime + nama
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'196702031989031001' + '2018-08-03 07:33:02' + 'FAJAR PERMADI'
'196810021993031001' + '2018-08-01 07:33:25' + 'ANDRI ANGGORO, SH'
'196911052000031001' + '2018-08-03 07:47:22' + 'SEMI TEDDY RORY, SS'
'196912221994032001' + '2018-08-01 08:03:59' + 'AI SALATUN'
'196912221994032001' + '2018-08-02 09:34:11' + 'AI SALATUN'
'196912221994032001' + '2018-08-03 07:33:18' + 'AI SALATUN'
'197012051993031001' + '2018-08-01 07:58:47' + 'AHMAD SODIKIN, SH'
'197012192001121001' + '2018-08-01 09:54:21' + 'JUARA PAHALA MARBUN, ST'
'197012192001121001' + '2018-08-02 09:39:41' + 'JUARA PAHALA MARBUN, ST'
and below is my query
SELECT a.ssn, a.checktime, b.nama
FROM hki_kepegawaian.fo_absensi a
left join hki_kepegawaian.fo_pegawai b on a.ssn = b.nip
where (substring(cast(checktime as DATE), 6, 2) = '08')
and (cast(a.checktime as TIME)) >= '07:30:00' and (cast(a.checktime as
TIME)) <= '10:00:00'
and (substring(golongan, 1, 2)) NOT IN ('IV')
group by ssn, cast(a.checktime as date)
and below is result that I expected
ssn + checktime + nama + total
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'196702031989031001' + '2018-08-03 07:33:02' + 'FAJAR PERMADI' + 1
'196810021993031001' + '2018-08-01 07:33:25' + 'ANDRI ANGGORO, SH' + 1
'196911052000031001' + '2018-08-03 07:47:22' + 'SEMI TEDDY RORY, SS' + 1
'196912221994032001' + '2018-08-01 08:03:59' + 'AI SALATUN' + 3
'197012051993031001' + '2018-08-01 07:58:47' + 'AHMAD SODIKIN, SH' + 1
'197012192001121001' + '2018-08-01 09:54:21' + 'JUARA PAHALA MARBUN, ST' + 2
Your expected output implies that you want to report the record having the earliest check time for each ssn/nama group of records. For the count, it just looks like the total number of records in each group.
SELECT
a.ssn,
MIN(CAST(a.checktime AS date)) AS checktime,
b.nama,
COUNT(*) AS total
FROM hki_kepegawaian.fo_absensi a
LEFT JOIN hki_kepegawaian.fo_pegawai b
ON a.ssn = b.nip
WHERE
MONTH(checktime) = 8 AND
CAST(a.checktime AS TIME) BETWEEN '07:30:00' AND '10:00:00' AND
SUBSTRING(golongan, 1, 2)) <> 'IV'
GROUP BY
a.ssn, CAST(a.checktime AS date);
I agree with Tim that you seem to want to take the earliest time. This is accomplished with a group by in this case.
However, there are some other fixes to the query that I would suggest:
Do not use string operations on date/times.
Use meaningful table aliases that are abbreviations for the table.
Include all unaggregated columns in the GROUP BY.
Use LIKE where appropriate.
So, I would suggest:
SELECT a.ssn, a.checktime, p.nama
FROM hki_kepegawaian.fo_absensi a LEFT JOIN
hki_kepegawaian.fo_pegawai b
ON a.ssn = p.nip
WHERE MONTH(checktime) = 8 AND
CAST(a.checktime as TIME) >= '07:30:00' AND
CAST(a.checktime as TIME)) <= '10:00:00' AND
golongan NOT LIKE 'IV%'
GROUP BY a.ssn, p.nama;
Look into count() function.
I can't check whether it works, but try the following query:
SELECT a.ssn, a.checktime, b.nama, count(*) as total
FROM hki_kepegawaian.fo_absensi a
left join hki_kepegawaian.fo_pegawai b on a.ssn = b.nip
where (substring(cast(checktime as DATE), 6, 2) = '08')
and (cast(a.checktime as TIME)) >= '07:30:00' and (cast(a.checktime as
TIME)) <= '10:00:00'
and (substring(golongan, 1, 2)) NOT IN ('IV')
group by ssn, nama
Having total>=1
I'm trying to perform calculations on select columns using aliases and a grouping by. Query is below(problem on line before the from):
select r.res_id,
r.arrive_date,
r.depart_date,
r.res_type,
if(DATEDIFF(r.depart_date, r.arrive_date) >29, 'LT', 'ST') as 'StayType',
SUM(r.rent + r.fee_arr_early + r.fee_dep_late + r.fee_peace_waiver + r.fee_pool + r.city_tax + r.fee_cleaning + r.fee_pet + r.fee_tshirt + r.fee_misc + r.fee_non_tax + r.fee_processing + r.fee_travel_ins + r.fee_event + r.fee_cancel) as 'folioTotal',
coalesce((select SUM(g.amount) from guest_payments as g where g.resId = r.res_id and charge_type = 'charge' and approved = 'Y'),0) as 'payments',
coalesce((select SUM(g.amount) from guest_payments as g where g.resId = r.res_id and charge_type = 'credit' and approved = 'Y'),0) as 'credits',
(SUM('folioTotal') - SUM('payments') + SUM('credits')) as 'folioBalance'
from reservations as r
join guest_payments as g
on r.res_id = g.resId
group by r.res_id
I've tried putting this inside another sum with the same outcome.
I was being stupid, I was referencing the aliases inside single ticks which is why it wasn't calculating. Solution:
select r.res_id,
r.arrive_date,
r.depart_date,
r.res_type,
g.entry_date,
if(DATEDIFF(r.depart_date, r.arrive_date) >29, 'LT', 'ST') as 'StayType',
(select SUM(r.rent + r.fee_arr_early + r.fee_dep_late + r.fee_peace_waiver + r.fee_pool + r.city_tax + r.fee_cleaning + r.fee_pet + r.fee_tshirt + r.fee_misc + r.fee_non_tax + r.fee_processing + r.fee_travel_ins + r.fee_event + r.fee_cancel) from reservations as r where r.res_id = g.resId) as 'folioTotal',
coalesce((select SUM(g.amount) from guest_payments as g where g.resId = r.res_id and charge_type = 'charge' ),0) as 'payments',
coalesce((select SUM(g.amount)* -1 from guest_payments as g where g.resId = r.res_id and charge_type = 'credit' and approved = 'Y'),0) as 'credits',
(select folioTotal - payments + credits)
from reservations as r
join guest_payments as g
on r.res_id = g.resId
group by r.res_id
Good evening to all,
as the subject, I have a query in which parameters can be used, preceded by the # character, in order to do the calculations. In order to test this query using MySQL Front. The problem is that if you launch the query the first time, I expected returns data, but if you run it again, I do not return anything. I also tried using Toad, but from the first attempt did not return anything. Thank you in advance.
SELECT(
SUM(
PzProd1 +
PzProd2 +
PzProd3 +
PzProd4 +
PzProd5 +
PzProd6
)) AS Pezzi_Prodotti,
PSLC.PSLC_Prod.LineaID as linea,
PSLC.PSLC_Prod.SezioneID,
PSLC.PSLC_Prod.Desc_Prod,
#cod_spezz := PSLC.PSLC_Prod.cod_spezz_1,
#l_spezz := REPLACE(FORMAT((SUBSTRING_INDEX(
PSLC.PSLC_Prod.lung_Spezz_1, '± ', 1) / 1000) ,2) , ',' , '.'
) as lunghezza_Spezzone,
REPLACE(FORMAT( #MtScrap := SUM(MtScrap) , 0) , ',' , '.') as MtScarto,
REPLACE(FORMAT((#MtScrap / #l_spezz),0), ',' , '.') as ScartoInPezzi ,
REPLACE(FORMAT(SUM((PzProd1 + PzProd2 + PzProd3 + PzProd4 + PzProd5 + PzProd6) * #l_spezz) ,0), ',' , '.') as MT_Prodotti,
REPLACE(FORMAT( #xxx := (
SUM(IF(PSLC.PSLC_CAUSALI.AvailableCausa_1 NOT LIKE ('%sezione%'),PSLC.PSLC_CAUSALI.AvailableTotTime_1 , 0)) +
SUM(IF(PSLC.PSLC_CAUSALI.AvailableCausa_2 NOT LIKE ('%sezione%'),PSLC.PSLC_CAUSALI.AvailableTotTime_2 , 0)) +
SUM(IF(PSLC.PSLC_CAUSALI.AvailableCausa_3 NOT LIKE ('%sezione%'),PSLC.PSLC_CAUSALI.AvailableTotTime_3 , 0)) +
SUM(IF(PSLC.PSLC_CAUSALI.AvailableCausa_4 NOT LIKE ('%sezione%'),PSLC.PSLC_CAUSALI.AvailableTotTime_4 , 0)) +
SUM(IF(PSLC.PSLC_CAUSALI.AvailableCausa_5 NOT LIKE ('%sezione%'),PSLC.PSLC_CAUSALI.AvailableTotTime_5 , 0)) +
SUM(IF(PSLC.PSLC_CAUSALI.AvailableCausa_6 NOT LIKE ('%sezione%'),PSLC.PSLC_CAUSALI.AvailableTotTime_6 , 0)) +
SUM(IF(PSLC.PSLC_CAUSALI.Causa_1 NOT LIKE ('%sezione%'),PSLC.PSLC_CAUSALI.Total_Time_1 , 0)) +
SUM(IF(PSLC.PSLC_CAUSALI.Causa_2 NOT LIKE ('%sezione%'),PSLC.PSLC_CAUSALI.Total_Time_2 , 0)) +
SUM(IF(PSLC.PSLC_CAUSALI.Causa_3 NOT LIKE ('%sezione%'),PSLC.PSLC_CAUSALI.Total_Time_3 , 0)) +
SUM(IF(PSLC.PSLC_CAUSALI.Causa_4 NOT LIKE ('%sezione%'),PSLC.PSLC_CAUSALI.Total_Time_4 , 0)) +
SUM(IF(PSLC.PSLC_CAUSALI.Causa_5 NOT LIKE ('%sezione%'),PSLC.PSLC_CAUSALI.Total_Time_5 , 0)) +
SUM(IF(PSLC.PSLC_CAUSALI.Causa_6 NOT LIKE ('%sezione%'),PSLC.PSLC_CAUSALI.Total_Time_6 , 0))),0), ',' , '.') as tempi,
#a := SAMPLE.products.vel_linea as Vel,
REPLACE(FORMAT(((#xxx * #a) / #l_spezz) ,0), ',' , '.') as MinFermoInPezzi
FROM PSLC.PSLC_Prod
JOIN PSLC.PSLC_CAUSALI ON (
(PSLC.PSLC_Prod.SezioneID = PSLC.PSLC_CAUSALI.IDSezione) and
(PSLC.PSLC_prod.giorno = PSLC.PSLC_CAUSALI.DataStartPrg) AND (
(PSLC.PSLC_prod.Cod_Spezz_1 = PSLC.PSLC_CAUSALI.IDSpezzone) OR
(PSLC.PSLC_prod.Cod_Spezz_2 = PSLC.PSLC_causali.IDSpezzone) OR
(PSLC.PSLC_prod.Cod_Spezz_3 = PSLC.PSLC_causali.IDSpezzone) OR
(PSLC.PSLC_prod.Cod_Spezz_4 = PSLC.PSLC_CAUSALI.IDSpezzone) OR
(PSLC.PSLC_prod.Cod_Spezz_5 = PSLC.PSLC_causali.IDSpezzone) OR
(PSLC.PSLC_prod.Cod_Spezz_6 = PSLC.PSLC_causali.IDSpezzone) ) AND
PSLC.PSLC_prod.ID_Prog = PSLC.PSLC_CAUSALI.ID_Prog)
JOIN sample.products ON (
PSLC.PSLC_Prod.SezioneID = sample.products.sku)
WHERE (PSLC.PSLC_Prod.giorno BETWEEN '2014-05-05' AND '2014-05-09') AND
(PSLC.PSLC_Prod.SezioneID = PSLC.PSLC_CAUSALI.IDSezione) AND (
((PSLC.PSLC_prod.Cod_Spezz_1 = #cod_spezz) OR
(PSLC.PSLC_prod.Cod_Spezz_2 = #cod_spezz) OR
(PSLC.PSLC_prod.Cod_Spezz_3 = #cod_spezz) OR
(PSLC.PSLC_prod.Cod_Spezz_4 = #cod_spezz) OR
(PSLC.PSLC_prod.Cod_Spezz_5 = #cod_spezz) OR
(PSLC.PSLC_prod.Cod_Spezz_6 = #cod_spezz))
)
after a bit of help please :)
The following MySQL query
select id, customer_id, user,
(((q_responce_time + o_responce_time + cs_responce_time + q_accuracy +
o_accuracy + cs_accuracy + q_personnel + o_personnel + cs_personnel +
q_communication + o_communication + d_communication + cs_communication +
q_overall + qu_overall + o_overall + d_overall + cs_overall + profile +
glass + parts + roof + in_full + direct_delivery + damages + service)/125)*100) as total,
month(create_datetime) as posted_month
from cs_review_centre
Where
create_datetime >= '2013-01-01'
and create_datetime < '2013-10-31'
and customer_id = 26
order by posted_month, customer_id
produces the following result
"customer_id" | "user" |"total" |"posted_month"|
"26" | "co2_test" |"72.8000" | "7" |
"26" | "co2_test" |"60.8000" | "8" |
"26" | "Lisa" |"81.6000" | "9" |
"26" | "Lisa" |"84.0000" | "10" |
"26" | "Lisa" |"52.0000" | "10" |
what I want to achieve is when a posted_month contains a duplicate value I want to average the "total"
Any help would very greatly appreciated
Thanks
Just GROUP BY customer_id, user,posted_month and use AVG() function
select customer_id, user,
AVG(
(((q_responce_time + o_responce_time + cs_responce_time + q_accuracy +
o_accuracy + cs_accuracy + q_personnel + o_personnel + cs_personnel +
q_communication + o_communication + d_communication + cs_communication +
q_overall + qu_overall + o_overall + d_overall + cs_overall + profile +
glass + parts + roof + in_full + direct_delivery + damages + service)/125)*100)
) as AverageTotal,
month(create_datetime) as posted_month
from cs_review_centre
Where
create_datetime >= '2013-01-01'
and create_datetime < '2013-10-31'
and customer_id = 26
GROUP BY customer_id, user,posted_month
order by posted_month, customer_id
Try:
select id, customer_id, user,
avg((((q_responce_time + o_responce_time + cs_responce_time + q_accuracy +
o_accuracy + cs_accuracy + q_personnel + o_personnel + cs_personnel +
q_communication + o_communication + d_communication + cs_communication +
q_overall + qu_overall + o_overall + d_overall + cs_overall + profile +
glass + parts + roof + in_full + direct_delivery + damages + service)/125)*100)) as total,
month(create_datetime) as posted_month
from cs_review_centre
Where
create_datetime >= '2013-01-01'
and create_datetime < '2013-10-31'
and customer_id = 26
group by id, customer_id, user, month(create_datetime)
order by posted_month, customer_id
The following code returns a NaN in situations where there are no records. How do I prevent this from being displayed in the report? A 0 would be preferred.
=FormatNumber(
((
(Code.NullSafeSplit(Fields!AvgLOSC1.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC1.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC2.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC2.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC3.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC3.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC4.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC4.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC5.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC5.Value, 1))
) / (
CInt(Code.NullSafeSplit(Fields!AvgLOSC1.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC2.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC3.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC4.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC5.Value, 1))
))
, 0)
I think the formula below should get it. I haven't tested this, so I might be missing a parenthesis. The problem is likely coming from a divide by zero when all entries are null. This catches that and sets the divisor to 1 in that case.
=FormatNumber(
(
(Code.NullSafeSplit(Fields!AvgLOSC1.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC1.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC2.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC2.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC3.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC3.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC4.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC4.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC5.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC5.Value, 1))
)
/
(
IIF((
CInt(Code.NullSafeSplit(Fields!AvgLOSC1.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC2.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC3.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC4.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC5.Value, 1))
) = 0,
1,
(
CInt(Code.NullSafeSplit(Fields!AvgLOSC1.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC2.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC3.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC4.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC5.Value, 1))
)
)
, 0)