The picture shows the table
and my question calculate and show lily total fees ( teacher + assistant)
year 2014 - teacher id (123), assistant id (142)
year 2015 - teacher id (523), assistant id (124)
Have you tried a group by?
SELECT
year,
SUM(charge) charge
FROM
your_table
GROUP BY
year
ORDER BY
year;
The structure of the table is not really good, for example the year data is missing.
But you can run the following SQL statement which uses union all:
select '2014' as year,
sum(charge)
from your_table
where id in (123, 142)
union all
select '2015' as year,
sum(charge)
from your_table
where id in (523, 124);
Related
I am a beginner, so apologies in advance for simple/non-technical terminology.
I have a table where each row shows the company name, day/month/year, and how many visits they received in that day. My goal is to show which company had the highest number of visits for January 2018.
I was able to find how many visits each company received in January 2018 using this query:
select to_char(datecolumn,'Mon') as monthkey, extract(year from datecolumn) as yearkey, companyname, sum(visits) as sumvisits
from t1
where monthkey = 'Jan' and yearkey = '2018'
group by monthkey, yearkey, companyname
order by companyname
Now I need to use a window function to find the max value of sumvisits of January along with the corresponding company, but I'm stuck.
I've tried partitioning by month:
select companyname, monthkey, max(sumvisits) over (partition by monthkey) as maxvisits
from (select to_char(f_date,'Mon') as monthkey, extract(year from f_date) as yearkey, companyname, sum(visits) as sumvisits
from t1
where monthkey = 'Jan' and yearkey = '2018'
group by monthkey, yearkey, dealername
order by companyname)
But this query just gives me the max visits of one company and lists it for every company.
I don't think I should use the limit function or anything like that because the query needs to be applicable to multiple months.
I want to to see:
monthkey yearkey companyname sumvisits
Jan 2018 ABCInc 5000
Can someone please help advise what I'm doing wrong/point me in the right direction?
Just use order by and limit:
select to_char(datecolumn,'Mon') as monthkey, extract(year from datecolumn) as yearkey, companyname,
sum(visits) as sumvisits
from t1
where monthkey = 'Jan' and yearkey = '2018'
group by monthkey, yearkey, companyname
order by sumvisits desc
limit 1;
i am relatively new to SQL and I'm looking to create a query that states how many records were created by those other than a certain "good" group of users (userids). If possible grouped by month as well. Any suggestions? I have some basic logic set out below.
Table is called newcompanies
SELECT COUNT(record_num), userid
FROM Newcompanies
WHERE userID <> (certain group of userIds)
GROUP BY Month
Will i be required to create a second table where the group of "good" userids is held
There are a few ways to do this. Without knowing your exact columns, this will be a rough estimate.
SELECT id,
DATEPART(MONTH, created_date) AS created_month,
COUNT(*)
FROM your_table
WHERE id NOT IN(
--hardcode userID's here
)
GROUP BY
id,
DATEPART(MONTH, created_date)
Or you could have a table with your good id's and then exclude those.
SELECT id,
DATEPART(MONTH, created_date) AS created_month,
COUNT(*)
FROM your_table
WHERE id NOT IN(
SELECT id
from your_good_id_table
)
GROUP BY
id,
DATEPART(MONTH, created_date)
-- if month is not a field in the table you will have to do a function to parse out the month that will depend on the sql database you are using, if it is MS SQL you can do Month(datefield)
SELECT COUNT(record_num), userid, Month
FROM Newcompanies
WHERE userID NOT IN (
Select UserID
from ExcludeTheseUserIDs
)
GROUP BY Month, userid
I have a data something like this:
declare #table table
(
CUSTNO varchar(35),
RELATIONNO int,
Sales numeric(5,2),
RelationDate dateTIME
)
insert into #table
select 'B1024818', 120, 189.26, '2013-10-27' union all
select 'B1024818', 120, 131.76, '2016-10-28' union all
select 'C0002227', 124, 877.16, '2012-08-26' union all
select 'C0002227', 124, 802.65, '2015-06-15'
I am trying to get a result like
CUSTNO RELATIONNO Sales Till Last Relation Year
----------------------------------------------------------
B1024818 120 321.02 2016
C0002227 124 1679.81 2015
Here sales is added for each customer from 1st Relation date to Last Relation date
In a Till Last Relation Year COLUMN it contain highest year for each customer
I am not sure whether it is possible in SQL.
Please share your suggestions.
Thanks
You could use:
SELECT CUSTNO, RELATIONNO, SUM(Sales) AS Sales, MAX(YEAR(RelationDate))
FROM #table
GROUP BY CUSTNO, RELATIONNO;
Rextester Demo
SELECT custno, RELATIONNO, sum(Sales), MAX(year(RelationDate ))
FROM #table
GROUP BY custno, RELATIONNO
you can use below query -
select CUSTNO ,RELATIONNO ,SUM(Sales) as Sales , max(Year(RelationDate )) [Till Last Relation Year]
from #table
group by CUSTNO ,RELATIONNO
I'm trying to display the average number of counts/records for each month in 2016. The following code does not display each month, rather displays only the monthly average for 2016:
SELECT AVG(DISTINCT DayCnt) AS AvgCnt
FROM
(
SELECT COUNT(*) As DayCnt
FROM table
WHERE YEAR(Insert_Date) = '2016'
GROUP BY MONTH(Insert_Date)
)
AS AvgCnt
You first should be group your result per month and days and count daily inserted records, after that to get average of per month inserted records try this:
SELECT monthGroup, AVG(DayCnt) AS AvgCnt
FROM
(
SELECT MONTH(Insert_Date) monthGroup, DAY(Insert_Date) dayGroup, COUNT(*) As DayCnt
FROM table
WHERE YEAR(Insert_Date) = '2016'
GROUP BY MONTH(Insert_Date), DAY(Insert_Date)
)
AS AvgCnt
GROUP BY monthGroup
I'm not sure since you didn't post the data but if I understand correctly, this should work for you:
SELECT AVG(DayCnt) AS AvgCnt, mth
FROM
(
SELECT COUNT(*) As DayCnt, MONTH(Insert_Date) as mth
FROM table
WHERE YEAR(Insert_Date) = '2016'
GROUP BY MONTH(Insert_Date), DAY(Insert_Date) )
AS AvgCnt
GROUP BY mth
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)
;