Showing posts with label dates. Show all posts
Showing posts with label dates. Show all posts

Tuesday, March 27, 2012

Help with Calculated member

I'm trying to create a calculate member in SQL Server 2005 that will return a distinct count of clients year to date for all dates within my time dimension. I have figured out that if I create a measure that returns a distinct count then I can use the aggregate function and YTD function to get the distinct count YTD for any date. However, the aggregate function requires a slicer dimension (i think) and every time I put the mdx into the expression box, it does not like the syntax.

I have tried this:

With member [Authorization Service Date].[Calendar].[BlaBla] as

Aggregate(

YTD(

[Authorization Service Date].[Calendar].Current)

)

select [Authorization Service Date].[Calendar].[BlaBla] on columns

from dss

where [Measures].[Authorized Member Distinct Count]

does not like syntax

I have tried this:

Aggregate(

YTD(

[Authorization Service Date].[Calendar].Current)

)

select [Authorization Service Date].[Calendar].[BlaBla] on columns

from dss

where [Measures].[Authorized Member Distinct Count]

does not like syntax

and I have tried this:

Aggregate(

YTD(

[Authorization Service Date].[Calendar].Current)

)

returns error: Error 1 MdxScript(DSS) (67, 4) The function can be called only on a named set. 0 0

I don't know wht this means

Any help would be deeply appreciated

I think you are looking to use the CURRENTMEMBER function instead of CURRENT.

I think this will simply take you to your next challenge. If you run this query, you'll likely get NULL for your result.

I think something like this will better serve your needs:

Code Snippet

With member [Measures].[x] as

COUNT(

EXISTS(

[Customer].[Customer].[Customer].Members,

YTD([Delivery Date].[Calendar].CurrentMember),

'Internet Sales'

)

)

select

[Measures].[x] on columns

from [Adventure Works]

where [Delivery Date].[Date].[June 1, 2002]

;

|||

Bryan,

I appreciate your taking your time to assist me with this. Unfortunately, it did not work. As translated, your mdx on my system looks like

with member [Measures].[x] as

COUNT(

EXISTS(

[CCE Members].[MemberId].Members,

YTD([Authorization Service Date].[Calendar].CurrentMember),

'Authorization Service Days Count'

)
)

SELECT [Measures].[x] ON 0

FROM [dss]

WHERE [Authorization Service Date].[Full Date].&[6303]

