I have a table that has around 100,000 records. It has a column named data_key, data_value and timestamp. Now if datakey = 'crop'; I've stored the corresponding data_value in a separate table. There are 52 different types of crops. So, I've two tables now 'Eventlogs' and 'Crops'.
Now I need to get the timestamp for all the crops searched on each day. I've written a query
Select date(a.timestamp) as Date, a.datavalue as Cropname, count(a.datavalue) as Quantity
from eventlog a, crops b
where a.datavalue = b.datavalue;
But it is only showing the first occurrence of any crop for any day.
So, if tomatoes and apples were sold on 24th September the Quantity is the sum of all the crops but the crop displayed is only the first one.
Sample Data is like
Timestamp | Datakey | DataValue
23-04-2002| Crop | Tomato
23-04-2002| Crop | Tomato
23-04-2002| Crop | Apple
24-04-2002| Crop | Apple
24-04-2002| Crop | Tomato
26-04-2002| Crop | Banana
26-04-2002| Crop | Banana
27-04-2002| Crop | Tomato
Data of the crops table would be like
Crops
------------
Apple
Tomato
Banana
Now I need to produce the output like
Timestamp | Crop_name | Quantity
23-04-2002| Tomato | 2
23-04-2002| Apple | 1
24-04-2002| Apple | 1
24-04-2002| Tomato | 1
26-04-2002| Banana | 2
27-04-2002| Tomato | 1