Showing posts with label float. Show all posts
Showing posts with label float. Show all posts

Friday, March 23, 2012

it doesn't correlate

SELECT [Pcode], avg(convert(float,[Seq]))
FROM [DB].[dbo].[PartyTime]
WHERE [Seq] IN (select top 8 [PartyTime].[Seq]
from [PartyTime] JOIN [PartyTime] [PT]
ON [PartyTime].[Pcode] = [PT].[Pcode]
AND [PartyTime].[Seq] = [PT].[Seq]
order by [PartyTime].[Seq])
GROUP By [Pcode]
..yields:
TOP 4.5
..but what I'm after is:
BOT 11954.5
TOP 4.5
It takes the top 8 of all of PartyTime rather than the top 8 of each
Pcode that it's averaging. Just a subselect without any correlation.
Anyone got a correct syntax for this or another approach? Remember, the
idea is not to hardcode Pcode values; the set is unknown at query time.
thx
md
*** Sent via Developersdex http://www.examnotes.net ***SELECT [Pcode], avg(convert(float,[Seq]))
FROM [DB].[dbo].[PartyTime]
WHERE [Seq] IN (select top 8 [PartyTime].[Seq]
from [PartyTime] JOIN [PartyTime] [PT]
ON [PartyTime].[Pcode] = [PT].[Pcode]
AND [PartyTime].[Seq] = [PT].[Seq]
Group by [PartyTime].[Seq]
order by [PartyTime].[Seq])
GROUP By [Pcode]
Another Example for the subquery:
Use northwind
Select TOP 5 CustomerID
From Orders
group by CustomerID
order by CustomerID
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--
"M D" <mardukes@.aol.com> schrieb im Newsbeitrag
news:eykIxmoSFHA.3392@.TK2MSFTNGP12.phx.gbl...
> SELECT [Pcode], avg(convert(float,[Seq]))
> FROM [DB].[dbo].[PartyTime]
> WHERE [Seq] IN (select top 8 [PartyTime].[Seq]
> from [PartyTime] JOIN [PartyTime] [PT]
> ON [PartyTime].[Pcode] = [PT].[Pcode]
> AND [PartyTime].[Seq] = [PT].[Seq]
> order by [PartyTime].[Seq])
> GROUP By [Pcode]
> ..yields:
> TOP 4.5
> ..but what I'm after is:
> BOT 11954.5
> TOP 4.5
> It takes the top 8 of all of PartyTime rather than the top 8 of each
> Pcode that it's averaging. Just a subselect without any correlation.
> Anyone got a correct syntax for this or another approach? Remember, the
> idea is not to hardcode Pcode values; the set is unknown at query time.
> thx
> md
> *** Sent via Developersdex http://www.examnotes.net ***|||This modification has no effect on the output except to slow the query.
thx
md
*** Sent via Developersdex http://www.examnotes.net ***|||> SELECT [Pcode], avg(convert(float,[Seq]))
> FROM [DB].[dbo].[PartyTime] PT INNER JOIN
(select top 8 [PartyTime].[Seq]
> from [PartyTime] JOIN [PartyTime] [PT]
> ON [PartyTime].[Pcode] = [PT].[Pcode]
> AND [PartyTime].[Seq] = [PT].[Seq]
> Group by [PartyTime].[Seq]
> order by [PartyTime].[Seq]) SUBQuery
on SUBQuery sq = PT.sq
> WHERE [Seq] IN GROUP By [Pcode]
Use this query to keep up performace, what does the inner query bring for a
resultset ?
Jens Suessmeyer.
"Jens Smeyer" <Jens@.Remove_this_For_Contacting.sqlserver2005.de> schrieb
im Newsbeitrag news:OXx213oSFHA.1152@.tk2msftngp13.phx.gbl...
> SELECT [Pcode], avg(convert(float,[Seq]))
> FROM [DB].[dbo].[PartyTime]
> WHERE [Seq] IN (select top 8 [PartyTime].[Seq]
> from [PartyTime] JOIN [PartyTime] [PT]
> ON [PartyTime].[Pcode] = [PT].[Pcode]
> AND [PartyTime].[Seq] = [PT].[Seq]
> Group by [PartyTime].[Seq]
> order by [PartyTime].[Seq])
> GROUP By [Pcode]
> Another Example for the subquery:
> Use northwind
> Select TOP 5 CustomerID
> From Orders
> group by CustomerID
> order by CustomerID
>
> HTH, Jens Suessmeyer.
> --
> http://www.sqlserver2005.de
> --
>
> "M D" <mardukes@.aol.com> schrieb im Newsbeitrag
> news:eykIxmoSFHA.3392@.TK2MSFTNGP12.phx.gbl...
>|||the inner query returns {1,2,3,4,5,6,7,8} which all happen to be of
Pcode "TOP". Hence I get no avg for Pcode "BOT".
What I need the inner query to return is the what this hardcoded query
returns:
select top 8 [Seq] from [ProfTraining]
where Pcode = "TOP"
order by [ProfTraining].[Seq]
UNION
select top 8 [Seq] from [ProfTraining]
where Pcode = "BOT"
order by [ProfTraining].[Seq]
Again, I can't hardcode the Pcodes because the next set of data might
have other values OR I don't want to code out n UNION statements.
Interestingly, if I change it to
.. EXISTS (select top 8 * ...
I get two values but they are the avg's of the top 8's, they're avg's of
the entire groups.
thx
md
*** Sent via Developersdex http://www.examnotes.net ***|||Correction:
I get two values but they AREN'T the avg's of the top 8's, they're avg's
of the entire groups.
More accurately, I need the inner query to correlate; it would look like
the UNION because the inner query would be offering the top 8 records
having the same Pcode as the outer record.
thx
md
*** Sent via Developersdex http://www.examnotes.net ***|||On Tue, 26 Apr 2005 10:59:27 -0700, M D wrote:

>SELECT [Pcode], avg(convert(float,[Seq]))
> FROM [DB].[dbo].[PartyTime]
> WHERE [Seq] IN (select top 8 [PartyTime].[Seq]
> from [PartyTime] JOIN [PartyTime] [PT]
> ON [PartyTime].[Pcode] = [PT].[Pcode]
> AND [PartyTime].[Seq] = [PT].[Seq]
> order by [PartyTime].[Seq])
> GROUP By [Pcode]
>..yields:
>TOP 4.5
>..but what I'm after is:
>BOT 11954.5
>TOP 4.5
(snip)
Hi md,
Try if this one works better:
SELECT Pcode, AVG(CAST(Seq AS float))
FROM PartyTime
WHERE Seq IN (SELECT TOP 8 PT.Seq
FROM PartyTime AS PT
WHERE PT.Pcode = PartyTime.Pcode
ORDER BY PT.Seq)
GROUP BY Pcode
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||BINGO!
thx
md
*** Sent via Developersdex http://www.examnotes.net ***|||MD,
THis should work...
Select Pcode, Avg(Cast(Seq As Float))
From PartyTime Pt
Where (Select Count(*)
From PartyTime
Where PCode = Pt.PCode
And Seq >= pt.Seq) <= 8
Group By PCode
"M D" wrote:

> SELECT [Pcode], avg(convert(float,[Seq]))
> FROM [DB].[dbo].[PartyTime]
> WHERE [Seq] IN (select top 8 [PartyTime].[Seq]
> from [PartyTime] JOIN [PartyTime] [PT]
> ON [PartyTime].[Pcode] = [PT].[Pcode]
> AND [PartyTime].[Seq] = [PT].[Seq]
> order by [PartyTime].[Seq])
> GROUP By [Pcode]
> ...yields:
> TOP 4.5
> ...but what I'm after is:
> BOT 11954.5
> TOP 4.5
> It takes the top 8 of all of PartyTime rather than the top 8 of each
> Pcode that it's averaging. Just a subselect without any correlation.
> Anyone got a correct syntax for this or another approach? Remember, the
> idea is not to hardcode Pcode values; the set is unknown at query time.
> thx
> md
> *** Sent via Developersdex http://www.examnotes.net ***
>|||TOP 11946.5
MPAW 23573.5
I can't figure out why you think that works but it's slower and wrong.
Examine the one above for the solution.
thx
md
*** Sent via Developersdex http://www.examnotes.net ***sql

Wednesday, March 7, 2012

Issue with float data type

Hi,

I have a table with float data type. When we insert or update data with some math(1+1.01) it converts the decimal value to approximate value.

Example:

Create a table say "Table1" with field "Field1" of data type float

Now insert a record with value 1.01

Select * from Table1 would give me out put at 1.01

Update Table1 set Field1 = 1.01+1

Now Select * from Table1

Instead of returning me 2.01, it returns 2.0099999999999998. Same thing happens if I insert record with following statement

insert into Table1(Field1) values (1+1.01)

Any idea why its doing this way? I know float is approximate data type, but shouldn't it be giving me correct value for what I am doing? Is there any server setting that I can change to get proper result?

Thanks,


Hiten:

I think the answer to your question is that there is no exact conversion of the number 2.01 into a hexidecimal floating point representation; what I remember is that it is initially surprising what numbers do not convert exactly and this is probably one of them. I think the answer is you need to live with the limitations of floating point representation or switch to an exact representation with a pre-defined precision and scale. If you stick with float, use of the ROUND function will be helpful. I verified and I receive the same behavior and same response that you do. Somebody check me on this?

Dave

|||

Hiten:

I found this article:

http://docs.python.org/tut/node16.html

The "B.1" section discusses the problem of representing 1/10 in binary; this should give the idea.

Dave

|||

Float is an approximate data type so it stores only close approximation of a value. See below link for more details:

http://msdn2.microsoft.com/en-us/library/ms187912.aspx

You need to use decimal or numeric to get exact representation of the value.

|||

Hi all,

What are the drawbacks of using decimal insted of float data type?

Thanks.

Issue with float data type

Hi,

I have a table with float data type. When we insert or update data with some math(1+1.01) it converts the decimal value to approximate value.

Example:

Create a table say "Table1" with field "Field1" of data type float

Now insert a record with value 1.01

Select * from Table1 would give me out put at 1.01

Update Table1 set Field1 = 1.01+1

Now Select * from Table1

Instead of returning me 2.01, it returns 2.0099999999999998. Same thing happens if I insert record with following statement

insert into Table1(Field1) values (1+1.01)

Any idea why its doing this way? I know float is approximate data type, but shouldn't it be giving me correct value for what I am doing? Is there any server setting that I can change to get proper result?

Thanks,


Hiten:

I think the answer to your question is that there is no exact conversion of the number 2.01 into a hexidecimal floating point representation; what I remember is that it is initially surprising what numbers do not convert exactly and this is probably one of them. I think the answer is you need to live with the limitations of floating point representation or switch to an exact representation with a pre-defined precision and scale. If you stick with float, use of the ROUND function will be helpful. I verified and I receive the same behavior and same response that you do. Somebody check me on this?

Dave

|||

Hiten:

I found this article:

http://docs.python.org/tut/node16.html

The "B.1" section discusses the problem of representing 1/10 in binary; this should give the idea.

Dave

|||

Float is an approximate data type so it stores only close approximation of a value. See below link for more details:

http://msdn2.microsoft.com/en-us/library/ms187912.aspx

You need to use decimal or numeric to get exact representation of the value.

|||

Hi all,

What are the drawbacks of using decimal insted of float data type?

Thanks.

Friday, February 24, 2012

Issue in using round in Vbscript

I am using a Round to get the Value of a Float datatype column from a database
and if I use the below code
and if the value of the Column is say 0
It do returns a value as 0 but it stores it into a table it's -1 my column for Temp is of Float datatype
please advice why this can be
Temp = round(0* 0.95,2)

> samay
>
Please don't ask the same question in more than one thread.
KritiVerma@.hotmail.com wrote:

> I am using a Round to get the Value of a Float datatype column from a database
> and if I use the below code
>and if the value of the Column is say 0
>It do returns a value as 0 but it stores it into a table it's -1 my column for Temp is of Float datatype
> please advice why this can be
> Temp = round(0* 0.95,2)
>
>
>
>

Issue in using round in Vbscript

I am using a Round to get the Value of a Float datatype column from a databa
se
and if I use the below code
and if the value of the Column is say 0
It do returns a value as 0 but it stores it into a table it's -1 my column f
or Temp is of Float datatype
please advice why this can be
Temp = round(0* 0.95,2)

> samay
>Please don't ask the same question in more than one thread.
KritiVerma@.hotmail.com wrote:

> I am using a Round to get the Value of a Float datatype column from a data
base
> and if I use the below code
>and if the value of the Column is say 0
>It do returns a value as 0 but it stores it into a table it's -1 my column
for Temp is of Float datatype
> please advice why this can be
> Temp = round(0* 0.95,2)
>
>
>
>

Issue in using Round

I am using a Round to get the Value of a Float datatype column from a database
and if the use the below code and if the value of the Column is say 0 it returns me -1. please advice why this can be
Temp = round(0* 0.95,2)
samay
I am using a Round to get the Value of a Float datatype column from a database
and if I use the below code
and if the value of the Column is say 0
It do returns a value as 0 but it stores it into a table it's -1 my column for Temp is of Float datatype
please advice why this can be
Temp = round(0* 0.95,2)

> samay
>
|||Samay,
Can you try to explain your problem again, and cut and paste the exact
statements that are not working? I don't understand what you are asking
at all. Since 0*0.95 is zero, the value of round(0*0.95,2) will be zero.
Steve Kass
Drew University
KritiVerma@.hotmail.com wrote:
[vbcol=seagreen]
> I am using a Round to get the Value of a Float datatype column from a database
> and if I use the below code
>and if the value of the Column is say 0
>It do returns a value as 0 but it stores it into a table it's -1 my column for Temp is of Float datatype
> please advice why this can be
> Temp = round(0* 0.95,2)
>
>
|||To add to Steve's response, the result of your round expression will always
be zero. I suspect your problem is due to persisting the result of a
boolean expression instead of the intended arithmetic expression result. A
VbScript True boolean value will be stored as -1 in a numeric datatype.
Hope this helps.
Dan Guzman
SQL Server MVP
"KritiVerma@.hotmail.com" <KritiVermahotmailcom@.discussions.microsoft.com>
wrote in message news:DCE60959-1658-4B62-B74A-30B1414229AB@.microsoft.com...
> I am using a Round to get the Value of a Float datatype column from a
database
> and if I use the below code
> and if the value of the Column is say 0
> It do returns a value as 0 but it stores it into a table it's -1 my column
for Temp is of Float datatype[vbcol=seagreen]
> please advice why this can be
> Temp = round(0* 0.95,2)
>

Issue in using Round

I am using a Round to get the Value of a Float datatype column from a databa
se
and if the use the below code and if the value of the Column is say 0 it ret
urns me -1. please advice why this can be
Temp = round(0* 0.95,2)
samayI am using a Round to get the Value of a Float datatype column from a databa
se
and if I use the below code
and if the value of the Column is say 0
It do returns a value as 0 but it stores it into a table it's -1 my column f
or Temp is of Float datatype
please advice why this can be
Temp = round(0* 0.95,2)

> samay
>|||Samay,
Can you try to explain your problem again, and cut and paste the exact
statements that are not working? I don't understand what you are asking
at all. Since 0*0.95 is zero, the value of round(0*0.95,2) will be zero.
Steve Kass
Drew University
KritiVerma@.hotmail.com wrote:
[vbcol=seagreen]
> I am using a Round to get the Value of a Float datatype column from a data
base
> and if I use the below code
>and if the value of the Column is say 0
>It do returns a value as 0 but it stores it into a table it's -1 my column
for Temp is of Float datatype
> please advice why this can be
> Temp = round(0* 0.95,2)
>
>|||To add to Steve's response, the result of your round expression will always
be zero. I suspect your problem is due to persisting the result of a
boolean expression instead of the intended arithmetic expression result. A
VbScript True boolean value will be stored as -1 in a numeric datatype.
Hope this helps.
Dan Guzman
SQL Server MVP
"KritiVerma@.hotmail.com" <KritiVermahotmailcom@.discussions.microsoft.com>
wrote in message news:DCE60959-1658-4B62-B74A-30B1414229AB@.microsoft.com...
> I am using a Round to get the Value of a Float datatype column from a
database
> and if I use the below code
> and if the value of the Column is say 0
> It do returns a value as 0 but it stores it into a table it's -1 my column
for Temp is of Float datatype[vbcol=seagreen]
> please advice why this can be
> Temp = round(0* 0.95,2)
>

Issue in using Round

I am using a Round to get the Value of a Float datatype column from a database
and if the use the below code and if the value of the Column is say 0 it returns me -1. please advice why this can be
Temp = round(0* 0.95,2)
samayI am using a Round to get the Value of a Float datatype column from a database
and if I use the below code
and if the value of the Column is say 0
It do returns a value as 0 but it stores it into a table it's -1 my column for Temp is of Float datatype
please advice why this can be
Temp = round(0* 0.95,2)
> samay
>|||Samay,
Can you try to explain your problem again, and cut and paste the exact
statements that are not working? I don't understand what you are asking
at all. Since 0*0.95 is zero, the value of round(0*0.95,2) will be zero.
Steve Kass
Drew University
KritiVerma@.hotmail.com wrote:
> I am using a Round to get the Value of a Float datatype column from a database
> and if I use the below code
>and if the value of the Column is say 0
>It do returns a value as 0 but it stores it into a table it's -1 my column for Temp is of Float datatype
> please advice why this can be
> Temp = round(0* 0.95,2)
>
>
>>samay
>>|||To add to Steve's response, the result of your round expression will always
be zero. I suspect your problem is due to persisting the result of a
boolean expression instead of the intended arithmetic expression result. A
VbScript True boolean value will be stored as -1 in a numeric datatype.
--
Hope this helps.
Dan Guzman
SQL Server MVP
"KritiVerma@.hotmail.com" <KritiVermahotmailcom@.discussions.microsoft.com>
wrote in message news:DCE60959-1658-4B62-B74A-30B1414229AB@.microsoft.com...
> I am using a Round to get the Value of a Float datatype column from a
database
> and if I use the below code
> and if the value of the Column is say 0
> It do returns a value as 0 but it stores it into a table it's -1 my column
for Temp is of Float datatype
> please advice why this can be
> Temp = round(0* 0.95,2)
>
> > samay
> >