Solved

MS SQL Server cube/Rollup query

Posted on 2015-01-23
4
48 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
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

861 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

23 Experts available now in Live!

Get 1:1 Help Now