Skip to content
Advertisement

SQL query to create a merged table with varied timestamps and varied column mapping

I am trying to write an complex mySQL query in which there are 2 tables action and revenue what I need is:

  1. From auction table take out location, postal code on the basis of user, cat_id, cat and spent and join with revenue table which has revenue column so as that given cat_id, cat and date I can figure out the returns that each ‘postal’ is generating.

Complexities:

User is unique key here

  1. In auction table has column ‘spent’ but its populates only when ‘event’ column has ‘show’ but it has ‘cat’ entry. And ‘cat_id’ starts populating at any event except show. So need to map cat_id from ‘cat’ for event ‘show’ to get the spent for that cat_id.

  2. The date has to be setup such that while joining the tables the timestamp should be compared for plus minus 10 mins. Right now in my query I have 24 hrs duration

  3. Aggregating on postal in desc order to postal giving highest returns

Want to create final table(by aggregating on revenue for each ‘postal’ area):

I have written a query but unable to figure out solution for above mention 3 complexities:

Any suggestion will be helpful

Advertisement

Answer

This answers the question for MySQL, which is the original tag on the question as well as mentioned in the question.

If I understand correctly, your issue is “joining” within a time frame. You can do what you want using a correlated subquery. Then the rest is aggregation, which I think is:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement