MS SQL Server cube/Rollup query

Not a DB Admin. Just an IT guy with limited SQL experience. I have been tasked to create an excel report from our bookstore’s Microsoft RMS  ‘commerce’ SQL Server 2005 (express) database that yields the following:

 lookup number , Description (Title), category,  quantity on hand, quantity sold (past month), quantity sold (year to date), quantity sold (last 12 months)

I can see all of the data I need on the REGISTER1 ‘commerce’ database with a few table joins involving the Item, Category, Transaction, and TransactionEntry tables with this query:

use commerce

SELECT Item.[Description] AS "Title", Item.ID AS "Lookup",Category.Name AS "Category", Item.Quantity AS "On Hand", [Transaction].[Time], TransactionEntry.Quantity, TransactionEntry.Price

From Item,[Transaction], Category, TransactionEntry

Category.ID = Item.CategoryID
[Transaction].TransactionNumber = TransactionEntry.TransactionNumber
Item.ID = TransactionEntry.ID

ORDER By Item.[Description]

Which returns output like this:

Title                                                    Lookup      Category      On Hand      Time      Quantity      Price
1 Year Dinner Table Devotions      1608      Parenting      6      3/9/2003 9:44      1      24.75
1 Yr Sports Devotions for Kids      1655      Children              5      3/9/2003 9:53      1      11
1&2 Thessalonians                      1803      Bible Study      0      3/9/2003 12:48      1      2.25
10 Commandmants CD              1604      Children              3      3/9/2003 9:42       2      7
10 Questions to Diagnose        220              Ch Life              7      2/2/2003 12:31      1      7.75

As you can see, I’ve got all the data required to calculate the output, and the necessary timestamps to create the various sales history columns, but not the knowhow to format the output in a rollup.  Further complicating the matter, is that to get the rollup amounts by date range you have to calculate quantity and price to get the total item count for the transaction entry,  then perform the COUNT rollup for each item, then return the output into three separate columns for monthly qty sold, ytd qty sold, and qty sold last 12 months.

Any suggestions?
Who is Participating?
LowfatspreadConnect With a Mentor Commented:
something like this?

SELECT I.[Description] AS "Title", I.ID AS "Lookup"
,C.Name AS "Category", I.Quantity AS "On Hand"
,count(*) as TransactionsPastYear
,sum(TE.Quantity * TE.Price) as AmtSoldPastYear
,Sum(TE.Quantity) as UnitsSoldPastYear
,count(case when t.time >= Dateadd(yy,0,Datediff(yy,0,getdate())) then 1 end) as TransactionsYTD
,sum(case when t.time >= Dateadd(yy,0,Datediff(yy,0,getdate())) then te.quantity * te.price end) as AmtSoldYTD
,sum(case when t.time >= Dateadd(yy,0,Datediff(yy,0,getdate())) then te.quantity end) as UnitsSoldYTD
,count(case when t.time >= Dateadd(MM,-1,Datediff(mm,0,getdate())) then 1 end) as Transactionslastmonth
,sum(case when t.time >= Dateadd(MM,-1,Datediff(MM,0,getdate())) then te.quantity * te.price end) as AmtlastMonthSold
,sum(case when t.time >= Dateadd(MM,-1,Datediff(MM,0,getdate())) then te.quantity end) as UnitsSoldlastmonth

From Item as I
left outer join TransactionEntry as TE
  on = TE.Transactionentry.ID
left outer join Category as C
  on = i.categoryid
left outer join [Transaction] as T
on T.TransactionNumber = TE.TransactionNumber

t.time >= Dateadd(yy,-1,getdate())
group by I.[Description] , I.ID 
,C.Name , I.Quantity 

ORDER By Item.[Description]

Open in new window

jm00rmanAuthor Commented:
Wow! that makes sense. I got an error on line 15,

on = TE.Transactionentry.ID

But I corrected the join to reflect the outer join between Items.ID and TransactionEntry.ItemID

left outer join TransactionEntry as TE
  on = TE.ItemID

So that part seems to be fixed, but now it's throwing a binding error on line 1 that doesn't make sense, because you declared "Item as I" in the FROM clause in line 13. I think that I'll just keep banging around with it until it works. Thanks!

Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Item.Description" could not be bound.

ORDER By Item.[Description]

should be
All Courses

From novice to tech pro — start learning today.