Need help DB Query for this scenario - mysql

I am unable to derive a SQL query for the following table content.
When i tried below query i am getting above said output. Can someone help me to give the required query for it.
select Name, count(Status) from mytable where Status='Open' group by mytable union
select Name, count(Status) from mytable where Status='Cleared' group by mytable

Use case expressions in the select list to do conditional aggregation.
select Name,
count(case when Status = 'Open' then 1 end) as opencnt,
count(case when Status = 'Cleared' then 1 end) as clearedcnt
from mytable
where Status in ('Open', 'Cleared')
group by Name
COUNT() counts non-null values. The case expressions above returns null when the conditions aren't fulfilled.

Related

Divide one table by another table created in the same query

I have the following SQL query:
SELECT `NeighbourhoodName`,
count(NAME) as `Number of Parks`,
sum(CASE
WHEN `parks`.`Advisories` = 'Y' THEN 1
ELSE 0
END) as Advisories,
FROM parks
GROUP BY `NeighbourhoodName`;
In the second line of the code, I create a column called "Number of Parks". I would like all the values in the next column (Advisories) to be divided by the values in "Number of parks". However, when I try to insert the division statement after the column like this:
SELECT `NeighbourhoodName`,
count(NAME) as `Number of Parks`,
sum(CASE
WHEN `parks`.`Advisories` = 'Y' THEN 1
ELSE 0
END)/`Number of Parks` as Advisories
FROM parks
GROUP BY `NeighbourhoodName`;
I get the following error:
Unknown column, `Number of Parks` in field list.
How can I perform this division while still keeping it in one query?
You need to use an outer query to do the division.
SELECT `NeighbourhoodName`,
`Number of Parks`,
Advisories/`Number of Parks` as Advisories
FROM ( SELECT `NeighbourhoodName`,
count(NAME) as `Number of Parks`,
sum( CASE WHEN `parks`.`Advisories` = 'Y' THEN 1 ELSE 0 END ) as Advisories
FROM parks
GROUP BY `NeighbourhoodName`
) as tbl;
Problems with Column Aliases
An alias can be used in a query select list to give a column a
different name. You can use the alias in GROUP BY, ORDER BY, or HAVING
clauses to refer to the column.
Or use the expression of count(Name) instead of Number of Parks:
select NeighbourhoodName,
count(Name) as `Number of Parks`,
sum(case when Advisories='Y' then 1 else 0 end)
/count(Name) as Advisories
from parks
group by NeighbourhoodName;

SQL query to get percentages within a grouping

I've looked over similar questions and I just can't seem to get this right.
I have a table with three columns: ID, Date, and Method. None are unique.
I want to be able to see for any given date, how many rows match a certain pattern on Method.
So, for example, if the table has 100 rows, and 8 of them have the date "01-01-2020" and of those 8, two of them have a method of "A", I would want a return row that says "01-01-2020", "8", "2", and "25%".
My SQL is pretty rudimentary. I have been able to make a query to get me the count of each method by date:
select Date, count(*) from mytable WHERE Method="A" group by Date;
But I haven't been able to figure out how to put together the results that I am needing. Can someone help me out?
You could perform a count over a case expression for that method, and then divide the two counts:
SELECT date,
COUNT(*),
COUNT(CASE method WHEN 'A' THEN 1 END),
COUNT(CASE method WHEN 'A' THEN 1 END) / COUNT(*) * 100
FROM mytable
GROUP BY date
I'm assuming you're interested in all methods rather than just 'A', so you could do the following:
with ptotals as
(
SELECT
thedate,
count(*) as NumRows
FROM
mytable
group by
thedate
)
select
mytable.thedate,
mytable.themethod,
count(*) as method_count,
100 * count(*) / max(ptotals.NumRows) as Pct
from
mytable
inner join
ptotals
on
mytable.thedate = ptotals.thedate
group by
mytable.thedate,
mytable.themethod
You can use AVG() for the ratio/percentage:
SELECT date, COUNT(*),
SUM(CASE WHEN method = 'A' THEN 1 ELSE 0 END),
AVG(CASE WHEN method = 'A' THEN 100.0 ELSE 0 END)
FROM t
GROUP BY date;

req_type wise count id in one query

I have an sql query problem . I don't want to execute three times query for same result.
In my table I have one field req_type which have three parameter ,
either 1, either 2, either 3 .
I want counter based on req_type in one query instead of by executing query 3 times like below
select count(id) as premium FROM tablename where req_type=1
select count(id) as premium1 FROm tablename where req_type=2
select count(id) as premium2 FROm tablename where req_type=3
I am stuck , can anybody help me?
You could use case for such type of count
select sum(case when req_type=1 then 1 else 0 end) as premium,
sum(case when req_type=2 then 1 else 0 end) as premium1,
sum(case when req_type=3 then 1 else 0 end) as premium2
FROM tablename
Use one query instead of threes by using group by cluase
select req_type , count(id) as premium
FROM tablename
where req_type in (1,2,3)
group by req_type
Use a GROUP BY
SELECT req_type, COUNT(id) AS count_premium
FROM tablename
GROUP BY req_type;

How to Perform Subtraction in SQL query based on some conditions?

I want to sum up qty*price where type is 'in' and then subtract the sum of qty*price where type is 'out'. Something like this.
SELECT cid, name, SUM(paid_amt), (SELECT SUM(qty*price) WHERE type = 'in' - SELECT SUM(qty*price) WHERE type = 'out'), type FROMtransactionsGROUP BY cid
Here's is my SQL query.
You can solve your problem by using two different sum expressions, each with a slightly different case statement inside of it. The idea is that your case statement only returns a value to the aggregate sum expression if it's of the correct typing.
select
cid
, sum(case when type = "in" then qty*price else 0 end)
- sum(case when type = "out" then qty*price else 0 end)
from
your_table
group by
cid

Using sum() to mimic count() of rows

I want to count the number of rows where a particular field = 'Q1'.
I usually use count(particular_field), but this does not allow me to count only when that field = 'Q1'.
Does the query SUM(particular_field = 'Q1') work for this matter? Or am I able to do count(particular_field = 'Q1')?
you can either do
select count(*)
from table
where particular_field = 'Q1'
or
select sum(case when particular_field ='Q1' then 1 else 0 end)
from table
You should be able to use a CASE statement with your SUM() (See SQL Fiddle)
SELECT SUM(CASE WHEN particular_field = 'Q1' THEN 1 ELSE 0 END) yourCount
FROM yourTable
Or (See SQL Fiddle) - this will give you a list of the count and each field. If you only want the one value, then use a WHERE clause to filter:
select count(*), particular_field
from yourTable
group by particular_field