Display Month is row wise - mysql

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`

Related

Compare SQL Result Sets While Leaving in Blanks Where No Data

I am trying to find a way of building a result set which is comprised a comparison of data from two specified years.
So it the result set would look something akin to this
PRODUCT - 2019 QTY - 2020 QTY
Car 10 10
Boat 10 0
Plane 0 10
You can use conditional aggregation. I am guessing something like this:
select product,
sum( year = 2019 ) as qty_2019,
sum( year = 2020 ) as qty_2020
from t
group by product;
This assumes that your raw data has one row per item you want to count.
If you need to sum quantity from a column then use:
select product,
sum( case when year = 2019 then quantity else 0 end ) as qty_2019,
sum( case when year = 2020 then quantity else 0 end ) as qty_2020
from t
group by product;

Calculate total sales without missing values while joining two tables

I have a query
I have to calculate monthly sales per branch and customer (Data coming from one table)
Data should look like below
I can write the query for Jan_2019 total sales:
I create a temp table for Feb_2019. I can use the join and combine the 2 tables, but in Feb_2019 if there are new customers added, then when joining the tables I am missing new customers, and due to this the total sales for that month are not matching.
Can any one help?
I have written the query like this below
;with a as
(
select branchid, customer, sum(totalsales) as jan_totalsales from tableA
where year = 2019 and month = 1
group by customer, branched
), feb as
(
select branchid, customer, sum(totalsales) as feb_totalsales from tableA
where year = 2019 and month = 2
group by customer, branched
)
select a.branchid, feb.branchid, a.jan_totalsales, feb.feb_totalsales
from a
left join feb on feb.branchid = a.branchid
I have to create this in a temp table and do it for march_2019
Again, I am not getting new customers as I am joining from Jan data.
Can anyone help me to make this simple?
What you are after here is a conditional aggregate. This should get you on the right path:
SELECT branchid,
customer,
SUM(CASE WHEN [Year] = 2019] AND [Month] = 1 THEN totalsales ELSE 0 END) AS JanSales,
SUM(CASE WHEN [Year] = 2019] AND [Month] = 2 THEN totalsales ELSE 0 END) AS FebSales,
....
FROM YourTable
GROUP BY branchid,
customer;
If you don't undertstand how this works, please do ask. At the end of the day, it's you who has to support the SQL, not myself or other volunteers on Stack Overflow.

SQL query to group transactions by month

----------------------------------------
|transaction_id|customer_id|month|value|
----------------------------------------
I am looking for a SQL query that would return customers ID of the customers that had more transactions in the month of October instead of November or of customers that ONLY had transactions in November (and none in October). The month column is of type string (not a date)
I was thinking an inner join could do it, but past that I do not know how to approach this problem.
This is what I attempted
select customer_id
from tbl_name
where (select count(month is 'October') > count(month is 'November') from tbl_name)
Thank you
You count records. So you aggregate. You want counts per customer_id. So you'd group by customer_id. You forgot this in your query.
Then you'd compare the counts in the HAVING clause, i.e. after having counted October and November records.
select customer_id
from tbl_name
where month in ('October', 'November')
group by customer_id
having count(case when month = 'October' then 1 end) >
count(case when month = 'November' then 1 end)
or count(case when month = 'October' then 1 end) = 0;
You don't need to access the table twice as you see. Just aggregate and see what you have :-)

Trying to get the count of items per day with SQL query

I have a db with several days of data and I want to get the count of each record with Status=0, Status=1...Status=8 for each day. I was to see something like.
DateAndTime State_0 State_1 State_2
2014-08-15 5 8 9
2014-08-16 2 5 6
2014-08-17 4 2 3
I was trying this:
SELECT DISTINCT DATEADD(dd,(DATEDIFF(dd,0,DateAndTime)),0) AS DateAndTime,
SUM( Case When State=0 Then 1 Else 0 End) AS State_0,
SUM( Case When State=1 Then 1 Else 0 End) AS State_1,
SUM( Case When State=2 Then 1 Else 0 End) AS State_2
FROM [DB_002].[dbo].[MyDb]
Group By DateAndTime
Order by DateAndTime
But it keeps adding rows for each state that I add. That is with 3 states I'm getting 4 rows for each date. Any suggestions on how to do this would be greatly appreciated.
You are grouping on the DateAndTime field, which contains also the time component. That makes each record practically unique, so there will be a single record in each group.
Group on the date only:
Group By DATEADD(dd,(DATEDIFF(dd,0,DateAndTime)),0)
Order by DATEADD(dd,(DATEDIFF(dd,0,DateAndTime)),0)
You are confused because you have changed a column and given it the same name as the original column. When you say group by DateAndTime, that is referring to the column in the table. Assuming this is SQL Server, I would write the query as something like this:
SELECT cast(d.DateAndTime as Date) as DateAndNoTime,
SUM(Case When d.State = 0 Then 1 Else 0 End) AS State_0,
SUM(Case When d.State = 1 Then 1 Else 0 End) AS State_1,
SUM(Case When d.State = 2 Then 1 Else 0 End) AS State_2
FROM [DB_002].[dbo].[MyDb] d
Group By cast(d.DateAndTime as Date)
Order by DateAndNoTime;
This changes the name of the alias in the select to avoid confusion. It uses the alias for column references, to also clarify the meaning of the query. And, it uses cast() instead of the old datedd/datediff trick.

How to display monthwise report in mysql

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