Archive for August, 2009

Post

MDX – SOLVE order “Watch your calculations”

In SQL Server 2008 on August 27, 2009 by ashwaniroy

Have a look at this query

WITH

MEMBER [Measures].[REFUNDS] AS [Measures].[Internet Sales Amount] – [Measures].[Internet Sales Amount] * .4

MEMBER [Measures].[PROFIT] AS [Measures].[Internet Sales Amount] – [Measures].[REFUNDS]

MEMBER [Product].[Product].[Top10] AS

sum(

TOPCOUNT([Product].[Product].[Product].MEMBERS

, 10

, [Measures].[Internet Sales Amount]

)

,

([Measures].CurrentMember)

)

MEMBER [Product].[Product].[Top10PercOfTotal] AS [Product].[Product].[Top10]/[Product].[Product].[All Products]

SELECT

{

[Measures].[Internet Sales Amount],

[Measures].[REFUNDS],

[Measures].[PROFIT]

} ON COLUMNS,

{

[Product].[Product].[Top10]

,[Product].[Product].[Top10PercOfTotal]

,[Product].[Product].[All Products]

}

ON ROWS

FROM [Adventure Works]

This yeilds same result as yours ie. not a flat cell by cell division

 

Internet Sales Amount

REFUNDS

PROFIT

Top10

$10,355,525.92

$6,213,315.55

$4,142,210.37

Top10PercOfTotal

0.35272454

0.211634724

0.141089816

All Products

$29,358,677.22

$17,615,206.33

$11,743,470.89

 

I don’t expect it to produce this result . If the computations was going as planned the output should be 0.35272454  (35 %) for each.

I have altered the SOLVE ORDER (this is the property which determines the order of cell computation)

Here is modified query

WITH
MEMBER [Measures].[REFUNDS] AS [Measures].[Internet Sales Amount] - [Measures].[Internet Sales Amount] * .4,SOLVE_ORDER = 1
MEMBER [Measures].[PROFIT] AS   [Measures].[Internet Sales Amount] - [Measures].[REFUNDS],SOLVE_ORDER = 2
 
MEMBER [Product].[Product].[Top10] AS
 sum(
  TOPCOUNT([Product].[Product].[Product].MEMBERS
  , 10
  , [Measures].[Internet Sales Amount]
  )
  ,
  ([Measures].CurrentMember)
  )
,SOLVE_ORDER = 3
 
MEMBER [Product].[Product].[Top10PercOfTotal] AS 
[Product].[Product].[Top10]/[Product].[Product].[All Products]
, FORMAT_STRING = "Percent"
 , SOLVE_ORDER = 4
 
 
 
SELECT 
 {
  [Measures].[Internet Sales Amount],
  [Measures].[REFUNDS],
  [Measures].[PROFIT]
  } ON COLUMNS,
 {
[Product].[Product].[Top10]
,[Product].[Product].[Top10PercOfTotal]
,[Product].[Product].[All Products]
 
  } 
  ON ROWS
FROM [Adventure Works]

·

 

Internet Sales Amount

REFUNDS

PROFIT

Top10

$10,355,525.92

6213315.552

4142210.368

Top10PercOfTotal

35.27%

35.27%

35.27%

All Products

$29,358,677.22

$17,615,206.33

$11,743,470.89

 

There you go. SOLVE_ORDER determines the series of how cell computation  will be formed. One with smallest SOLVE_ORDER will be evaluated first.

Hope this explains SOLVE_ORDER and how it works.

Post

Thursday, 10th September – SQL Server User Group Meeting

In SQL Server 2008 on August 25, 2009 by ashwaniroy

The evening’s agenda includes the following presentations (see Events section for more details):

What’s new in SQL Server 2008 for BI

Presented by Ashwani Roy, MCTS MCITP MCAD MCP

We’re also pleased to let you know that SQL Server MVP, Simon Sabin will be presenting. Details about this presentation will be made available shortly.

 

Location:
Bank Of America 
5 Canada Square
London E14 5AQ
United Kingdom

Register @ http://www.sqlpass.org.uk/

Post

Favorite SQL Server Interview Questions

In Interview Questions,Microsoft SQL Server,SQL Server 2008 on August 18, 2009 by sqlapostle

Every interviewer has some favorite questions that he likes to ask in almost each and every interview. Here are some of mine, and a bit of advice for those who think these are easy, I would hold off on the answers till next week for you to try and take a guess at the answers (Without executing the commands on SQL Server, of course)

1. The Setup :

Employee

  • EmployeeId int
  • EmployeeName nvarchar(50)
  • Sex ( constraint M / F) char(1)
  • DepartmentId int

 

The requirement is to produce the following output in an optimized way

DepartmentId                          CountFemale                      CountMale                  EmployeeCount

 

2. The Setup :

USE PlayGround
GO
BEGIN TRAN
    Create Table Employee
    (
        EmployeeId int
        ,EmployeeName nvarchar(100)
    )
    exec xp_cmdshell ‘bcp Playground.dbo.Employee in c:\Employee.txt -E -c’
    TRUNCATE TABLE Employee
ROLLBACK TRAN
GO
SELECT * FROM Employee
GO

What is the expected result of the above script ?

3. A variation on the above :

USE PlayGround
GO
    Create Table Employee
    (
        EmployeeId int
        ,EmployeeName nvarchar(100)
    )
    Insert Into Employee Select 1, ‘TestEmployee’
GO

BEGIN TRAN
    TRUNCATE TABLE Employee
ROLLBACK TRAN
GO
SELECT * FROM Employee

4. Is it possible to insert into two tables having same structure using a single insert statement ?

Follow

Get every new post delivered to your Inbox.