Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 [RESOLVED]struggling to "see" grouping date values

Author  Topic 

Pace
Constraint Violating Yak Guru

264 Posts

Posted - 2008-08-20 : 06:38:50
Hi.

I have some data that I want to group in months. I can work out my filtering for arguments that I wish to pass but im struggling conceptually how im going to group the data.

I have a cost of a product and a value. I also have the date that this product was made. What I want to do is list months and sum up all the products costs and values so that I have something resembling below eg;

Value Cost
Jan 1000 700
Feb 1200 500
Mar 800 400

I can sum the values fine for a given month and show that. What im struggling with is how I can run a query which will produce the entire result set. I have been thinking, do I need a bunch of derived tables? *GASP* Do I need a cursor?

Any help/guidance appreciated.

Pace

"Impossible is Nothing"

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-20 : 06:50:55
just use like this
SELECT MONTH(Datefield),
SUM(Quantity1),SUM(Quantity2)
FROM YourTable
GROUP BY MONTH(DateField)
Go to Top of Page

Pace
Constraint Violating Yak Guru

264 Posts

Posted - 2008-08-20 : 07:01:52
Ah yes. Thanks for that. I see more clearly now, I could use the DATEPART function to do various similar things, like show a trend etc. Nice

Many thanks.

"Impossible is Nothing"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-08-20 : 07:09:47
If you want them for each year and month

SELECT dateadd(month,datediff(month,0,Datefield),0)
SUM(Quantity1),SUM(Quantity2)
FROM YourTable
GROUP BY dateadd(month,datediff(month,0,Datefield),0)


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Pace
Constraint Violating Yak Guru

264 Posts

Posted - 2008-08-20 : 08:23:38
ooo nice, thanking you :)

My completed sql;

-- total cost & value of items over a given time
SET DATEFORMAT DMY
DECLARE @Start DATETIME
DECLARE @Finish DATETIME
DECLARE @Cell VARCHAR(10)
-- debugging so have these set static for now
SET @Cell = '%'
SET @Start = '01/01/08'
SET @Finish = GETDATE()
SELECT
SUM(Product.Cost) AS TotalCost,
SUM(Product.[Selling Price]) AS TotalValue,
CASE DATEPART(MONTH, Job.[Compl Date])
WHEN 1 THEN 'Jan'
WHEN 2 THEN 'Feb'
WHEN 3 THEN 'Mar'
WHEN 4 THEN 'Apr'
WHEN 5 THEN 'May'
WHEN 6 THEN 'Jun'
WHEN 7 THEN 'Jul'
WHEN 8 THEN 'Aug'
WHEN 9 THEN 'Sep'
WHEN 10 THEN 'Oct'
WHEN 11 THEN 'Nov'
WHEN 12 THEN 'Dec'
END AS [Month],
DATEPART(YEAR, Job.[Compl Date]) AS [Year],
-- use below to order the items as you want so it makes sense to the managers
DATEPART(MONTH, Job.[Compl Date]) AS Sequence
FROM
Product INNER JOIN Item ON
Product.[Product Code] = Item.Code
--join jobs for dates
INNER JOIN Job ON item.[Job No] = job.[Job No]
WHERE
Job.ProdStartActual >= @Start AND
Job.[Compl Date] <= @Finish AND
Product.ProductionGroup LIKE @Cell
GROUP BY
DATEPART(MONTH, Job.[Compl Date]),
DATEPART(YEAR, Job.[Compl Date])
-- add for clarity
ORDER BY
[Sequence] ASC


"Impossible is Nothing"
Go to Top of Page
   

- Advertisement -