If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Counting orders where customers purchased 1 item, >1 same color, >1 different color?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-20-11, 13:47
clawlan clawlan is offline
Registered User
 
Join Date: Oct 2010
Posts: 55
Question Counting orders where customers purchased 1 item, >1 same color, >1 different color?

I am struggling with this query. In our system, items are organized by itemNo (collection of like items) and SKU (unique item within the itemNo).

For example, itemNo UL005 is a chair, and there are 5 SKUs for that ItemNo because there are 5 different color choices: UL005BL, UL005BR, etc.

I am trying to figure break out the orders into 3 buckets:

1. Orders with only 1 purchased (pretty straightforward. group by ItemNo where qty = 1)
2. Orders with >1 and like color (ie only 1 SKU in the order and qty>1)
3. Orders with >1 and different colors (multiple SKUs in same order)

NOTE: In an order that has >1 like color AND an additional color(s), defaults to #3. In other words, to fit into group #2, must have purchased >1 and only one color.

#1 is easy enough. But I am struggling with #2 and #3. Ideas?
Reply With Quote
  #2 (permalink)  
Old 12-20-11, 15:21
blindman blindman is online now
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,732
use Count(DISTINCT SKU) to determine whether multiple SKUs were ordered.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #3 (permalink)  
Old 12-21-11, 04:57
dm1 dm1 is offline
Registered User
 
Join Date: Dec 2011
Posts: 13
Let's assume we have the following table and data:

Code:
CREATE TABLE orders
(
  id INT,
  item VARCHAR(30),
  sku VARCHAR(30)
);

INSERT INTO orders VALUES (1, 'chair', 'brown');

INSERT INTO orders VALUES (2, 'chair', 'green');
INSERT INTO orders VALUES (2, 'chair', 'green');

INSERT INTO orders VALUES (3, 'chair', 'brown');
INSERT INTO orders VALUES (3, 'chair', 'green');
Now to return orders with one item purchased (returns id = 1):

Code:
SELECT id FROM orders
GROUP BY id 
HAVING COUNT(*) = 1;
Orders with multiple items purchased but having the same type (2 green chairs):

Code:
SELECT id FROM orders
GROUP BY id 
HAVING COUNT(DISTINCT sku) = 1 AND COUNT(*) > 1;
Orders with multiple items having different types (green and brown chairs):

Code:
SELECT id FROM orders
GROUP BY id 
HAVING COUNT(DISTINCT sku) > 1 AND COUNT(*) > 1;
Dmitry

--
http://www.sqlines.com - SQL Articles and Free Online SQL Conversion Tool
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On