Showing posts with label calculate. Show all posts
Showing posts with label calculate. Show all posts

Friday, March 23, 2012

Number of records in table

Hi!
How I can calculate number of records in the table which has filtering?
E.g., DataSet "dsABC" return 1000 rows. After filtering table contains 300 rows.
Function:
= CountRows("dsABC") return 1000.
How I can get 300?

Alexey,

I'm not sure I completely understand your question. Do you want the number of rows to show up on the report? Is this correct?

|||Thanks. It's correct.
I found workaround - create expression in the table footer (e.g. texbox name "TableRows") :
= Count()
Then in the textbox on the report header area I create exppression:
=ReportItems!TableRows.Value
It's work!

Wednesday, March 21, 2012

Number of days in previous quarter?

Hello
I need to calculate the number of days in the previous quarter relative to
'today'.
I thought it would be as simple as the number of days between today and one
quarter earlier:
datediff(dd,dateadd(qq,-1,getdate()),getdate())
but that gives 92 (it should be 31+28+31=90 today).
Subtracting months gives the same result:
select datediff(dd,dateadd(mm,-3,getdate()),getdate())
What am I thinking wrong here,
and what would be the correct formula?
I have tried several things, but it's turning into a morass of datepart(),
dateadd(), iif(), cast() etc
TIA
JanSurely if you're running it today (June 9th), then it will give you the
number of days since March 9th, which is 92. (31 days in March and May, 30
in April).
"Jan Doggen" <j.doggen@.BLOCKqsa.nl> wrote in message
news:O22KBI5iGHA.4284@.TK2MSFTNGP05.phx.gbl...
> Hello
> I need to calculate the number of days in the previous quarter relative to
> 'today'.
> I thought it would be as simple as the number of days between today and
> one
> quarter earlier:
> datediff(dd,dateadd(qq,-1,getdate()),getdate())
> but that gives 92 (it should be 31+28+31=90 today).
> Subtracting months gives the same result:
> select datediff(dd,dateadd(mm,-3,getdate()),getdate())
> What am I thinking wrong here,
> and what would be the correct formula?
> I have tried several things, but it's turning into a morass of datepart(),
> dateadd(), iif(), cast() etc
> TIA
> Jan
>|||Aargh, yes of course! It was late last evening.
Now the 'only' thing left is finding the right solution ;-)
Jan
"Rob Farley" <rob_farley@.hotmail.com> wrote in message
news:eVx2jl5iGHA.4080@.TK2MSFTNGP03.phx.gbl...
> Surely if you're running it today (June 9th), then it will give you the
> number of days since March 9th, which is 92. (31 days in March and May, 30
> in April).
> "Jan Doggen" <j.doggen@.BLOCKqsa.nl> wrote in message
> news:O22KBI5iGHA.4284@.TK2MSFTNGP05.phx.gbl...
to
datepart(),
>|||How about :
select datediff(dd,LastQuarter,ThisQuarter)
from
(
select
dateadd(qq,datediff(qq,0,current_timesta
mp),0) as ThisQuarter,
dateadd(qq,datediff(qq,0,current_timesta
mp)-1,0) as LastQuarter
) as a
ThisQuarter & LastQuarter represent the first day of the current quarter
and the first day of the previous quarter respectively (using a trick
about stripping off parts of a datetime value - usually used to strip
off the time part by specifying dateadd(dd,...)). So the number of days
in the previous quarter, relative to today, is just the difference in
days between ThisQuarter and LastQuarter (as defined above).
*mike hodgson*
http://sqlnerd.blogspot.com
Jan Doggen wrote:

