My data:
How can I get there? I tried to make a MySQL view on my table TAX.
CREATE VIEW TaxSplitPerOrder AS
SELECT
OrderNumber, tax_invoiced, total_invoiced, created_at, tax_percent, SUM(tax_invoiced_order_item)
FROM
test
GROUP by OrderNumber;
Can anyone help me?
You need to include all of the other non-summarized columns from your SELECT in the GROUP BY clause. You may want to sum total_invoiced too or else you will still have 2 rows for each order number.
The query:
SELECT OrderNumber, tax_invoiced, total_invoiced, created_at,
MAX(CASE WHEN tax_percent = 21 THEN tax_invoiced_order_item END) tax_21,
MAX(CASE WHEN tax_percent = 6 THEN tax_invoiced_order_item END) tax_6
FROM test
GROUP BY OrderNumber;
Related
I have a database of Time Entries which stores userID, roundedHours, dateSpent, and taskID for each entry (these are daily timecards). I am trying to create one query that will output sums of roundedHours grouped by userID where the taskID meets a specific value between specified dates.
The code I've attempted is below. I need it to be output and grouped by userID but instead, my output is the total sum for both overhead_admin and total_hours (same on each row) and not specific to the userID.
SELECT userName, userID,
(SELECT sum(roundedHours)
FROM `db`.`TimeEntries`
WHERE taskId = 3050483 AND (spendDate BETWEEN '2020-11-02' AND '2020-11-08')) as overhead_admin,
(SELECT sum(roundedHours)
FROM `db`.`TimeEntries`
WHERE (spendDate BETWEEN '2020-11-02' AND '2020-11-08')) as total_hours
FROM `db`.`TimeEntries`
GROUP BY userID
Can anyone help me so that each row will have the total SPECIFIC to that user and NOT summed as a whole? The output I'm getting that is wrong is shown in the image below:
Use conditional aggregation:
SELECT userName, userID,
sum(case when taskId = 3050483 AND (spendDate BETWEEN '2020-11-02' AND '2020-11-08' then roundedHours end) as overhead_admin,
sum(case when spendDate BETWEEN '2020-11-02' AND '2020-11-08' then roundedHours end) as total_hours
FROM `db`.`TimeEntries`
GROUP BY userID
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 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;
Here's a certain part of my SQL query:
SELECT pir.paymentInstrumentItemID AS id,
SUM(pir.priceChange) AS priceSum,
pir.clientInfoID AS clientID,
IFNULL(SUM(pir.costChange), 0) AS selfPrice,
pir.paymentInstrumentItemID
FROM paymentInstrumentRegister as pir
GROUP BY pir.paymentInstrumentItemID
What I need to do is to count the amount of rows that got grouped by inside the first SUM function and make a CASE statement out of it, so something like that:
SUM(CASE WHEN amtRows = 1 THEN 0 ELSE pir.priceChange) as priceSum
So basically whenever there is a single row I don't need the value, but as long as there's two or more of those, I need their sum.
Will appreciate feedback.
I think the best option is to add COUNT() to query, and let programming language to do the rest, but if you really need do that by MySQL:
SELECT
id,
(CASE WHEN amtRows = 1 THEN 0 ELSE priceSum) as priceSum,
amtRows,
clientID,
selfPrice,
paymentInstrumentItemID
FROM (
SELECT
pir.paymentInstrumentItemID AS id,
SUM(pir.priceChange) AS priceSum,
COUNT(pir.priceChange) as as amtRows,
pir.clientInfoID AS clientID,
IFNULL(SUM(pir.costChange),0) AS selfPrice,
pir.paymentInstrumentItemID
FROM
paymentInstrumentRegister as pir
GROUP BY
pir.paymentInstrumentItemID
) tmp
I have a schema that looks something like this:
id type date
1 like 2013-12-25
2 comment 2013-12-25
3 like 2013-12-26
4 comment 2013-12-26
What I am trying to do is aggregate and count all the likes and comments on a specific and retrieve them in a single query. Right now I am using this:
SELECT
(CASE WHEN type = 'like' THEN COUNT(id) END) as likes,
(CASE WHEN type = 'comment' THEN COUNT(id) END) as comments,
date_trunc('day', date)
FROM
my_table
GROUP BY date_trunc('day', date), type
The problem is, the rows are not returning in a way that aggregates that data correctly. So how can I retrieve different values on the same column with a group by?
Also, the database being used is Postgres but a Mysql solution with be acceptable answer as well.
Try this..
SELECT SUM(IF(type ='like',1,0) )as Likes,
SUM(IF(type='comment',1,0)) comments,
DAY(DATE) 'day'
FROM `test_types`
GROUP BY DAY(DATE)
Try this:
SELECT DAY(tt.DATE) 'day', SUM(tt.type ='like')AS Likes,
SUM(tt.type='comment') comments
FROM `test_types` tt
GROUP BY DAY(tt.DATE)