I'm trying to figure out how to put two and possibly additional select queries into 1 big query for a view but having trouble. The end result should be 1 row containing the count of selected fields with two columns. Do I use a subselect for this?
Here are the first two queries:
SELECT
COUNT(PIN.APP_UID) AS `Type A outstanding`
FROM PMT_INSP_NORMAL PIN
WHERE
PIN.APP_STATUS = "To_Do"
AND
PIN.DATE_COMPLETED IS NULL
SELECT
COUNT(PSN.APP_UID) AS `Type B outstanding`
FROM PMT_SIGN_NORMAL PSN
WHERE
PSN.APP_STATUS = "To_Do"
AND
PSN.DATE_COMPLETED IS NULL
Try this:
Select
(SELECT COUNT(PIN.APP_UID)
FROM PMT_INSP_NORMAL PIN
WHERE PIN.APP_STATUS = "To_Do"
AND PIN.DATE_COMPLETED IS NULL) as `Type A outstanding`,
(SELECT COUNT(PSN.APP_UID)
FROM PMT_SIGN_NORMAL PSN
WHERE
PSN.APP_STATUS = "To_Do"
AND PSN.DATE_COMPLETED IS NULL) AS `Type B outstanding`
Yes a sub query could do this. It would look something like the following
SELECT (SELECT COUNT(PIN.APP_UID)
FROM PMT_INSP_NORMAL PIN
WHERE PIN.APP_STATUS = 'To_Do' AND PIN.DATE_COMPLETED IS NULL
) AS 'Type A outstanding'
,
(SELECT COUNT(PSN.APP_UID)
FROM PMT_SIGN_NORMAL PSN
WHERE PSN.APP_STATUS = 'To_Do' AND PSN.DATE_COMPLETED IS NULL
) AS 'Type B outstanding'
Related
I have used some sub query , its not working at all ,if i am giving hardcoded id its working .
SELECT
(
SELECT
COUNT(*)
FROM
inventory_set_variations isv
WHERE
isv.c_catid LIKE '%[{"id":"c1.catid"}]%' AND isv.company_id = 1
) AS 'count_total_no_of_variations',
`c1`.`catid` AS `catid`
FROM
category c1
WHERE
(
`c1`.`catsid` <> 3 AND c1.company_id = '1'
)
this is not working ,
below code is working as i gave hard coded data
SELECT
(
SELECT
COUNT(*)
FROM
inventory_set_variations isv
WHERE
isv.c_catid LIKE '%[{"id":"1000020"}]%' AND isv.company_id = 1
) AS 'count_total_no_of_variations',
`c1`.`catid` AS `catid`
FROM
category c1
WHERE
(
`c1`.`catsid` <> 3 AND c1.company_id = '1'
)
You can use CONCAT to form like condition
Change
LIKE '%[{"id":"c1.catid"}]%'
to
LIKE CONCAT('%[{"id":"',c1.catid,'"}]%')
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.
I've looked at a bunch of articles on here but I can't find quite what I'm looking for
SELECT SUM(adult) as adult_sold_1 WHERE showtime = '1'From tickets
Everything here works but when I try to use WHERE the entire thing stops working...
What's wrong with this?
EDIt:
Also is there a way to do multiple at once??
Like
SELECT SUM(adult) as adult_sold_1 From tickets WHERE showtime = '1'
then the same thing with student in spot of adult?
You have no FROM (table) which is causing you to have an error
SELECT SUM(adult) as adult_sold_1 FROM TABLE_NAME WHERE showtime = '1'
SELECT query should be like:
SELECT `field_name` FROM `table_name`
On having WHERE clause:
SELECT `field_name` FROM `table_name` WHERE `field_name` = 'some_value'
In your query, FROM is not written & and is not properly placed. It should be like:
SELECT SUM(`adult`) as adult_sold_1 FROM `tickets` WHERE `showtime` = '1'
SUM() Syntax:
SELECT SUM(`column_name`) FROM `table_name` WHERE condition;
Multiple SUM() Example:
Ex 1:-
SELECT SUM(CASE WHEN `item` = 'ABC' THEN `price` END) as ABC_price,
SUM(CASE WHEN `item` = 'XYZ' THEN `price` END) as XYZ_price
FROM `item_table`
Ex 2:-
SELECT SUM(`price`) as total_price, SUM(`quantity`) as total_quantity FROM `item_table`
SELECT SUM(adult) as adult_sold_1
From tickets
WHERE `showtime` = 1
if your showtime is integer I think it doesn't need ' '
if your showtime is string use LIKE instead of =
WHERE `showtime` like '1'
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)
The query below does exactly what I expect it to do, is intuitive and doesn't generate intermediary tables. The downside is that it takes a long time to complete.
What I'll do in such cases is break the query in steps and create those intermediary tables & indexes. This time around I'd like to get a better handle on the hints provided by explain, and would appreciate any pointers: what obvious optimization steps am I missing in the query below?
Following the advice in MySQL query optimization and EXPLAIN for a noob I've created indices on order_number , order_type and item in orders_raw. It's unclear however how these would carry over character processing/regexes.
SELECT bundle_headers.order_number , bundle_headers.title , digital_subs.subscription_id , 1 as bundle_component
from
(
select order_number , substring( item , 1 , 3 ) as title , quantity from orders_raw
where order_type in (4,6)
) bundle_headers
inner join
(
select order_number , subscription_id , item as title , quantity from orders_raw
where order_type = 0 and length( item ) = 4
) digital_subs
on bundle_headers.order_number = digital_subs.order_number and
digital_subs.title regexp concat( '.*' , bundle_headers.title , '.*' ) and
bundle_headers.quantity = digital_subs.quantity
UNION
SELECT bundle_headers.order_number , bundle_headers.title , print_subs.subscription_id , 1 as bundle_component
from
(
select order_number , substring( item , 1 , 3 ) as title , quantity from orders_raw
where order_type in (4,6)
) bundle_headers
inner join
(
select order_number , subscription_id , item as title , quantity from orders_raw
where order_type = 0 and length( item ) = 3
) print_subs
on bundle_headers.order_number = print_subs.order_number and
print_subs.title regexp concat( '.*' , bundle_headers.title , '.*' ) and
bundle_headers.quantity = print_subs.quantity;
EDIT, #tin tran: I've yet to rigorously time both the query above and your query (after a couple corrections, copied below) starting out on an idle machine. I did submit it, and didn't see an obvious reduction in run time.
SELECT bundle_headers.order_number,
substring(bundle_headers.item,1,3) as title,
subs.subscription_id,
1 as bundle_component
FROM orders_raw bundle_headers
INNER JOIN orders_raw subs ON (bundle_headers.order_number = subs.order_number)
WHERE (bundle_headers.order_type = 4 OR bundle_headers.order_type = 6)
AND subs.order_type = 0
AND bundle_headers.quantity = subs.quantity
AND subs.item LIKE CONCAT('%',substring(bundle_headers.item,1,3),'%')
AND (length(subs.item) = 4 OR length(subs.item) = 3)
please try this query see if it produces the same result. And if it's any faster
SELECT bundle_headers.order_number,substring(bundle_headers.title,1,3) as title,subs.subscription_id,1 as bundle_component
FROM order_type bundle_headers
INNER JOIN orders_raw subs ON (bundle_headers.order_number = subs.order_number)
WHERE (bundle_headers.order_type = 4 OR bundle_headers.order_type = 6)
AND subs.order_type = 0
AND bundle_headers.quantity = subs.quantity
AND subs.title LIKE CONCAT('%',substring(bundle_headers.title,1,3),'%')
AND (length(subs.item) = 4 OR length(subs.item) = 3)