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
Related
i have this table
(date format : dd/mm/yyyy)
+ order_id + buyer + date + status +
+----------+-------+------------+-------------+
+ CSR001 + AAA + 01/01/2020 + delivered +
+ CSR002 + AAA + 03/01/2020 + canceled +
+ CSR003 + BBB + 01/01/2020 + delivered +
+ CSR004 + BBB + 04/01/2020 + delivered +
+ CSR005 + AAA + 01/02/2020 + canceled +
+ CSR006 + BBB + 01/02/2020 + delivered +
+ CSR007 + AAA + 01/02/2020 + delivered +
+ CSR008 + AAA + 01/02/2020 + delivered +
+----------+-------+------------+-------------+
what is the query to create view in mysql and make it like this, (Group By buyer, status, and count how many of delivered/cancelled value every month)
+ buyer + status + january + february +
+----------+-----------+-----------+------------+
+ AAA + delivered + 1 + 2 +
+ AAA + cancelled + 1 + 1 +
+ BBB + delivered + 2 + 0 +
+ BBB + cancelled + 0 + 0 +
+----------+-----------+-----------+------------+
You can have condition inside count to differentiate the month:
SELECT t.buyer,
t.status,
COUNT(IF(MONTH(t.date) = 1, 1, NULL)) AS jan,
COUNT(IF(MONTH(t.date) = 2, 1, NULL)) AS feb
FROM TABLE AS t
GROUP BY buyer,
status;
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 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
I am tring to construct a select query that sums the total of each column only if the column contains 1 (one).
This is what I have so far:
SELECT
UniqueID,
SUM(
SeqID0101 + SeqID0102 + SeqID0103 + SeqID0104 + SeqID0105 + SeqID0106 + SeqID0107 + SeqID0108 + SeqID0109 + SeqID0110 +
SeqID0201 + SeqID0202 + SeqID0203 +
SeqID0301 + SeqID0302+ SeqID0303 + SeqID0304 +
SeqID0401 + SeqID0402 + SeqID0403 + SeqID0404 +
SeqID0501 + SeqID0502 +
SeqID0601 + SeqID0602 +
SeqID0701 + SeqID0702 +
SeqID0801 + SeqID0802 +
SeqID0901 + SeqID0902 + SeqID0903 +
SeqID1001 + SeqID1002 + SeqID1003 + SeqID1004)
WHERE
(SeqID0101 = 1 OR SeqID0102 = 1 OR SeqID0103 = 1 OR SeqID0104 = 1 OR SeqID0105 = 1 OR SeqID0106 = 1 OR SeqID0107 = 1 OR SeqID0108 = 1 OR SeqID0109 = 1 OR SeqID0110 = 1 OR SeqID0201 = 1 OR SeqID0202 = 1 OR SeqID0203 = 1 OR SeqID0301 = 1
OR SeqID0302 = 1 OR SeqID0303 = 1 OR SeqID0304 = 1 OR SeqID0401 = 1 OR SeqID0402 = 1 OR SeqID0403 = 1 OR SeqID0404 = 1 OR SeqID0501 = 1 OR SeqID0502 = 1 OR SeqID0601 = 1 OR SeqID0602 = 1 OR SeqID0701 = 1 OR SeqID0702 = 1 OR SeqID0801 = 1
OR SeqID0802 = 1 OR SeqID0901 = 1 OR SeqID0902 = 1 OR SeqID1001 = 1 OR SeqID1002 = 1 OR SeqID1003 = 1)
The issue I am having is, if a column contant 3, that 3 is getting included in the SUM total.
Am I doing this wrong and if so how should I construct the query.
Many thanks in advance for your time.
Cheers.
Your text is a bit unclear, but you should be able to use this:
SELECT
UniqueID,
SUM(IF(SeqID0101 = 1, 1, 0)
+ IF(SeqID0102 = 1, 1, 0)
...
+ IF(SeqID1003 = 1, 1, 0))
FROM
my_table
GROUP BY
UniqueID;
This is what I've tried so far:
SELECT M.ID, M.NAME, M.SEQ
FROM M JOIN MCA
ON M.ID = MCA.M_ID
JOIN MC
ON MCA.CAT_ID = MC._ID
WHERE MCA.CAT_ID = "
+ inputCategoryID
+ " AND (M.NAME LIKE '"
+ inputSearch.replace("'", "''")
+ "%' OR M.NAME LIKE '%"
+ inputSearch.replace("'", "''")
+ "%' ) AND M.START_DATE <= CURRENT_DATE " +
AND M.EXPIRY_DATE >= CURRENT_DATE " +
ORDER BY M.SEQ DESC, M.NAME ASC
SAMPLE RESULTS:
ID NAME SEQ
1 Tes1 0
2 Arj2 0
3 Jfa3 1
4 Pof4 0
DESIRED RESULTS:
ID NAME SEQ
3 Jfa3 1
2 Arj2 0
4 Pof4 0
1 Tes1 0
But if the same SEQ value (i.e., 0, 0), it should display Name alphabetically. Any ideas how to do that? Thanks.