Split column conditionally - mysql

I want to split a column based on if a condition is true or false and count the number of patients in each category for each doctor
This is my code:
SELECT p.MRP_CP_ID, COUNT(p.PTNT_ID) FROM PATIENT p
JOIN PATIENT_SCHEDULE ps ON ps.PTNT_ID = p.PTNT_ID
WHERE ps.MLTPL_PHRM_FLG = 0
GROUP BY MRP_CP_ID;
But i want to display the count of patients for where the MLTPL_PHRM_FLG is 1 in another column. Is there an easy way to do this?
current result from that displayed here

This can be done with conditional aggregation.
SELECT p.MRP_CP_ID,
COUNT(*) Total,
COUNT(CASE WHEN ps.MLTPL_PHRM_FLG = 0 then 1 END) as PHRM_FLG_0_Counts,
COUNT(CASE WHEN ps.MLTPL_PHRM_FLG = 1 then 1 END) as PHRM_FLG_1_Counts
FROM PATIENT p
JOIN PATIENT_SCHEDULE ps ON ps.PTNT_ID = p.PTNT_ID
GROUP BY MRP_CP_ID;

Related

How to run two SQL queries together?

I have two sql queries I want to run at once so I don't have to run twice to get the result
SELECT COUNT(*) FROM attendance WHERE month =10 and grade = 4
SELECT COUNT(*) from attendance WHERE month = 10 and grade = 4 AND userid = 24 and attendance = 'present'
I want two counts of total class and total number of classes in which student is present.
You can combine the second criteria using conditional aggregation, this way the table is only read once.
select
Count(*) as TotalCount,
Count(case when userid = 24 and attendance = 'present' then 1 end) as StudentCount
from attendance
where month = 10 and grade = 4;

MYSQL GROUP BY 2 field with condition

need help
I have a table:
user | visit |....
user = 1,2,3,4
visit = 1,2,3,4
QUERY will work, but i want to agragate fields in MYSQL
SELECT COUNT(visit) as c, t.visit, t.user FROM t GROUP BY t.visit, t.user
I need return GROUP BY user fields without duplicate rows in filed user:
t.user, COUNT (t.visit where t.visit=1), COUNT( all t.visit)
user = 1 | visit1 = 10|. visit total = 100
......
To conditionally count a column, count the result of a CASE expression...
SELECT
t.user,
COUNT(CASE WHEN t.visit = 1 THEN 1 END) AS visit_1,
COUNT(*) AS visit_total
FROM
t
GROUP BY
t.user
Notes:
if a CASE expression doesn't have an ELSE clause, it defaults to ELSE NULL.
COUNT() only counts values that are NOT NULL.

Count Case Statement - When One Field Greater Than Another

I'm trying to determine how pervasive a particular mistake is in my database. I'm comparing one field against another, and when that field is greater then the other, I want it to count it. I'm also grouping it by a different statement. The purpose of this query is to determine where there are cases in my data base when one price field is larger then another.
The part of the query that is causing problems is "COUNT(CASE when p.IMAP > p.MSRP = 1 ELSE NULL END)" in the select statement. I put two little stars around it, hoping that'd help highlight where it is.
select b.brandName, b.BrandCode, p.ProductVendorStockNumber, **COUNT(Case When p.IMAP > p.MSRP = 1 ELSE NULL END) as 'Count'**
from products p
join brands b on p.brandID = b.brandID
where b.assignedTo = 'Steve' and p.IMAP > p.MSRP and status = 1
GROUP BY b.BrandName
For the count value You could use sum instead of count adding 1 when the condition is true and 0 when false
In sql for aggregated select the select for columns not in aggregated function and not mentioned in group by is deprecated, in the most recent version of mmysql is not allowed and for the older version the result for these values in unpredicatble so you should in group by then column that you have not in aggregation function in select eg:
select b.brandName
, b.BrandCode
, p.ProductVendorStockNumber
,sum(Case When p.IMAP > p.MSRP THEN 1 ELSE 0 END) as my_count
from products p
join brands b on p.brandID = b.brandID
where b.assignedTo = 'Steve' and p.IMAP > p.MSRP and status = 1
GROUP BY b.BrandName, b.BrandCode, p.ProductVendorStockNumber
or filter the result using the rows without aggregation and a join on the right aggregated rows

