When i select data only show first lot data . but i need all lot data.
Here is my query:
SELECT lot,
(select count(pass) FROM pass_fail_result where pass=0) toatl_fail,
(select count(pass) FROM pass_fail_result where pass=1) toatl_pass FROM pass_fail_result group by lot;
I want to show all pass result like pass=10 and fail=2
The easiest way to do this is via conditional aggregation, where we count or sum CASE expressions which target the failing or passing records:
SELECT
lot,
COUNT(CASE WHEN pass = 0 THEN 1 END) AS toatl_fail,
COUNT(CASE WHEN pass = 1 THEN 1 END) AS toatl_pass
FROM pass_fail_result
GROUP BY
lot;
Related
I am currently developing a system which generates various reports for a client and i need pointing in the direction in-order to achieve what i need.
The current situation is that i need to produce an overview of tasks which are outstanding (and some details about what is remaining).
So far i have created a list of all tasks so far (including information about the customer), this was achieved in the following query:
SELECT `fusion_repairs`.*, `fusion_customers`.*
FROM `fusion_repairs`
RIGHT JOIN `fusion_customers`
ON `fusion_repairs`.`customer_id` = `fusion_customers`.`customer_id`
ORDER BY `fusion_repairs`.`repair_id`
DESC LIMIT 20
Ideally i need to be able to check to see if the customer has been emailed about the task (fusion_mail table). If there are no mail records found for this task then i still want to show it (so another right join is out of the question.
I also need to do the same with the fusion_response table to check to see if the customer has sent in a response. If they haven't then i still want to display the task anyway.
Obviously i know i can achieve this by running a query within the main select query, but this doesn't sit well in my head. Is there a way i can do this in 1 query instead?
Thank you.
Change your query structure to use left joins only - essentially the same as a right join but then not having to swap between left and right join.
The fact you right-joined customers onto repairs tells me customers is the main table so now we can say get me everything from customers, join in repairs if a record exists.
SELECT `fusion_repairs`.*, `fusion_customers`.*
FROM `fusion_customers`
LEFT OUTER JOIN `fusion_repairs` ON `fusion_repairs`.`customer_id` = `fusion_customers`.`customer_id`
ORDER BY `fusion_repairs`.`repair_id`
DESC LIMIT 20
Here we can use left outer join to see if we have any mail, if not, do not remove the row and the same with response
SELECT `fusion_repairs`.*, `fusion_customers`.*
FROM `fusion_customers`
LEFT OUTER JOIN `fusion_repairs` ON `fusion_repairs`.`customer_id` = `fusion_customers`.`customer_id`
LEFT OUTER JOIN `fusion_mail` ON `fusion_mail`.`repair_id` = `fusion_repairs`.`repair_id`
LEFT OUTER JOIN `fusion_response` ON `fusion_response`.`repair_id` = `fusion_repairs`.`repair_id`
ORDER BY `fusion_repairs`.`repair_id`
DESC LIMIT 20
Now my question is.. What do you want to be returned? If there are 5 mail records then there will be 5 records for the 1 repair record. Do you just want a yes/no tick just to say they have then maybe a correlated subquery in the main select is actually what you want or a group by and count the number of mail inside a case to tick.
Let me know what you want as your output to finish the query up.
EDIT:
I've made an update to the query to take into account the information you gave me. Please note I have no idea how you want to output the final result so I just made a guess! Let me know if this solves it otherwise back to the drawing board for me!
SELECT fusion_repairs.*,
fusion_customers.*,
CONCAT( CASE WHEN fusionMail.mail_type_A > 0 THEN 'displayA' ELSE '' END,
CASE WHEN fusionMail.mail_type_B > 0 THEN 'displayB' ELSE '' END,
CASE WHEN fusionMail.mail_type_C > 0 THEN 'displayC' ELSE '' END) as email_font_awesome_icon,
CONCAT( CASE WHEN fusionResponse.response_status_A > 0 THEN 'displayA' ELSE '' END,
CASE WHEN fusionResponse.response_status_B > 0 THEN 'displayB' ELSE '' END,
CASE WHEN fusionResponse.response_status_C > 0 THEN 'displayC' ELSE '' END) as response_font_awesome_icon
FROM fusion_customers
LEFT OUTER JOIN fusion_repairs ON fusion_repairs.customer_id = fusion_customers.customer_id
LEFT OUTER JOIN (
SELECT repair_id,
SUM(CASE WHEN mail_type = 1 THEN 1 else 0 END) AS mail_type_A,
SUM(CASE WHEN mail_type = 2 THEN 1 else 0 END) AS mail_type_B,
SUM(CASE WHEN mail_type = 3 THEN 1 else 0 END) AS mail_type_C
FROM fusion_mail
GROUP BY repair_id
) fusionMail ON fusionMail.repair_id = fusion_repairs.repair_id
LEFT OUTER JOIN (
SELECT repair_id,
SUM(CASE WHEN response_status = 1 THEN 1 else 0 END) AS response_status_A,
SUM(CASE WHEN response_status = 2 THEN 1 else 0 END) AS response_status_B,
SUM(CASE WHEN response_status = 3 THEN 1 else 0 END) AS response_status_C
FROM fusion_response
GROUP BY repair_id
) fusionResponse ON fusionResponse.repair_id = fusion_repairs.repair_id
I'm working with the join plus union plus group by query, and I developed a query something like mentioned below:
SELECT *
FROM (
(SELECT countries_listing.id,
countries_listing.country,
1 AS is_country
FROM countries_listing
LEFT JOIN product_prices ON (product_prices.country_id = countries_listing.id)
WHERE countries_listing.status = 'Yes'
AND product_prices.product_id = '3521')
UNION
(SELECT countries_listing.id,
countries_listing.country,
0 AS is_country
FROM countries_listing
WHERE countries_listing.id NOT IN
(SELECT country_id
FROM product_prices
WHERE product_id='3521')
AND countries_listing.status='Yes')) AS partss
GROUP BY id
ORDER BY country
And I just realised that this query is taking a lot of time to load results, almost 8 seconds.
I was wondering if there is the possibility to optimize this query to the fastest one?
If I understand the logic correctly, you just want to add a flag for the country as to whether or not there is a price for a given product. I think you can use an exists clause to get what you want:
SELECT cl.id, cl.country,
(exists (SELECT 1
FROM product_prices pp
WHERE pp.country_id = cl.id AND
pp.product_id = '3521'
)
) as is_country
FROM countries_listing cl
WHERE cl.status = 'Yes'
ORDER BY country;
For performance, you want two indexes: countries_listing(status, country) and
product_prices(country_id, product_id)`.
Depending on how often it is executed, prepared statements could help. See PDO for more information.
in below query it returns 12 record while query in from clause (as t) returns 18 record, can anyone help what is the issue in this query?
SELECT count(abc.id) as total_this_month,t.*
FROM email_details abc
JOIN
(SELECT count(email_details.id) as total_emails,MAX(`email_details`.email_date) as email_date1, `email_details`.* FROM (`cld_users` join email_details on email_details.fk_user_id = cld_users.id) GROUP BY `email_details`.`email_title` ORDER BY `email_details`.`email_date` DESC) as t
ON abc.email_title = t.email_title
where (MONTH(abc.email_date)=MONTH(NOW()) AND YEAR(abc.email_date)=YEAR(NOW()))
group by t.email_title
ORDER BY t.`email_date` DESC
In your query, you specify
where (MONTH(abc.email_date)=MONTH(NOW()) AND YEAR(abc.email_date)=YEAR(NOW()))
But in the subquery (the one returning 18 results), you have 6 emails with a month that is not december 2014. There's no way that those emails can be returned by a query that explicitly excludes them.
You want those emails as well so you get 18 results ? Remove the WHERE clause excluding them:
SELECT Count(abc.id) AS total_this_month,
t.*
FROM email_details abc
JOIN (SELECT Count(email_details.id) AS total_emails,
Max(`email_details`.email_date) AS email_date1,
`email_details`.*
FROM (`cld_users`
JOIN email_details
ON email_details.fk_user_id = cld_users.id)
GROUP BY `email_details`.`email_title`
ORDER BY `email_details`.`email_date` DESC) AS t
ON abc.email_title = t.email_title
GROUP BY t.email_title
ORDER BY t.`email_date` DESC
Starting from there, if you want to count the emails from the current month, simply replace:
SELECT Count(abc.id) AS total_this_month,
with
SELECT SUM(CASE WHEN MONTH(abc.email_date)=MONTH(NOW()) AND YEAR(abc.email_date)=YEAR(NOW()) THEN 1 ELSE 0 END) AS total_this_month,
The scenario is I have a column named "States" in a table, States can be 0,1,2,3 or 4. What I want to do is get a count of each state using WHERE State = in a single query.
The main purpose is I want to show the count of records (identified by their state). Like this, 20 records have State 0 etc.
Is this possible? If yes, then how can I achieve this?
Edit: I know about Count. Here's what I have tried:
SELECT State, Date_Created, (SELECT COUNT(Id) FROM [ECOS].[eco].[tb_projects_details] WHERE State=1) as State_One, (SELECT COUNT(Id) FROM [ECOS].[eco].[tb_projects_details] WHERE State=2) as State_Two, (SELECT COUNT(Id) FROM [ECOS].[eco].[tb_projects_details] WHERE State=0) as State_Zero, (SELECT COUNT(Id) FROM [ECOS].[eco].[tb_projects_details] WHERE State=4) as State_Four FROM [ECOS].[eco].[tb_projects_details] WHERE Date_Created < dateadd(week,-3,getdate());
If I understand it correctly, you want to group and count:
SELECT mt.States, COUNT(*) total
FROM my_table mt
GROUP BY mt.States
To generate a list of values and their counts you can do this:
SELECT State, COUNT(*) AS C
FROM mytable
GROUP BY State
To generate one row that contains value counts as columns you can do this:
SELECT
COUNT(CASE State WHEN 0 THEN 1 ELSE NULL END) AS State_0_Count,
COUNT(CASE State WHEN 1 THEN 1 ELSE NULL END) AS State_1_Count,
COUNT(CASE State WHEN 2 THEN 1 ELSE NULL END) AS State_2_Count,
COUNT(CASE State WHEN 3 THEN 1 ELSE NULL END) AS State_3_Count,
COUNT(CASE State WHEN 4 THEN 1 ELSE NULL END) AS State_4_Count
FROM [...]
WHERE [...]
It will be very easy if you use group by clause after where condition. Then you will get the number of each state very easily.
SELECT state, count(*)
FROM table_name
GROUP BY state
Next time read a sql for beginers book first.
Select state, count(*)
from table
group by state
I have two queries:
SELECT opr, COUNT(*) FROM table WHERE field = 'YES' GROUP BY opr
SELECT opr, MAX(category) FROM table WHERE field = 'NO' GROUP BY opr
So basically in the first query I am getting the number of transactions a user makes.
In the second query I am getting a category that all of those transactions fall under. I don't want to get all categories for each transaction they made, just the Max of the category field so that I have one entry per operator.
Until now, I have been catching both result sets in separate arrays and the looping through both arrays to get the complete opr->picks->category.
This doesn't always work it sometimes associates the wrong category to the wrong operator.
Is there a way to combine these two queries into one, so that I get the operator and picks, then the MAX(category)? The issue is that the conditions for each query are different on the field column.
SELECT opr,
COUNT(CASE WHEN field = 'YES' THEN 1 END) AS number_of_transactions,
MAX(CASE WHEN field = 'NO' THEN category END) AS category
FROM table
GROUP BY opr ;
The above logic can be attained in one query by using CASE
SELECT opr, COUNT(CASE WHEN `field` = 'YES' THEN opr END) as `count`,
MAX(CASE WHEN `field` = 'NO' THEN category END) as `max`
FROM table GROUP BY opr
Just add it in:
SELECT opr,count(*), MAX(category) FROM table WHERE field = 'YES' GROUP BY opr
You might also be able to drop it from 2 queries to 1 with this:
SELECT opr,count(*), MAX(category), field FROM table GROUP BY opr, field
even faster on the processor, use a sum bool:
select
opr,
sum(field='yes') as transactions,
max(if(field='no',category, null)) as category
from table
group by opr