SQL Subquery IN SELECT [Symfony3] - mysql

I Have Table Actions with the following fields :
idAction
Cloture
Date
I'd Like to loop through and display a list of all idAction In my DB + idAction with Cloture = 0 Group By the same Date (both of them).
I tried below method. But it doesn't work. Can anyone help me?
$query = $this->getEntityManager()->createQuery(
'SELECT COUNT(a.idAction) AS nmbreAction , week(a.dateOuverture) AS week,( SELECT COUNT(c.idAction) , week(c.dateOuverture) FROM ActionActionBundle:Action c
WHERE c.cloture = 0 ) AS nmbreRetard FROM ActionActionBundle:Action a
GROUP BY week');

Mmm, you question lacks a lot of information. Is this what you need?
SELECT COUNT(a.idAction) AS nmbreAction ,
week(a.dateOuverture) AS week,
(SELECT COUNT(c.idAction)
FROM ActionActionBundle:Action c
WHERE c.cloture = 0
and week(c.dateOuverture) = week(a.dateOuverture)) AS nmbreRetard
FROM ActionActionBundle:Action a
GROUP BY week(a.dateOuverture)
You can't select more than 1 column in a sub query\correlated query in the select list, which was probably showed to you in the error message.
EDIT: Better of do that:
SELECT COUNT(a.idAction) AS nmbreAction ,
week(a.dateOuverture) AS week,
COUNT(CASE WHEN a.cloture = 0 THEN 1 END) as nmbreRetard
FROM ActionActionBundle:Action a
GROUP BY week(a.dateOuverture)

Related

SQL count show group by show in column instead of rows

I have a table like this
Now my output would like to be
total_rows | completed | incomplete
------------------------------------
7 2 5
How can I achieve that.
You could use condition aggregation
select count(*) total ,
sum(completed = 1) completed ,
sum(completed = 0) incompleted
from your_table
Try the following.
select
count(*) as total_rows,
sum(case when completed = 1 then 1 else 0 end) as completed,
sum(case when completed = 0 then 1 else 0 end) as incomplete
from myTable
With conditional aggregation:
select
count(*) as total_rows,
sum(completed) completed,
sum(1 - completed) incomplete
from tablename
or:
select
count(*) as total_rows,
sum(completed) completed,
sum(not completed) incomplete
from tablename
I think this will help you
select count(id) as total_rows ,
sum(completed = 1) as completed ,
sum(completed = 0) as incompleted from sales_call_task_jo_iformation;
please try this if you need I will help you

Sql query with three conditions

I have a database with a table having content as below :
message_number message_type message_chat
0 IN Hi
1 OB Hello
2 IN Help
3 IN Want to find this thing
4 OB Sure
5 OB Please let me know
I have written 5 rows since i want to incorporate all possible cases that i want in my query in the example table that i showed.
Now in my query output, i want something like :
message_in message_out
Hi Hello
Help NULL
Want to find this string Sure
NULL Please let me know
So the cases that i want to consider are :
suppose if message_number=0 and message_number=1 both have message_type value as IN then put message_chat_in as message_chat(at message_number=0) and message_chat out as NULL and the iterate over message_number=1
if message_number =0 have message_type=IN and message_number =1 have message_type=OB, then show message_chat(at message_number=0) as message_chat_in and message_chat(at message_number=1) as message_out and dont iterate over message_number=1;
hope i have clarified the condition though i have included all three condition in the expected output.How should my sqlquery look like?
Edit : I am using mysql version 5.5.8
Try the following query
SELECT
q1.message_number in_num,
q1.message_chat in_chat,
q2.message_number out_num,
q2.message_chat out_chat
FROM
(
SELECT *,#i1:=IFNULL(#i1,0)+1 num
FROM Chat
ORDER BY message_number
) q1
LEFT JOIN
(
SELECT *,#i2:=IFNULL(#i2,0)+1 num
FROM Chat
ORDER BY message_number
) q2
ON q2.num=q1.num+1 AND q2.message_type<>q1.message_type
WHERE q1.message_type='IN'
UNION ALL
SELECT
q1.message_number in_num,
q1.message_chat in_chat,
q2.message_number out_num,
q2.message_chat out_chat
FROM
(
SELECT *,#i3:=IFNULL(#i3,0)+1 num
FROM Chat
ORDER BY message_number
) q1
RIGHT JOIN
(
SELECT *,#i4:=IFNULL(#i4,0)+1 num
FROM Chat
ORDER BY message_number
) q2
ON q2.num=q1.num+1 AND q2.message_type<>q1.message_type
WHERE q2.message_type='OB'
AND q1.message_type IS NULL
ORDER BY IFNULL(in_num,out_num)
SQL Fiddle - http://sqlfiddle.com/#!9/95a515/1
The second variant
SET #i1 = 0;
SET #i2 = 0;
SET #i3 = 0;
SET #i4 = 0;
-- the same query
SQL Fiddle - http://sqlfiddle.com/#!9/95a515/2
Or
SELECT 0,0,0,0 INTO #i1,#i2,#i3,#i4;
-- the same query
SQL Fiddle - http://sqlfiddle.com/#!9/95a515/5
why not using a analytic function here? I would do it with Lead() like this:
with inc as (
--Do the incorporation in this block. could be subquery too
--but its easier to read this way.
select
case when message_type = 'IN'
then message_chat
end as message_in
,case when LEAD(message_type) OVER (Order by message_number) = 'OB' --get the next message by number if it is type OB
then LEAD(message_chat) OVER (order by message_number)
end as message_out
from input
)
select *
from inc
where coalesce(message_in, message_out) is not null --filter out rows where with in & out is null
Ok, since there is no analytical functions in MySQL less than 8 the code may not be easy to follow:
with data_rn as
(
-- this isolate consecutive rows with the same message_type
select d1.*, count(d2.message_number) rn
from data d1
left join data d2 on d1.message_number > d2.message_number and d1.message_type != d2.message_type
group by d1.message_number
),
data_rn2 as
(
-- this marks the rows where new rows has to be added (i.e. when rn2 != 0)
select d1.*, count(d2.message_number) rn2
from data_rn d1
left join data_rn d2 on d1.rn = d2.rn and d1.message_type = d2.message_type and d1.message_number > d2.message_number
group by d1.message_number
),
data_added as
(
-- this add new rows
select message_number, message_type, message_chat
from data_rn2
union all
select message_number - 0.5, 'OB', NULL from data_rn2 where message_type = 'IN' and rn2 != 0
union all
select message_number - 0.5, 'IN', NULL from data_rn2 where message_type = 'OB' and rn2 != 0
order by message_number
), data_added_rn as
(
-- this compute new row numbering
select d1.*, ceil((count(d2.message_number)+1)/2) rn
from data_added d1
left join data_added d2 on d1.message_number > d2.message_number
group by d1.message_number
)
-- this will do the final formating
select max(case when message_type = 'IN' then message_chat end) message_in,
max(case when message_type = 'OB' then message_chat end) message_out
from data_added_rn
group by rn
demo
I have tried to comment each section appropriately.

