简体   繁体   中英

SQL query in Laravel DB::raw() to order column and get first result

I'm trying to order a specific column in my DB::raw() select statement, I need to order the items for each day and get the first one for each day, so I'd end up with 5 results for 5 months but my error is

Illuminate\Database\QueryException with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax

What am I missing?

GoogleAnalytics::where('event_category', 'Category')
                ->where('event_action', 'Action')
                ->whereDate('period_from', '>=', '2021-06-08')
                ->whereDate('period_to', '<=', '2021-06-08')
                ->select(DB::raw('DATE_FORMAT(created_at, "%Y-%m") as created_at'), DB::raw('FIRST(event_count ORDER BY created_at DESC ) as sessions'))
                ->groupByRaw('DATE_FORMAT(created_at, "%Y-%m")')
                ->orderBy('created_at', 'desc')
                ->get();

Some more context here, each day might have 100 entries, so there could be 3,000 entries per month, I need to get just a single entry in each day so I end up with 30 entries, and then add up those 30 result to produce a single result for that one month, so I'd end up with 5 items in my returned collection, but right now I can't seem to get DB::raw('FIRST(event_count ORDER BY created_at DESC ) as sessions') to grab the column, order it and just get the first result.

I don't think FIRST is a function at all, hence the error. If we break your problems into two you want:

  1. One row for each day with the event_count column present. As per your comment above the column increments and you want the last value, thus you want the max value per day.
  2. Sum the values of the event_count column, grouping by the month and year

1:

A select statement for this would look like

select date(created_at), max(event_count) from google_analytics group by date(created_at) where ...

Using the Eloquent builder it would look something like this:

$query = GoogleAnalytics::select(
            DB::raw('date(created_at) as created_at'),
            DB::raw('max(event_count) as sessions')
        )
        ->where('event_category', 'Category')
        ->where('event_action', 'Action')
        ->whereDate('period_from', '>=', '2021-06-08')
        ->whereDate('period_to', '<=', '2021-06-08')
        ->groupByRaw('date(created_at)');

Note:

  • your where clause only looks at one date (2021-06-08)
  • this query will not return complete GoogleAnalytics models

2:

We can use the above $query to group the results per month:

$result = DB::table($query, 'per_day')->select(
            DB::raw('date_format(created_at, "%Y-%m") as created_at'),
            DB::raw('sum(sessions) as sessions')
        )
        ->groupByRaw('date_format(created_at, "%Y-%m")')
        ->orderByRaw('date_format(created_at, "%Y-%m") desc')
        ->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM