Get Sum & Average of each month using SQL [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have below database table,In here i need group some data and get sum and avg of some data.
In here i need to calculate Item marketers monthly sales(Each Month)
As a Example :
I need to get a Patric Newton's Sales in Each months.
And also i need to calculate AvarageFactor of each month Each employee.
AvgFactor = SUM(Daily Sales)/ SUM(Value Factor) * 100( Each Month )
Database Table
I have tried it like below,
SELECT ItemMarketerName,DailySales,ValueFactor,[Month],[Year]
FROM [SR_Hotel].[dbo].[Table_1]
WHERE ItemMarketerName IS NOT NULL
Group by ItemMarketerName,DailySales,ValueFactor,[Month],[Year]
Order by ItemMarketerName

You should not include your aggregated columns in your GROUP BY.
Try:
SELECT ItemMarketerName, [Month], [Year], SUM(DailySales), SUM(DailySales) / SUM(ValueFactor) * 100
FROM [SR_Hotel].[dbo].[Table_1]
WHERE ItemMarketerName IS NOT NULL
GROUP BY ItemMarketerName, [Month], [Year]
ORDER BY ItemMarketerName

Related

Get minimum time and max time for each day in sql query [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have an data which includes Processeddate, start time and end time. I need to get minimum starttime and max endtime for each day.
Process date should be the first coulmn
Just use min() and max() function as shown below.
;WITH cte
AS (
SELECT Cast(processedDate AS DATE) AS processedDate
,min(startTime) minStartTime
,max(endTime) maxEndTime
FROM mytable
GROUP BY Cast(processedDate AS DATE)
)
SELECT *
FROM cte
WHERE month(processedDate) = 1
AND year(processedDate) = 2020
ORDER BY processedDate
db<>fiddle

AVERAGE NUMBER OF DAYS BETWEEN DATES [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to calculate the average of the date according to the number of days between Factor2 and Facort3 where the Factor1 Value is A
how can I do it, please ?
select avg(DATEDIFF(DAY,Factor2,Factor3)) from [TestingTable]
where Factor1='A'
I've taken the difference between Factor2 and Factor 3 where Factor1 is A, then Find the average of the column.
and the result must be "3"
it's in SQL :
1- DATEDIFF ( datepart , startdate , enddate )
2-AVG ( [ ALL | DISTINCT ] expression )
3- datepart : it could be Day Month, Year....etc

How to use group and sum same data based on different criteria on same table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am trying to sum data in a table.
Data in my table:
Month Osl Sale
10 8-02-01-01 38440.5
10 8-02-01-03 14961
10 8-03-02-01 10388.3
10 8-05-04-01 81666.6
10 8-05-04-05 29431.8
10 8-07-01-09 9821.4
10 8-09-01-01 7567.5
And my expected output is:
I think union all is the simplest method:
select month, osl, sale
from t
union all
select month, left(osl, 7), sum(sale)
from t
group by month, left(osl, 7);
Not all databases support left(). In those that don't, either substr() or substring() can extract the first seven characters.
Unsure of your RDMS, but I would suggest something along the lines of:
select * from table1
union all
select t.month, left(t.osl, len(t.osl)-3), sum(t.sale)
from table1 t
group by t.month, left(t.osl, len(t.osl)-3)
Change table1 to your table name as appropriate.

postgres count customers with 2 or more orders [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to get monthly numbers of customers who made 2 or more orders. I'm not that good with postgresql so any help would be appreciated.
Table name is shop_orders
Relevant columns are 'email' - customer, 'status_code' should have value 'CONFIRMED', date - timestamp.
Thank you.
Try this:
select EXTRACT(month FROM datecolumn ) as month,email
from shop_orders
where status_code = 'CONFIRMED'
group by EXTRACT(month FROM datecolumn ),email
having count(1) >2

Select data by month [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I Need help to select a number of data per month, there are 4 retailers and I need to select a total of transactions per month and the total amount for that month showing each month separately in sql.
Extract the month from the date and group by it. Then you can use aggregate functions to sum and count
select month(date_column),
sum(amount) as total_amount,
count(*) as transaction_count
from your_table
group by month(date_column)