suppose I have this record
empcode net year month
602256 3479.97 2014 1
602256 33125.98 2014 1
602256 5247.11 2014 2
602256 7698.39 2014 2
602256 2941.46 2013 3
602256 5515.57 2014 3
602256 5758.68 2014 3
602256 4966.89 2013 4
602256 4984.06 2013 4
602256 5951.63 2014 4
602256 19861.04 2014 4
what i want to happen is that i want to sum the net with the same year and month hope you can help me thank you in advance.
You should use the GROUP BY clause:
SELECT SUM(net) FROM table GROUP BY [year],[month]
Its very simple by Group By cluase. Also whatever you given in group by clause , you can define in select like below, which gives better idea for calculation.
SELECT year, month, SUM(net) FROM table GROUP BY [year],[month]
Related
I have a table like:
AVG_AMOUNT
WAS_MEMBER
YEAR
200.00
True
2018
100.00
False
2018
20.00
True
2019
300.00
False
2019
400.00
True
2020
10.00
False
2020
And I want a table like:
DIFF_AVG_AMOUNT
YEAR
100.00
2018
-280.00
2019
390.00
2020
How can I get a difference of means based on membership for each year? Probably uses a partition over but not sure how to use difference with a partition.
You can use conditional aggregation containing SUM() such as
SELECT SUM(CASE WHEN was_member='True' THEN avg_amount
WHEN was_member='False' THEN - avg_amount END) AS diff_avg_amount,
year
FROM tab
GROUP BY year
ORDER BY year
I have the following example of a table from which I want all rows where Year is '2016' and '2017' but want to exclude CustID 'AB17' and 'AB18' from Year '2017'. In total I should get 12 rows. See this fiddle
Example:
SQL:
SELECT * FROM testing
WHERE Year In ('2016','2017')
AND (CustID NOT In ('AB17','AB18') AND Year = '2017');
Table:
Year CustID Revenue
2016 AB12 10
2016 AB13 11
2016 AB14 12
2016 AB15 13
2016 AB16 14
2016 AB17 15
2016 AB18 16
2017 AB12 10
2017 AB13 11
2017 AB14 12
2017 AB15 13
2017 AB16 14
2017 AB17 15
2017 AB18 16
2018 AB12 17
2018 AB13 18
2018 AB14 19
2018 AB15 20
2018 AB16 21
2018 AB17 22
2018 AB18 23
Any suggestions?
This should work:
WHERE
Year = '2016'
OR (Year = '2017' AND CustID NOT In ('AB17','AB18'))
A pretty direct translation of:
Year is '2016' and '2017' but want to exclude CustID 'AB17' and 'AB18' from Year '2017'.
is:
where year in (2016, 2017) and
not (year = 2017 and custID in ('AB17', 'AB18'))
My sql table is
Week Year Applications
1 2017 0
2 2017 10
3 2017 20
4 2017 50
5 2017 0
1 2018 10
2 2018 0
3 2018 40
4 2018 50
5 2018 10
And I want SQL query which give below output
Week Year Applications
1 2017 0
2 2017 10
3 2017 30
4 2017 80
5 2017 80
1 2018 10
2 2018 10
3 2018 50
4 2018 100
5 2018 110
Can anyone help me to write below query?
You could use SUM() OVER to get cumulative sum:
SELECT *, SUM(Applications) OVER(PARTITION BY Year ORDER BY Week)
FROM tab
It looks like you want a cumulative sum:
select week, year,
sum(applications) over (partition by year order by week) as cumulative_applications
from t;
I have a data set here:
YOA Count
2014 43
2014 2
2014 17
2016 47
2015 191
2017 185
2016 26
2014 119
2016 33
What I'd like to do is write a SQL query that displays:
2017 - all counts for 2017
2016 - all counts for 2017
2015 - all counts for 2017
2014 - all counts for 2017
Try this:
SELECT YOA, SUM(count) total
FROM tableName
GROUP BY YOA
Use SUM with GROUP BY:
SELECT YOA,SUM([Count]) FROM myTable GROUP BY YOA
HI, I have the following table which save agent ranking on daily basis on basis of tickets status.
No. **Agent Name** **Incidents** **workorder** **Rank** **TimeStamp**
1 cedric 200 29 1 21 Jan 2011
2 poul 100 10 2 21 Jan 2011
3 dan 200 20 1 21 Jan 2011
4 cedric 100 19 2 22 Jan 2011
5 poul 200 26 1 22 Jan 2011
6 dan 150 20 2 22 Jan 2011
Now i need query which fetch ranking between two dates means if i select date between 21 jan 2011 to 22 jan 2011 then query return me agents average ranking between these two dates of agent not return the agent ranking details on date wise. I need single name of agent with his ranking.
Regards,
Iftikhar hashmi
Try
SELECT [Agent Name], AVG(RANK) FROM MY_TABLE WHERE [TimeStamp] BETWEEN DATE1 AND DATE2
GROUP BY [Agent Name]
(Update)
Thanks to Martin which reminded me I need to cast RANK.
SELECT [Agent Name], AVG(CAST(RANK AS FLOAT)) FROM MY_TABLE WHERE [TimeStamp] BETWEEN DATE1 AND DATE2
GROUP BY [Agent Name]