Postgresql- divide sum by total already in table - sql

I have a table with several time intervals as rows with one "total" row. I have four columns; car, bus, truck, and total, that refer to the number of vehicles leaving a warehouse at each time interval by category and the total number of vehicles at each time interval. My table looks like this:
time car truck bus total
12-6am 10 15 10 35
7am-12pm 8 12 8 28
Total 18 27 18 63
I want to create a percent total row that takes the total value in each row (35 and 28) and divides it by the maximum value in the total row (63).
How do I do this?

If you look at the schema of your table, it doesn't make sense to have an extra row in it, but only an extra column.
However, even that is a bad idea. A database is not a spreadsheet, where you have largely free-form data. It's a collection of tables. Total rows should be calculated with SELECT statements, not make some attempt to have them in the table. Unlike a spreadsheet, Postgres won't auto-update that as rows are added and deleted. (Note: Yes, sometimes you need to materialize this summary stuff for efficiency, but that's the advanced course.)

Related

Microsoft Access- How to create dynamic variables that queries a selection of Columns

Example Data Picture
My main data table is constructed in the following way:
1.State
2.Product
3.Account Name
4.Jan-20
5.Feb-20
.
.
.
N.)Recent Month- Recent Year
My goal is to get 6 total sums based on 6 different contiguous that are user selected. For example, if someone wanted the value of an Account Given a State and Product for FY-2020, they would sum columns 4 to column 15 (Twelve Months).
I am going to be running joins and queries off of the State, Product, Account combinations (first three columns), but I need to create a method to sum the Data table given a list of Column Numbers.
At this point, I am not looking to put non-contiguous columns in a selected Time Period (i.e. all time period selections will be from Col.Beg_TPn to and including Col.End_TPn). The Data Table houses monthly data that will have one new column every month. The Column Number should stay consistent as we are not looking back further than FY-2020.
This a much easier problem in Excel as you can do a simple SumIfs with an Index of the Column Range as the SumRange and then you filter on Columns 1,2,3. My data table is about 30,000 rows, so Excel freezes on me when could calculations and functions on the entire data set.
What is the best way to go about this in Microsoft Access? Ideally, I would like to create a CTE_TimePeriodTotals that houses the First 3 Columns (State,Product,Account) and then 6 TP Columns (tp-1,tp-2...) that holds the sum of each time period for each row based on the Time Period Column Starts and Time Period Column End.

Loop through columns in Access SQL

In my Access database, I have a query (call it qrySource) that adds a new column every month. When you run the query in March, the datasheet looks like this:
ID January February March
1 20% 50% 100%
2 10% 60% 95%%
3 25% 60% 80%
4 20% 30% 75%
5 15% 50% 100%
Next month it would have an April column, etc.
I have another query that pulls from this one:
SELECT qrySource.ID, qrySource.January AS Jan, qrySource.February AS Feb, qrySource.March AS Mar
FROM qrySource
As it stands, I have to update this query every month to pull in the new monthly column from qrySource.
(I can't just use SELECT *, because the real query is more complex than this. For one thing, it pulls fields from several "source" queries.)
Is there a way to make this query "loop" through all the columns in qrySource without knowing exactly how many there are?

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

Iterate through table by date column for each common value of different column

Below I have the following table structure:
CREATE TABLE StandardTable
(
RecordId varchar(50),
Balance float,
Payment float,
ProcDate date,
RecordIdCreationDate date,
-- multiple other columns used for calculations
)
And here is what a small sample of what my data might look like:
RecordId Balance Payment ProcDate RecordIdCreationDate
1 1000 100 2005-01-01 2005-01-01
2 5000 250 2008-01-01 2008-01-01
3 7500 350 2006-06-01 2006-06-01
1 900 100 2005-02-01 NULL
2 4750 250 2008-02-01 NULL
3 7150 350 2006-07-01 NULL
The table holds data on a transactional basis and has millions of rows in it. The ProcDate field indicates the month that each transaction is being processed. Regardless of when the transaction occurs throughout the month, the ProcDate field is hard coded to the first of the month that the transaction happened in. So if a transaction occurred on 2009-01-17, the ProcDate field would be 2009-01-01. I'm dealing with historical data, and it goes back to as early as 2005-01-01. There are multiple instances of each RecordId in the table. A RecordId will show up in each month until the Balance column reaches 0. Some RecordId's originate in the month the data starts (where ProcDate is 2005-01-01) and others don't originate until a later date. The RecordIdCreationDate field represents the date where the RecordId was originated. So that row has millions of NULL values in the table because every month that each RecordId didn't originate in is equal to NULL.
I need to somehow look at each RecordId, and run a number of different calculations on a month to month basis. What I mean is I have to compare column values for each RecordId where the ProcDate might be something like 2008-01-01, and compare those values to the same column values where the ProcDate would be 2008-02-01. Then after I run my calculations for the RecordId in that month, I have to compare values from 2008-02-01 to values in 2008-03-01 and run my calculations again, etc. I'm thinking that I can do this all within one big WHILE loop, but I'm not entirely sure what that would look like.
The first thing I did was create another table in my database that had the same table design as my StandardTable and I called it ProcTable. In that ProcTable, I inserted all of the data where the RecordIdCreationDate was not equal to NULL. This gave me the first instance of each RecordId in the database. I was able to run my calculations for the first month successfully, but where I'm struggling is how I use the column values in the ProcTable, and compare those to the column values where the ProcDate is the month after that. Even if I could somehow do that, I'm not sure how I would repeat that process to compare the 2nd month's data to the 3rd month's data, and the 3rd month's data to the 4th month's data, etc.
Any suggestions? Thanks in advance.
Seems to me, all you need to do is JOIN the table to itself, on this condition
ON MyTable1.RecordId = MyTable2.RecordId
AND MyTable1.ProcDate = DATEADD(month, -1, MyTable2.ProcDate)
Then you will have all the rows in your table (MyTable1), joined to the same RecordId's row from the next month (MyTable2).
And in each row you can do whatever calculations you want between the two joined tables.

Sum Multiple Column MS Access Via Sql Query or VBA

Problem:I am trying to make a Inventory Management Database on Microsoft Access 2010, and since i needed records date wise my Table looks like follows
SKU 2016-03-16 2016-03-17 2016-03-18 ... Total
AAA 10 -5 15 ... 20
BBB 05 05 25 ... 35
CCC 06 -5 24 ... 25
This way i wanted to add records for each day, but i am unable to make a "Total" column which will total all the columns (Sum(Columns*))
I am aware of sum and groupby but that works across multiple rows i am looking for something which can do the same for column
Expected Solution: A way either by Sql Query or VBA to total all the columns for SKU AAA,BBB,CCC, if not possible to total in the same table then i am open to total the columns in a new table.
Such a table is normally result of pivoting summary data. Real data would look like:
SKU, Date, Quantity
...
Then probably you would want to create a PIVOT table using SKU for rows, Date for Columns and Quantity for data (default operation is SUM). It would have the row totals by default.
It may have been named crosstab, cross tab .. or so in Access 2010.
(you may want to do the pivoting in Excel)