Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


query to get the name of a product with only incremental values in the quantity column


query to get the name of a product with only incremental values in the quantity column

Author
Message
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45235 Visits: 39927
Nisean (12/12/2012)
i need a query to get the names of the product which has incremental or equal values every year 2009-2012(in its QTY column)
Product year QTY
Computer 2009 100
Computer 2010 200
Computer 2011 300
Computer 2012 400
printer 2009 100
printer 2010 200
printer 2011 250
printer 2012 250
flash drive 2009 400
flash drive 2010 500
flash drive 2011 700
flash drive 2012 900
monitor 2009 200
monitor 2010 300
monitor 2011 250
monitor 2012 400
keyboard 2009 100
keyboard 2010 150
keyboard 2011 200
keyboard 2012 150

-------------output----------
Computer
printer
flashdrive


What do you want to do if there's a year missing from any given product?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
sql84
sql84
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 352
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:-)
sql84
sql84
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 352
Jeff Moden (12/18/2012)
Nisean (12/18/2012)
thank you for the response. FYI this is not a homework or an interview question. i know how to create a table and insert data on it! i need a query which will give me the required result.and yes i even couldnt figure out how to start on this!!
anybody!!???


The purpose is that a lot of people like to test their solutions before they post them. If you post readily consumable data, it makes it easier for them which means you get better answers quicker. Please see the first link after my signature line below for a more detailed explanation.


thank you for your response JEFF. the query provided by CELKO worked. and i will definately go through the links.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search