Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

query to get the name of a product with only incremental values in the quantity column Expand / Collapse
Author
Message
Posted Wednesday, December 12, 2012 10:55 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, August 29, 2014 2:48 PM
Points: 18, Visits: 198
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
Post #1395812
Posted Wednesday, December 12, 2012 11:12 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Wednesday, September 3, 2014 12:30 PM
Points: 945, Visits: 1,772
This looks like homework or an interview question so ill post some usable data. Can you show what you have tried so far?

CREATE TABLE tmpProduct (
Product VARCHAR(32),
[Year] INT,
QTY INT,
CONSTRAINT PK_tmpProduct PRIMARY KEY (Product, [Year])
)
INSERT INTO tmpProduct (Product, [Year], QTY)
SELECT * FROM (VALUES
('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))X(Product, [Year], QTY)




For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden for the best way to ask your question.

For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw

Need to Split some strings? Jeff Moden's DelimitedSplit8K
Jeff Moden's Cross tab and Pivots Part 1
Jeff Moden's Cross tab and Pivots Part 2

Jeremy Oursler
Post #1395819
Posted Tuesday, December 18, 2012 1:31 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, August 29, 2014 2:48 PM
Points: 18, Visits: 198
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!!???
Post #1398010
Posted Tuesday, December 18, 2012 1:46 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:32 PM
Points: 13,455, Visits: 12,318
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!!???


Interesting....then how did this other post from today just happen to have not only the same requirements but the exact same data?

http://www.sqlservercentral.com/Forums/Topic1398007-392-1.aspx


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1398026
Posted Tuesday, December 18, 2012 2:01 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, August 29, 2014 2:48 PM
Points: 18, Visits: 198
yea i jus saw tht...he is my friend ...we both are new to this forum and we both trying to figure out the solution...we tryin to delete one post but jus couldnt figure out how...man i dont get it...why everybody are acting like teachers??!!! or a spy..if u dont want to help thn jus move on.
Post #1398039
Posted Tuesday, December 18, 2012 3:44 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 10:38 AM
Points: 1,945, Visits: 3,004
.why everybody are acting like teachers?


Some of us are teachers. And it is our duty to find students who cheat on their homework and get them expelled from their schools for it. Over the years I have gotten two students (New Zealand, guys with "do my homework" requests) and one instructor (reproducing copyrighted materials -- my books -- without permission from my publisher).

Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. If you know how, follow ISO-11179 data element naming conventions and formatting rules. Temporal data should use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect.

This is minimal polite behavior on SQL forums.


Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1398080
Posted Tuesday, December 18, 2012 4:00 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, August 29, 2014 2:48 PM
Points: 18, Visits: 198
CELKO (12/18/2012)
.why everybody are acting like teachers?


Some of us are teachers. And it is our duty to find students who cheat on their homework and get them expelled from their schools for it. Over the years I have gotten two students (New Zealand, guys with "do my homework" requests) and one instructor (reproducing copyrighted materials -- my books -- without permission from my publisher).

Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. If you know how, follow ISO-11179 data element naming conventions and formatting rules. Temporal data should use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect.

This is minimal polite behavior on SQL forums.


thank you for the explanation CELKO. it does make sense... i dont want this forum to be cheaters way out either...i am not a DBA either a student, was jus trying to learn SQL by myself. well as i said i am new and havent gone through all those formats/rules (which i should). but at least if somebody can tell me wht functions can get me to my result i could start trying them out.
Post #1398086
Posted Tuesday, December 18, 2012 4:06 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 10:38 AM
Points: 1,945, Visits: 3,004
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.


Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1398087
Posted Tuesday, December 18, 2012 9:50 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 8:34 PM
Points: 37,098, Visits: 31,648
CELKO (12/18/2012)
Let's use {snip} and the MySQL year format:


Since this is an SQL Server forum, let's not. Besides, that break a cardinal rule... though shalt not store dates as character based data.

Untested


Why not? You've got the data and the code. Press the {f5} key and run it!


--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1398149
Posted Tuesday, December 18, 2012 10:03 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 8:34 PM
Points: 37,098, Visits: 31,648
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.


--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1398159
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse