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
Solved

MS SQL Server cube/Rollup query

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Re-appearing SQL Server Agent jobs 7 29
Query for timesheet application 3 18
Run Stored Procedure uisng ADO 5 21
Isolation level setting TSQL View 10 29
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

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