I'm trying to find the minimum date at which patient took a drug with a certain flag. Here is the code I'm using:
create table Schema.Want as select
*, min(case when Flag="True" then Date end) as First_Date
from Schema.Have group by Patient_ID;
I have also tried listing out all the variables, but that didn't help either. When I run that code I get the Error Code 1055 associated with "only_full_group". I've seen others suggesting that I turn this off (which I have NOT tried yet) but I'm concerned about why this error is being thrown in the first place. When I get this error code, does that mean that I have duplicate rows in my table?
I've got Patient_ID(NUM), Drug_ID(NUM), Quantity(NUM), Fill_Date (Date/Char), Flag(CHAR), Comp_Set(CHAR), and Drug_strength(CHAR). So if one patient filled two prescriptions for the same strength and quantity of the same exact drug on the same day, then two of their rows would be identical. That doesn't seem likely to me though.
All I need to do is create a separate column with the oldest date at which a patient was prescribed a certain drug. My code worked using proc sql in SAS, but not when I use MySQL. Thanks for your help in advance!!
You need to remerge the MIN() value back onto the detail records. If you check the log in SAS you will see that it says that it is doing the remerge for you.
create table Schema.Want as
select a.*,b.First_Date
from Schema.Have a
inner join
(select Patient_ID, min(case when Flag="True" then Date end) as First_Date
from Schema.Have group by Patient_ID) b
on a.Patient_ID=b.Patient_ID
;
Use select into:
select
*, min(case when Flag="True" then Date end) as First_Date into Schema.Want
from Schema.Have group by Patient_ID
Also replace * with column names and group by all names
All the values you have in the Select except the min must also be in the group by clause.
create table Schema.Want as select
Patient_ID, min(case when Flag="True" then Date end) as First_Date
from Schema.Have group by Patient_ID;
Note: If you have other numeric variables in the select; you will have to give them an aggregate function (min, max, avg ..) and not include them in the group by .
I recommend doing a select only in SQL before deciding to create the table:
select Patient_ID, min(case when Flag="True" then Date end) as First_Date
from Schema.Have group by Patient_ID;
Related
I have Data which indicates month of Order date and month of Shipment date. I want to convert the records which will show, in each month, what is the count of orders and in same month what is the count of shipments
Because I am a beginner to SQL I could not try any way but this is the expected table.
I want to make this happen with Select statement. Please refer the image for the data by clicking here Data with expected result
Your question text is a bit unspecific but it's tagged "mysql" - so I assume this is what you use.
Something like this would work (please replace with exact column/table names):
SELECT
YEAR(order_date),
MONTH(order_date),
COUNT(order_date) AS order_count,
SUM(CASE WHEN MONTH(order_date) = MONTH(shipment_date) THEN 1 ELSE 0 END) AS shipped_count
FROM orders
GROUP BY YEAR(order_date), MONTH(order_date)
Looks like there is a lack of additional information in your question, but maybe you would need something like this
SELECT Month(order_date) as Month, order_count, shipment_count
SUM (order_count)
FROM orders
GROUP BY order_date;
I have my table
I want to get duplicates from the Name and Status column, count them, and sum values from the Sum column. I want to look like this:
I am new to SQL so that it may be an easy answer, but I can't seem to find a solution.
This is how far I got, but I can't seem to get the count and sum without errors.
SELECT name, COUNT(*) AS recovered
FROM complaints
WHERE status = "Recovered"
GROUP BY name
HAVING COUNT(name) > 0
myQuery
You can do conditional aggregation:
select
name,
sum(status = 'Recovered') recovered,
sum(status = 'Unrecovered') unrecovered,
sum(case when status = 'Recovered' then `sum` end) total_recovered_value,
sum(case when status = 'Unrecovered' then `sum` end) total_unrecovered_value
from mytable
group by name
order by name
Side note: sum is a language keyword, hence not a good choice for a column name.
I'm using MySql 5.7, and I'm getting an error in this query:
SELECT
COUNT(id) AS sales,
SUM(amount) AS total,
created
FROM
`payments`
WHERE status =1
GROUP BY MONTH(created);
if I change GROUP BY MONTH(created)
with:
GROUP BY created
then the error is gone. I dont have access to my.ini, to make changes to sql_mode.
Since you group month-wise, you shold output the months also
SELECT COUNT(id) AS sales,
SUM(amount) AS total,
MONTH(created)
FROM payments
WHERE status = 1
GROUP BY MONTH(created)
Otherwise MySQL has to pick a created value from the group. But you should define what to display.
Another possibility instead of outputting MONTH(created) would be a aggreagtion of the date like min(created) which would output the earliest date of each month.
i have 3 tables
supplier(id_supp, name, adress, ...)
Customer(id_cust, name, adress, ...)
Order(id_order, ref_cust, ref_supp, date_order...)
I want to make a job that counts the number of orders by Supplier, for last_week, last_two_weeks with Talend
select
supp.name,
(
select
count(*)
from
order
where
date_order between sysdate-7 and sysdate
nd ref_supp=id_supp
) as week_1,
(
select
count(*)
from
order
where
date_order between sysdate-14 and sysdate-7
nd ref_supp=id_supp
) as week_2
from supplier supp
the resaon for what i'm doing this, is that my query took to much time
You need a join between supplier and order to get supplier names. I show an inner join, but if you need ALL suppliers (even those with no orders in the order table) you may change it to a left outer join.
Other than that, you should only have to read the order table once and get all the info you need. Your query does more than one pass (read EXPLAIN PLAN for your query), which may be why it is taking too long.
NOTE: sysdate has a time-of-day component (and perhaps the date_order value does too); the way you wrote the query may or may not do exactly what you want it to do. You may have to surround sysdate by trunc().
select s.name,
count(case when o.date_order between sysdate - 7 and sysdate then 1 end)
as week_1,
count(case when o.date_order between sysdate - 14 and sysdate - 7 then 1 end)
as week_2
from supplier s inner join order o
on s.id_supp = o.ref_supp
;
I am querying a mysql table and want results group by date, and one column name is type. There are two value for the type call and email. I want to find count for call and email for each day.
Find the SQL Fiddle here
I am trying with this query. Which only gets me total counts:
SELECT Date(date) date,
COUNT(type) total,
COUNT(type='email') emails,
COUNT(type='call') calls
from leads
where user_id = 1
GROUP BY Date(date)
Use SUM() instead. The type='email' in the function returns either 0 (false) or 1 (true).
SELECT Date(date) date,
COUNT(type) total,
SUM(type='email') emails,
SUM(type='call') calls
from leads
where user_id = 1
GROUP BY Date(date)