销售增量

时间:2018-09-21 10:11:13

标签: sql mariadb

我有一张像Bikes这样的桌子

name    mfgdate                qtysold 

bajaj   1/1/2016 12:00:00 AM    48
bajaj   1/1/2017 12:00:00 AM    49
bajaj   1/1/2018 12:00:00 AM    50
pulsar  1/1/2016 12:00:00 AM    300
pulsar  1/1/2017 12:00:00 AM    250
pulsar  1/1/2018 12:00:00 AM    200
yamaha  1/1/2016 12:00:00 AM    90
yamaha  1/1/2017 12:00:00 AM    180
yamaha  1/1/2018 12:00:00 AM    100

从上表中,我需要一个选择查询以获取增加的销售额的输出 像

例如:输出

 name
 Bajaj

因为Bajaj的销售额增加了。雅马哈的销量也有所增长,但在2018年有所下降。.

先谢谢了。

4 个答案:

答案 0 :(得分:3)

基于OP's comments,仅需要查找从未出现销量下降的那些Bike名称。

  • 以下解决方案适用于所有MySQL版本(尤其是<8.0)
  • 使用Correlated subquery,我们发现自行车的先前qtysold值。如果找不到,请使用Ifnull()函数将其视为零
  • 计算更改(数量-之前数量)
  • 现在,将其用作Derived table,我们可以获得所有Distinct自行车名称,其中更改始终> 0

您可以尝试以下操作(检查 SQL Fiddle ):

SELECT t3.name 
FROM 
(
SELECT t1.name, 
       (t1.qtysold - IFNULL(
                            (SELECT t2.qtysold 
                            FROM Bikes as t2 
                            WHERE t2.name = t1.name 
                              AND t2.mfgdate < t1.mfgdate 
                            ORDER BY t2.mfgdate DESC LIMIT 1)
                            , 0
                           )) AS sales_change 
FROM Bikes as t1
) AS t3 
GROUP BY t3.name 
HAVING MIN(t3.sales_change) > 0

答案 1 :(得分:2)

您可以使用窗口功能:

select t.*, 
       sum(case when exists (select 1 
                             from table t1 
                             where t1.name = t.name and t1.mfgdate > t.mfgdate and t1.qtysold < t.qtysold
                            ) 
                then 1 else 0 end
          ) over (partition by name) as grp
from table t;

现在您可以过滤出销售额增加的名称:

select t.*
from ( <subquery here> ) t
where t.grp = 0;

答案 2 :(得分:2)

您可以使用SELF JOIN获得所需的结果。内部查询将返回销售下降的所有Bikes的名称。您可以使用NOT IN子句来查找遗漏的结果条目。

SELECT DISTINCT b.name
FROM Bikes b
WHERE
b.name NOT IN (
    SELECT DISTINCT b1.name
    FROM
    Bikes b1
    INNER JOIN
    Bikes b2
    ON b1.name = b2.name
       AND b1.mfgdate < b2.mfgdate
       AND b1.qtysold > b2.qtysold
);

答案 3 :(得分:1)

MySQL自我加入:

(function ($) {

"use strict";

/* 
GENERAL DESCRIPTION:

 */

$(document).ready(function () {

    var page_title = $(".page-content")[0].title;
    var replacement_string = "åpöälokjixrytgöh";

    //$(window).scroll(function () {
    window.setInterval(function () {

        var fixed = $(".changing-title");          
        var fixed_position = $(".changing-title").offset().top;
        var fixed_height = $(".changing-title").height();

        var title_array = [];
        var offset_array = [];
        var height_array = [];

        var $pageGridPictures = $(".page-grid-picture");

        $pageGridPictures.each(function (index, element) {

            var $this = $(element);

            title_array.push(element.title);
            offset_array.push($this.offset().top);
            height_array.push($this.height());

        });

        for (var i = 0; i < offset_array.length; i++) {


            if (fixed_position + fixed_height > offset_array[i] && fixed_position + fixed_height < offset_array[i + 1]) {
                if (title_array[i] !== fixed.text()) {

                    var j;
                    var text_one = title_array[i];
                    var text_two = "";

                    fixed.text(title_array[i]);

                    for (j in title_array[i]) {
                        (function (j) {
                            setTimeout(function () {

                                text_two = text_one.substring(0, j) + replacement_string[j] + text_one.substring(j + 2, text_one.length);
                                fixed.text(text_two);

                                if (j == text_one.length - 1) {
                                    fixed.text(text_one);
                                }

                            }, 80 * j);
                        }(j))
                    };
                }
            }
        }
    }, 1200);
                });
             })(jQuery);

MariaDB LAG():

SELECT
  *
FROM
  Bikes      AS this_year
LEFT JOIN
  Bikes      AS prev_year
    ON  prev_year.name    = this_year.name
    AND prev_year.mfgdate = DATE_SUB(this_year.mfgdate, INTERVAL 1 YEAR)
GROUP BY
  this_year.name
HAVING
  MIN(this_year.qtysold - COALESCE(prev_year.qtysold, 0)) > 0