Question: I'm trying to understand the Gradient descent algorithm for linear regression. 

The question is why we multiply by x(1) at the end of theta1 and don't do that at the end of theta0? 

Thanks a lot!

[![enter image description here](https://i.stack.imgur.com/gie8S.png)](https://i.stack.imgur.com/gie8S.png)

Answer: 
On quick review, there are few challenges I'm seeing.

The first is that the `ctripnumber` ("Load ID") is different between between the initial driver (`342135`) and any subsequent drivers (`34215S`). A quick fix there might be to update your `group by` and `outer join` statements to normalize the `ctripnumber` by e.g. stripping the trailing `S`—or any other suffixes that you might be adding to your `ctripnumber`.

The second is that several of your `group by` fields will vary between records. For example, `driverid` and `deliverydate` are *always* going to be different, so if you keep them in your `group by` you're *always* going to get multiple records. In order to mitigate that, you might do something like `MAX(deliverydate) AS 'Delivery Date'` in order to only select the highest value. Selecting only the first `driverid` is a bit more involved, and requires you to do something like ranking your records using a `ROW_NUMBER() OVER()` function.

***That said*, this *still* leaves you with the problem of ensuring your splits have been *fulfilled*, and aren't *in progress*.**

Assuming you don't need to aggregate data for any interstitial drivers (e.g., `count(driverid)` or `sum(revenue)`), you should be able to just do something like:

```sql
JOIN        tripnumber tn_finished 
ON          tn_finished.ctripnumber =
  WHEN      tn.csplittripnumber is null
  THEN      tn.ctripnumber
  ELSE      tn_finished.ctripnumber
AND         tn_finished.ctripstatus = 'Completed'

```

> 
> *Note:* The `WHEN` statement ensures that the initial driver's record joins to itself for cases where the delivery was not split, and the initial driver completed the final delivery.
> 
> 
> 

This ensures that the split has been finished. But this *also* addresses the first two issues, as you can now use `tn.tripnumber` to get the *base* Load ID, `tn.driverid` to get the *initial* driver, and the `tn_finished.deliverydate` to get the *final* delivery date—and you shouldn't need to fuss with a `group by` at all.

Again, though, this only works by completely ignoring all interstitial records (i.e., drivers that didn't do the initial pickup or the final delivery) so this only makes sense if you can safely ignore those records for the sake of this report.
I would recommend using a CTE (Common Table Expression) for this. The CTE will give the first result set, then the query below it filters it down again to the rows you want.

Since you did not provide a sample script, this code is untested:

```
;with BaseQuery as 
(
    select 
        tn.ctripnumber as 'Load ID',
        tn.driverid as 'Driver ID'
        tn.ctripstatus as 'Trip Status',
        tn.ddeliverydate as 'Delivery Date',
        tn.corigcity as 'Origin City',
        tn.cdestcity as 'Destination City',
        tn.csplittripnumber as 'Split Number',
        'Revenue'
    from tripnumber tn  
    left outer join (
        select ctripnumber, sum(curevenue) as "Revenue" 
        from revenuedtl 
        group by ctripnumber
    ) r on tn.ctripnumber = r.ctripnumber 
    where tn.ctripnumber like ('342135%')  
    group by
        tn.ctripnumber,
        tn.driverid,
        tn.ctripstatus,
        tn.ddeliverydate,
        tn.corigcity,
        tn.cdestcity,
        tn.csplittripnumber,
        Revenue
)
select 'Load ID', 'Driver ID' 'Trip Status',
       'Delivery Date', 'Origin City', 'Destination City',
       'Split Number', Revenue
from   BaseQuery
where  "Revenue" is not null

```