sql query to get count of records - sql-server-2008

I have "Result" column in my table. Result column contains one of the below three values,
Pass, Fail, Incomplete.
I need to write a query which gives below three results
1. total count of passed records
2. total count of failed records
3. total count of incomplete records
Is it possible? If yes, please guide me to write it. Thanks in advance.

SELECT COUNT(CASE result WHEN 'Pass' THEN 1 END) AS passed,
COUNT(CASE result WHEN 'Failed' THEN 1 END) AS failed,
COUNT(CASE result WHEN 'Incomplete' THEN 1 END) AS incomplete
FROM mytable

Related

Get total from database query

Does anyone know on how can I execute a query on how can I get the total quantity by making a transaction using type (add/subtract) column.
As you can see, raw_material_id=1 first transaction is 200.00.
Second transaction subtract 0.025,
so it should be 200.00-0.025=total quantity
This is a job for SUM() and CASE/WHEN/THEN.
SELECT SUM(CASE WHEN type='add' THEN quantity
WHEN type='subtract' THEN -quantity
ELSE 0 END) total
FROM mytable

Subquery returns more than 1 row in like %..%

i am working with bank transaction table witch contain 67000 transactions for around 800 customer from January to October so what i want is to calculate number of transactions for each customer per month i want the final result will be like cust_id , number of transaction for jan ,number of transaction for Feb ... til October i tryed this query
SELECT
cut_id,
CASE WHEN tra_date LIKE '%Oct%'
THEN
(select count(tra_date) from tab where tra_date like '%Oct%' GROUP by cut_id)
ELSE 0
END AS oct,
CASE WHEN tra_date LIKE '%Sep%'
THEN
(select count(tra_date) from tab where tra_date like '%Sep%' GROUP by cut_id)
ELSE 0
END AS sept
FROM tab
GROUP by cut_id
but i found this error #1242 - Subquery returns more than 1 row
I'm going to assume you have a sane schema that uses actual an actual datetime type for your transaction dates, rather than the apparent insane schema in the question that uses strings. Given that, your query would look like this:
SELECT
cut_id,
SUM(CASE WHEN month(tra_date) = 10 THEN 1 ELSE 0 END) AS Oct,
SUM(CASE WHEN month(tra_date) = 9 THEN 1 ELSE 0 END) AS Sept
FROM tab
GROUP by cut_id
In this section
(select count(tra_date) from tab where tra_date like '%Oct%' GROUP by cut_id)
When you do a count with a group by, and there are multiple values for the thing you are grouping by, you get multiple values. Based on your requirements, you need to choose which single value you want.
Maybe you need to do this instead
(select count(tra_date) from tab t1 where t1.tra_date like '%Oct%' and t1.cut_id = tab.cut_id)

Query - To display the Failure rate %

I have written this query to get my data, and all the data is fine.
I have one column which has either Pass Or Fail. I want to calculate the % of number of bookings that failed, and output it in a single value.
I will have to write another query to show that one number.
For example : The below data, I have 4 bookings , out which 2 failed. So 50% is the failure rate. I am omitting some columns , in the display, but can be seen in the query.
That's an aggregation over all records and simple math:
select count(case when decision = 'Fail' then 1 end) / count(*) * 100
from (<your query here>) results;
Explanation: COUNT(something) counts non null values. case when decision = 'Fail' then 1 end is 1 (i.e. not null) for failures and null otherwise (as null is the default for no match in CASE/WHEN &dash; you could as well write else null end explicitly).
Modify your original condition to the following. Notice that there is no need to wrap your query in a subquery.
CONCAT(FORMAT((100 * SUM(CASE WHEN trip_rating.rating <= 3 AND
(TIMESTAMPDIFF(MINUTE,booking.pick_up_time,booking_activity.activity_time) -
ROUND(booking_tracking_detail.google_adjusted_duration_driver_coming/60)) /
TIMESTAMPDIFF(MINUTE,booking.pick_up_time,booking_activity.activity_time)*100 >= 15
THEN 1
ELSE 0
END) / COUNT(*)), 2), '%') AS failureRate
This will also format your failure rate in the format 50.0%, with a percentage sign.

Query to retrieve values form DB on multiple criteria

I Need to retrieve values from database to plot them in graph. For that I need to get values on criteria basis. Data matching different criteria has to be returned as different rows/ column to my query
(i.e)
I have a table called TABLEA which has a column TIME. I need to get the value based on time critreia as a result, count of rows which are matching TIME>1 and TIME<10 as a result, TIME>11 and TIME <20 as a result and so on. Is it possible to get the values in a single query. I use Mysql with JDBC.
I should plot all the counts in a graph
Thanks in advance.
select sum(case when `time` between 2 and 9 then 1 else 0 end) as count_1,
sum(case when `time` between 12 and 19 then 1 else 0 end) as count_2
from your_table
This can be done with CASE statements, but they can get kind of verbose. You may just want to rely on Boolean (true/false) logic:
SELECT
SUM(TIME BETWEEN 1 AND 10) as `1 to 10`,
SUM(TIME BETWEEN 11 and 20) as `11 to 20`,
SUM(TIME BETWEEN 21 and 30) as `21 to 30`
FROM
TABLEA
The phrase TIME BETWEEN 1 AND 10) will either returnTRUEorFALSEfor each record.TRUEbeing equivalent to1andFALSEbeing equivalent to0`, we then only need sum the results and give our new field a name.
I also made the assumption that you wanted records where 1 <= TIME <= 10 instead of 1 < TIME < 10 which you stated since, as stated, it would drop values where the TIME was 1,10,20, etc. If that was your intended result, then you can just adjust the TIME BETWEEN 1 AND 10 to be TIME BETWEEN 2 AND 9 instead.

Two rows of data combined into one

A beginner in SQL but given the opportunity to create my own database.
So I have a table Invoice with similar reference numbers but different status code numbers.
Table Invoice:
Reference Status Code Status Date
10198053 300 08/07/2013
10198053 500 08/09/2013
I would like the output to show:
Table Invoice:
Reference Status Code Status Date Status Date 2
10198053 300 08/07/2013 08/09/2013
Code:
select reference r, status Code s, status Date,
case
when r=r and s=s (???)
from Table Invoice
Assuming that you need to see a date for 300 and a date for 500, you could aggregate two columns based on each of those Status_Code:
SELECT
Reference,
MIN(CASE WHEN Status_Code = '300' THEN Status_Date ELSE NULL END) AS Status_Date_300,
MIN(CASE WHEN Status_Code = '500' THEN Status_Date ELSE NULL END) AS Status_Date_500
FROM Invoice
GROUP BY Reference;
As is indicated in the comments, this could change depending on the requirements of exactly what you are trying to find - however, this should get you started.
SQL Fiddle to test the above.
EDITED
Try this:
SELECT Invoice.reference, MIN(Invoice.statusCode) AS statusCode, MIN(Invoice.statusDate) AS statusDate, MAX(Invoice.statusDate) AS statusDate2
FROM Invoice
GROUP BY Invoice.reference;
GroupBy your reference number to get one instance of it on your query. Then use the MIN and MAX function to get the first and last instance of statusDate on a specific reference.