>Aargh, yes of course! It was late last evening.
>Now the 'only' thing left is finding the right solution ;-)
>Jan
>"Rob Farley" <rob_farley@.hotmail.com> wrote in message
>news:eVx2jl5iGHA.4080@.TK2MSFTNGP03.phx.gbl...
>
>to
>
>datepart(),
>
>
>|||Smart!
I never thought about selecting from a select.
Must be a blind spot because I was only dealing with variables and not
tabular data ;-)
I was already working towards a table 'DaysInQuarter' so that I could just
look it up:
CREATE TABLE DaysInQuarter
(Yr INT, Q Int, YrQ CHAR(6), DIQ INT)
INSERT INTO DaysInQuarter VALUES(2000,1,'200001',91)
INSERT INTO DaysInQuarter VALUES(2000,2,'200002',91)
INSERT INTO DaysInQuarter VALUES(2000,3,'200003',92)
INSERT INTO DaysInQuarter VALUES(2000,4,'200004',92)
etc
then:
select DIQ from DaysInQuarter
WHERE Q = datepart(qq, dateadd(mm,-3,getdate()))
AND Yr = year(dateadd(mm,-3,getdate()))
(kind of 'brute force' since Q2, Q3 and Q4 never change)
Thanks
Jan|||You could have done it with a calendar table, that's not a bad idea -
auxiliary tables (like calendar & numbers tables (see
http://www.aspfaq.com/2519)) come in handy for solving all sorts of
problems. As for my solution, there's no reason I needed to use a
derived table like I did, it just made the example clearer and made it
easier to explain. I could have included the whole lot in a single
select (with no FROM clause) but it would have made it a bit uglier and
more confusing to try to understand.
*mike hodgson*
http://sqlnerd.blogspot.com
Jan Doggen wrote:

>Smart!
>I never thought about selecting from a select.
>Must be a blind spot because I was only dealing with variables and not
>tabular data ;-)
>I was already working towards a table 'DaysInQuarter' so that I could just
>look it up:
>CREATE TABLE DaysInQuarter
>(Yr INT, Q Int, YrQ CHAR(6), DIQ INT)
>INSERT INTO DaysInQuarter VALUES(2000,1,'200001',91)
>INSERT INTO DaysInQuarter VALUES(2000,2,'200002',91)
>INSERT INTO DaysInQuarter VALUES(2000,3,'200003',92)
>INSERT INTO DaysInQuarter VALUES(2000,4,'200004',92)
>etc
>then:
>select DIQ from DaysInQuarter
>WHERE Q = datepart(qq, dateadd(mm,-3,getdate()))
>AND Yr = year(dateadd(mm,-3,getdate()))
>(kind of 'brute force' since Q2, Q3 and Q4 never change)
>Thanks
>Jan
>
>

Saturday, February 25, 2012

NULL to Zero

Is there any function that would change a NULL value to zero? I am writing a
view that need to calculate the net inventory by subtracting the export qty
from import qty. However, in case of no export, 1-Null=Null. Thanks.[posted and mailed, please reply in news]

John Q (johnq@.hkayp.org) writes:
> Is there any function that would change a NULL value to zero? I am
> writing a view that need to calculate the net inventory by subtracting
> the export qty from import qty. However, in case of no export,
> 1-Null=Null. Thanks.

coalesce(val1, val2, ..., valn)

returns the first non-NULL value in the list.

(There is also a function isnull() which has a clearer name, but is
restricted to two parameters only. coalesce() is from the ANSI standard.)

--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Erland,

Thank you very much. This function is very useful to me.

Best Regards,
Frederick

"Erland Sommarskog" <sommar@.algonet.se> bl
news:Xns93EC5BCD76E8Yazorman@.127.0.0.1 g...
> [posted and mailed, please reply in news]
> John Q (johnq@.hkayp.org) writes:
> > Is there any function that would change a NULL value to zero? I am
> > writing a view that need to calculate the net inventory by subtracting
> > the export qty from import qty. However, in case of no export,
> > 1-Null=Null. Thanks.
> coalesce(val1, val2, ..., valn)
> returns the first non-NULL value in the list.
> (There is also a function isnull() which has a clearer name, but is
> restricted to two parameters only. coalesce() is from the ANSI standard.)
>
> --
> Erland Sommarskog, SQL Server MVP, sommar@.algonet.se
> Books Online for SQL Server SP3 at
> http://www.microsoft.com/sql/techin.../2000/books.asp