Union two SQL Queries with one same column ( using Eloquent )

i have two queries which should be union (with laravel eloquent) but there is a duplicate column called group_date in both query and I should show one of them
SELECT
to_char(CREATE_UTC_DATETIME, 'yyyy-mm-dd') AS group_date,
COUNT(*) AS successful_transaction
FROM "REPORT_EVENTS"
WHERE "RESULT_CODE" = '0' AND "EVENT_TYPE" = 'BILL'
GROUP BY to_char(CREATE_UTC_DATETIME, 'yyyy-mm-dd')
ORDER BY "GROUP_DATE" DESC
SELECT
to_char(CREATE_UTC_DATETIME, 'yyyy-mm-dd') AS group_date,
COUNT(*) AS unsuccessful_transaction
FROM "REPORT_EVENTS"
WHERE "RESULT_CODE" = '1' AND "EVENT_TYPE" = 'BILL'
GROUP BY to_char(CREATE_UTC_DATETIME, 'yyyy-mm-dd')
ORDER BY "GROUP_DATE" DESC
You don't want a UNION here, but rather a single query which uses conditional aggregation:
SELECT
TO_CHAR(CREATE_UTC_DATETIME, 'yyyy-mm-dd') AS group_date,
SUM(CASE WHEN RESULT_CODE = '0' THEN 1 ELSE 0 END) AS successful_transaction,
SUM(CASE WHEN RESULT_CODE = '1' THEN 1 ELSE 0 END) AS unsuccessful_transaction
FROM "REPORT_EVENTS"
WHERE "EVENT_TYPE" = 'BILL'
GROUP BY TO_CHAR(CREATE_UTC_DATETIME, 'yyyy-mm-dd')
ORDER BY "GROUP_DATE" DESC
I am not giving any Eloquent/Laravel code here, but I am fairly certain that you would need a custom raw query to handle this. So, your actual PHP code would more or less just have the above query in its raw form.

SQL Union query error

Im trying to join two count querys
SELECT COUNT(*) AS total FROM clients WHERE addedby = 1
UNION
SELECT COUNT(*) AS converts FROM clients WHERE addedby = 1 AND status = '6'
What this returns is
total
4
0
this is the correct data, what I was expecting was this
total converts
4 0
You don't need a UNION query to do this. SELECT A UNION SELECT B returns the rows of A followed by the rows of B (deduplicated; if you want all rows from both datasets, use UNION ALL).
What you want is something like this:
select
(select count(*) from clients where addedby=1) as total,
(select count(*) from clients where addedby=1 and status='6') as converts
Other way to do this is using a case ... end expression that returns 1 if status='6':
select
count(*) from clients,
sum(case when status='6' then 1 else 0 end) as converts
from clients
No UNION needed, do it in one pass.
SELECT COUNT(*) as total,
SUM(CASE status WHEN '6' THEN 1 ELSE 0 END) as converts
FROM clients;
The simplest way to write this query is as a conditional aggregation:
select count(*) as total, sum(status = '6') as converts
from cleints
where addedby = 1;
MySQL treats booleans as integers with 1 being true and 0 being false. You can just sum of the values to get a count.

Where mistake in sql LIKE clause?

I try to count all items from another table with this select:
SELECT id, name, (SELECT count(*)
FROM prekes_main
WHERE prekes_main.pristKaina = 1
and prekes_main.pg_kodas LIKE 'grupes_main.pg_kodas%') as pristKaina
FROM grupes_main
WHERE grupes_main.level = 1
and grupes_main.name <> ''
In LIKE clause I want automatically get selected grupes_main column pg_kodas, but in this query it always returns 0, where is mistake in LIKE function? thx
SELECT id, name,
(
SELECT COUNT(*)
FROM prekes_main
WHERE prekes_main.pristKaina = 1
AND prekes_main.pg_kodas LIKE CONCAT(grupes_main.pg_kodas, '%')
) pristKaina
FROM grupes_main
WHERE grupes_main.level = 1
AND grupes_main.name <> ''