Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


problem .... income during month in sql server 2008


problem .... income during month in sql server 2008

Author
Message
amr.moussa.87
amr.moussa.87
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 3
i have tried many thing

i have table called visiting, it have two column (visiting date) and ( cost ) like that

visiting_date cost
20-6-2014 50
20-6-2014 50
21-7-2014 200
21-7-2014 200

i want to make view that can sum cost of each month individual so output will be

month income
6 100
7 400

than you for helping
Sachin Nandanwar
Sachin Nandanwar
Old Hand
Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)

Group: General Forum Members
Points: 397 Visits: 2633

SELECT DATEPART(MM,VISITINGDATE),SUM(COST) FROM yourtable
GROUP BY DATEPART(MM,VISITINGDATE)



--------------------------------------------------------------------------------------------------
I am just an another naive wannabe DBA trying to learn SQL Server
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45071 Visits: 39906
amr.moussa.87 (6/19/2014)
i have tried many thing

i have table called visiting, it have two column (visiting date) and ( cost ) like that

visiting_date cost
20-6-2014 50
20-6-2014 50
21-7-2014 200
21-7-2014 200

i want to make view that can sum cost of each month individual so output will be

month income
6 100
7 400

than you for helping


What do you want to display when the year changes over?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
vijred
vijred
SSC Veteran
SSC Veteran (200 reputation)SSC Veteran (200 reputation)SSC Veteran (200 reputation)SSC Veteran (200 reputation)SSC Veteran (200 reputation)SSC Veteran (200 reputation)SSC Veteran (200 reputation)SSC Veteran (200 reputation)

Group: General Forum Members
Points: 200 Visits: 208
I use to create unique MonthID based on Year and month combination.


select (datepart(yy,SalesDate)-2010)*12+datepart(MM,SalesDate) as MonthID, sum(total)
group by (datepart(yy,SalesDate)-2010)*12+datepart(MM,SalesDate)
-- Assuming 2010 as the minimum month



-Vijred (http://vijredblog.wordpress.com)
amr.moussa.87
amr.moussa.87
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 3
thank you guys your answer help me so much Smile i find the solution

SELECT SUM(cost) as income,
DATEPART(Month, visiting_date) as month,
DATEPART(Year, visiting_date) as year
FROM table1
GROUP BY DATEPART(Year, visiting_date), DATEPART(Month, visiting_date)
ORDER BY DATEPART(Year, visiting_date), DATEPART(Month, visiting_date)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search