How can I improve my SQL code for correct results?

vap0991

Well, I am not sure what is the best way to this problem. But let me give you an example. What I am trying to achieve here is get the P2 value which is equal to carry when trying to sum P2 values in the descending order.. 10 to 1.

I have a huge table:

Category_Id Brand_Id Carry P2_0  P2_1  P2_3 ... P2_10
9           54       59    12    3     17       .
7           6        102   4     0     3        .
9           71       54    20    1     0        .
9           75       98    34    4     0        .
7           10       140   59    5     4        .

This is main logic of my code:

SELECT CategoryCode,Brand_Id, (CASE
    WHEN P2_10 > =  Carry Then 'Error' 
    WHEN P2_10 + P2_9 > =  Carry Then '10' 
    WHEN P2_10 + P2_9 + P2_8  > =  Carry Then '9' 
    WHEN P2_10 + P2_9 + P2_8 + P2_7  >=  Carry Then '8' 
    WHEN P2_10 + P2_9 + P2_8 + P2_7 + P2_6  >=  Carry Then '7'
    WHEN P2_10 + P2_9 + P2_8 + P2_7 + P2_6 + P2_5 > =  Carry Then '6' 
    WHEN P2_10 + P2_9 + P2_8 + P2_7 + P2_6 + P2_5 + P2_4 > =  Carry Then '5'
    WHEN P2_10 + P2_9 + P2_8 + P2_7 + P2_6 + P2_5 + P2_4 + P2_3 > =  Carry Then '4' 
    WHEN P2_10 + P2_9 + P2_8 + P2_7 + P2_6 + P2_5 + P2_4 + P2_3 + P2_2 > =  Carry Then '3'
    WHEN P2_10 + P2_9 + P2_8 + P2_7 + P2_6 + P2_5 + P2_4 + P2_3 + P2_2 + P2_1 > =  Carry Then '2'
    WHEN P2_10 + P2_9 + P2_8 + P2_7 + P2_6 + P2_5 + P2_4 + P2_3 + P2_2 + P2_1 + P2_0 > =  Carry Then '1'
    ELSE NULL END) As Threshold  from BQ_15

Now the issue here is say for a brand_id 6 if the carry is 106 then

 P2_10(50) + P2_9(50) + P2_8(3) + P2_7(3) = Carry (106) gives the right result 

 but if  P2_10 + P2_9 + P2_8 + P2_7 > Carry it has to go back to previous result, if in the previous result the new P2 was '0' it has to back further. 

 so if P2_10(50) + P2_9(50) + P2_8(2) + P2_7(0) + P2_6(30) > Carry (106) then it should skip P2_7 (because it is zero) and go to P2_8 (desired result) but for my code it goes to P2_7.

I know I haven't included anything that skips '0' that is where my entire issue lies is how do I iterate in SQL that my code will work for both the cases and get the desired result.

Thanks in advance

Serg

Use cross apply to make row scope calculations. I took 4 p2_xx columns, extend it as needed.

from (
     -- sample data
     values (9,54,106,  50,50,2,0,30)     
     ) hugeTable (Category_Id, Brand_Id, Carry, P2_10, P2_9, P2_8, P2_7, P2_6) 
cross apply (
    select Threshold = min(p2n)
    from (
        select p2n,
             s = sum(p2val) over(order by p2n desc)
        from (
             values
             (10, P2_10), (9, P2_9), (8, P2_8), (7, P2_7), (6, P2_6) 
        ) t(p2n, p2val)
        where p2val>0
    ) t
    where s <= Carry
) t      

I use sum() over(), if you are on 2008 or earlier version then

from (
     -- sample data
     values (9,54,106,  50,50,2,0,30)     
     ) hugeTable (Category_Id, Brand_Id, Carry, P2_10, P2_9, P2_8, P2_7, P2_6) 
cross apply (
    select Threshold = min(p2n)
    from (
        select p2n,
             s = (select sum(p2val)
                 from (
                      values
                      (10, P2_10), (9, P2_9), (8, P2_8), (7, P2_7), (6, P2_6)  
                 ) t2(p2n, p2val)
                 where t2.p2n>=t.p2n )
        from (
             values
             (10, P2_10), (9, P2_9), (8, P2_8), (7, P2_7), (6, P2_6)  
        ) t(p2n, p2val)
        where p2val>0
    ) t
    where s <= Carry
) t

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I improve my SQL code for correct results?

From Dev

How can I correct my Java code ?

From Dev

How can i count results in my sql?

From Dev

How can I improve my code to reduce the synthesis time?

From Dev

How can I improve my webpage code to be scalable for smaller sizes?

From Dev

How can I improve my code to reduce the synthesis time?

From Dev

How can i improve my python code regarding while loops

From Dev

How can I improve my code to handle large numbers?

From Dev

How can I improve my "if and else if" VBA? code

From Dev

PL\SQL How can I improve the execution speed of my function?

From Java

How can I get my code to return the correct variable?

From Dev

How can I correct my code to produce a nested dictionary?

From Dev

How can i check to see if my WordPress code is correct?

From Dev

How can I improve the performance of my script?

From Dev

How can I improve my linux security?

From Dev

How can I improve my android layout

From Dev

I want to display the occurrence of a character in string. How can I improve my code?

From Dev

How can I improve and shorten this block of code?

From Dev

How can I improve the performance of this code?

From Dev

How can I improve this code in python?

From Dev

How can I improve this GPA calculation code?

From Dev

How can I improve this JavaScript code?

From Dev

How can I improve my code design to remove the need for 'instanceof' in Java?

From Dev

How can I improve my code design to remove the need for 'instanceof' in Java?

From Dev

How can I improve this query depending on the explain results

From Dev

How can I improve this query depending on the explain results

From Dev

How do i improve my existing code for GetOpenfile function

From Dev

How do I improve my Python code about heap sort?

From Java

How can I improve the run-time complexity of my method?

Related Related

  1. 1

    How can I improve my SQL code for correct results?

  2. 2

    How can I correct my Java code ?

  3. 3

    How can i count results in my sql?

  4. 4

    How can I improve my code to reduce the synthesis time?

  5. 5

    How can I improve my webpage code to be scalable for smaller sizes?

  6. 6

    How can I improve my code to reduce the synthesis time?

  7. 7

    How can i improve my python code regarding while loops

  8. 8

    How can I improve my code to handle large numbers?

  9. 9

    How can I improve my "if and else if" VBA? code

  10. 10

    PL\SQL How can I improve the execution speed of my function?

  11. 11

    How can I get my code to return the correct variable?

  12. 12

    How can I correct my code to produce a nested dictionary?

  13. 13

    How can i check to see if my WordPress code is correct?

  14. 14

    How can I improve the performance of my script?

  15. 15

    How can I improve my linux security?

  16. 16

    How can I improve my android layout

  17. 17

    I want to display the occurrence of a character in string. How can I improve my code?

  18. 18

    How can I improve and shorten this block of code?

  19. 19

    How can I improve the performance of this code?

  20. 20

    How can I improve this code in python?

  21. 21

    How can I improve this GPA calculation code?

  22. 22

    How can I improve this JavaScript code?

  23. 23

    How can I improve my code design to remove the need for 'instanceof' in Java?

  24. 24

    How can I improve my code design to remove the need for 'instanceof' in Java?

  25. 25

    How can I improve this query depending on the explain results

  26. 26

    How can I improve this query depending on the explain results

  27. 27

    How do i improve my existing code for GetOpenfile function

  28. 28

    How do I improve my Python code about heap sort?

  29. 29

    How can I improve the run-time complexity of my method?

HotTag

Archive