Finding difference of two columns with where clause - MySQL

I'm trying to get the difference of two columns(OTHER AND SENIOR) with WHERE clause along with the Sum of their amount. The formulas are working fine, however, I'm not getting the result I want.
I tried to do it using INNER JOIN and unfortunately its not the result that I expected
Here's my query:
select a.ID as EMPLOYEE,
sum(a.amount) as other,
sum(b.amount) as senior,
(sum(a.amount) - sum(b.amount)) as result
from gndsale a
INNER join gndsale
b ON a.ID= b.ID
where a.TYPE = "5" and b.seniortype = "10"
group by a.ID
Result:
What I want to do is get the difference of Column (Other) where type = 5 and Column (Senior) where seniortype = 10
Here is my query for Senior:
select ID as EMPLOYEE, sum(amount)
from gndsale
where seniortype = "10"
group by ID
Result:
Here is my query for Other:
select ID as employee, sum(amount)
from gndsale
where type = "5"
group by ID
The result should be
9907 = 530
9912 = 63.71
Anyone can help me with this? :(
Sample/Expected output:
You have chosen a very bad name for the column. In spite of its name ID is not an ID to identify a record in the table.
You are probably looking for something like the following, an aggregation per ID where you sum type 5 and seniortype 10 separately:
select
a.ID as employee,
coalesce(sum(case when type = 5 then amount end), 0) as other,
coalesce(sum(case when seniortype = 10 then amount end), 0) as senior,
coalesce(sum(case when type = 5 then amount end), 0) -
coalesce(sum(case when seniortype = 10 then amount end), 0) as result
from gndsale
group by a.id
having sum(type = 5) > 0 and sum(seniortype = 10) > 0;
(The HAVING clause ensures only to get IDs that have both records with type = 5 and seniortype = 10. We use MySQL's true = 1 / false = 0 here. Remove it, if you want other IDs, too. If you keep it and value cannot be null, then you can remove the coalesces instead.)
You might give this a try:
SELECT
a.`employee`,
sum(a.`amount`) - sum(b.`amount`) as total
FROM `gndsale` a
JOIN `gndsale` b
ON a.`employee` = b.`employee` and b.`senior_type` = "10"
WHERE a.`type` = "5"
GROUP BY a.`employee`
ORDER BY a.`employee`;
It simply joins your two queries.
Please try this query
select employee, other, senior, (other-senior) AS result
From (
select ID as employee,
sum(if(type=5,amount,0)) AS other,
sum(if(seniortype =10,amount,0)) AS senior
from gndsale group by ID
) a

MySQL count with a range condition

I have a query like this
select newscategory.CategoryName, count(distinct newsmain.id)
from newsmain join newscategory on
newscategory.CategoryName = newsmain.Category
group by CategoryName
and it is returning a correct results, like this:
CategoryName count(distinct newsmain.id)
Acupunctura 1
Neurologie 1
Test Category 2
"newsmain" table has an "AppPublished" datetime field and what I'm trying to do is to add a condition to the count which will do the counting based on if that "AppPublished" is in the range of two datetime variables. For an example, I would need a result like this:
CategoryName count(distinct newsmain.id)
Acupunctura 0
Neurologie 0
Test Category 1
Do I need to make a subquery or is there a way to add some condition to this query?
Because any added conditions in this query are resulting in unwanted filtering of the "CategoryName" column.
You can use a CASE condition like
select newscategory.CategoryName,
count(CASE WHEN AppPublished BETWEEN date1 and date2 THEN distinct newsmain.id END)
from newsmain join newscategory on
newscategory.CategoryName = newsmain.Category
group by CategoryName
(OR) like this
select newscategory.CategoryName,
sum(CASE WHEN AppPublished BETWEEN date1 and date2 THEN 1 ELSE 0 END)
from newsmain join newscategory on
newscategory.CategoryName = newsmain.Category
group by CategoryName