Showing posts with label date. Show all posts
Showing posts with label date. Show all posts

Thursday, March 29, 2012

count records in several months

Hi,

I've a small problem. I have a table in which one column is date. I want to
count the records for statiscs in a temptable grouped by months lets say 12
months back.
e.g.
month 1 counts 164 rec month 2 counts 87 records and so on.
I tried to solve this like this with a function SELECT COUNT(*) FROM TABLE
WHERE DATEDIFF(m,Col1,GETDATE())=@.counter.
But I don't know how to get this thing count from 0 up to 11 to get this
thing recursive.
Does anyone know how to tackel my problem? I wouls apreciate any answer.
Greetz to you allYou need counts based on months? If your date column is datetime datatype,
something like this could work:

select count(*) as No, month(col1) as month, year (col1) as year
from table
where datediff(m, col1, getdate()) <= 12 -- if you want only last 12 months
group by month(col1) as month, year (col1) as year

If you posted create statements and some sample data I could test this...

MC

"Sjef ten Koppel" <sjeftenkoppel@.home.nlwrote in message
news:er97ad$qkb$1@.news6.zwoll1.ov.home.nl...

Quote:

Originally Posted by

Hi,
>
I've a small problem. I have a table in which one column is date. I want
to count the records for statiscs in a temptable grouped by months lets
say 12 months back.
e.g.
month 1 counts 164 rec month 2 counts 87 records and so on.
I tried to solve this like this with a function SELECT COUNT(*) FROM TABLE
WHERE DATEDIFF(m,Col1,GETDATE())=@.counter.
But I don't know how to get this thing count from 0 up to 11 to get this
thing recursive.
Does anyone know how to tackel my problem? I wouls apreciate any answer.
Greetz to you all

|||Sjef ten Koppel (sjeftenkoppel@.home.nl) writes:

Quote:

Originally Posted by

Oops you are fast! thank you.
I could send you a create sql but I don't know how to extract sample data
from my db see att.


It does not have to be real-world data, you could just fill in some
sample data, and tell us what the result you want from the sample. That
helps to clarify your question.

But did not the query that Macro posted fit your needs?

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Great I tried your solution. It works great. Thank you!!
"MC" <marko.NOSPAMculo@.gmail.comschrieb im Newsbeitrag
news:er983u$64q$1@.ss408.t-com.hr...

Quote:

Originally Posted by

You need counts based on months? If your date column is datetime datatype,
something like this could work:
>
select count(*) as No, month(col1) as month, year (col1) as year
from table
where datediff(m, col1, getdate()) <= 12 -- if you want only last 12
months
group by month(col1) as month, year (col1) as year
>
If you posted create statements and some sample data I could test this...
>
>
MC
>
>
"Sjef ten Koppel" <sjeftenkoppel@.home.nlwrote in message
news:er97ad$qkb$1@.news6.zwoll1.ov.home.nl...

Quote:

Originally Posted by

>Hi,
>>
>I've a small problem. I have a table in which one column is date. I want
>to count the records for statiscs in a temptable grouped by months lets
>say 12 months back.
>e.g.
>month 1 counts 164 rec month 2 counts 87 records and so on.
>I tried to solve this like this with a function SELECT COUNT(*) FROM
>TABLE WHERE DATEDIFF(m,Col1,GETDATE())=@.counter.
>But I don't know how to get this thing count from 0 up to 11 to get this
>thing recursive.
>Does anyone know how to tackel my problem? I wouls apreciate any answer.
>Greetz to you all


>
>

|||Hallo,

Probeer dit eens:

select
YYYYMM=
(case
when datepart(m,Col1) < 10
then convert(int,convert(char(4),datepart(yyyy,Col1))+ '0'+convert(char(1),datepart(m,Col1)))
else convert(int,convert(char(4),datepart(yyyy,Col1))+c onvert(char(2),datepart(m,Col1)))
end), count(*) as [COUNT] from Table1

group by

(case
when datepart(m,Col1) < 10
then convert(int,convert(char(4),datepart(yyyy,Col1))+ '0'+convert(char(1),datepart(m,Col1)))
else convert(int,convert(char(4),datepart(yyyy,Col1))+c onvert(char(2),datepart(m,Col1)))
end)

order by

(case
when datepart(m,Col1) < 10
then convert(int,convert(char(4),datepart(yyyy,Col1))+ '0'+convert(char(1),datepart(m,Col1)))
else convert(int,convert(char(4),datepart(yyyy,Col1))+c onvert(char(2),datepart(m,Col1)))
end)

