Suppose I have a table:
col1, col2, col3
1 0 1.3
0 0 0
0 1 0
1 1.5 1
Now, let's say each row has a "weight" calculated as this:
(col1 > 0 ? 1 : 0) + (col2 > 0 ? 1 : 0) + (col3 > 0 ? 1 : 0)
How can I select the total weight of all rows?
With the data I have given the total weight is 2+0+1+3=6
You just need one aggregate SUM() surrounding all the conditions with no GROUP BY.
SELECT
SUM(
(CASE WHEN col1 > 0 THEN 1 ELSE 0 END)
+ (CASE WHEN col2 > 0 THEN 1 ELSE 0 END)
+ (CASE WHEN col3 > 0 THEN 1 ELSE 0 END)
) AS total_weight
FROM your_table
http://sqlfiddle.com/#!2/b0d82/1
I am using CASE WHEN... here as it is portable across any RDBMS. Since MySQL will return a boolean 0 or 1 for the conditions though, it can be simplified in MySQL as:
SELECT
SUM(
(col1 > 0) /* returns 1 or 0 */
+ (col2 > 0)
+ (col3 > 0)
) AS total_weight
FROM your_table
http://sqlfiddle.com/#!2/b0d82/2
Related
#my code
select('a.client_location_id as location_id','b.name as location_name','c.name as client_name',
DB::raw('SUM(CASE WHEN a.type = 1 employee_qty THEN 1 ELSE 0 END) as e_qty_in'),
DB::raw('SUM(CASE WHEN a.type = 2 employee_qty THEN 1 ELSE 0 END) as e_qty_out')
)
i want to do IF conditioning like , if type = 1 result is SUM employee_qty AS e_qty_in and if type 2 the result is SUM employee_qty AS e_qty_out,
#table
I want to retrieve columns name from table whose value is 1
My query is give below...
mysqli_query($conn,"SELECT * FROM `tbl_therapist_schedule` WHERE `schedule_date`='30.11.2017'");
My table structure is give below...
slot1 slot2 slot3
1 1 0
2 1 1
1 1 2
3 1 0
my result...
slot1 slot2 slot3
I have the serious feeling that your data is not normalized. Instead of having separate columns for each slot, I would store all this data in a single column with metadata to relate each slot's state.
That being said, one way to get the output you want would be to aggregate over each slot column and check for the presence/absence of at least one 1 value. I then use GROUP_CONCAT to generate a CSV list of matching slots.
SELECT GROUP_CONCAT(slots)
FROM
(
SELECT
CASE WHEN SUM(CASE WHEN slot1 = 1 THEN 1 ELSE 0 END) > 0
THEN 'slot1' END AS slots
FROM tbl_therapist_schedule
WHERE schedule_date = '30.11.2017'
UNION ALL
SELECT
CASE WHEN SUM(CASE WHEN slot2 = 1 THEN 1 ELSE 0 END) > 0
THEN 'slot2' END
FROM tbl_therapist_schedule
WHERE schedule_date = '30.11.2017'
UNION ALL
SELECT
CASE WHEN SUM(CASE WHEN slot3 = 1 THEN 1 ELSE 0 END) > 0
THEN 'slot3' END
FROM tbl_therapist_schedule
WHERE schedule_date = '30.11.2017'
) t;
Demo
We have the table structure like
Fieldid userid data
41 2 1
12 2 0
41 3 1
12 3 1
We want the user which have (data=1 and filedid = 41) and (data = 0 and filedid=12)
We tried the query
select userid from table
where (data=1 and filedid = 41)
AND (data = 0 and filedid=12)
but it returns empty results.
Can anyone help how we will get the record using MySQL?
You can use grouping with a conditional aggregate in the HAVING clause:
SELECT userid
FROM mytable
GROUP BY userid
HAVING COUNT(CASE WHEN data = 1 AND FieldId = 1 THEN 1 END) >= 1 AND
COUNT(CASE WHEN data = 0 AND FieldId = 12 THEN 1 END) >= 1
you need to use union all like:
select * from table1 where Fieldid = 41 and data = 1 union all
select * from table1 where Fieldid = 12 and data = 0
attached image shows the output
Similar to #Giorgos, with multicolumn compare for conciseness:
select userid
from t
group by userid
having sum((data,fieldId) = (1, 41)) > 0
and sum((data,fieldId) = (0, 12)) > 0
When searching for results with one or another set of data you have to use OR keyword, not AND.
SELECT userid FROM table
WHERE (data = 1 AND filedid = 41)
OR (data = 0 AND filedid = 12)
I found this procedure here in the mantis database and need to run it straight to the bank to see what results it will bring , but do not know how to perform this procedure , which way to understand this ? How do you perform this procedure specifies?
DROP PROCEDURE IF EXISTS testlink.PR_SEL_RELAT_PRODUTIVIDADE;
CREATE PROCEDURE testlink.`PR_SEL_RELAT_PRODUTIVIDADE`(
idProjetoTestLink INT,
idPlanoTeste INT,
DataReferencia VARCHAR(10),
idProjetoMantis INT)
BEGIN
DECLARE vDataReferencia DATE;
DECLARE vIdQuery char(36);
/* Carrega os dias Testes na tabela Tempor?ria */
SELECT FN_SELECIONA_CASO_TESTES(idProjetoTestLink, idPlanoTeste)
INTO vIdQuery;
SELECT DATE(DataReferencia)
INTO vDataReferencia;
SELECT a.username,
a.realname,
sum(a.falha) falha,
sum(a.passou) passou,
SUM(a.bloqueado) bloqueado,
sum(a.EmExecucao) EmExecucao,
sum(a.DefeitosFechados) DefeitosFechados,
sum(a.DefeitosReabertos) DefeitosReabertos
FROM (SELECT X.LOGIN_TESTER username,
X.NOME_TESTER realname,
CASE STATUS WHEN 'f' THEN 1 ELSE 0 END Falha,
CASE STATUS WHEN 'p' THEN 1 ELSE 0 END Passou,
CASE STATUS WHEN 'b' THEN 1 ELSE 0 END Bloqueado,
0 EmExecucao,
0 DefeitosFechados,
0 DefeitosReabertos
FROM testlink.tmp_result_report X
WHERE X.ID_QUERY = vIdQuery
AND X.DATA_EXECUCAO = vDataReferencia
UNION ALL
SELECT X.LOGIN_TESTER username,
X.NOME_TESTER realname,
0 Falha,
0 Passou,
0 Bloqueado,
CASE STATUS WHEN 'r' THEN 1 ELSE 0 END EmExecucao,
0 DefeitosFechados,
0 DefeitosReabertos
FROM testlink.tmp_result_report X
WHERE X.ID_QUERY = vIdQuery
UNION ALL
SELECT user.username,
user.realname,
0 Falha,
0 Passou,
0 Bloqueado,
0 EmExecucao,
CASE WHEN bug.resolution <> 30 THEN 1 ELSE 0 END
DefeitosFechados,
CASE WHEN bug.resolution = 30 THEN 1 ELSE 0 END
DefeitosReabertos
FROM mantis.mantis_bug_table bug,
mantis.mantis_category_table categ,
mantis.mantis_user_table user
WHERE bug.project_id = idProjetoMantis
AND bug.category_id = categ.id
AND bug.status = 90 -- Fechado
AND bug.resolution <> 10 -- Aberto
AND date(FROM_UNIXTIME(bug.last_updated)) = vDataReferencia
AND bug.reporter_id = user.id) a
WHERE a.username IS NOT NULL
GROUP BY a.username;
END;
#1318 - Incorrect number of arguments for PROCEDURE testlink.PR_SEL_RELAT_PRODUTIVIDADE; expected 4, got 0
You execute a procedure using CALL (documentation).
With your procedure, you execute it using:
CALL PR_SEL_RELAT_PRODUTIVIDADE([int], [int], [string], [int]);
For example:
CALL PR_SEL_RELAT_PRODUTIVIDADE(3, 4, 'foo', 5);
i retrieved the following data using sql query from mysql
TOTAL COMPUTER DATE GROUP
-----------------------------------------------
48 LAPTOP2 2009-08-19 1
77 LAPTOP2 2009-08-20 1
0 LAPTOP2 2009-08-21 1
15 LAPTOP1 2009-08-19 1
25 MAIN 2009-08-23 1
25 LAP3 2009-08-18 2
3 LAP3 2009-08-19 2
55 LAP3 2009-08-20 2
i would like to rearrange the data like using php
group computer 2009-08-18 2009-08-19 2009-08-20 2009-08-21 2009-08-22 2009-08-23
------------------------------------------------------------------------------------------------
1 LAPTOP2 0 48 77 0 0 0
1 LAPTOP1 0 15 0 0 0 0
1 MAIN 25
2 LAP3 25 3 55 0 0 0
Use the following query to pivot the data:
SELECT t.group,
t.computer,
MAX(CASE WHEN t.date = '2009-08-18' THEN t.total ELSE 0 END) AS '2009-08-18',
MAX(CASE WHEN t.date = '2009-08-19' THEN t.total ELSE 0 END) AS '2009-08-19',
MAX(CASE WHEN t.date = '2009-08-20' THEN t.total ELSE 0 END) AS '2009-08-20'
--, etc...
FROM TABLE t
GROUP BY t.group, t.computer
Your options are either to define each column for the data you are pivoting, or you can use MySQL's Prepared Statement syntax to dynamically create those columns.
I feel the need to point out that your example is inconsistent - for LAPTOP2, you have zero as the value for 2009-08-18, but the main value for that is blank. Neither have a record for that date. If you want these to show as blank/etc, change ELSE 0 END to ELSE NULL END in the CASE statements.