0

I am trying to select two things customer_id and segment(based on some logic) which require both of them to be in GROUP BY

I tried using without alias but it failed as there is MAX in a a GROUP BY filedname . What should I do to take care off this ?

user1548157
  • 81
  • 1
  • 2
  • 7
  • Hey dognose what would be the remedy, I tried nested query, but it still failed. Any urgent help would be extremely appreciated. – user1548157 Apr 29 '14 at 19:50
  • You definitely can't use MAX(), an aggregate function, in a result column that is used to define the GROUP BY. (How would MySQL know how to aggregate before applying GROUP BY?) –  Apr 29 '14 at 19:53
  • Since you can't use the alias name in the group by clause, use whatever is being aliased. – Dan Bracuk Apr 29 '14 at 19:54
  • @user1548157 Just remove the MAX(). It may be close in that case, see how far off it is by trying it. Basically, it's not clear because this isn't really a duplicate and the linked answer is a bit obtuse. You can't invoke the aggregate function MAX() before applying the GROUP BY. –  Apr 29 '14 at 20:01
  • @user1548157 Also, just removing the SEGMENT from GROUP BY might give you exactly what you're after. A single ID can't be both ways you're just applying a categorization here... If you're worried about sorting, add an ORDER BY. –  Apr 29 '14 at 20:30

1 Answers1

0

Try the below query. Hope it will do the work

