Solved

MS SQL Server cube/Rollup query

Posted on 2015-01-23
4
61 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 total points
ID: 40567039
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
ID: 40567446
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
ID: 40567457
oops

ORDER By Item.[Description]

should be
i.description
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

738 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