Equation needing to be in a group by statement? - sql

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 ...

Related

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 ()

Error in SQL sum()

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]

how to select columns with aggregate function sql server

i need to select two columns.1. calculate sum of one column and display it 2.display column as it is. so i tried below code
SELECT Sum(CONVERT(FLOAT, Replace(total, Char(0), ''))) AS Total,
[product name]
FROM tb_sales_entry_each_product
GROUP BY [sales date]
error message
Column 'tb_sales_entry_each_product.Product Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
where i made error.thanks
just need to group
select SUM(CONVERT(float, REPLACE(Total, CHAR(0), ''))) as Total, [Product Name]
from tb_sales_entry_each_product group by [Sales Date], [product name]
When ever you do a numercial count sum etc, any other columns need to be grouped.
thats all your missing
Try this:
select SUM(CONVERT(float, REPLACE(Total, CHAR(0), ''))) as Total,
[Product Name] ,[Sales Date]
from tb_sales_entry_each_product
group by [Sales Date],[Product Name]

SQL aggregate and other fields showing in query

I have a query, where I need the MIN of a DateTime field and then I need the value of a corresponding field in the same row.
Now, I have something like this, however I cannot get Price field without putting it also in an aggregate clause, which is not what I want.
SELECT MIN([Registration Time]), Price FROM MyData WHERE [Product Series] = 'XXXXX'
I need the MIN of the Registration Time field and then I just want the corresponding Price field for that row, however how do I show that?
I do also need my WHERE clause as shown.
I'm sure I've overlooked something really obvious. Using SQL Server 2008
If you want just one record with [Registration Time], Price, it'd be as simple as this:
select top 1 [Registration Time], Price
from MyData
where [Product Series] = 'XXXXX'
order by [Registration Time]
If you want minimum [Registration Time] and corresponding Price for all [Product Series], then there's a few approaches, for example, using row_number() function:
with cte as (
select
[Registration Time], Price,
row_number() over(partition by [Product Series] order by [Registration Time]) as rn
from MyData
)
select
[Registration Time], Price, [Product Series]
where rn = 1

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]);