I get x[:#]Error

Just for your info however, I know that all of the underlying attributes and metrics join up properly becuase when I execute:

SELECT [Measures].[Authorization Service Days Count] ON 0,

[CCE Members].[MemberId].Members ON 1

FROM [dss]

WHERE [Authorization Service Date].[Full Date].&[6303]

it works.

Again, your assistance is trully appreciated.

Thanks,

Michael

|||

Two things I noticed in the EXISTS function. Try changing the set to include just the leaf level members:

[CCE Members].[MemberId].Members --> [CCE Members].[MemberId].[MemberId].Members

Also, is 'Authorization Service Days Count' the name of the measure or the measure group? You need to identify the measure group.

B.

sql

Monday, March 26, 2012

Help with an Outer Join

Hello I'm tying to create query that selects data from two tables depending on the Employee and the range of dates. I have not used outer joins before and I either receive no data or receive too much data.

SELECT qad.NEW_USERS, qad.TRANSACTIONS, CONVERT( VARCHAR, qad.DATA_DATE, 101) DATA_DATE,
qas.QA_SCORE
FROM QA_SCORES qas FULL OUTER JOIN QA_DATA qad
ON (qas.EMPLOYEE_ID = qad.EMPLOYEE_ID AND qas.QA_DATE >= @.startDate AND qas.QA_DATE < @.endDate)
WHERE qad.DATA_DATE >= @.startDate AND qad.DATA_DATE < @.endDate
ORDER BY qad.DATA_DATE

Here are the tables:

QA_DATA:

QA_DATA_ID

EMPLOYEE_ID

NEW_USERS

TRANSACTIONS

DATA_DATE

4

11

0

0

12/1/2006

5

11

9

14

12/2/2006

6

1

2

3

1/1/2006

7

1

2

3

12/1/2006

8

11

6

18

12/3/2006

9

11

14

17

12/4/2006

10

11

7

16

12/5/2006

11

12

8

15

12/1/2006

12

12

0

0

12/3/2006

13

12

8

21

12/4/2006

QA_SCORES:

QA_ID

QA_DATE

QA_SCORE

EMPLOYEE_ID

1

1/18/2007

85

11

2

1/9/2007

83.01

11

Sometimes I receive this:

NEW_USERS

TRANSACTIONS

DATA_DATE

QASCORE

0

0

12/1/2006

85

0

0

12/1/2006

83.01

9

14

12/2/2006

83.01

9

14

12/2/2006

85

6

18

12/3/2006

85

6

18

12/3/2006

83.01

14

17

12/4/2006

83.01

14

17

12/4/2006

85

7

16

12/5/2006

85

7

16

12/5/2006

83.01

If the startDate = 12/01/2006 and the endDate = 12/31/2006 it should look like this:

NEW_USERS

TRANSACTIONS

DATA_DATE

QASCORE

0

0

12/1/2006

9

14

12/2/2006

6

18

12/3/2006

14

17

12/4/2006

7

16

12/5/2006

Because there are no QA_SCOREs in 2006

And if the start date is 1/01/2007 and the endDate is 1/31/2007 it should look like this:

NEW_USERS

TRANSACTIONS

DATA_DATE

QASCORE

1/9/2007

85

1/18/2007

83.01

I have tried quite a few things and just can't figure this out.

Any help is appreciated.

.

Milla:

(1) It appears that your data has the 85 score and the 83 score switched with each other. (2) Also, it appears to me that you have an additional filter criteria that you are probably leaving out -- probably a filter based on "EMPLOYEE_ID". Based on this guess I came up with this query that seems to return the results that you are looking for. I have no idea whether this is close or not, but maybe it will help:


declare @.filter_id integer
set @.filter_id = 11

SELECT qad.NEW_USERS,
qad.TRANSACTIONS,
CONVERT( VARCHAR, coalesce (qad.DATA_DATE, qas.qa_date), 101)
DATA_DATE,
qas.QA_SCORE
FROM QA_SCORES qas
FULL OUTER JOIN QA_DATA qad
ON qas.EMPLOYEE_ID = qad.EMPLOYEE_ID
AND qad.DATA_DATE >= @.startDate AND qad.DATA_DATE < @.endDate
AND qas.QA_DATE >= @.startDate AND qas.QA_DATE < @.endDate
WHERE coalesce (qad.DATA_DATE, qas.qa_date) >= @.startDate
AND coalesce (qad.DATA_DATE, qas.qa_date) < @.endDate
AND coalesce (qas.employee_id, qad.employee_id) = @.filter_id
ORDER BY coalesce (qad.DATA_DATE, qas.qa_date)


-- - Sample Output for 12/1/6 through 12/31/6: -

-- NEW_USERS TRANSACTIONS DATA_DATE QA_SCORE
-- -- --
-- 0 0 12/01/2006 NULL
-- 9 14 12/02/2006 NULL
-- 6 18 12/03/2006 NULL
-- 14 17 12/04/2006 NULL
-- 7 16 12/05/2006 NULL


-- - Sample Output for 1/1/7 through 1/31/7: -

-- NEW_USERS TRANSACTIONS DATA_DATE QA_SCORE
-- -- --
-- NULL NULL 01/09/2007 83.01
-- NULL NULL 01/18/2007 85.00


Dave

|||

Milla:

(If this post is a duplicate, I am sorry; it seems that my post has been lost.)

It appears to me that your query is missing a filter -- probably based on EMPLOYEE_ID. Based on this guess, the following query seems to return the correct data:


declare @.filter_id integer
set @.filter_id = 11

SELECT qad.NEW_USERS,
qad.TRANSACTIONS,
CONVERT( VARCHAR, coalesce (qad.DATA_DATE, qas.qa_date), 101)
DATA_DATE,
qas.QA_SCORE
FROM QA_SCORES qas
FULL OUTER JOIN QA_DATA qad
ON qas.EMPLOYEE_ID = qad.EMPLOYEE_ID
AND qad.DATA_DATE >= @.startDate AND qad.DATA_DATE < @.endDate
AND qas.QA_DATE >= @.startDate AND qas.QA_DATE < @.endDate
WHERE coalesce (qad.DATA_DATE, qas.qa_date) >= @.startDate
AND coalesce (qad.DATA_DATE, qas.qa_date) < @.endDate
AND coalesce (qas.employee_id, qad.employee_id) = @.filter_id
ORDER BY coalesce (qad.DATA_DATE, qas.qa_date)


-- - Sample Output for 12/1/6 through 12/31/6: -

-- NEW_USERS TRANSACTIONS DATA_DATE QA_SCORE
-- -- --
-- 0 0 12/01/2006 NULL
-- 9 14 12/02/2006 NULL
-- 6 18 12/03/2006 NULL
-- 14 17 12/04/2006 NULL
-- 7 16 12/05/2006 NULL


-- - Sample Output for 1/1/7 through 1/31/7: -

-- NEW_USERS TRANSACTIONS DATA_DATE QA_SCORE
-- -- --
-- NULL NULL 01/09/2007 83.01
-- NULL NULL 01/18/2007 85.00

|||

Hi,

Can you please tell what you want to do in text?

By setting the WHERE clause like this, you are not making a FULL OUTER JOIN but a RIGHT OUTER JOIN.

Why? For all the records that are in QA_SCORES and that have no associated record in QA_DATA, the field QA_DATA.DATA_DATE will always be NULL. Therefor, these records can not pass the WHERE clause.

You can prevent this by changing the WHERE keyword to the AND keyword so that the condition is included in the JOIN.

Greetz,

Geert

Geert Verhoeven
Consultant @. Ausy Belgium

My Personal Blog

|||

Take a look at your WHERE clause:

SELECT qad.NEW_USERS, qad.TRANSACTIONS,
CONVERT( VARCHAR, qad.DATA_DATE, 101) DATA_DATE,
qas.QA_SCORE
FROM QA_SCORES qas
FULL OUTER JOIN QA_DATA qad
ON (qas.EMPLOYEE_ID = qad.EMPLOYEE_ID
AND qas.QA_DATE >= @.startDate AND qas.QA_DATE < @.endDate)
WHERE qad.DATA_DATE >= @.startDate AND qad.DATA_DATE < @.endDate
ORDER BY qad.DATA_DATE

This where clause will apply to all rows that are returned from the FROM clause. So you will not receive qas rows where the qad.DATA_DATE is not greater than @.startDate. So any null rows would be eliminated from the set. You can put the WHERE clause stuff up in the ON clause and this won't affect how rows match in the qas set.

|||Since you didn't tell us what you are trying to get as a result, use "LEFT OUTER JOIN" instead of "FULL OUTER JOIN", that will probably get you closer.|||

Thanks for the replies. What you are saying makes sence.

But I still can't seem to get it to work.

Here is what I have now:

declare @.startDate datetime
declare @.endDate datetime
set @.startDate = 1/1/2007
set @.endDate = 1/31/2007

SELECT emp.NAME NAME, qa.NEW_USERS NewUsers, qa.TRANSACTIONS Trans,
CONVERT( VARCHAR, coalesce (qa.DATA_DATE, qas.qa_date), 101) DATA_DATE, qas.QA_SCORE
FROM EMPLOYEE emp INNER JOIN
QA_DATA qa ON (emp.EMPLOYEE_ID = qa.EMPLOYEE_ID AND emp.REPORTABLE = 1) FULL OUTER JOIN QA_SCORES qas
ON (qas.EMPLOYEE_ID = qa.EMPLOYEE_ID
AND qa.DATA_DATE >= @.startDate AND qa.DATA_DATE < @.endDate
AND qas.QA_DATE >= @.startDate AND qas.QA_DATE < @.endDate)

No matter what dates I enter it returns all rows. I'm guessing it's because there is no where statement, but if I enter a where statement it returns no rows.

Here is what it is returning:

Allard

Rich

0

0

12/1/2006

NULL

Allard

Rich

9

14

12/2/2006

NULL

Allard

Rich

6

18

12/3/2006

NULL

Allard

Rich

14

17

12/4/2006

NULL

Allard

Rich

7

16

12/5/2006

NULL

Bass

Gary

8

15

12/1/2006

NULL

Bass

Gary

0

0

12/3/2006

NULL

Bass

Gary

8

21

12/4/2006

NULL

NULL

NULL

NULL

1/18/2007

85

NULL

NULL

NULL

1/9/2007

83.01

This is what it should look like:

NULL

NULL

NULL

1/18/2007

85

NULL

NULL

NULL

1/9/2007

83.01

And if I change the dates to 12/1/2007 and 12/31/2007 the result should be this:

Allard

Rich

0

0

12/1/2006

NULL

Allard

Rich

9

14

12/2/2006

NULL

Allard

Rich

6

18

12/3/2006

NULL

Allard

Rich

14

17

12/4/2006

NULL

Allard

Rich

7

16

12/5/2006

NULL

Bass

Gary

8

15

12/1/2006

NULL

Bass

Gary

0

0

12/3/2006

NULL

Bass

Gary

8

21

12/4/2006

NULL

I know this is probably an easy fix but I can't seem to figure out what I'm doing wrong.

Thanks in advance!

|||

Another thing I just noticed is it is returning :

NULLNULLNULL1/18/200785
NULLNULLNULL1/9/200783.01

and it should look like this:

Allard, Rich NULL NULL 1/18/2007 85
Allard, Rich NULL NULL 1/9/2007 83.01

|||

Milla:

I used the following data:

insert into QA_DATA values (4, 11, 0, 0, '12/1/2006' )
insert into QA_DATA values (5, 11, 9, 14, '12/2/2006' )
insert into QA_DATA values (6, 1, 2, 3, '1/1/2006' )
insert into QA_DATA values (7, 1, 2, 3, '12/1/2006' )
insert into QA_DATA values (8, 11, 6, 18, '12/3/2006' )
insert into QA_DATA values (9, 11, 14, 17, '12/4/2006' )
insert into QA_DATA values (10, 11, 7, 16, '12/5/2006' )
insert into QA_DATA values (11, 12, 8, 15, '12/1/2006' )
insert into QA_DATA values (12, 12, 0, 0, '12/3/2006' )
insert into QA_DATA values (13, 12, 8, 21, '12/4/2006' )

insert into dbo.QA_SCORES values (1, '1/18/2007', 85, 11 )
insert into dbo.QA_SCORES values (2, '1/9/2007', 83.01, 11 )

truncate table dbo.employee
insert into employee values (1, 'Employee #1')
insert into employee values (11, 'Allard, Rich')
insert into employee values (12, 'Bass, Gary')

with this query:

declare @.startDate datetime
declare @.endDate datetime
--set @.startDate = '12/1/6'
--set @.endDate = '12/31/6'
set @.startDate = '1/1/7'
set @.endDate = '1/31/7'

SELECT emp.[name],
qad.NEW_USERS,
qad.TRANSACTIONS,
CONVERT( VARCHAR, coalesce (qad.DATA_DATE, qas.qa_date), 101)
DATA_DATE,
qas.QA_SCORE
FROM QA_SCORES qas
FULL OUTER JOIN QA_DATA qad
ON qas.EMPLOYEE_ID = qad.EMPLOYEE_ID
AND qad.DATA_DATE >= @.startDate AND qad.DATA_DATE < @.endDate
AND qas.QA_DATE >= @.startDate AND qas.QA_DATE < @.endDate
inner join employee emp
on coalesce (qas.employee_id, qad.employee_id) = emp.employee_id
WHERE coalesce (qad.DATA_DATE, qas.qa_date) >= @.startDate
AND coalesce (qad.DATA_DATE, qas.qa_date) < @.endDate
ORDER BY EMP.[name], coalesce (qad.DATA_DATE, qas.qa_date)

to get these results:

name NEW_USERS TRANSACTIONS DATA_DATE QA_SCORE
-- --
Allard, Rich NULL NULL 01/09/2007 83.01
Allard, Rich NULL NULL 01/18/2007 85.00

name NEW_USERS TRANSACTIONS DATA_DATE QA_SCORE
-- --
Allard, Rich 0 0 12/01/2006 NULL
Allard, Rich 9 14 12/02/2006 NULL
Allard, Rich 6 18 12/03/2006 NULL
Allard, Rich 14 17 12/04/2006 NULL
Allard, Rich 7 16 12/05/2006 NULL
Bass, Gary 8 15 12/01/2006 NULL
Bass, Gary 0 0 12/03/2006 NULL
Bass, Gary 8 21 12/04/2006 NULL
Employee #1 2 3 12/01/2006 NULL

|||

Not sure what I did the first time that made it not work but the last one you entered is working for me.

Thanks Waldrop!

|||

You didn't do anything wrong the first time; my guess was wrong! Remember: I made an assertion that the employee_id was 11 -- and this was not correct. When you gave some more information it became apparent that my guess was not correct and I dropped this part from my version of the query. Glad it worked out!


Dave

Help with an MS SQL Server Query to return the latest dates against each recordID.

The following SQL query :-

SELECT CardHolder.RecordID, History.GenTime, History.Link1
FROM History FULL OUTER JOIN
Card ON History.Param3 =
LTRIM(RTRIM(Card.CardNumber)) FULL OUTER JOIN
CardHolder ON Card.CardHolderID =
CardHolder.RecordID
WHERE (Card.Deleted = 0) AND (History.GenTime IS NOT NULL)
ORDER BY CardHolder.RecordID, History.GenTime DESC

returns :-

RecordID GenTime Link1

2 04/06/2004 15:30:00 123
2 01/06/2004 16:00:00 123
2 01/06/2004 08:00:00 110
1155 02/06/2004 11:30:00 123
1155 02/06/2004 08:00:00 110
3925 03/06/2004 09:00:00 123
3925 03/06/2004 08:00:00 110
4511 06/06/2004 11:30:00 123
4511 06/06/2004 10:30:00 110

Is there a way of modifying this query to just return the lastest
genTime for each RecordID ? ie return just rows 1,4,6 & 8.

I assume it is something to do with MAX, but I can't seem to get my
head around it.

Any help, or pointers would be appreciated.

Oh, running query on Microsoft SQL Server 2000.

Regards
DaveHere's one possible method (untested). Note that I've changed the first of
your joins to INNER because your WHERE clause makes the FULL JOIN redundant.

SELECT CH.recordid, H.gentime, H.link1
FROM History AS H
JOIN Card AS C
ON H.param3 = LTRIM(RTRIM(C.cardnumber))
AND H.gentime =
(SELECT MAX(gentime)
FROM History
WHERE param3 = H.param3)
FULL OUTER JOIN CardHolder AS CH
ON C.cardholderid = CH.recordid
WHERE C.deleted = 0
AND H.gentime IS NOT NULL
ORDER BY cardholder.recordid, H.gentime DESC

--
David Portas
SQL Server MVP
--|||
David,

Many many thanks for you help - that did the trick.

Simple now that I've seen how you've approached it.

Regards,

Dave

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!|||Hi

Having Card Holder in this seems to be redundant!

This will also assume that H.GenTime is unique..

SELECT C.CardHolderID, H.GenTime, H.Link1
FROM History H
FULL OUTER JOIN Card C ON H.Param3 = LTRIM(RTRIM(C.CardNumber))
WHERE C.Deleted = 0
AND H.GenTime IS NOT NULL
AND H.GenTime = ( SELECT MAX(I.GenTime) FROM History I WHERE I.Param3 =
H.Param3 )
ORDER BY C.CardHolderID, H.GenTime DESC

John

"DaveHayzen" <dhayzen@.ersltd.co.uk> wrote in message
news:f60a2659.0406180143.66d459ca@.posting.google.c om...
> The following SQL query :-
> SELECT CardHolder.RecordID, History.GenTime, History.Link1
> FROM History FULL OUTER JOIN
> Card ON History.Param3 =
> LTRIM(RTRIM(Card.CardNumber)) FULL OUTER JOIN
> CardHolder ON Card.CardHolderID =
> CardHolder.RecordID
> WHERE (Card.Deleted = 0) AND (History.GenTime IS NOT NULL)
> ORDER BY CardHolder.RecordID, History.GenTime DESC
> returns :-
> RecordID GenTime Link1
> 2 04/06/2004 15:30:00 123
> 2 01/06/2004 16:00:00 123
> 2 01/06/2004 08:00:00 110
> 1155 02/06/2004 11:30:00 123
> 1155 02/06/2004 08:00:00 110
> 3925 03/06/2004 09:00:00 123
> 3925 03/06/2004 08:00:00 110
> 4511 06/06/2004 11:30:00 123
> 4511 06/06/2004 10:30:00 110
> Is there a way of modifying this query to just return the lastest
> genTime for each RecordID ? ie return just rows 1,4,6 & 8.
> I assume it is something to do with MAX, but I can't seem to get my
> head around it.
> Any help, or pointers would be appreciated.
> Oh, running query on Microsoft SQL Server 2000.
> Regards
> Dave