SQL Query for summation of a difference - sql

I am trying to get the total spending by customers (and their industry types) as (Invoice total- Credit Memo). I tried doing this as a summation of the difference in the SELECT statement with a GROUP BY Cust. Code. But this doesn't work at all. Where am I going wrong
SELECT "CardCode",
"CardName",
"IndName",
(OINV."DocTotal" - ORIN."DocTotal") AS InvoiceTotal
GROUP BY "CardCode"
Appreciate any help!

Related

SUM rows by column unknown parameter - SQL

I've a table like this one
Where each customer has its own budget, and he/she spent it on some fruits. Budget isn't deduplicated by row (so Mike has 20 bucks to spend overall), whilst spent budget is purchase-specific (Mike has now 1 dollar to spend only)
I'd like to add a column that sum the overall spend by each customer in a non deduplicated way.
Like this:
I can't specify any WHERE clause, as I don't know all the names of the customers.
Any idea on how to go from one table to the other?
Thanks,
Carlo
You could use this query to achieve the data you need:
select
customer_name,
customer_budget,
purchase_type,
budget,
customer_budget - sum(budget) as remaining_budget
from Customers
group by customer_name, customer_budget, purchase_type, budget
Hope this helps :)

Nesting Queries in Access SQL

I have recently started using SQL and am stuck when applying it to Access. My previous experience (limited) has been with PostgreSQL, and I understand that SQL in Microsoft Access requires you to nest queries into sub-queries, which I am not familiar with.
I believe the code in SQL (not for access would look something like this...)
select weeks, sum(sweets_eaten), count(distinct (sweet))
from table
group by weeks;
This would then give me a table where I would have the unique weeks, the sum of sweets eaten per week and the number of sweets per week.
Ideally, what the query would then do is also tell me the average sweets eaten per week by dividing the total sweets eaten per week by the number of sweets.
Does anyone know how to write a query so this will work in Microsoft Access?
Thanks!
Edited code, this is what I am entering
select f15, sum(f16), count(*)
from (select f15, sum(f16) as sum_sweets_eaten
from table1
group by f15, f16
) as t
group by f15;
For the average, would it be possible to do this in addition to the sum.
The query you have written will not work in MS Access, because it does not support count(distinct).
You can pre-aggregate to get the result you want:
select weeks, sum(sum_sweets_eaten), count(*)
from (select weeks, sum(sweets_eaten) as sum_sweets_eaten
from table
group by weeks, sweet
) as t
group by weeks;
To get the average, use avg() rather than sum().

Is it possible to do a query for a specific value, whereby it is increased by a percentage?

This may seem like a hard question to understand; I'm brand new to any types of coding and scripting and therefore I cannot explain this better than I have, sorry :)
I have been tasked with writing and executing a query for the following requests: "Output the sales order ID, order date, subtotal, and the tax amount increased by 2.5% for each sales order." That last section is where the problem arises. I have been able to bring up a super simple table by just entering:
SELECT SalesOrderID, OrderDate, SubTotal, TaxAmt
FROM SalesLT.SalesOrderHeader
When I executed this the table worked as intended, but my problem comes when I am asked to have the tax amount "increased by 2.5%"; what does this mean?
Again, sorry if my explanation isn't very useful; it's the best I can provide.
If you need any extra information, please let me know :)
Well, my first guess would be:
SELECT SalesOrderID, OrderDate, SubTotal,
(TaxAmt * 1.025) as TaxAmt
FROM SalesLT.SalesOrderHeader;
You need to add like this.
SELECT (TaxAmt * 1.025) as tax

SQL DateDiff Syntax

I have a homework problem that I'm having a lot of trouble with... I don't expect the answer and I truly want to learn it. Could somebody help me out with the syntax?
Problem:
For each Sales Order, show how many days it took to ship the order in order by the longest order, then by Sales Order Number. Display Sales Order Number and the number of days to ship. Include the orders that have not yet shipped.
So far I have:
SELECT SalesOrder.SalesOrderNumber,
DATEDIFF (d, MIN(SalesOrder.OrderDate), MAX(Shipment.ShipmentDate)) AS "DaysToShip"
FROM SalesOrder, Shipment
GROUP BY SalesOrder.SalesOrderNumber;
Sometimes it's helpful to see an intermediate form of your query to evaluate if it's providing the correct data at some stage.
Consider the following query, pulled from your example minus some elements:
SELECT SalesOrder.SalesOrderNumber, SalesOrder.OrderDate, Shipment.ShipmentDate
FROM SalesOrder, Shipment
You should observe the results of this query and see how they differ from what you expect. In this case, you haven't indicated how SalesOrder and Shipment are related. The result will be many more rows than there are orders, with each SalesOrder related to each and every other Shipment record (a cross-join).
Once you provide the correct join condition and achieve the desired results at that stage, try adding in aggregation (GROUP BY, MIN, MAX) and test that form of your query. Finally, when you're convinced that you have the correct inputs, add in DATEDIFF and you'll have your final query.
SELECT SalesOrder.SalesOrderNumber,
DATEDIFF (d, MAX(SalesOrder.OrderDate), MAX(Shipment.ShipmentDate)) AS "DaysToShip"
FROM SalesOrder, Shipment
GROUP BY SalesOrder.SalesOrderNumber;

sql average of a sum divided by a count

I am trying to do an SQL query to get the average spend for a specific customer.
I have written the following SQL (this is slighlty cut down for this example)..
SELECT SUM(price) as sumPrice, COUNT(transactionId) as transactionCount, customerName
FROM customers, transactions
WHERE customers.customerId = transactions.customerId
AND transactiontypeId = 1
GROUP BY customers.customerId
This gives me the sum of the transaction and the count. With this I can then divide the sum by the count to get the average spend. However I would like to be able to get the Average as a value straight out of the database rather than manipulate the data once I have got it out.
Is there any way to do this? I have played around with writing a select within a select but haven;t had much luck as of yet, hence asking on here.
Thanks in advance
MySQL has a mean average function built-in.
SELECT AVG(price) AS averageSpend, customerName
FROM customers, transactions
WHERE customers.customerId = transactions.customerId
AND transactiontypeId = 1
GROUP BY customers.customerId