| Author |
Topic |
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-03-15 : 14:59:19
|
| Hi,I am using SQL 2005.I would like to get the following calculated values with one decimal place.% value of TradeCount/InquiryCount. When I do as below I get a value of 63 for HitRatioCount SELECT HitRatioCount = TradeCount*100/InquiryCount , HitratioVolume = TradeVolume*100/InquiryVolume --format to one decimal placeINTO #HitRatioFROM #DealerTemp example TradeCount = 14InquiryCount = 22.When I do as belowSELECT HitRatioCount = TradeCount/InquiryCount * 100 , HitratioVolume = TradeVolume/InquiryVolume * 100 --format to one decimal placeINTO #HitRatioFROM #DealerTemp I get 0 for HitRatioCount Which is the correct way and how do i specify one decimal place?Thanks |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-03-15 : 15:12:16
|
| SELECTHitRatioCount = TradeCount/InquiryCount * 100*1.0 ,HitratioVolume = TradeVolume/InquiryVolume * 100*1.0--format to one decimal placeINTO #HitRatioFROM #DealerTempPBUH |
 |
|
|
Ehan
Starting Member
19 Posts |
Posted - 2010-03-15 : 15:16:16
|
| declare @TradeCount int , @InquiryCount intset @TradeCount = 14set @InquiryCount = 22SELECT HitRatioCount = CAST(CAST(@TradeCount as decimal) /@InquiryCount * 100 AS decimal(3,1)) |
 |
|
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-03-15 : 15:29:07
|
| Thanks you so much, that worked. I was trying the CONVERT function with decimal and that gave me an incorrect result set. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-16 : 00:33:49
|
quote: Originally posted by Idera SELECTHitRatioCount = TradeCount/InquiryCount * 100*1.0 ,HitratioVolume = TradeVolume/InquiryVolume * 100*1.0--format to one decimal placeINTO #HitRatioFROM #DealerTempPBUH
its same as 100.0 so this will suffice TradeCount/InquiryCount * 100.0------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-03-16 : 10:39:40
|
| Thanks.When I implemented the following :SELECT --HitRatioCount = CONVERT(decimal(3,3),ROUND(4,1,TradeCount/InquiryCount)), HitRatioCount = CAST(CAST(TradeCount as decimal) /InquiryCount * 100 AS decimal(3,1)), HitRatioVolume = CAST(CAST(TradeVolume as decimal) /InquiryVolume * 100 AS decimal(3,1)) --format to one decimal placeINTO #HitRatioFROM #DealerTemp I get the results fine.However, when I am joining to various work/temp tables I get the below error for Arithmatic Overflow. I am concerned about the final select when I join to the table that has the HitRatio.SELECT Product = prd.Description, SizeOrder = CASE WHEN iql.USDSize <= 1000 THEN 1 WHEN iql.USDSize > 1000 THEN 2 END, SizeBucket = CASE WHEN iql.USDSize <= 1000 THEN '<=1MM' WHEN iql.USDSize > 1000 THEN '>1MM' END, --DealerBucket = CASE --WHEN rsc.DealerCnt <= 10 THEN '<=10 Dealers' --WHEN rsc.DealerCnt > 10 THEN '>10 Dealers' --END,-- ResponseCount = rsc.ResponseCnt, InquiryCount = COUNT(DISTINCT inq.MISInquiryID), InquiryVolume = SUM(iql.USDSize), TradeCount = COUNT(DISTINCT trd.MISInquiryID), TradeVolume = SUM(IsNull(trd.USDSize,0)), --AvgDlrCnt = AVG(convert(float, rsc.DealerCnt)), --AvgRspCntWithLevel = AVG(convert(float, rsc.ResponseCnt)), ProductID = iql.ProductID -- HitRatioCount = TradeCount/InquiryCount -- HitRatioVolume = TradeVolume/InquiryVolumeINTO #DealerTempFROM Inquiry inqINNER JOIN InquiryLeg iqlON inq.MISInquiryID = iql.MISInquiryID--INNER JOIN --#ResponseCnt rsc--ON --inq.MISInquiryID = rsc.MISInquiryIDLEFT JOIN Trade trdON trd.MISInquiryID = iql.MISInquiryIDand trd.LegSequence = iql.LegSequenceand trd.IsVolume = 1INNER JOIN Product prdON prd.ProductID = iql.ProductIDWHERE iql.IsVolume = 1and inq.IsVolume = 1and inq.InquiryDate BETWEEN @StartDate and @EndOfEndDateand inq.InquiryType = 'F'and iql.ProductID in (1,2,6,7,4,40)GROUP BYCASE WHEN iql.USDSize <= 1000 THEN 1 WHEN iql.USDSize > 1000 THEN 2END,CASE WHEN iql.USDSize <= 1000 THEN '<=1MM' WHEN iql.USDSize > 1000 THEN '>1MM'END,iql.ProductID,prd.Descriptionselect * from #DealerTempSELECT --HitRatioCount = CONVERT(decimal(3,3),ROUND(4,1,TradeCount/InquiryCount)), HitRatioCount = CAST(CAST(TradeCount as decimal) /InquiryCount * 100 AS decimal(3,1)), HitRatioVolume = CAST(CAST(TradeVolume as decimal) /InquiryVolume * 100 AS decimal(3,1)) --format to one decimal placeINTO #HitRatioFROM #DealerTemp select * from #HitRatiodrop table #HitRatio--CASE --WHEN rsc.DealerCnt <= 10 THEN '<=10 Dealers' --WHEN rsc.DealerCnt > 10 THEN '>10 Dealers'--ENDSELECT Product, SizeBucket, --DealerBucket, InquiryCount, InquiryVolume, TradeCount, TradeVolume, --AvgDlrCnt, --AvgRspCntWithLevel-- SizeOrder,-- ProductIDfrom #DealerTemporder by SizeOrder, ProductID, --DealerBucketWarning: Null value is eliminated by an aggregate or other SET operation.(66403 row(s) affected)Warning: Null value is eliminated by an aggregate or other SET operation.(210 row(s) affected)Msg 8115, Level 16, State 8, Line 135Arithmetic overflow error converting numeric to data type numeric.The statement has been terminated.(210 row(s) affected)Should I try the ISNULL. Not sure what is causing an overflow. |
 |
