Home Forums SQL Server 2008 T-SQL (SS2K8) query to get the name of a product with only incremental values in the quantity column RE: query to get the name of a product with only incremental values in the quantity column

  • CELKO (12/18/2012)


    I think you meant monotonic increasing and not increment . Let's use ISO-11179 names and the MySQL year format:

    CREATE TABLE Inventory

    (product_name VARCHAR(32) NOT NULL,

    inventory_year CHAR(10) NOT NULL,

    PRIMARY KEY(product_name, inventory_year),

    onhand_qty INTEGER NOT NULL

    CHECK(onhand_qty >= 0));

    INSERT INTO Inventory

    VALUES

    ('Computer', '2009-00-00' 100),

    ('Computer', '2010-00-00' 200),

    ('Computer', '2011-00-00' 300),

    ('Computer', '2012-00-00' 400),

    ('printer', '2009-00-00' 100),

    ('printer', '2010-00-00' 200),

    ('printer', '2011-00-00' 250),

    ('printer', '2012-00-00' 250),

    ('flash Drive', '2009-00-00' 400),

    ('flash Drive', '2010-00-00' 500),

    ('flash Drive', '2011-00-00' 700),

    ('flash Drive', '2012-00-00' 900),

    ('monitor', '2009-00-00' 200),

    ('monitor', '2010-00-00' 300),

    ('monitor', '2011-00-00' 250),

    ('monitor', '2012-00-00' 400),

    ('keyboard', '2009-00-00' 100),

    ('keyboard', '2010-00-00' 150),

    ('keyboard', '2011-00-00' 200),

    ('keyboard', '2012-00-00' 150));

    SELECT X.product_name

    FROM (SELECT product_name,

    (ROW_NUMBER() OVER(PARTITION BY product_name

    ORDER BY inventory_year) -

    ROW_NUMBER() OVER(PARTITION BY product_name

    ORDER BY onhand_qty))

    AS compare_seq_delta

    FROM Inventory) AS X

    GROUP BY X.product_name

    HAVING MIN(compare_seq_delta) = 0

    AND MAX(compare_seq_delta) = 0;

    Untested.

    thank you CELKO!!! it worked ..learned something new abt ROW_NUMBER function. and thanks for all the info abt the forum:-)