如何编写查询以获取促成80%最高销售额的产品ID?

时间:2019-05-14 02:08:12

标签: sql

假设我有一个产品是“销售”列

#include <time.h>
#include <immintrin.h>
#include <stdio.h>
#include <unistd.h>

typedef struct _object{
  int value;
  char pad[60];
} object;

int main() {
    volatile object * array;
    int arr_size = 1000000;
    array = (object *) malloc(arr_size * sizeof(object));

    for(int i=0; i < arr_size; i++){
        array[i].value = 1;
        _mm_clflush((const void*)&array[i]);
    }
    _mm_mfence();

    sleep(1);
    // printf("Starting main loop after %zu ms\n", (size_t)clock() * 1000u / CLOCKS_PER_SEC);

    int tmp;
    for(int i=0; i < arr_size-105; i++){
        array[i].value = 2;
        //tmp = array[i].value;
        // _mm_mfence();
    }
}

编写一个SQL查询以获取所有构成销售额最高80%的产品ID?

1 个答案:

答案 0 :(得分:0)

为此,您需要一个累加的总和。大概,您想要最畅销的此类产品,所以:

select p.*
from (select p.*,
             sum(sales) over (order by sales desc) as running_sales,
             sum(sales) over () as total_sales,
      from products
     ) p
where running_sales - sales < 0.8 * total_sales;

这将返回达到或首先超过总销售额80%的产品。