0

I need to find the percent the product was on shelf.

So I use this query:

select 
    *, 
    cast((t1.CountProd / t1.SumProd)*100 as dec(12,10)) as asasa
from 
   (select 
        *,
        count(Nalichie) over (partition by ProductID) as CountProd,
        count(Nalichie) over() as SumProd
    from 
        [saleit_db_Danone].[OSA].[tbl_FullFCompetitive]) as t1

Why do I get 0 in the last column?

Here is my result:

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Andriy
  • 123
  • 1
  • 9

2 Answers2

3

As per comment, you are doing an integer division then casting to a decimal afterwards. Try this:

select
    *,
    (CAST(CountProd AS DEC(16, 10)) / SumProd) * 100 AS asasa
from (
    select
        *,
        count(Nalichie) over (partition by ProductID) as CountProd,
        count(Nalichie) over() as SumProd
    FROM [saleit_db_Danone].[OSA].[tbl_FullFCompetitive]
) as t1
Chris Pickford
  • 8,642
  • 5
  • 42
  • 73
  • 1
    Up vote, but I have an additional note. Based on the numbers in the screenshot this leads to an Arithmetic overflow error (when I ran it anyway), so I think @Andrey will need to increase the size of the datatype. I went up to DEC(16,10) and it worked with the example data. – TLaV Jun 15 '16 at 14:14
  • thats won't working. Becose first sql find count ant it is alredy int and then you try to convert into decimal.Ypu will receive an converting error. Solve this trick by adding an `t1.CountProd + 0.0` without converting – Andriy Jun 15 '16 at 14:14
  • If `CountProd ` and `SumProd ` are integer - that will cause arithmetic overflow error on converting – gofr1 Jun 15 '16 at 14:14
  • Updated to `DEC(16,10)`. Tested and working. See [working example](https://gist.github.com/chrispickford/905343507347cfa309a7fd708c722837). – Chris Pickford Jun 15 '16 at 14:30
0

Also if you convert into numeric instead decimal you will get a correct answer.

Andriy
  • 123
  • 1
  • 9