Solved

MS SQL Server cube/Rollup query

Posted on 2015-01-23
4
42 Views
Last Modified: 2016-06-18
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

WHERE
Category.ID = Item.CategoryID
AND
[Transaction].TransactionNumber = TransactionEntry.TransactionNumber
AND
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?
0
Comment
Question by:jm00rman
  • 2
4 Comments
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 total points
Comment Utility
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 i.id = TE.Transactionentry.ID
left outer join Category as C
  on c.id = i.categoryid
left outer join [Transaction] as T
on T.TransactionNumber = TE.TransactionNumber

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

ORDER By Item.[Description]

Open in new window

0
 
LVL 1

Author Comment

by:jm00rman
Comment Utility
Wow! that makes sense. I got an error on line 15,

on i.id = 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 i.id = 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.
0
 
LVL 50

Expert Comment

by:Lowfatspread
Comment Utility
oops

ORDER By Item.[Description]

should be
i.description
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now