Groeten,

Wim Venema
delerium@.chello.nl
"Sjef ten Koppel" <sjeftenkoppel@.home.nlwrote in message news:er97ad$qkb$1@.news6.zwoll1.ov.home.nl...
Hi,

I've a small problem. I have a table in which one column is date. I want to
count the records for statiscs in a temptable grouped by months lets say 12
months back.
e.g.
month 1 counts 164 rec month 2 counts 87 records and so on.
I tried to solve this like this with a function SELECT COUNT(*) FROM TABLE
WHERE DATEDIFF(m,Col1,GETDATE())=@.counter.
But I don't know how to get this thing count from 0 up to 11 to get this
thing recursive.
Does anyone know how to tackel my problem? I wouls apreciate any answer.
Greetz to you allsql

Count on a date column

All,
I need to count dates in a column but how can i cut of the time hour minutes and seconds?
I need to rport how many records hav been added on one date...
con someone help me getting on the richt track?
regards
select count(*)
from tbl
where convert(varchar(8),dte,112) = '20040515'
select count(*)
from tbl
where dte >= '20040515' and dte < '20040515'
select dte = convert(varchar(8),dte,112), num = count(*)
from tbl
group by convert(varchar(8),dte,112)
order by convert(varchar(8),dte,112)
one of those should help.
Nigel Rivett
www.nigelrivett.net
*** Sent via Developersdex http://www.codecomments.com ***
Don't just participate in USENET...get rewarded for it!

Tuesday, March 27, 2012

Count of employees

Hi,

We got of count of employees from measures against a date dimension.

we need to get average count for a time period (ie..week,quarter,year ).

and the formula for avg employee count: (empl-count on firstday of period+empl-count on last dayof period)/2

Date EMPCount

for ex : 1-Nov-2005 2361

2-Nov-2005 2521

3-Nov-2005 2762

4-Nov-2005 2500

avg count for week in novemeber: 2361+2500/2.

Kindly let me know how we can do this in SSAS cube.

Thanks in advance

Raj

I can think of a couple of ways of doing this, I don't have a descent sample set to perfomance test against, I suspect the second method may be faster, specially at higher level as it will not have to evaluate a large set of days.

Method "a" gets the descendants of the current time member and grabs the first and last member of that set to average them.

create member measures.a as ((Head({descendants([Time].[Financial].CurrentMember,[Time].[Financial].[Financial Date]) as mths},1).item(0),Measures.Amount) + (TAIL(mths,1).item(0),Measures.Amount))/2

Method "b" uses 2 recursive functions to walk down the time hierarchy to grab the first and last member underneath the currentmember. The third calculation then simple adds the first 2 and divides by 2.

create member measures.bhead as iif([Time].Financial.CurrentMember.Level is [Time].[Financial].[Financial Date],Measures.Amount,([Time].Financial.CurrentMember.FirstChild,Measures.Measures.bHead))

create member measures.btail as iif([Time].Financial.CurrentMember.Level is [Time].[Financial].[Financial Date],Measures.Amount,([Time].Financial.CurrentMember.LastChild,Measures.Measures.bTail))

create member measures.b as (measures.bhead + measures.btail)/2

|||

Hi,

I tried with method "b" it's showing "#Value!"

We have server created time Dimension is it because of that ?

or we are getting active employees count thro named query?

server created time dimension is "atrntime" and dimension attributes are Date,year,week,day of week,day of year.

please help

thanks

|||

No, neither of those things should stop the query from working. I did not have access to Adventure Works when I posted the last sample, so I had to remove some client specific stuff from the sample I sent. Below is an actual working query that will run against the Adventure Works sample database.

WITH

member measures.DateHead as iif([Date].Fiscal.CurrentMember.Level is [Date].[Fiscal].[Date]
,[Measures].[Internet Order Count]
,([Date].Fiscal.CurrentMember.FirstChild,measures.DateHead)
)

member measures.DateTail as iif([Date].Fiscal.CurrentMember.Level is [Date].[Fiscal].[Date]
,[Measures].[Internet Order Count]
,([Date].Fiscal.CurrentMember.LastChild,measures.DateTail)
)

member measures.AvgOrderCnt as (measures.DateHead + measures.DateTail)/2

SELECT
{Measures.DateHead
,Measures.DateTail
,Measures.AvgOrderCnt
,Measures.[Internet Order Count]} ON COLUMNS

