SQL Query for sum of columns based on trip - sql

I am having below table and here I need to calculate the total distance travelled. For Ex:
enter image description here
Here I need to calculate the total trip distance its covered from Start Location to end location. like From A to A total distance is 55 km. From E to E 41 KM. Not sure how to calculate it in SQL. Can you any one help me to give some idea to fix this.
Regards
Anand

Related

SQL Weighted Average - Population & Income

I am trying to find the average household income while using a weighted average. I have a data source of ZIP codes with the total population and the average household income. I want to be able to select multiple ZIP codes and still pull an accurate average household income.
Can I use SQL to pull a weighted average like this?
ZIP
TOTAL_POP
AVG_HH_INC
12345
130350
66000
54321
55750
78000
44668
17300
89000
If you want the overall average, then use arithmetic:
select sum(total_pop * avg_hh_inc) / sunm(total_pop)
from t;
Note: If the values are stored as 4-byte integers, then this runs the risk of overflow. Just use a different numeric representation if that is an issue.

SQL - Finding Percent Of a Total and subtracting to get two new totals

In my SQL course I'm trying to answer a problem in which i'm being asked to find X% of a Total then subtracting that result from the original total to produce another result. Then putting these results (the X% of the total and the total-X% of total) in two new columns.
Example: We need to know how much money we owe Tom and Ted. We have total up sales to $1,000,000.00. We owe Tom 75% of that total. The remainder goes to Ted.
I can't seem to find anything in my readings/videos about this nor a google search that isn't an answers that produces ratios or comparing to other records in the table. Also, not sure about how to get the results into their own columns. Thanks for any advice!
Example of what I got so far:
SELECT SUM(Sale_Amount) From Order_Table
Now I have to find the % of that SUM then subtract it from the SUM and push both results to two new columns, one for the percent of the SUM(Sale_Amount) and one for the remainder.
Given it's an SQL course (and it's not 100% clear what's being asked), I'm not going to give you the total answer, but I'll give you components but you'll need to understand them to put them together.
In SQL, you can
Get totals using SUM and GROUP BY
Do normal maths e.g., SELECT 10000 * 60/100 to get percentages of totals
'Save' results by a) having columns/fields to save them in, and b) UPDATE those fields with relevant data
Note if you're not saving data, and simply reporting them, you can just add those to a SELECT statement e.g., SELECT 100000 AS Total, 100000 * 0.75 AS Toms_Share, 100000 * 0.25 AS Teds_Share.

Pivot Table Calculated Field to Avg the Sums totaled

Need this to average = 7.41
But this happens when I use AVG
I am trying to calculate the daily average of each employee based on the number of days worked.
I already used a pivot table to calculate the daily total hours of each employee per day but I cannot figure out how to get the pivot table to display the average work day. When I alter the field settings, it averages the source data which I do not need.
The employees worked a different # of workdays, so I need the average function to calculate based on the # of instances for each employee. When I highlight the first employees data, Excel returns an average of 7.41.
Further down the list though, there are employees with 0.00 hours for a date that is beiong calculated into the averages.
How do I get this pivot table to give me a true snapshot of the persons daily hours worked - without having to manually delete 0.00 hour instances in the source data?

Calculate average VBA from dynamic table

I ve a table on Excel with the week number and the weight (in Kg.)
Somebody can insert his weight every day during a week or just once or not at all. I can't manage that.
Then my table can literally change. from zero to 7 even more lines a week (like the yellow side of the image).
What I wanna do is to calculate the weight average per week. and then I will have one line for each week, when i got at least one weight (sometimes I won't have any line). We can have week without any weight so then I don't want this line at all. We can also easily have a weight for the week 2 but between the weeks 5 and 6 in the yellow table. That would happen if someone insert his weight after others.
How can I say this two weeks are similar, so we calculate the average for this two weight ?
I hope it's enough clear with this picture
Use formula below in Column C to calculate average(assume Week in column A and Weight in column B)
=AVERAGEIF(A:A,A2,B:B)
Average Column Copy->PasteSpecial value only,
then Remove Duplicates base on Week and the new Average Column

Calculating difference in column value from one row to the next

I am using an Access DB to keep track of Utility Usage on hundreds of accounts. The meters in these accounts have consumption values only per month. I need to take the consumption value of this month and subtract it from the value of the previous month. to get the consumption for this month. I know that in SQL Server, there is a lead/lag function that can calculate those differences. Is there a similar function in access? or is there a simple way to subtract the value in one row from the one above it?
Ex.
The first Line is Billed Date
The Second Line is the Meter Reading
The Third Line is Consumption
1/26/2014
2/25/2014
3/27/2014
4/28/2014
5/26/2014
7/29/2014
0
3163
4567
5672
7065
8468
1538
1625
1404
1105
1393
1403
I do not quite get some of your results, but I think you want something like:
SELECT Meters.MeterDate,
Meters.MeterReading,
(SELECT TOP 1 MeterReading
FROM Meters m WHERE m.MeterDate <Meters.MeterDate
ORDER BY MeterDate DESC) AS LastReading,
[MeterReading]-Nz([LastReading],0) AS MonthResult
FROM Meters
ORDER BY Meters.MeterReading;