|
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-03-16 : 10:54:29
|
| I tried replacing the CAST with the below but my data looks weird.HitRatioCount = TradeCount/InquiryCount * 100.0,HitRatioVolume = TradeVolume/InquiryVolume * 100.0Result:Prd HitCount HitVol1 0.0 0.000001 0.0 74.570001 0.0 65.61000 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-16 : 11:23:24
|
| what do you mean by weird? can you explain what you're getting & what you expect?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-03-16 : 11:30:21
|
| I expect something like this:HitRatio HitRatioCount Volume73.3 72.557.9 60.737.1 33.7 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-16 : 11:32:29
|
| on what basis? what are values for source fields to get above output?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-03-16 : 11:38:08
|
| example 61256/83561 should give 73.3 % 602/1040 should give 57.9 % |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-16 : 11:40:23
|
| does this make any difference?HitRatioCount = TradeCount* 100.0/InquiryCount ,HitRatioVolume = TradeVolume* 100.0/InquiryVolume------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-03-16 : 12:28:06
|
| I get results as below: I need to format values to decimal(3,1)? I tried CONVERT(FLOAT(TradeCount* 100.0/InquiryCount),3,1) but it gives an error.0.000000000000 0.00000000000000042.882882882882 39.18855096818126965.009940357852 63.54136359228008969.940476190476 69.137830238579104 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-16 : 12:31:10
|
| HitRatioCount = cast(TradeCount* 100.0/InquiryCount as decimal(3,1)),HitRatioVolume = cast(TradeVolume* 100.0/InquiryVolume as decimal(3,1))...------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-03-16 : 12:35:29
|
| I get the arithmatic overflowMsg 8115, Level 16, State 8, Line 141Arithmetic overflow error converting numeric to data type numeric. |
 |
|
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-03-16 : 12:38:22
|
| It works when I exec teh below query separatleySELECT --HitRatioCount = CONVERT(decimal(3,3),ROUND(4,1,TradeCount/InquiryCount)), --HitRatioCount = CAST(CAST(TradeCount as decimal) /InquiryCount * 100 AS decimal(3,1)), --HitRatioVolume = CAST(CAST(TradeVolume as decimal) /InquiryVolume * 100 AS decimal(3,1)) --format to one decimal place--HitRatioCount = (TradeCount/InquiryCount )* 100.0,--HitRatioVolume = (TradeVolume/InquiryVolume) * 100.0HitRatioCount = cast(TradeCount* 100.0/InquiryCount as decimal(3,1)),HitRatioVolume = cast(TradeVolume* 100.0/InquiryVolume as decimal(3,1))INTO #HitRatioFROM #DealerTemp select * from #HitRatioHit HitRatio VolumeCount Count73.3 72.557.9 60.737.1 33.767.6 73.963.6 58.650.0 48.672.2 70.371.7 76.829.9 27.576.3 71.578.9 69.254.7 51.1 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-16 : 12:38:36
|
| HitRatioCount = cast(TradeCount* 100.0/InquiryCount as decimal(10,1)),HitRatioVolume = cast(TradeVolume* 100.0/InquiryVolume as decimal(10,1))------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-03-16 : 13:12:17
|
| This worked now. Thanks a lot. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-16 : 13:13:13
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|