• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 43
  • Last Modified:

SQL Group By

The setup of my question:
data:
customer_id                   Item_ID                order_date
1                                      apples                  11/1/17
2                                      apples                   1/1/17
3                                      apples                   5/1/17

My question is I want to see what customer purchase apples last and what was the sales date

Below is my script but when I run this it show them all since I have customer_id in the group by.

SELECT DISTINCT item_id, MAX(order_date) AS last_sales_date, DATEDIFF(day, MAX(order_date), GETDATE()) AS days_since_last_sold, customer_id
FROM            p21_order_view
GROUP BY item_id, customer_id
HAVING        (item_id = 'apples')
ORDER BY last_sales_date

Below are the results I want to see:
customer_id                   Item_ID                order_date
1                                      apples                  11/1/17

Thanks
0
abarefoot
Asked:
abarefoot
  • 4
  • 4
  • 3
1 Solution
 
NerdsOfTechTechnology ScientistCommented:
try:

 SELECT customer_id, item_id, MAX(order_date) AS last_sales_date, DATEDIFF(day, MAX(order_date), GETDATE()) AS days_since_last_sold
 FROM  p21_order_view
 GROUP BY item_id 
 HAVING item_id = 'apples'
 ORDER BY last_sales_date

Open in new window

0
 
abarefootAuthor Commented:
All you did is remove customer_id from the group by which will not work.  When you do that you get the below error

Msg 8120, Level 16, State 1, Line 2
Column 'p21_order_view.customer_id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
0
 
NerdsOfTechTechnology ScientistCommented:
You're right, this works:

 
 SELECT TOP 1
 customer_id, 
 item_id, 
 MAX(order_date) AS last_sales_date, 
 DATEDIFF(day, MAX(order_date), GETDATE()) AS days_since_last_sold
 FROM  p21_order_view
 GROUP BY item_id, customer_id 
 HAVING item_id = 'apples'
 ORDER BY last_sales_date DESC;

Open in new window


http://sqlfiddle.com/#!6/3132fd/23
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
abarefootAuthor Commented:
HA yes that works but not what I'm looking for.   I'm needing the one with the latest order_date.  So if the second one had the latest order date and all you do it say select Top 1 it will not select the correct one.  I explained this in the original post but just encase this should clear it up.

customer_id                   Item_ID                order_date
2                                      apples                  1/1/17
1                                      apples                   11/1/17
3                                      apples                   5/1/17

The results still need to be
1                                      apples                   11/1/17
0
 
NerdsOfTechTechnology ScientistCommented:
I'm returning the correct results

customer_id      item_id      last_sales_date      days_since_last_sold
1      apples      2017-11-01      14

I'll even add some more values:

Proof:

http://sqlfiddle.com/#!6/6b207/1
0
 
Pawan KumarDatabase ExpertCommented:
Please use this tested solution-

/*------------------------

 SELECT customer_id, Item_ID, order_date FROM
 (
	 SELECT * , ROW_NUMBER() OVER (PARTITION BY Item_ID ORDER BY order_date DESC) rnk
	 FROM p21_order_view
	 WHERE Item_ID = 'apples'
)o WHERE rnk = 1 
------------------------*/
customer_id Item_ID                                            order_date
----------- -------------------------------------------------- ----------
4           apples                                             2017-11-01

(1 row(s) affected)

Open in new window

0
 
abarefootAuthor Commented:
OK that's great but what if I'm wanting to select more then just apples and have hundreds of items I'm wanting to select the latest order date and customer
Example:
customer_id                   Item_ID                order_date
2                                      apples                  1/1/17
4                                      orange                  4/1/17
1                                      apples                   11/1/17
3                                      apples                   5/1/17
1                                      orange                  12/1/16


results need to be
1                                      apples                   11/1/17
4                                      orange                  4/1/17
0
 
Pawan KumarDatabase ExpertCommented:
you can use below. Just remove the where clause,

/*------------------------

 SELECT customer_id, Item_ID, order_date FROM
 (
	 SELECT * , ROW_NUMBER() OVER (PARTITION BY Item_ID ORDER BY order_date DESC) rnk
	 FROM p21_order_view	 
)o WHERE rnk = 1 
------------------------*/
customer_id Item_ID                                            order_date
----------- -------------------------------------------------- ----------
1           apples                                             2017-11-01
6           oranges                                            2017-06-01
7           peaches                                            2017-07-01

(3 row(s) affected)

Open in new window

1
 
abarefootAuthor Commented:
Thanks for the help!  That's exactly what I was looking for.
0
 
NerdsOfTechTechnology ScientistCommented:
ROW_NUMBER OVER (PARTITION BY will do the trick. Thanks Pawan Kumar. Your solution is optimal.
0
 
Pawan KumarDatabase ExpertCommented:
welcome. glad to help as always.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now