i want to order my results by month name but it is not getting ordered in sql server .
my query:
select DATEname(month,signdate) as month1,
COUNT(contractid) as noofcontracts, sum(loanamt) as totalloan ,
min(loanamt) as minloan
from contracts
group by DATEname(month,signdate) order by DATEname(month, signdate);
i have tried using datepart function but then it is giving error
select DATEname(month,signdate) as month1,
COUNT(contractid) as noofcontracts, sum(loanamt) as totalloan ,
min(loanamt) as minloan
from contracts
group by DATEname(month,signdate)
order by DATEPART(m,signdate) ;
error:Column "contracts.signdate" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.
output which im getting:
month count loan_amt min_loan
April 2 14998 4999
February 1 8189 8189
June 5 133645 2599
May 2 21947 10048
Desired output:
February record should come first and so on..
Pls help.
Try using an aggregation function for the order by:
order by min(signdate);
Alternatively, include the month number in the group by:
group by datename(month, signdate), month(signdate)
order by month(signdate)
Note that you have the month in the query without the year -- either as a filter or a group by expression. This usually indicates an problem with the query logic.
Related
PostgreSQL 13
Assuming a simplified table plans like the following, it can be assumed that there is at least 1 row for every month and sometimes multiple rows on the same day:
id
first_published_at
12345678910
2022-10-01 03:58:55.118
abcd1234efg
2022-10-03 03:42:55.118
jhsdf894hld
2022-10-03 17:34:55.118
aslb83nfys5
2022-09-12 08:17:55.118
My simplified query:
SELECT TO_CHAR(plans.first_published_at, 'YYYY-MM') AS publication_date, COUNT(*)
FROM plans
WHERE plans.first_published_at IS NOT NULL
GROUP BY TO_CHAR(plans.first_published_at, 'YYYY-MM');
This gives me the following result:
publication_date
count
2022-10
3
2022-09
1
But the result I would need for October is 4.
For every month, the count should be an aggregation of the current month and ALL previous months. I would appreciate any insight on how to approach this.
I would use your query as a CTE and run a select that uses cumulative sum as a window function.
with t as
(
SELECT TO_CHAR(plans.first_published_at, 'YYYY-MM') AS publication_date,
COUNT(*) AS cnt
FROM plans
WHERE plans.first_published_at IS NOT NULL
GROUP BY publication_date
)
select publication_date,
sum(cnt) over (order by publication_date) as "count"
from t
order by publication_date desc;
Demo on DB fiddle
i am trying to work with this query to produce a list of all 11 years and 12 months within the years with the sales data for each month. Any suggestions? this is my query so far.
SELECT
distinct(extract(year from date)) as year
, sum(sale_dollars) as year_sales
from `project-1-349215.Dataset.sales`
group by date
it just creates a long list of over 2000 results when i am expecting 132 max one for each month in the years.
You should change your group by statement if you have more results than you expected.
You can try:
group by YEAR(date), MONTH(date)
or
group by EXTRACT(YEAR_MONTH FROM date)
A Grouping function is for takes a subsection of the date in your case year and moth and collect all rows that fit, and sum it up,
So a sĀ“GROUp BY date makes no sense, what so ever as you don't want the sum of every day
So make this
SELECT
extract(year from date) as year
,extract(MONTH from date) as month
, sum(sale_dollars) as year_sales
from `project-1-349215.Dataset.sales`
group by 1,2
Or you can combine both year and month
SELECT
extract(YEAR_MONTH from date) as year
, sum(sale_dollars) as year_sales
from `project-1-349215.Dataset.sales`
group by 1
I have tried using the following query
select distinct Year (SaleDate) AS SaleYear,Max(SalePrice)
from Sale
group by SaleDate
The years 2010 and 2014 are showing twice,even though i used distinct and group by. the amounts in Maxprice are different as well. am i doing something wrong here?
You need to repeat year() in the group by:
select Year(SaleDate) AS SaleYear, Max(SalePrice)
from Sale
group by year(SaleDate);
SELECT DISTINCT with GROUP BY is almost never correct. All that your query does is aggregate by SaleDate and in the result set extract the year. That is why you see duplicates.
i need to get the data of monthly report which should exclude Sunday in all the weeks...where am not able to exclude Sunday. Kindly check the below query:
select Name,Actual_Hours,round((Actual_Hours/((DATEDIFF(DAY,'12-01-2016' ,'12-14-2016')+1)*8.5))*100,0)as percentage
from (select Name , SUM(actual_Hours) Actual_Hours
from ( select Name,ACTUAL_HOURS=sum(DATEPART(hh,[Time_Taken_in_Min]))+sum(DATEPART(MINUTE,[Time_Taken_in_Min])) /60
FROM [HR_Admin].[dbo].[Mst_Daily_Report_Pds] where date between '12-01-2016' and '12-14-2016' and (DATENAME(weekday,Date)) not in ('sunday')
GROUP BY Name )o group by Name )a order by Actual_Hours desc
Don't know exactly what error you are getting but I don't see the issue in SQL, but I believe it's because of the use of DATEDIFF in the select query instead of using the actual days.
Check the below query which uses the actual days (already removed Sunday in Where clause) and using less subquery. You can further improve this by taking only the time factor in the innermost query and then rounding off after summing all the hours.
Another suggestion is to avoid using the column name as reserved word like Date as it could create confusion, or using the column in Square brackets like [Date].
select Name
, sum(Actual_Hours) as Actual_Hours
, round((sum(Actual_Hours)/COUNT([Day])*8.5)*100,0) as percentage
from (select Name
, DATEPART(DAYOFYEAR,[Date]) as [Day]
, ACTUAL_HOURS=sum(sum(DATEPART(hh,[Time_Taken_in_Min]))+sum(DATEPART(MINUTE,[Time_Taken_in_Min])) /60)
FROM [HR_Admin].[dbo].[Mst_Daily_Report_Pds]
where date between '12-01-2016' and '12-14-2016'
and (DATENAME(weekday,[Date])) not in ('Sunday')
GROUP BY Name, [Date]
) a
GROUP BY Name
order by Actual_Hours desc
I am trying to write an SQL statement based on the following code.
CREATE TABLE mytable (
year INTEGER,
month INTEGER,
day INTEGER,
hoursWorked INTEGER )
Assuming that each employee works multiple days over each month in a 3 year period.
I need to write an sql statement that returns the total hours worked in each month, grouped by earliest year/month first.
I tried doing this, but I don't think it is correct:
SELECT Sum(hoursWorked) FROM mytable
ORDER BY(year,month)
GROUP BY(month);
I am a little confused about how to operate the sum function in conjunction with thee GROUP BY or ORDER BY function. How does one go about doing this?
Try this:
SELECT year, month, SUM(hoursWorked)
FROM mytable
GROUP BY year, month
ORDER BY year, month
This way you will have for example:
2014 December 30
2015 January 12
2015 February 40
Fields you want to group by always have be present in SELECT part of query. And vice-versa - what you put in SELECT part, need be also in GROUP BY.
SELECT year, month, Sum(hoursWorked)as workedhours
FROM mytable
GROUP BY year,month
ORDER BY year,month;
You have to group by year and month.
Is this what you are trying to do. This will sum by Year/Month and Order by Year/Month.
Select [Year], [Month], Sum(HoursWorked) as WorkedHours
From mytable
Group By [Year], [Month]
Order by [Year], [Month]
You have to group by year and month, otherwise you will have the hours you worked on March 2014 and 2015 in one record :)
SELECT Sum(hoursWorked) as hoursWorked, year, month
FROM mytable
GROUP BY(year, month)
ORDER BY(year,month)
;