October 13, 2017 at 5:26 pm
Hi,
TOTAL datatype is (numeric(10,0),null)
STR datatype is (numeric(20,15),null)
REPLACE(FORMAT(CAST(ROUND((
CASE
WHEN SUM(CASE WHEN a.TOTAL IS NOT NULL THEN a.STR_WEIGHT ELSE 0 END) = 0 THEN 0
ELSE SUM(CASE WHEN a.TOTAL IS NOT NULL THEN a.TOTAL * a.STR ELSE 0 END) / SUM(CASE WHEN a.TOTAL IS NOT NULL THEN a.STR ELSE 0 END)
END),0) AS INT),'C'), '.00', '') AS TOTALVALUE
Result is not accurate , sometimes its giving one value low with above code.
Example: Result should be $1452, but i am getting $1451 .
Any suggestions please ?.
Thanks.
October 13, 2017 at 8:16 pm
adisql - Friday, October 13, 2017 5:26 PMHi,TOTAL datatype is (numeric(10,0),null)
STR datatype is (numeric(20,15),null)REPLACE(FORMAT(CAST(ROUND((
CASE
WHEN SUM(CASE WHEN a.TOTAL IS NOT NULL THEN a.STR_WEIGHT ELSE 0 END) = 0 THEN 0
ELSE SUM(CASE WHEN a.TOTAL IS NOT NULL THEN a.TOTAL * a.STR ELSE 0 END) / SUM(CASE WHEN a.TOTAL IS NOT NULL THEN a.STR ELSE 0 END)
END),0) AS INT),'C'), '.00', '') AS TOTALVALUEResult is not accurate , sometimes its giving one value low with above code.
Example: Result should be $1452, but i am getting $1451 .Any suggestions please ?.
Thanks.
Without example data for STR_Weight and STR, there's little we can do to help you fix the problem BUT, I can tell you what the basis of the problem is. Please refer to the article at the following link.
https://docs.microsoft.com/en-us/sql/t-sql/data-types/precision-scale-and-length-transact-sql
--Jeff Moden
Change is inevitable... Change for the better is not.
October 13, 2017 at 8:56 pm
Jeff Moden - Friday, October 13, 2017 8:16 PMadisql - Friday, October 13, 2017 5:26 PMHi,TOTAL datatype is (numeric(10,0),null)
STR datatype is (numeric(20,15),null)REPLACE(FORMAT(CAST(ROUND((
CASE
WHEN SUM(CASE WHEN a.TOTAL IS NOT NULL THEN a.STR_WEIGHT ELSE 0 END) = 0 THEN 0
ELSE SUM(CASE WHEN a.TOTAL IS NOT NULL THEN a.TOTAL * a.STR ELSE 0 END) / SUM(CASE WHEN a.TOTAL IS NOT NULL THEN a.STR ELSE 0 END)
END),0) AS INT),'C'), '.00', '') AS TOTALVALUEResult is not accurate , sometimes its giving one value low with above code.
Example: Result should be $1452, but i am getting $1451 .Any suggestions please ?.
Thanks.
Without example data for STR_Weight and STR, there's little we can do to help you fix the problem BUT, I can tell you what the basis of the problem is. Please refer to the article at the following link.
https://docs.microsoft.com/en-us/sql/t-sql/data-types/precision-scale-and-length-transact-sql
Thank you for the response.
Here are the sample data of these three columns.
STR_WEIGHT | STR | TOTAL |
1.68342732 | 1.68342732 | 16619 |
2.79759193 | 2.79759193 | 8000 |
1.99858073 | 1.99858073 | 1000 |
2.847469739 | 2.847469739 | 15000 |
1.96987144 | 1.96987144 | 0 |
2.180844646 | 2.180844646 | 5500 |
5.957095665 | 5.957095665 | 1000 |
2.475605388 | 2.475605388 | 600 |
3.61219082 | 3.61219082 | 6285 |
2.40803061 | 2.40803061 | 1000 |
2.92346998 | 2.92346998 | 8250 |
1.31192635 | 1.31192635 | 28250 |
3.910988321 | 3.910988321 | 1500 |
2.847469739 | 2.847469739 | 4250 |
2.426679944 | 2.426679944 | 0 |
1.08481867 | 1.08481867 | 1500 |
1.11575356 | 1.11575356 | 1500 |
2.532385328 | 2.532385328 | 4938 |
1.53426418 | 1.53426418 | 1400 |
4.268175829 | 4.268175829 | 9500 |
1.44240487 | 1.44240487 | 1000 |
1.44497825 | 1.44497825 | 1150 |
1.60558853 | 1.60558853 | 2000 |
5.806289469 | 5.806289469 | 0 |
2.37547525 | 2.37547525 | 15000 |
�{
October 14, 2017 at 2:14 am
adisql - Friday, October 13, 2017 8:56 PMThank you for the response.
Here are the sample data of these three columns.
STR_WEIGHT STR TOTAL 1.68342732 1.68342732 16619 2.79759193 2.79759193 8000 1.99858073 1.99858073 1000 2.847469739 2.847469739 15000 1.96987144 1.96987144 0 2.180844646 2.180844646 5500 5.957095665 5.957095665 1000 2.475605388 2.475605388 600 3.61219082 3.61219082 6285 2.40803061 2.40803061 1000 2.92346998 2.92346998 8250 1.31192635 1.31192635 28250 3.910988321 3.910988321 1500 2.847469739 2.847469739 4250 2.426679944 2.426679944 0 1.08481867 1.08481867 1500 1.11575356 1.11575356 1500 2.532385328 2.532385328 4938 1.53426418 1.53426418 1400 4.268175829 4.268175829 9500 1.44240487 1.44240487 1000 1.44497825 1.44497825 1150 1.60558853 1.60558853 2000 5.806289469 5.806289469 0 2.37547525 2.37547525 15000 �{
And now something usable:CREATE TABLE #Sample
(STR_WEIGHT numeric(20,15), --Guessed this
[STR] numeric(20,15),
[TOTAL] numeric(10,0));
GO
INSERT INTO #Sample
VALUES
(1.68342732,1.68342732,16619),
(2.79759193,2.79759193,8000),
(1.99858073,1.99858073,1000),
(2.847469739,2.847469739,15000),
(1.96987144,1.96987144,0),
(2.180844646,2.180844646,5500),
(5.957095665,5.957095665,1000),
(2.475605388,2.475605388,600),
(3.61219082,3.61219082,6285),
(2.40803061,2.40803061,1000),
(2.92346998,2.92346998,8250),
(1.31192635,1.31192635,28250),
(3.910988321,3.910988321,1500),
(2.847469739,2.847469739,4250),
(2.426679944,2.426679944,0),
(1.08481867,1.08481867,1500),
(1.11575356,1.11575356,1500),
(2.532385328,2.532385328,4938),
(1.53426418,1.53426418,1400),
(4.268175829,4.268175829,9500),
(1.44240487,1.44240487,1000),
(1.44497825,1.44497825,1150),
(1.60558853,1.60558853,2000),
(5.806289469,5.806289469,0),
(2.37547525,2.37547525,15000);
GO
SELECT *
FROM #Sample;
SELECT REPLACE(FORMAT(CAST(ROUND((
CASE WHEN SUM(CASE WHEN TOTAL IS NOT NULL THEN STR_WEIGHT ELSE 0 END) = 0 THEN 0
ELSE SUM(CASE WHEN TOTAL IS NOT NULL THEN TOTAL * STR ELSE 0 END) /
SUM(CASE WHEN TOTAL IS NOT NULL THEN STR ELSE 0 END)
END),0) AS INT),'C'), '.00', '') AS TOTALVALUE
FROM #Sample;
GO
DROP TABLE #Sample;
GO
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
October 14, 2017 at 2:20 am
Also, for the values you have supplied, what is the expected value for your expression? The TotalValue is 4,944 for the sample data and expression you've supplied, which doesn't match that in your original post.
Thanks.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
October 14, 2017 at 9:16 am
adisql - Friday, October 13, 2017 8:56 PMJeff Moden - Friday, October 13, 2017 8:16 PMadisql - Friday, October 13, 2017 5:26 PMHi,TOTAL datatype is (numeric(10,0),null)
STR datatype is (numeric(20,15),null)REPLACE(FORMAT(CAST(ROUND((
CASE
WHEN SUM(CASE WHEN a.TOTAL IS NOT NULL THEN a.STR_WEIGHT ELSE 0 END) = 0 THEN 0
ELSE SUM(CASE WHEN a.TOTAL IS NOT NULL THEN a.TOTAL * a.STR ELSE 0 END) / SUM(CASE WHEN a.TOTAL IS NOT NULL THEN a.STR ELSE 0 END)
END),0) AS INT),'C'), '.00', '') AS TOTALVALUEResult is not accurate , sometimes its giving one value low with above code.
Example: Result should be $1452, but i am getting $1451 .Any suggestions please ?.
Thanks.
Without example data for STR_Weight and STR, there's little we can do to help you fix the problem BUT, I can tell you what the basis of the problem is. Please refer to the article at the following link.
https://docs.microsoft.com/en-us/sql/t-sql/data-types/precision-scale-and-length-transact-sqlThank you for the response.
Here are the sample data of these three columns.
STR_WEIGHT STR TOTAL 1.68342732 1.68342732 16619 2.79759193 2.79759193 8000 1.99858073 1.99858073 1000 2.847469739 2.847469739 15000 1.96987144 1.96987144 0 2.180844646 2.180844646 5500 5.957095665 5.957095665 1000 2.475605388 2.475605388 600 3.61219082 3.61219082 6285 2.40803061 2.40803061 1000 2.92346998 2.92346998 8250 1.31192635 1.31192635 28250 3.910988321 3.910988321 1500 2.847469739 2.847469739 4250 2.426679944 2.426679944 0 1.08481867 1.08481867 1500 1.11575356 1.11575356 1500 2.532385328 2.532385328 4938 1.53426418 1.53426418 1400 4.268175829 4.268175829 9500 1.44240487 1.44240487 1000 1.44497825 1.44497825 1150 1.60558853 1.60558853 2000 5.806289469 5.806289469 0 2.37547525 2.37547525 15000 �{
In the future, please take the extra bit of time to make the data "readily consumable" so that we can help more quickly. Please see the article at the first link in my signature line below under "Helpful Links" for one way to do such a thing. It also gives us great clues as to what the correct datatypes and other things may be without having to go back and forth with clarification questions, etc.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 14, 2017 at 12:59 pm
Jeff Moden - Saturday, October 14, 2017 9:16 AMadisql - Friday, October 13, 2017 8:56 PMJeff Moden - Friday, October 13, 2017 8:16 PMadisql - Friday, October 13, 2017 5:26 PMHi,TOTAL datatype is (numeric(10,0),null)
STR datatype is (numeric(20,15),null)REPLACE(FORMAT(CAST(ROUND((
CASE
WHEN SUM(CASE WHEN a.TOTAL IS NOT NULL THEN a.STR_WEIGHT ELSE 0 END) = 0 THEN 0
ELSE SUM(CASE WHEN a.TOTAL IS NOT NULL THEN a.TOTAL * a.STR ELSE 0 END) / SUM(CASE WHEN a.TOTAL IS NOT NULL THEN a.STR ELSE 0 END)
END),0) AS INT),'C'), '.00', '') AS TOTALVALUEResult is not accurate , sometimes its giving one value low with above code.
Example: Result should be $1452, but i am getting $1451 .Any suggestions please ?.
Thanks.
Without example data for STR_Weight and STR, there's little we can do to help you fix the problem BUT, I can tell you what the basis of the problem is. Please refer to the article at the following link.
https://docs.microsoft.com/en-us/sql/t-sql/data-types/precision-scale-and-length-transact-sqlThank you for the response.
Here are the sample data of these three columns.
STR_WEIGHT STR TOTAL 1.68342732 1.68342732 16619 2.79759193 2.79759193 8000 1.99858073 1.99858073 1000 2.847469739 2.847469739 15000 1.96987144 1.96987144 0 2.180844646 2.180844646 5500 5.957095665 5.957095665 1000 2.475605388 2.475605388 600 3.61219082 3.61219082 6285 2.40803061 2.40803061 1000 2.92346998 2.92346998 8250 1.31192635 1.31192635 28250 3.910988321 3.910988321 1500 2.847469739 2.847469739 4250 2.426679944 2.426679944 0 1.08481867 1.08481867 1500 1.11575356 1.11575356 1500 2.532385328 2.532385328 4938 1.53426418 1.53426418 1400 4.268175829 4.268175829 9500 1.44240487 1.44240487 1000 1.44497825 1.44497825 1150 1.60558853 1.60558853 2000 5.806289469 5.806289469 0 2.37547525 2.37547525 15000 �{
In the future, please take the extra bit of time to make the data "readily consumable" so that we can help more quickly. Please see the article at the first link in my signature line below under "Helpful Links" for one way to do such a thing. It also gives us great clues as to what the correct datatypes and other things may be without having to go back and forth with clarification questions, etc.
Thank you .
Actually i am looking if any issues with rounding or any syntax problem with this script code.
Please suggest if any issues with this script calculation.
October 14, 2017 at 1:48 pm
adisql - Saturday, October 14, 2017 12:59 PMThank you .
Actually i am looking if any issues with rounding or any syntax problem with this script code.
Please suggest if any issues with this script calculation.
Issues as in..? That's very vague.
You started off by saying you were getting the wrong results. Is it giving the right results now? What did you change so it does? Do we have an older version..?
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
October 14, 2017 at 5:32 pm
adisql - Saturday, October 14, 2017 12:59 PMJeff Moden - Saturday, October 14, 2017 9:16 AMadisql - Friday, October 13, 2017 8:56 PMJeff Moden - Friday, October 13, 2017 8:16 PMadisql - Friday, October 13, 2017 5:26 PMHi,TOTAL datatype is (numeric(10,0),null)
STR datatype is (numeric(20,15),null)REPLACE(FORMAT(CAST(ROUND((
CASE
WHEN SUM(CASE WHEN a.TOTAL IS NOT NULL THEN a.STR_WEIGHT ELSE 0 END) = 0 THEN 0
ELSE SUM(CASE WHEN a.TOTAL IS NOT NULL THEN a.TOTAL * a.STR ELSE 0 END) / SUM(CASE WHEN a.TOTAL IS NOT NULL THEN a.STR ELSE 0 END)
END),0) AS INT),'C'), '.00', '') AS TOTALVALUEResult is not accurate , sometimes its giving one value low with above code.
Example: Result should be $1452, but i am getting $1451 .Any suggestions please ?.
Thanks.
Without example data for STR_Weight and STR, there's little we can do to help you fix the problem BUT, I can tell you what the basis of the problem is. Please refer to the article at the following link.
https://docs.microsoft.com/en-us/sql/t-sql/data-types/precision-scale-and-length-transact-sqlThank you for the response.
Here are the sample data of these three columns.
STR_WEIGHT STR TOTAL 1.68342732 1.68342732 16619 2.79759193 2.79759193 8000 1.99858073 1.99858073 1000 2.847469739 2.847469739 15000 1.96987144 1.96987144 0 2.180844646 2.180844646 5500 5.957095665 5.957095665 1000 2.475605388 2.475605388 600 3.61219082 3.61219082 6285 2.40803061 2.40803061 1000 2.92346998 2.92346998 8250 1.31192635 1.31192635 28250 3.910988321 3.910988321 1500 2.847469739 2.847469739 4250 2.426679944 2.426679944 0 1.08481867 1.08481867 1500 1.11575356 1.11575356 1500 2.532385328 2.532385328 4938 1.53426418 1.53426418 1400 4.268175829 4.268175829 9500 1.44240487 1.44240487 1000 1.44497825 1.44497825 1150 1.60558853 1.60558853 2000 5.806289469 5.806289469 0 2.37547525 2.37547525 15000 �{
In the future, please take the extra bit of time to make the data "readily consumable" so that we can help more quickly. Please see the article at the first link in my signature line below under "Helpful Links" for one way to do such a thing. It also gives us great clues as to what the correct datatypes and other things may be without having to go back and forth with clarification questions, etc.
Thank you .
Actually i am looking if any issues with rounding or any syntax problem with this script code.
Please suggest if any issues with this script calculation.
In that case, take a look at the link I posted because the rounding issues are given in that link.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply