Sum up a group of rows in the output - sum

I'm trying to do a sum of a select group of rows. ex. I have a warehouse with 10 routes and I would like to sum the total weight of the cases on the truck by route. I have rows listed with the total weight but want to make an extra row with the total.

Related

SQL Percent Rank use 4-week average number to find percentage rank within full FlowFunds values. Partition by FlowGroup

I want to find the percentage rank of where a 4-week average sits across an entire time series of data.
[table attached here]
In this example, I want to find how the 4-week avg of FlowGroup a (507.5858) ranks in comparison to the other 4 values of FlowGroup a (804.6113, 1781.3388, 627.1612, -1182.7681).
The return value for FlowGroup a should be = 0.311
Currently I am ranking each individual Flow$ against the total like this:
PERCENT_RANK ()
OVER (PARTITION BY FlowGroup ORDER BY Flow$) as PctRank
I do not know how I can percent rank the 4-week average against the total Flow$, partitioned by FlowGroup.
Cheers!

SQL - how to show SKUs with the lowest price from a query

I am making a query for a price comparison site and I have to provide them a csv file with certain data. I have done the query and the first problem was that the prices that showed up in it were the full prices from sql database. So when we have for example a discount applied to the whole category of products the price shown is still the full price not the discounted one. I solved that by checking if an SKU is in a discounted category and the query calculates the price accordingly.
The new problem is now that a query checks through categories and if one sku is in multiple categories it creates a row with same SKU for each category and I end up with multiple rows with same SKU and only one has correct price (discounted one).
Example query result. NOTE that the Price column is a calculated result not the data from the table.
SKU
Product
Price
A
Ball
19
A
Ball
19
A
Ball
15
B
Cube
10
B
Cube
8
How do I filter it to just this
SKU
Product
Price
A
Ball
15
B
Cube
8
You need GROUP BY with aggregate function MIN()
SELECT SKU
, Product
, MIN(Price) as Price
FROM <table>
GROUP
BY SKU
, Product

SUM results is multiplying by number of rows

I'm creating a crystal report from several tables.
One table has fields that I want to have sum totals on, but these sum fields are being distorted by number of rows from another table. There are no fields other than DocEntry that I can link with between the two tables.
Here, total bales is repeating 4 times:
If I sum the field total bales, instead of showing 12 the result is 48:
Please assist.
Insert a Group on DocEntry.
Add a Running Total that sums Bales but evaluates only on change of group.

Postgres cross tabs mixes categories in result set

I have a query returning me three columns 'Date', 'District' and 'Total'. There are four districts but some time a district may not have record for corresponding date. I want to put all my districts in column and their total in rows as ..
Date, District1, District2, District3, District4
While in my rows will be date along with the total value per district.
I am using crosstab to get the desired results. But in the result total for one district is listed in other district though the datewise grand total is same.
I don't know why crosstab query mixes categories. Please help.

Subtract/divide values from different rows in the same group

I don't know how to calculate the variance percentage of revenues from this year and the past year. In order to obtain the variance by percentage, I have to subtract two values from different rows.
Here is the demonstration of my report and the results that I wanted to obtain: