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]);
Related
I have been getting this error:
Column 'dbo.MainDB.Packaging Quantity' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
This problem came up after I added the equation for Total and I have attempted to add aggregate functions to the equation, but it just gives me the wrong output if I add any. Also, if I add it to the group by clause then it adds unnecessary groups, does anyone know a solution?
Code:
SELECT
CONVERT(varchar,shipdate,101) AS Shipdate,
ID,
[Last Name],
Address,
SUM([Packaging Quantity]) AS Quantity,
Size,
SUM(Cost) AS Price,
ROUND(Size/[Packaging Quantity], 0) AS Total
FROM dbo.MainDB
WHERE
Shipdate BETWEEN '09/01/2019' AND '09/30/2019 11:59:59PM'
AND Cost <> '0'
GROUP BY
CONVERT(VARCHAR, shipdate, 101),
ID,
[First Name],
Address,
Size
The error message is clear enough.
SELECT
CONVERT(varchar,shipdate,101) AS Shipdate,
ID,
[Last Name],
Address,
SUM([Packaging Quantity]) AS Quantity, -- aggregation on [Packaging Quantity]
Size,
SUM(Cost) AS Price,
ROUND(Size/[Packaging Quantity], 0) AS Total -- no aggregation on [Packaging Quantity]
FROM ...
You need an aggregate function in the second hilighted occurence of [Packaging Quantity].Given the fact that you are SUMing the first time, it would assume that you want to aggregate the same way in the other column, so:
SELECT
CONVERT(varchar,shipdate,101) AS Shipdate,
ID,
[Last Name],
Address,
SUM([Packaging Quantity]) AS Quantity,
Size,
SUM(Cost) AS Price,
ROUND(Size/SUM([Packaging Quantity]), 0) AS Total
FROM ...
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 ()
OBJECTIVE
Develop a sales catalog for a COMPANY ID based on ITEM ID and latest PRICE paid (based on LAST SHIP DATE).
APPROACH
Pull in CUSTOMER, SALES, ITEM Tables
Run Query Link tables based off of CUSTOMER ID and ITEM to understand purchase history
Export a table showing a COMPANY ID, ITEM ID, LAST SALES PRICE, LAST SHIP DATE
CODE
SELECT
[Sales Order Details (F42119)].SDAN8 AS [COMPANY ID],
[Sales Order Details (F42119)].SDITM AS [ITEM ID],
[Sales Order Details (F42119)].SDAITM AS STYLE,
(CCur([SDLPRC])/10000) AS PRICE,
Max([Sales Order Details (F42119)].SDDRQJ) AS [LAST SHIP DATE]
INTO [Table - Sales Details]
FROM [Sales Order Details (F42119)]
GROUP BY
[Sales Order Details (F42119)].SDAN8,
[Sales Order Details (F42119)].SDITM,
[Sales Order Details (F42119)].SDAITM,
(CCur([SDLPRC])/10000);
ISSUE/QUESTION
CUSTOMER A bought ITEM ABC # 3 different prices on 3 different dates. I've taken the Max of Ship Date in hope to show the LAST PRICE PAID (resulting in one single value for price). However, for some reason, I am still receiving the three different prices on three different dates. How can I have MS Access only display the latest price based off of the latest ship date?
NOTE: SDLPRC = "Sold Price". I have to convert SLDPRC into a Currency and then divide by 1000; this is due to our current database setup. Also, SDAITM is an "Abbreviated Item Number" that is more customer-friendly.
The problem is that you're grouping by your Price variable (CCur([SDLPRC])/10000). When you use GROUP BY, Access/SQL will split the rows by all the variables in the GROUP BY statement. So you need to not group by price.
Change your query to use a subquery that finds the last date of a sale grouped by [Company ID], [Item ID] and Style. The use an outer query to grab the price for that particular record. Something like:
SELECT b.[COMPANY ID], b.[ITEM ID], b.STYLE, b.[LAST SHIP DATE], CCur(a.[SDLPRC])/10000 as PRICE
INTO [Table - Sales Details]
FROM [Sales Order Details (F42119)] as a
INNER JOIN
(SELECT
[Sales Order Details (F42119)].SDAN8 AS [COMPANY ID],
[Sales Order Details (F42119)].SDITM AS [ITEM ID],
[Sales Order Details (F42119)].SDAITM AS STYLE,
Max([Sales Order Details (F42119)].SDDRQJ) AS [LAST SHIP DATE]
FROM [Sales Order Details (F42119)]
GROUP BY
[Sales Order Details (F42119)].SDAN8,
[Sales Order Details (F42119)].SDITM,
[Sales Order Details (F42119)].SDAITM
) as b
ON a.SDAN8 = b.[COMPANY ID]
and a.SDITM = b.[ITEM ID]
and a.SDAITM = b.STYLE
and a.SDDRQJ = b.[LAST SHIP DATE]
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.
I have this SQL line
SELECT No_, sum(Quantity) AS Sold, [Shipment Date] AS SoldDate, [Item Category Code],
Description, [Description 2] FROM dbo.[3S Company A_S$Sales Invoice Line]
WHERE [Item Category Code] = '5104' GROUP BY No_
But i got this error for my script.
Column 'dbo.3S Company A_S$Sales Invoice Line.Shipment Date' is invalid in the select
list because it is not contained in either an aggregate function or the GROUP BY clause.
Can anyone help me with why?
If you use GROUP BY in your query, only the columns used in your grouping clause and any aggregate functions like SUM are allowed in the select list. In your case, you specify GROUP BY No_, so that is the only column you can select without using an aggregate function.
If you want to get the remaining columns, you could select No_ and the other aggregate columns in a subquery and then select other columns by matching the No_ column with the corresponding column in subquery.
The error means you have a column which may have multiple values when grouped and SQL doesn't know which value to select in the column
You cn use e.g. min() to select min value. Like this
SELECT No_,
sum(Quantity) AS Sold,
min([Shipment Date]) AS SoldDate,
min([Item Category Code]),
min(Description),
min([Description 2])
FROM dbo.[3S Company A_S$Sales Invoice Line]
WHERE [Item Category Code] = '5104'
GROUP BY No_
Or read about aggregate functions to choose proper one
BWT it's not MySQL but rather MS SQL (MySQL does not complain the column usages)
TRY THIS
SELECT No_, sum(Quantity) AS Sold, [Shipment Date] AS SoldDate, [Item Category Code],
Description, [Description 2] FROM dbo.[3S Company A_S$Sales Invoice Line]
WHERE [Item Category Code] = '5104' GROUP BY No_,[Shipment Date],
[ItemCategoryCode], Description,[Description 2]
IN SQL IF YOU USE ANY COLUMN NAME IN SELECT CLAUSE EXCEPT AGGREGRATE FUNCTION THEN YOU NEED TO ADD ALL THE COLUMNS IN GROUP BY ALSO OTHER WISE IT WILL SHOW EXCEPTION
IF YOU WANT SUM ONLY BY NO_ Column then you have to write a subquery with the aggregarte function and join it to you other columns as folows
SELECT No_ ,quant.sold, [Shipment Date] AS SoldDate, [Item Category Code],
Description, [Description 2] FROM dbo.[3S Company A_S$Sales Invoice Line] INV,
(SELECT No_, sum(Quantity) AS Sold from dbo.[3S Company A_S$Sales Invoice Line] where
WHERE [Item Category Code] = '5104' group by No_) quant
WHERE [Item Category Code] = '5104' and
inv.no_=quant.no_
The columns (Other than aggregate functions) that exists in select clause should also present in the group by clause. This is what the error message states.
Select Productid, Sum(Saled) from product
Group by ProductId
In the above example, ProductId is in the group by clause. So that query is valid. If you introduce one more column that should also be in the group by clause to avoid error.
You have to put columns not using the aggregate functions in your GROUP BY :
GROUP BY
No_
, [Shipment Date]
, [Item Category Code]
, Description
, [Description 2]