I would like to count the number of alerts and the number of alerts processed by user and type type = C and M
I started with this query it gives me the number of major and critical alerts right but I'm stuck for numbers of alerts processed
SELECT
TYPE,
user_name,
COUNT(type_alertes) AS nb,
id_user,
SUM(CASE WHEN TYPE ='C' THEN 1 ELSE 0 END) AS critique,
SUM(CASE WHEN TYPE ='M' THEN 1 ELSE 0 END) AS majeur
FROM stat_alert
LEFT OUTER JOIN user_qdf ON user_qdf.`id` = id_user
WHERE
user_qdf.`id`=id_user
AND TYPE IN ('M', 'C')
GROUP BY id_user
who can help me
If you want number of all alerts and from that number of C and number of M then
SELECT
user_name,
COUNT(type_alertes) AS nb,
id_user,
SUM(CASE WHEN TYPE ='C' THEN 1 ELSE 0 END) AS critique,
SUM(CASE WHEN TYPE ='M' THEN 1 ELSE 0 END) AS majeur
FROM stat_alert
JOIN user_qdf ON user_qdf.`id` = id_user
GROUP BY id_user, user_name
should do that.
I assume that user_name is from user_qdf table and other are from stat_alert and id_user is not null.
EDIT: For sum of alertes
SELECT
user_name,
COUNT(type_alertes) AS nb,
id_user,
SUM(CASE WHEN TYPE ='C' THEN 1 ELSE 0 END) AS critique,
SUM(CASE WHEN TYPE ='M' THEN 1 ELSE 0 END) AS majeur
SUM(CASE WHEN TYPE ='M' OR TYPE = 'C' THEN 1 ELSE 0 END) AS both_c_and_m
FROM stat_alert
JOIN user_qdf ON user_qdf.`id` = id_user
GROUP BY id_user, user_name
EDIT2:
SELECT
user_name,
COUNT(type_alertes) AS nb,
SUM(CASE WHEN TYPE ='C' THEN type_alertes ELSE 0 END) AS critique_alerts,
SUM(CASE WHEN TYPE ='M' THEN type_alertes ELSE 0 END) AS majeur_alertes
id_user,
SUM(CASE WHEN TYPE ='C' THEN 1 ELSE 0 END) AS critique,
SUM(CASE WHEN TYPE ='M' THEN 1 ELSE 0 END) AS majeur
FROM stat_alert
JOIN user_qdf ON user_qdf.`id` = id_user
WHERE TYPE in ('C','M')
GROUP BY id_user, user_name
is that what you want?
Related
Why DISTINCT UID doesn't work in my code below?
$q = mysql_fetch_array(mysql_query("SELECT COUNT(DISTINCT UID) AS TOTAL,
SUM(CASE WHEN SYSTEM = 'Android' THEN 1 ELSE 0 END) AS A,
SUM(CASE WHEN SYSTEM = 'IOS' THEN 1 ELSE 0 END) AS I,
SUM(CASE WHEN SYSTEM = 'Windows' THEN 1 ELSE 0 END) AS W FROM user_visits"));
The query returns all items from the database selected even I put DISTINCT UID or *. Same result.
The distinct keyword is supposed to be outside like below,
SELECT DISTINCT column1, column2, ...
FROM table_name;
?
Also, you are trying to sum few things, It should be something like below,
SELECT UID, COUNT(UID) AS TOTAL,
SUM(CASE WHEN SYSTEM = 'Android' THEN 1 ELSE 0 END) AS A,
SUM(CASE WHEN SYSTEM = 'IOS' THEN 1 ELSE 0 END) AS I,
SUM(CASE WHEN SYSTEM = 'Windows' THEN 1 ELSE 0 END) AS W FROM user_visits
GROUP BY UID
with aggregate (group by)
I want to use a table's column value as column name and use it as JOIN ON value with another table column name. Below is my query.
SELECT nid, sid,
MAX(CASE WHEN cid = 2 THEN data END) first_name,
MAX(CASE WHEN cid = 3 THEN data END) last_name,
MAX(CASE WHEN cid = 4 THEN data END) email,
MAX(CASE WHEN cid = 5 THEN data END) phone_number,
MAX(CASE WHEN cid = 6 THEN data END) transitions,
MAX(CASE WHEN cid = 7 THEN data END) **upload**,
MAX(CASE WHEN cid = 9 THEN data END) coupon_code
FROM wsd AS wsd JOIN files AS fm ON **upload** = fm.fid WHERE nid IN(66,67) GROUP BY sid
As you see the above code, I want to use upload column as the JOIN on another table. Also, is it possible to embed HTML tags on the results?
I tried to get the results using temporary table. Here is my answer.
CREATE TEMPORARY TABLE IF NOT EXISTS my_temp_table SELECT sid, cid, data FROM wsd WHERE cid = 7 AND nid IN (66,67);
SELECT wsd.nid, wsd.sid, fm.filename,
MAX(CASE WHEN wsd.cid = 2 THEN wsd.data END) fn,
MAX(CASE WHEN wsd.cid = 3 THEN wsd.data END) ln,
MAX(CASE WHEN wsd.cid = 4 THEN wsd.data END) email,
MAX(CASE WHEN wsd.cid = 5 THEN wsd.data END) ph,
MAX(CASE WHEN wsd.cid = 6 THEN wsd.data END) tr,
MAX(CASE WHEN wsd.cid = 7 THEN wsd.data END) upr,
MAX(CASE WHEN wsd.cid = 9 THEN wsd.data END) coc
FROM wsd wsd JOIN my_temp_table ON wsd.sid = my_temp_table.sid JOIN files fm ON my_temp_table.data = fm.fid WHERE nid IN(66,67) GROUP BY wsd.sid;
Instead of temporary table, better query as follows:
SELECT wsd.nid, wsd.sid, fm.filename,
MAX(CASE WHEN wsd.cid = 2 THEN wsd.data END) fn,
MAX(CASE WHEN wsd.cid = 3 THEN wsd.data END) ln,
MAX(CASE WHEN wsd.cid = 4 THEN wsd.data END) email,
MAX(CASE WHEN wsd.cid = 5 THEN wsd.data END) ph,
MAX(CASE WHEN wsd.cid = 6 THEN wsd.data END) tr,
MAX(CASE WHEN wsd.cid = 7 THEN wsd.data END) upr,
MAX(CASE WHEN wsd.cid = 9 THEN wsd.data END) coc
FROM wsd wsd LEFT JOIN files fm ON wsd.data = fm.fid WHERE nid IN(66,67) GROUP BY wsd.sid;
I am using this code to get the data of a team
SELECT `in_standings`.*, `in_teams`.`name` as team_name, `in_teams`.`logo` as team_logo, `in_teams`.`shortcode` as team_shortcode, lr.*, in_teams.id as team_id
FROM `in_standings` daa
LEFT JOIN `in_teams` ON `in_standings`.`team_id`= `in_teams`.`id`
left join
(SELECT team1_id,daa.team_id as teamID,
MAX(CASE WHEN rn = 1 THEN winning_team END) AS result1,
MAX(CASE WHEN rn = 2 THEN winning_team END) AS result2,
MAX(CASE WHEN rn = 3 THEN winning_team END) AS result3,
MAX(CASE WHEN rn = 4 THEN winning_team END) AS result4,
MAX(CASE WHEN rn = 5 THEN winning_team END) AS result5
FROM (
SELECT team1_id, winning_team,
#row_number := #row_number + 1 AS rn
FROM in_games
CROSS JOIN (SELECT #row_number := 0) AS vars
WHERE team1_id = teamID <!-- here is problem -->
) AS t) as lr on lr.team1_id=in_standings.team_id
WHERE `in_standings`.`division_id` = '21'
ORDER BY `in_standings`.`points` DESC
My sub query where i comment is not getting the team_id by which i want to set the result of last 5 matchs in a same row.
Could anyone solve where is the problem?
I have a table in my database named contacts and a table named views.
On the table contacts I have the following fields:
id
status
first_name
last_name
The status can be cold, prospect or lost.
On the table views I have the following fields:
user_id
art_views
art_title
The relation between those 2 tables is id and user_id.
I need a query to make a new html table with the following columns:
art_title
cold
prospect
lost
Now I have the following query (UPDATED):
SELECT
v.art_title,
SUM(CASE c.status WHEN 'cold' THEN v.art_views ELSE 0 END) cold,
SUM(CASE c.status WHEN 'prospect' THEN v.art_views ELSE 0 END) prospect,
SUM(CASE c.status WHEN 'lost' THEN v.art_views ELSE 0 END) lost
FROM views v
JOIN contacts c ON v.user_id = c.id
GROUP BY v.art_title
This query is working now (thanks to Gerv) but i still have users who don't have a status. So i leave the field user_id in the table 'views' empty. How can i change the query for those users so i can count them also?
I tried to: SUM(CASE v.user_id WHEN ' ' THEN v.art_views ELSE 0 END) test,
but with no result here.
You can switch the logic by selecting from the views table and joining de contacts table.
Below query will pivot the status with a CASE clause
SELECT
v.art_title,
SUM(CASE c.status WHEN 'cold' THEN v.art_views ELSE 0 END) cold,
SUM(CASE c.status WHEN 'prospect' THEN v.art_views ELSE 0 END) prospect,
SUM(CASE c.status WHEN 'lost' THEN v.art_views ELSE 0 END) lost,
SUM(CASE c.status WHEN NULL THEN v.art_views ELSE 0 END) no_user
FROM views v
LEFT JOIN contacts c ON v.user_id = c.id
GROUP BY v.art_title
ORDER BY (cold+lost+prospect+no_user) DESC
LIMIT 10
Try
SELECT art_title, SUM(art_views), status FROM contacts AS c INNER JOIN views AS v ON v.user_id = c.id GROUP BY v.art_title, c.status
I have this tbl_religion with fieldname creldesc
and tbl_member with these fields
and I have this query to count all existing religion in tbl_member and count also the no. of male and female with that religion so i made this query :
SELECT m.creldesc as type,
COUNT(m.creldesc) as total,
SUM(CASE WHEN m.cgender='Male' THEN 1 ELSE 0 END) as male,
SUM(CASE WHEN m.cgender='Female' THEN 1 ELSE 0 END) as female
FROM tbl_member as m, tbl_barangay as b, tbl_household as h
WHERE m.chholdnumber = h.chholdnumber and h.cbrgycode=b.cbrgycode and b.cbrgyname = 'AGAO'
and m.crelationdesc !='Brgy. Captain'
GROUP BY m.creldesc
ORDER BY tot DESC;
and get this output :
what I want to achieve is also display the remaining religion from tbl_religion with 0 as value. I made a left join but get an error. Can someone help me how can I achive that ?
type total male female
Roman Catholic 7 4 3
Baptist 3 1 2
Islam 3 3 0
Iglesia ni Cristo 1 1 0
Free Methodist 1 1 0
Ang Dating Daan 1 0 1
Aglipay/Filipinsta 0 0 0
All the Gospel 0 0 0
Alpha Omega 0 0 0
and so on.............................
Use same joining approach for all tables, like this:
SELECT m.creldesc as type, COUNT(m.creldesc) as tot,
SUM(CASE WHEN m.cgender='Male' THEN 1 ELSE 0 END) as male,
SUM(CASE WHEN m.cgender='Female' THEN 1 ELSE 0 END) as female
FROM tbl_religion
LEFT JOIN tbl_member as m
ON tbl_religion.creldesc=m.creldesc
LEFT JOIN tbl_household as h
ON m.chholdnumber = h.chholdnumber
LEFT JOIN tbl_barangay as b
ON h.cbrgycode=b.cbrgycode
WHERE b.cbrgyname = 'AGAO' AND m.crelationdesc !='Brgy. Captain'
GROUP BY m.creldesc
ORDER BY tot DESC
Your current query approach of ... FROM tbl_member, tbl_barangay, tbl_household LEFT JOIN tbl_religion ... is wrong and will produce syntax error.
You can not make this:
FROM #TABLE1, #TABLE2, #TABLE3 LEFT JOIN #TABLE0 ON ...
You can select items only from a table at once... same for join.
SELECT m.creldesc as type,
COUNT(m.creldesc) as tot,
SUM(CASE WHEN m.cgender='Male' THEN 1 ELSE 0 END) as male,
SUM(CASE WHEN m.cgender='Female' THEN 1 ELSE 0 END) as female
FROM tbl_member as m
LEFT JOIN tbl_religion z ON z.creldesc = m.creldesc
LEFT JOIN tbl_barangay h ON h.cbrgycode=b.cbrgycode
SO ON.... WITH JOINS
GROUP BY m.creldesc
UPDATE WOKING CODE:
SELECT m.creldesc as type, COUNT(m.creldesc) as tot, SUM(CASE WHEN m.cgender='Male' THEN 1 ELSE 0 END) as male,
SUM(CASE WHEN m.cgender='Female' THEN 1 ELSE 0 END) as female
FROM tbl_member as m
LEFT JOIN tbl_religion r ON r.creldesc = m.creldesc
LEFT JOIN tbl_barangay as b ON m.chholdnumber = b.cbrgycode
LEFT JOIN tbl_household as h ON h.cbrgycode = b.cbrgycode
WHERE b.cbrgyname = 'AGAO' and m.crelationdesc !='Brgy. Captain'
GROUP BY m.creldesc
ORDER BY tot DESC
Here's what I did:
SELECT tbl_member.creldesc AS type,
COUNT(tbl_member.creldesc) AS total,
SUM(CASE WHEN tbl_member.cgender::text = 'Male'::text THEN 1 ELSE 0 END) AS male,
SUM(CASE WHEN tbl_member.cgender::text = 'Female'::text THEN 1 ELSE 0 END) AS female
FROM tbl_member
GROUP BY tbl_member.creldesc
UNION
SELECT b.creldesc AS type, 0 AS total, 0 AS male, 0 AS female
FROM tbl_religion b
WHERE NOT (b.creldesc::text IN
( SELECT DISTINCT tbl_member.creldesc FROM tbl_member))
GROUP BY b.creldesc;
and here is it's output :
that's all :) I used UNION instead of LEFT JOIN.