SELECT COUNT(DISTINCT column) doesn't work - php

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)

Related

subqueries inside laravel raw syntax

I am trying to use DB::raw('raw sql query') to run the query below:
$rates = DB::raw('SELECT
mid,
x.qty_t/x.qty_total,
x.qty_t,
x.qty_total,
FROM
(SELECT
mid,
SUM(CASE WHEN (mtc="qty") THEN 1 ELSE 0 END) AS qty_total,
SUM(CASE WHEN (mtc="qty") THEN rte ELSE 0 END) AS qty_t,
STDDEV(CASE WHEN (mtc="qty") THEN rte ELSE 0 END) AS qty_sd
FROM
t_r
GROUP BY
mid) x')->get();
I'm getting a syntax error after (SELECT on mid, mtc and t_r.
How can I get this to work using raw?
You need to wrap DB::select around it. Something like this should work.
$rates = DB::select(DB::raw('SELECT
mid,
x.qty_t/x.qty_total,
x.qty_stddev,
x.qty_total,
FROM
(SELECT
mid,
SUM(CASE WHEN (mtc="qty") THEN 1 ELSE 0 END) AS qty_total,
SUM(CASE WHEN (mtc="qty") THEN rte ELSE 0 END) AS qty_t,
STDDEV(CASE WHEN (mtc="qty") THEN rte ELSE 0 END) AS qty_sd
FROM
t_r
GROUP BY
mid) x'))->get();

Transform MySQL Query in HTML Form

I have that query:
SELECT P.dt_venda as venda,
SUM(CASE WHEN P.id_consultor = 4 THEN IR.receita_gerada_receita END) as consultor4,
SUM(CASE WHEN P.id_consultor = 3 THEN IR.receita_gerada_receita END) as funcionario3,
SUM(CASE WHEN P.id_consultor = 2 THEN IR.receita_gerada_receita END) as funcionario2
FROM item_receitas as IR, funcionarios as F, pedidos as P
WHERE P.id_consultor = F.id
AND IR.id_pedido = P.id
GROUP BY venda, F.nome
The result is this:
Query result
Now I need help how i can build a HTML form like those image.
Remember - The fields names are dynamics, can be funcionario5,funcionario6...

Mysql inner join on 2 tables don't work

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

Group by query sql

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?

Query two tables together, only one record displaying

I am building a polling system and I have a query right now that is supposed to select all of the polls and count all of the votes for each of the polls (which lie in a separate table). So my tables look like:
Polls:
ID
Title
Body
Votes:
ID
PollID
Vote (This value is either 0 or 1)
Well the totaling of the votes looks like it is working, the issue is that it is currently only displaying one record.
Currently my query looks like this:
SELECT POLLS.ID,
POLLS.TITLE,
POLLS.BODY,
Sum(CASE
WHEN VOTES.VOTE = 1
AND VOTES.POLLID = POLLS.ID THEN 1
ELSE 0
END) AS yay,
Sum(CASE
WHEN VOTES.VOTE = 0
AND VOTES.POLLID = POLLS.ID THEN 1
ELSE 0
END) AS nay,
FROM polls,
VOTES
ORDER BY POLLS.ID
Also I am using PHP with Codeigniter.
SELECT polls.ID,polls.title,polls.body,
SUM(case when votes.vote = 1 then 1 else 0 end) AS yay,
SUM(case when votes.vote = 0 then 1 else 0 end) AS nay,
FROM polls
JOIN votes ON polls.ID = votes.pollID
GROUP BY poll.ID, polls.title, polls.body
ORDER BY polls.ID
You need to GROUP BY pollID the values in the votes table:
SELECT p.ID, p.title, p.body, v.yay, v.nay
FROM
(
SELECT PollID,
SUM(CASE WHEN vote = 1 THEN 1 ELSE 0 END) AS yay,
SUM(CASE WHEN vote = 0 THEN 1 ELSE 0 END) AS nay,
FROM votes
GROUP BY PollID
) v
INNER JOIN Polls p ON v.PollID = p.Id
ORDER BY p.ID
I'd go with:
SELECT polls.ID, polls.title, polls.body,
SUM(IF(votes.vote = 1, 1, 0)) AS yay,
SUM(IF(votes.vote = 0, 1, 0)) AS nay
FROM polls
JOIN votes ON (votes.pollID = polls.ID)
GROUP BY ID, title, body
What you are missing is "GROUP BY". Use GROUP BY PollID while taking sum.

Categories