SELECT distinct 
t1.CUSTOMER_ID AS CUSTOMER_ID,
CASE WHEN tab.SEGMENT = 1 AND t2.CUSTOMER_ID IS NOT NULL THEN 'SweepAcquired'
ELSE 'NonSweepAcquired' END AS NEW_SEGMENT
FROM tablename1 t1, tablename2 t2
join
(
select CUSTOMER_ID,
max(CASE WHEN t2.event_date=t1.event_date 
AND t2.SOURCE_TYPE='sweepstakes'  
AND t2.SOURCE_ID IS NOT NULL 
AND lower(t2.SOURCE_ID) <> 'unknown' THEN 1 ELSE 0 END) AS SEGMENT
from tablename1 t1
join tablename2 t2
on t1.CUSTOMER_ID=t2.CUSTOMER_ID
) tab on t1.CUSTOMER_ID=tab.CUSTOMER_ID
WHERE t1.CUSTOMER_ID=t2.CUSTOMER_ID(+)
AND t1.MARKETPLACE_ID = '{MARKETPLACE_ID}'
AND lower(t2.event_type(+))='like'
AND lower(t2.SOURCE_TYPE(+))='sweepstakes'
AND t2.SOURCE_ID(+) IS NOT NULL
AND lower(t2.SOURCE_ID(+)) <> 'unknown'
AND t2.ENTITY_ID(+)='{FREE_FORM}'
GROUP BY t1.CUSTOMER_ID, NEW_SEGMENT;
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • Statement 2 is not valid. ORA-00937: not a single-group group function This is what I get if I try removing group by – user1548157 Apr 29 '14 at 20:04
  • @user1548157, try the edited query once. – Rahul Apr 29 '14 at 20:13
  • How can we use max which is an aggregate function without group by – user1548157 Apr 29 '14 at 20:14
  • @user1548157, Yes, you can use aggregate function even without group by. – Rahul Apr 29 '14 at 20:16
  • Get this error Statement 2 is not valid. ORA-00918: column ambiguously defined – user1548157 Apr 29 '14 at 20:21
  • @user1548157 MAX() with no GROUP BY is equivalent to something like GROUP BY 1. (ie: all rows merge to just 1 row) PS - Are you sure you even **want** MAX() in this case? I mean, how can a single ID have both 1 and 0 there? Seems like you're bending a lot of conventions with this one. FOO_ID should usually be "auto-increment ID". Then again, I don't know the names of the tables. Just that might help me get what you're trying to accomplish. –  Apr 29 '14 at 20:21
  • 1
    @user1548157, error is because `segment` in 2nd line should be `tab.segment` – Rahul Apr 29 '14 at 20:24
  • @Rahul I'm pretty sure you would want to GROUP BY CUSTOMER_ID in your inner query... –  Apr 29 '14 at 20:27
  • Statement 2 is not valid. ORA-00918: column ambiguously defined error I get with Rahul's latest query – user1548157 Apr 29 '14 at 20:28
  • @ebyrob, may be not sure; I am bit confused with the query syntax itself. Moreover, you are correct, I don't see any need of `max()` function here .. probably a `sum()` function may be helpful. – Rahul Apr 29 '14 at 20:29
  • @user1548157, strange! did you tried the recent one (I edited after your last comment here). so you shouldn't get that error; at least. – Rahul Apr 29 '14 at 20:31
  • Yes, I tried the latest now you have – user1548157 Apr 29 '14 at 20:32
  • @user1548157, I am out of idea. If you want me to help further I would require your table schema for both table1 and table2. – Rahul Apr 29 '14 at 20:36
  • yeah getting the same error, I am unsure if I replace in GROUP BY the whole case thing why it did not work. Also, I dont find any issue with Rahul's latest query and it stll did not work. – user1548157 Apr 29 '14 at 20:39
  • @user1548157 You may just want to open a new question. Be sure to include 1) schema info. 2) overall goals. 3) What you have tried. PS - I think you can change group by to: "GROUP BY CUSTOMER_ID, t2.event_date = t1.event_date". However, I can't answer a closed as duplicate question. –  Apr 29 '14 at 20:50
  • Rahul, your query looked ok but the error returned seems to be that there is ambiguity resolving column names which usually happens when we use join and column names are same. – user1548157 Apr 29 '14 at 20:52
  • @ebyrop your GROUP BY suggestion does not work – user1548157 Apr 29 '14 at 20:56
  • @user1548157 Your "does not work" is broken. Please explain what "doesn't work" because I know it would resolve your specific syntax errors. (Maybe t1. needs to stay in front of CUSTOMER_ID in your specific Oracle DB product?) –  Apr 29 '14 at 20:57
  • @ebyrob, sorry if I offended you. I used GROUP BY CUSTOMER_ID, t2.event_date = t1.event_date and also t1.CUSTOMER_ID in my original query. It says SQL command not properly ended. – user1548157 Apr 29 '14 at 21:05
  • @user1548157, I think extended discussion as comment over here may not be good. As suggested by ebyrob .. open a new post with all valid information. Once opened let me know (if you feel so); can try then. will delete this post. – Rahul Apr 29 '14 at 21:15
  • @Rahul are you online ? – user1548157 Apr 30 '14 at 01:18
  • @user1548157; yes unfortunately – Rahul Apr 30 '14 at 01:23
  • t2 has schema as customer_id,entity_id,event_type,event_date,source_id,source_type ,marketplace_id . t1 has schema as ACTUAL, CUSTOMER_ID, DSI, DSIE_RUN_DATE, DSIE_VERSION, DSI_SPLIT, EVENT_DATE, HORIZON, MARKETPLACE_ID, METRIC, SE_DSI – user1548157 Apr 30 '14 at 01:54
  • @user1548157 putting `t2.event_date = t1.event_date` as a term in your group by will work. It will do exactly what you were trying to do with your original query (which probably still isn't what you're after). Perhaps if your particular syntax engine doesn't like it you can wrap it with `CASE WHEN foo THEN 1 ELSE 0 END` or even alias it as a separate column. The basic concept *will* work since it doesn't require applying MAX() before GROUP BY (If it doesn't work, there's something wrong with your query engine). PS - You know you are allowed to edit your question instead of using comments. –  Apr 30 '14 at 14:01
  • 1
    @user1548157 Since you won't open a new question and I can't answer, here's the fiddle: http://sqlfiddle.com/#!4/630d7/46 –  Apr 30 '14 at 15:20
  • @ebyrob Here is the new question I posted – user1548157 May 06 '14 at 10:51
  • http://stackoverflow.com/questions/23490091/sql-query-how-to-select-a-generated-field-in-group-by – user1548157 May 06 '14 at 11:00
  • @user1548157, since you already opened a new post and ebyrob have answered; I believe this answer would be of little/no use. I will delete then. – Rahul May 06 '14 at 17:42