Sum of Total Billed for last month and this month in a single SQL query - sql

I don't really know what to try, even, but we need to show a column for last month's total billing and another column for this month's total billing. Currently, we show just the total billing to date.
Here is what we have so far:
SELECT
MAX(JCCM.JCCo) as [Company],
JCCM.Contract as [Contract],
JCCM.ContractAmt as [Contract Amount],
JCCM.BilledAmt as [Billed Amount],
JCCM.ContractAmt - JCCM.BilledAmt as [Remaining Amount]
FROM
JCCM
INNER JOIN PMOI
ON PMOI.PMCo=JCCM.JCCo AND PMOI.Project = JCCM.Contract
WHERE
JCCM.JCCo=1 AND PMOI.PMCo=1 AND JCCM.Contract LIKE '2%'
GROUP BY JCCM.JCCo, JCCM.Contract, JCCM.ContractAmt, JCCM.BilledAmt
The [Billed Amount] pulls the total billed to date correctly, but we need to do two separate columns, one for the previous month total and then this month's total.

Related

Query to Sum values from one table based on values from another table, considering null values

I have two tables in MS Access. One for contracts, with contract id and total value. Another one for the payments made in each contract.
So for example when a client gets a new contract for a total value of USD 100.00 then he can pay it in any amount he want up to the total value, like two payments of 50.00 or one of 30.00 and another of 70.00 or even one hundred payments of 1.00.
And those payments go to the payments table with the contract id and the payment amount.
I need to make a query to find how much do they still have to pay for each contract. What is the net amount for each contract.
I managed to make a query that gives me the result I need, but if the contract is new and there are no payments on the payments table for this new contract, the net amount shown in my query is zero (blank field) and I needed it to show the total value of the contract instead.
For example
Contract of 100.00 with two payments of 10.00 and 15.00 should show the net value as 75.00
Contract of 100.00 with no payments should show net value as 100.00
Here is what I have so far:
'Query 1
SELECT Contracts.[Contract ID],
Sum(Payments.[Payment Value]
FROM Contracts
LEFT JOIN Payments
ON Contracts.[Contract ID] = Payments.[Contract ID]
GROUP BY Contracts.[Contract ID]
Query 2:
SELECT Contracts.[Contract ID],
Contracts.[Contract Value],
Query1.[SumOfValue Payment Value],
[Contract Value] - [SumOfValue Payment Value]
INTO NetValues
FROM Contracts
INNER JOIN Query1
ON Contracts.[Contract ID] = Query1.[Contract ID]
Basically the Query 1 , sums the payments for each contract and Query 2 calculates the net value (total value - paid amount)
If there is any better way to achieve all of this please let me know.
You can do it as a single query as follows:
SELECT c.[Contract ID],
c.[contract value],
p.Paid,
c.[contract value]-nz(p.paid) AS Remaining
FROM Contracts AS c
LEFT JOIN
(SELECT [Contract ID], sum([Payment Value]) as paid
FROM Payments
group by [Contract ID]) AS p
ON c.[Contract ID] = P.[Contract ID];
Given these two tables:
The query will produce this result:
" ... if the contract is new and there are no payments on the payments table for this new contract, the net amount shown in my query is zero (blank field)"
Actually, when you have no payments recorded for a given contract, Query1 gives you Null, not zero, as the sum of the payments. And any number minus Null gives you Null, not that first number.
If you want the Null treated as zero, use the
Nz Function
Here's an Immediate window example of those issues:
Contract_Value = 100.00
SumOfValue_Payment_Value = Null
? Contract_Value - SumOfValue_Payment_Value
Null
' use Nz() to substitute zero for Null
? Contract_Value - Nz(SumOfValue_Payment_Value, 0)
100
You can do the same in Query2 by including this:
[Contract Value] - Nz([SumOfValue Payment Value], 0)

mssql: add column with the same value for all rows to search results

I have my query:
SELECT [Shipment Date], [Amount] as [Running Costs], Sum([Amount]) OVER
(ORDER BY [Shipment Date]) as [Total Running Costs]
FROM...
This gets me 3 columns:
Shipment Date | Running Costs | Total Running Costs
I would like to add a fourth column to this query which has the same value for all rows, and the same number of rows as my original query results.
I know you could add for example '999'as Something to the search results, but how can I do the same for a sum of another column (example: Imagine the total sum of the a column in another table is 1500, and I want to have 1500 for all rows in the fourth column. Something like select sum(column_name)?
The database engine is MSSQL.
You can use a nested query
SELECT [Shipment Date], [Amount] as [Running Costs], [Total Running Costs], SUM([Total Running Costs] OVER ())
FROM
(
SELECT [Shipment Date], [Amount] as [Running Costs], Sum([Amount]) OVER
(ORDER BY [Shipment Date]) as [Total Running Costs]
FROM...
)
Nested window function should also work
SUM(SUM([Running costs]) OVER (ORDER BY [Shipment Date])) OVER ()

Calculate the percentile of products in ms-access query

I have 3 fields in a table, namely category, product, avg.sales. How do I calculate the percentile of each product under its respective category? Currently, I make different tables for each category and then calculate the percentile, but is it possible to do it in just one table?
Here is the code that I used. "MobilesCount" is another query which stores the total number of products in mobile category. I hope this helps
SELECT
o.CategoryID,
o.product,
o.brand,
(SELECT
COUNT(i.[Avg Sales])
FROM
[ProductItemsSold] i
WHERE
i.[Avg Sales] <= o.[Avg Sales]
AND
CategoryID = 28
AND
[Avg Sales]>0
)/(SELECT [Total Products] From MobilesCount) AS [Sales percentile]
FROM
[ProductItemsSold(Weekly)] AS o
WHERE
o.CategoryID = 28
AND
o.[Avg Sales]>0
ORDER BY
o.product;
Thanks for the help :-)

Show sales grouped per month in Access?

I'm trying to show sales grouped per month, to show something like:
201001 10000
201002 13000
201003 11000
201004 8000
Why doesn't this work?
SELECT [Transaction details].[Sales volume LOC]
FROM [Transaction details]
GROUP BY Month([Transaction details].[YYYY-MM-DD]);
I get the error message: "Your query does not include the specified expression 'Sales volume LOC' as part of an aggregate function."
The error is because you do not have the column [Sales volume LOC] in an aggregate function or in the GROUP BY clause. If you want the totals for each month then you should add the [Sales volume LOC] to the sum() aggregate function:
SELECT year([Transaction details].[YYYY-MM-DD]) as [Year],
Month([Transaction details].[YYYY-MM-DD]) as [Month],
sum([Transaction details].[Sales volume LOC]) as Total
FROM [Transaction details]
GROUP BY year([Transaction details].[YYYY-MM-DD]), Month([Transaction details].[YYYY-MM-DD])
ORDER BY year([Transaction details].[YYYY-MM-DD]), Month([Transaction details].[YYYY-MM-DD]);

Displaying different date periods on income data

I have the below query which displays data like so:
Income Type This Month Last Month This Year Last Year
1 179640.00 179640.00 179640.00 179640.00
2 12424440.00 12424440.00 12424440.00 12424440.00
Select
Income_Type As [Income Type],
Sum(Income_Amount) As [This Month],
Sum(Income_Amount) As [Last Month],
Sum(Income_Amount) As [This Year],
Sum(Income_Amount) As [Last Year]
From Income I
Left Join Finance_Types FT On I.Income_Type = FT.Type_ID
Group By
Income_Type
The Income table has a Income_Date which is a datetime column.
I'm struggling to get my head around how I would pull out the data for 'This Month', 'Last Month', 'This Year', 'Last Year' with the correct Sums in one query if possible?
Use date functions:
SUM(CASE WHEN YEAR(yourdatefield) = YEAR(GetDate()) - 1 THEN Income_Amount ELSE 0 END) AS 'Last Year'
That case statement only returns the Income_Amount if it was the last year, so you would be summing up only those amounts.
If you're not using SQL Server, the syntax might be a bit different.