I have 1 table name input_table . In this table we have 3 columns . Date , quarter and name .the values inside this columns are like
Date -- 22/07/1992 , 22/08/2022, 28/06/1991. ...
Quarter -- Q1 , Q2, Q3, Q4,
Name -- Ram , shyam , pooja, ...
Let suppose if for "Ram" he is missing Q4 for period 2022 . I want to figure out this only . That if any name is missing any quarter like Q1, Q2, Q3 or Q4 any one of them , then in that case I should get that missing value in my result.
That if any name is missing any quarter like Q1, Q2, Q3 or Q4 any one of them , then in that case I should get that missing value in my result.
First, you need to select a name from the quarter and then you have to use GROUP BY and HAVING clauses. I hope this query will be helpful:
SELECT name, quarter
FROM input_table
GROUP BY name
HAVING COUNT(*) < 4
Related
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;
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.
I run a Bridal Shop. I have made a table, where we insert all the dresses which are tried on by the brides. I have the fields, branch, model, date and ordered. If a dress will be ordered, then ordered will be set to 1. I have also the field "status" which will be set to 2 if the dress will be deleted from this table.
I have this statement to show year, month, branch, model and the quantity each model has been tried on:
SELECT count(*) as Anzahl, MONTHNAME( date ) AS Monat, YEAR( date ) AS Jahr,
branch as Branch FROM wccrm_anprobe where status = 1 GROUP BY MONTHNAME(date),
YEAR(date), branch ORDER BY date,branch ASC
What can I do to achieve the following: I want to count the field "ordered" and put it to my select? I want to see, how many dresses have been sold per year, month and branch.
I tried:
SELECT count(*) as Anzahl, MONTHNAME( date ) AS Monat, YEAR( date ) AS Jahr,
branch as Branch, ordered as Ordered FROM wccrm_anprobe where status = 1 GROUP
BY MONTHNAME(date),
YEAR(date), branch, ordered ORDER BY date,branch ASC
but I do not get the correct results as I think the ordered field will displayed once - it will not be count.
So for example, last month I sold 6 dresses, but the statement above just shows 3 dresses.
Thanks for any suggestions.
Best Regards,
Stefan
You can use conditional aggregation, in your case it should look something like this:
COUNT(CASE `ordered` WHEN 1 THEN 1 ELSE NULL END) AS numberOrdered
COUNT, like most aggregate functions, ignores NULL values. Alternatively, you could use SUM
SUM(CASE `ordered` WHEN 1 THEN 1 ELSE 0 END) AS numberOrdered
I have a SQL/Java code issue. The basic overlay is as follows: a MySQL database with a table. In this table there are multiple columns. One column consists of names. An associated column is months. In the third column there is counts. So a sample table would be
john - january - 5
john - january - 6
mary - january - 5
Alex - February- 5
John - February - 6
John - February - 4
Mary - February - 3
John - march - 4
The table continues to month May.
So John appears in five months, Mary in 3, and Alex in one. Currently, my SQL query somewhat looks like this.
select name, sum(count)/4
from table where (category ='something'
AND month not like 'May') group by name;
Basically, what this query is supposed to do is just display each name with the average counts per month. Hence, the sum will be divided by four (because I exclude May, so it must divide Jan-April/4). However, the issue is that some names only appear in one month (or two or three).
This means for that name, the sum of the counts would only be divided by that specific number, to get the average counts over the months. How would I go about this? I feel as if this will require some if statement in a where clause. Kind of like where if the count of the distinct (because months may repeat) is a certain number, then divide the sum(count) by that number for each name?
Also, I think it may not be a where if clause issue. I've read some forums where possibly some use of case could be utilized?
If you need average per month, you can GROUP BY name and month and use AVG function:
SELECT `name`, `month`, avg(`count`)
FROM table
WHERE `category` ='something' AND `month` NOT LIKE 'May'
GROUP BY `name`, `month`;
If you need average for all period, just GROUP BY name and AVG count:
SELECT `name`, avg(`count`)
FROM table
WHERE `category` ='something' AND `month` NOT LIKE 'May'
GROUP BY `name`;
And another option, if you don't like AVG:
SELECT `name`, sum(`count`)/(SELECT count(*) FROM `table` AS `t2` WHERE `category` ='something' AND `month` NOT LIKE 'May' and `t1`.`name` = `t2`.`name`)
FROM `table` AS `t1`
WHERE `category` ='something' AND `month` NOT LIKE 'May')
GROUP BY name;
But I would stay with AVG.
Actually, i prefer to use != instead of NOT LIKE it's improves readability
Just for completness sake here is a WORKING FIDDLE. using the AVG function is the way to go as it will do the average per person per month. look at John in January.. his result is 5.5 when the count (in january) is 5 and 6.. average = 5.5.
SELECT
person,
month,
avg(counter)
FROM testing
where
(
category ='something'
AND month <> 'May'
)
GROUP BY person, month;
If you want to see the data in one like as it sounds like that from your post then you can do this. ANOTHER FIDDLE
SELECT
person,
group_concat(month),
group_concat(average_count)
FROM(
SELECT
person,
month,
avg(counter) as average_count
FROM testing
where
(
category ='something'
AND month <> 'May'
)
GROUP BY person, month
) as t
group by person;
Try this :
SELECT name, SUM(count) / COUNT(DISTINCT month)
FROM table
WHERE month != 'May'
AND category = 'something'
GROUP BY name
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