,[Date].Fiscal.Month.Members ON ROWS

FROM [Adventure Works]

If you are still having issues, you may find that displaying the results of the two underlying measures may help to diagnose any issues. If you are still unable to resolve the #value problem, try posting your calcuations and I (or someone else) may be able to spot the issue.

|||

Hi,

Thanks for the answer.

I tried with this calculated measure

CREATE MEMBER CURRENTCUBE.[MEASURES].DateHead
AS iif([AtrnTime].[drill].CurrentMember.Level is [AtrnTime].[drill].[Week]
,[Measures].[ActiveEmpl]
,([AtrnTime].[drill].CurrentMember.FirstChild,measures.DateHead)
);
and the output from cube browser was

Active Empl

DateHead

Calendar 2006

Week 25, 2006

Monday, June 19 2006

2368

Tuesday, June 20 2006

2369

Thursday, June 22 2006

2367

Friday, June 23 2006

2365

Saturday, June 24 2006

2364

Total

2375

2375

Week 26, 2006

Monday, June 26 2006

2374

Tuesday, June 27 2006

2373

Wednesday, June 28 2006

2372

Total

2375

2376

Total

2387

but the expected answer for datehead measure was
2368 for week 25 and 2374 for week 26

and one more thing, i was not able to figure it out is total :2375 for wk25 and 2375 for week 26 (it's not the average also....)

We have sever created time dimension "AtrnTime" and "drill" is the herarchy defined as year-week-date

thanks

Raj

|||

I think what you are getting is the distinct count for the week and I think what you are after for the DateHead measure is the count for the first day. By putting the Week Level/Attribute in the test for the IIF clause, you have effectively stopped the recursion there. Changing the level in the test for the iif clause should give you the result you are after.

CREATE MEMBER CURRENTCUBE.[MEASURES].DateHead
AS iif([AtrnTime].[drill].CurrentMember.Level is [AtrnTime].[drill].[Date]
,[Measures].[ActiveEmpl]
,([AtrnTime].[drill].CurrentMember.FirstChild,measures.DateHead)
);

I could possibly have coded my example better to show what I was intending, by using the IsLeaf() function, maybe the following is a better way of coding this measure.

CREATE MEMBER CURRENTCUBE.[MEASURES].DateHead
AS iif( IsLeaf([AtrnTime].[drill].CurrentMember)
,[Measures].[ActiveEmpl]
,([AtrnTime].[drill].CurrentMember.FirstChild,measures.DateHead)
);

This will make the measure recurse down until it hits the leaf level of the hierarchy.

|||

Hi,

Yeah you were right, i was getting Distinct Count for ActiveEmpl measure and i am after getting count for the first day.

i tried executing bothe the MDX scripts. It works at the day level but when i aggregate to the week level i should get the count of first day in that week .. but i am getting blank in that place.

CREATE MEMBER CURRENTCUBE.[MEASURES].DateHead1

AS iif( IsLeaf([AtrnTime].[drill].CurrentMember)

,[Measures].[ActiveEmpl]

,([AtrnTime].[drill].CurrentMember.FirstChild,measures.DateHead1)

);

CREATE MEMBER CURRENTCUBE.[MEASURES].DateHead2

AS iif([AtrnTime].[drill].CurrentMember.Level is [AtrnTime].[drill].[date]

,[Measures].[ActiveEmpl]

,([AtrnTime].[drill].CurrentMember.FirstChild,measures.DateHead2)

);

and the output of that was

Active Employees

DateHead1

DateHead2

Calendar 2006

Week 25, 2006

Monday, June 19 2006

2368

2368

2368

Tuesday, June 20 2006

2369

2369

2369

Thursday, June 22 2006

2367

2367

2367

Friday, June 23 2006

2365

2365

2365

Saturday, June 24 2006

2364

2364

2364

Total

2375

Week 26, 2006

Monday, June 26 2006

2374

2374

2374

Tuesday, June 27 2006

2373

2373

2373

Wednesday, June 28 2006

2372

2372

2372

Total

2375

Total

2387

but when i aggregate to the week level datehead1 and datehead2 was blank as below.

Active Employees

DateHead1

DateHead2

Calendar 2006

Week 25, 2006

2375

Week 26, 2006

2375

Total

2387

datehead1/datehead2 should be 2368 for wk25 and 2374 for wk26

thanks in advance..

|||

Would I be right if I were to guess that your week starts on Sunday, which normally does not have any data? I'm guessing that members without data are probably what is causing the blanks here. There are probably a number of ways of dealing with this, we could nest another IIF clause to effectively "walk" along the siblings at the day level, looking for a non-empty one, but I don't think that would be terribly efficient.

We could grab all the siblings at the date level and return the first non-empty.

eg.

CREATE MEMBER CURRENTCUBE.[MEASURES].DateHead1
AS iif( IsLeaf([AtrnTime].[drill].CurrentMember)
,HEAD(NONEMPTY([AtrnTime].[drill].CurrentMember.Siblings, {[Measures].[ActiveEmpl]}),1)
,([AtrnTime].[drill].CurrentMember.FirstChild,measures.DateHead1)
);

But if we have to deal with sets of members and finding non-empty children it might be better not to use recursion and to grab all the non-empty descendants of the time dimension.

CREATE MEMBER CURRENTCUBE.[MEASURES].DateHead1
AS HEAD(NONEMPTY(Descendants([AtrnTime].[drill].CurrentMember
,[AtrnTime].[drill].[Date]), {[Measures].[ActiveEmpl]}),1)
;

And if we are going down that path I would suggest looking into coding the whole thing into one measure so that you do not have to do the nonempty twice (once for the first day and once for the last day). You can do this by naming the non empty set and re-using it.

eg

CREATE MEMBER CURRENTCUBE.[MEASURES].Avg
AS (HEAD(
NONEMPTY(Descendants([AtrnTime].[drill].CurrentMember
,[AtrnTime].[drill].[Date]) * {[Measures].[ActiveEmpl]}) AS NonEmptySet
,1).Item(0)
+
TAIL( NonEmptySet
,1).Item(0))
/ 2;


;

|||

yeah your guess was right ,,sunday was the starting day of the week, and the blank row was because of no data for that day..,, finally the avg query worked which takes head/tail of nonempty set.

Thanks a lot Darren.

Thursday, March 22, 2012

COUNT and TOP

I want to do something like this:

SELECT COUNT (SELECT TOP(10) * FROM MyTable order by Date Desc) FROM MyTable where User = 'Scott'

What I want is to return the number of affected rows where the column 'User' equals 'Scott'...But is should only check in the 10 latest inserted rows....

Hope you understand what I mean...

when I want to do things like that especially if performance isnt completly critical, I just take the easier to read approach and do a sub query.

Select Count(*)
FROM ( Select Top(10) * From... ) As MyAlias

That way you know for sure things will be working out as you are thinking them.|||

Hi Tigers21,

Use the following: -

SELECT COUNT(*) FROM MyTable WHERE MyTableUniqueFieldID IN
(
SELECT TOP 10 MyTableUniqueFieldID FROM MyTable
ORDER BY [DATE] DESC
)
AND [User] = 'scott'

Substitute MyTableUniqueFieldID with the primary key field of the MyTable table.

Kind regards

Scotty

|||

Thanks for your answers!...

Both ways seems to work correct for me, but which one is best for the performance?

|||

Shados has the better solution.

Count and Group By with DateTime!

Hi all,

I have a problem with my query which is suppose to select count posts group by the date, the query works but doesnt return the count as i want, i think the problem that the datetime column contains also Time in hours which ofcourse isnt same in all rows in same day, so i dont know what to do, Here's my Query:

SELECT

PostID,Date,COUNT(Date)AS'TotalPosts'
FROMPosts
GROUP BYPostID,Date

Thank you for help

I didn't have that table available but I tested the same query against Sales.SalesOrderDetail table in AdventureWorks sample database. I believe you get the idea from it

SELECT Count(*) TheCount, [Day]
FROM (
SELECT
CAST(
CAST(MONTH(ModifiedDate) AS varchar(2) ) + '/' +
CAST(DAY(ModifiedDate) AS varchar(2) ) + '/' +
CAST(YEAR(ModifiedDate) AS varchar(4) )
AS datetime
)
as [Day]
FROM Sales.SalesOrderDetail

) TMP
GROUP BY [Day]
ORDER BY [Day]

Just change the table and field names respectively in the query

|||AmazingYes, Thank you alot

Sunday, February 19, 2012

Could not adjust the space allocation message

Hi all -
Recieve the message
Event Type: Information
Event Source: MSSQLSERVER
Event Category: (2)
Event ID: 17055
Date: 09/08/2005
Time: 10:13:47 AM
User: N/A
Computer: JackBox
Description:
17052 :
Could not adjust the space allocation for file 'Jack_Data_1'.
The machine is win2K sp4 running sql2K sp3. Could not find this error on
support.microsoft.com - could anyone explain the meaning?
Thanks.
TitoHi
Maybe something like:
http://support.microsoft.com/default.aspx?scid=kb;en-us;254253
What maintainance are you carrying out?
You may want to stop autoshrink if it is on!
John
"Tito Madrid" <tito_madrid@.yahoo.com> wrote in message
news:uRupdzJtFHA.3328@.TK2MSFTNGP11.phx.gbl...
> Hi all -
> Recieve the message
> Event Type: Information
> Event Source: MSSQLSERVER
> Event Category: (2)
> Event ID: 17055
> Date: 09/08/2005
> Time: 10:13:47 AM
> User: N/A
> Computer: JackBox
> Description:
> 17052 :
> Could not adjust the space allocation for file 'Jack_Data_1'.
>
> The machine is win2K sp4 running sql2K sp3. Could not find this error on
> support.microsoft.com - could anyone explain the meaning?
> Thanks.
> Tito
>|||John - Thanks for the link - I really did try the site first, but only got
off-point results.
I did have autoshrink turned on - it is now off - thanks. I also re-index
and integrity check it nightly.
Dave
"John Bell" <jbellnewsposts@.hotmail.com> wrote in message
news:%23W2cPtKtFHA.3548@.TK2MSFTNGP11.phx.gbl...
> Hi
> Maybe something like:
> http://support.microsoft.com/default.aspx?scid=kb;en-us;254253
> What maintainance are you carrying out?
> You may want to stop autoshrink if it is on!
> John
> "Tito Madrid" <tito_madrid@.yahoo.com> wrote in message
> news:uRupdzJtFHA.3328@.TK2MSFTNGP11.phx.gbl...
> > Hi all -
> >
> > Recieve the message
> >
> > Event Type: Information
> > Event Source: MSSQLSERVER
> > Event Category: (2)
> > Event ID: 17055
> > Date: 09/08/2005
> > Time: 10:13:47 AM
> > User: N/A
> > Computer: JackBox
> > Description:
> > 17052 :
> > Could not adjust the space allocation for file 'Jack_Data_1'.
> >
> >
> >
> > The machine is win2K sp4 running sql2K sp3. Could not find this error
on
> > support.microsoft.com - could anyone explain the meaning?
> >
> > Thanks.
> >
> > Tito
> >
> >
>|||Hi
I think the message would probably appear whilst an activity was happening
therefore that is why I think it may be autoshink. You don't say how
reguarly this occurred but if you monitor the event file for this you may
see if it happens again.
John
"Tito Madrid" <tito_madrid@.yahoo.com> wrote in message
news:OK%23qYrLtFHA.1136@.TK2MSFTNGP12.phx.gbl...
> John - Thanks for the link - I really did try the site first, but only got
> off-point results.
> I did have autoshrink turned on - it is now off - thanks. I also re-index
> and integrity check it nightly.
> Dave
> "John Bell" <jbellnewsposts@.hotmail.com> wrote in message
> news:%23W2cPtKtFHA.3548@.TK2MSFTNGP11.phx.gbl...
>> Hi
>> Maybe something like:
>> http://support.microsoft.com/default.aspx?scid=kb;en-us;254253
>> What maintainance are you carrying out?
>> You may want to stop autoshrink if it is on!
>> John
>> "Tito Madrid" <tito_madrid@.yahoo.com> wrote in message
>> news:uRupdzJtFHA.3328@.TK2MSFTNGP11.phx.gbl...
>> > Hi all -
>> >
>> > Recieve the message
>> >
>> > Event Type: Information
>> > Event Source: MSSQLSERVER
>> > Event Category: (2)
>> > Event ID: 17055
>> > Date: 09/08/2005
>> > Time: 10:13:47 AM
>> > User: N/A
>> > Computer: JackBox
>> > Description:
>> > 17052 :
>> > Could not adjust the space allocation for file 'Jack_Data_1'.
>> >
>> >
>> >
>> > The machine is win2K sp4 running sql2K sp3. Could not find this error
> on
>> > support.microsoft.com - could anyone explain the meaning?
>> >
>> > Thanks.
>> >
>> > Tito
>> >
>> >
>>
>