I have created 6 months data in my database which is having column name as productiondate. i have provided 6 months date for this productiondate column. Right now i need to retrieve data for monthwise data from this single productiondate column.
I need to display in the following manner
jan , feb, mar,........dec in seperate columns
You did not provide any details on your current table structure or existing query, but it sounds like you are trying to pivot data from rows into columns.
MySQL does not have a pivot function so you will need to replicate it using an aggregate function with a CASE expression. You code would be similar to this:
select otherColumns,
max(case when month(productiondate)=1 then value end) Jan,
max(case when month(productiondate)=2 then value end) Feb,
max(case when month(productiondate)=3 then value end) Mar,
max(case when month(productiondate)=4 then value end) Apr,
max(case when month(productiondate)=5 then value end) May,
max(case when month(productiondate)=6 then value end) Jun,
....
from yourtable
group by otherColumns
You would then replace otherColumns with any other columns that you want included in your final result. These columns would then be included in the GROUP BY. Also you would replace the value with the name of the column that you want displayed under each month.
You need to use GROUP BY month and generate month wise report, You may also need to use Aggregate functions like SUM, COUNT, AVG based on your requirements.
You can utilize MonthName function to get Month Name from date
e.g.
mysql> SELECT MONTHNAME('1998-02-05');
-> 'February'
so your query might look like below:
SELECT MONTHNAME(productiondate) as mon, SUM(XYZ), COUNT(*)
FROM Your_Table_Name
GROUP BY mon
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;
In my warehouse project, I want to call mysql data and want to display record per month name as row wise.
Below is my mysql code
SELECT `Invoice_No`,
`WH_Name`,
SUM(`Balance`),
`Material_Issue_Date`
FROM `NEW_STOCK_TEMP`
and below is my current output
But I want to get output like below
Any suggestions to modify the code or what would be the best query to get output on same way?
thanks in advance for your support here.
Use case statement for every month of a year. If getting particular year data then use YEAR() or for few months then use between startdate and enddate. WH_name have been used in group by clause along with invoiceNo if same warehouse name for multiple invoices otherwise use MAX(WH_Name) in select clause and remove from group by clause.
-- MySQL
SELECT Invoice_No
,WH_Name
,SUM(CASE WHEN MONTH(Material_Issue_Date) = 1 THEN Balance END) jan
,SUM(CASE WHEN MONTH(Material_Issue_Date) = 2 THEN Balance END) feb
,SUM(CASE WHEN MONTH(Material_Issue_Date) = 3 THEN Balance END) mar
,SUM(CASE WHEN MONTH(Material_Issue_Date) = 4 THEN Balance END) apr
...
,SUM(CASE WHEN MONTH(Material_Issue_Date) = 12 THEN `Balance` END) dec
FROM NEW_STOCK_TEMP
WHERE YEAR(Material_Issue_Date) = 2021
GROUP BY Invoice_No
,WH_Name;
You can do something like this
SELECT `Invoice_No`,
`WH_Name`,
(SELECT SUM(`Balance`) FROM NEW_STOCK_TEMP where Material_Issue_Date='January') AS January, (SELECT SUM(`Balance`) FROM NEW_STOCK_TEMP where Material_Issue_Date='February') AS February
FROM `NEW_STOCK_TEMP`
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 have the following code:
SELECT COUNT(CASE WHEN Title LIKE '%Superintendent%' THEN 1 END) AS 'Bob',
COUNT(CASE WHEN Title LIKE '%Machine%' THEN 1 END) AS 'Control',
COUNT(CASE WHEN Title LIKE '%Estimator%' THEN 1 END) AS 'Estimator'
FROM pants;
Current result looks like this:
Bob Control Estimator
230 550 1243
Instead, I would like the resulting view from the query to have the columns in descending order: Estimator --> Control --> Bob
It's not recommended to mess around dynamically with the order of columns in the result set. The query should, instead, have a result set with a highly predictable (ideally static) set of columns.
To re-order the columns is, I'd recommend, the job of whatever renders that result set after the database returns it.
SQLs sorting is designed to order rows, not columns. In order to change the column order in your output, you would need to order your data, then build a dynamic query to return the columns in the right order. However, it is a lot easier just to do the first part (ordering your data) if a result like this would be acceptable for what you need.
Name Total
Estimator 1243
Control 550
Bob 230
This query could look something like this (my MySQL syntax is rusty, but this should be close)
SELECT 'Bob' as Name, COUNT(CASE WHEN Title LIKE '%Superintendent%' THEN 1 END) AS Total From Pants
UNION ALL
SELECT 'Control' as Name, COUNT(CASE WHEN Title LIKE '%Machine%' THEN 1 END) AS Total From Pants
UNION ALL
SELECT 'Estimator' as Name, COUNT(CASE WHEN Title LIKE '%Estimator%' THEN 1 END) AS Total From Pants
ORDER BY Total desc;
;
WITH cte AS
(
SELECT dtls,
pantcounts
FROM (
SELECT Count(
CASE
WHEN title LIKE '%Superintendent%' THEN 1
END) AS 'Bob',
Count(
CASE
WHEN title LIKE '%Machine%' THEN 1
END) AS 'Control',
Count(
CASE
WHEN title LIKE '%Estimator%' THEN 1
END) AS 'Estimator'
FROM pants) P UNPIVOT (pantcounts FOR dtls IN (bob,
[Control],
[Estimator])) AS unp
select dtls,
pantcounts,
row_number() OVER (ORDER BY pantcounts DESC) AS rownum
FROM cte
This will give the required sorted data in row. Then service layer should do the transpose from rows to column.
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;