Unable to select column from other table while with COUNT and GROUP BY - sql

I have two tables in a SQL Server database - IT_Order and Product. I want to group by the product_id from IT_Order but also select the product name from Product. But I get an error for this query. Please help...
The tables:
The query:
SELECT
Product.product_name, IT_Order.product_id,
COUNT(IT_Order.it_order_id) AS Product_volume
FROM
IT_Order, Product
WHERE
IT_Order.product_id = Product.product_id
GROUP BY
IT_Order.product_id;
I get this error:
Column 'Product.product_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

The error message is clear, you can use column without aggregation, in a GROUP BY, but as every product has only one name(i guess) you can make
SELECT Product.product_name, IT_Order.product_id, COUNT(IT_Order.it_order_id) as
Product_volume
FROM IT_Order JOIN Product
ON IT_Order.product_id = Product.product_id
GROUP BY IT_Order.product_id,Product.product_name;
also please use in future JOIN for your table, as they are around for 30 years now.

Related

Left Join on three tables in access with Group by

I have broken my head with syntax error response from Access Jet engine.
I have three tables.
First one "tblMstItem" is the master table for Item details contains two columns "colITemID" PK and "colItemName"
Second one "tblStocks" is the table where the purchases are maintained. This table has a column "colRQty" which keeps the quantity of the particular item purchased. "colItemID" FK from "tblMstItem"
Third one "tblSales" is the table where the sales are maintained. This table has a column "colSoldQty" which keeps the quantity of the particular item sold. "colItemID" FK from "tblMstItem"
Therefore "colItemID" is common in all the three tables and has links created.
My requirement is I need all the Items listed in the "tblMstItem" table columns are "colItemID" "colItemName" and if there is any item purchased or any item sold should be shown as sum of that particular item.
I have used Left Join shown in the following select statement but it always giving me an error message.
Select statement as follows:
SELECT
i.colItemID,
i.colItemName,
s.rqty,
n.soldqty
from tblMstItem i
left join
( select sum( colRQty ) as rqty from tblStocks group by colItemID ) s
on i.colItemID = s.colItemID
left join
( select sum( colSoldQty ) as soldqty from tblSales group by colItemID ) n
on i.colItemID=n.colItemID``
I tried the above given code with many different syntax but every time I get syntax error. It is making me to doubt do MS Access support three table joins, I am sure I am wrong.
See the error Message below
Table columns and table link shown below
I would be very thankful to get any help on this. Access sql please because this I am able to get results in SQL Server.
Thanks in Advance
MS Access has a picky syntax. For instance, joins need extra parentheses. So, try this:
select i.colItemID, i.colItemName,
s.rqty, n.soldqty
from (tblMstItem as i left join
(select colItemID, sum(colRQty ) as rqty
from tblStocks
group by colItemID
) as s
on i.colItemID = s.colItemID
) left join
(select colItemID, sum( colSoldQty ) as soldqty
from tblSales
group by colItemID
) as n
on i.colItemID = n.colItemID;
You also need to select colItemID in the subqueries.

My question is about SQL, using a TOP function inside a sub-query in MS Access

Overall what I'm trying to achieve is a query that shows the most ordered item from a customer in a database. To achieve this I've tried making a query showing how many times a customer has ordered an item, and now I am trying to create a sub-query in it using TOP1 to discern the most bought items.
With the SQL from the first query (looking weird because I made it with the Access automatic creator):
SELECT
Customers.CustomerFirstName,
Customers.CustomerLastName,
Products.ProductName,
COUNT(SalesQuantity.ProductCode) AS CountOfProductCode
FROM (Employees
INNER JOIN (Customers
INNER JOIN Sales
ON Customers.CustomerCode = Sales.CustomerCode)
ON Employees.EmployeeCode = Sales.EmployeeCode)
INNER JOIN (Products
INNER JOIN SalesQuantity
ON Products.ProductCode = SalesQuantity.ProductCode)
ON Sales.SalesCode = SalesQuantity.SalesCode
GROUP BY
Customers.CustomerFirstName,
Customers.CustomerLastName,
Products.ProductName
ORDER BY
COUNT(SalesQuantity.ProductCode) DESC;
I have tried putting in a subquery after FROM line:
(SELECT TOP1 CountOfProduct(s)
FROM (.....)
ORDER by Count(SalesQuantity.ProductCode) DESC)
I'm just not sure what to put in for the "from"-every other tutorial has the data from an already created table, however this is from a query that is being made at the same time. Just messing around I've put "FROM" and then listed every table, as well as
FROM Count(SalesQuantity.ProductCode)
just because I've seen that in the order by from the other code, and assume that the query is discerning from this count. Both tries have ended with an error in the syntax of the "FROM" line.
I'm new to SQL so sorry if it's blatantly obvious, but any help would be greatly appreciated.
Thanks
As I understand, you want the most purchased product for each customer.
So, begin by building aggregate query that counts product purchases by customer (appears to be done in the posted image). Including customer ID in the query would simplify the next step which is to build another query with TOP N nested query.
Part of what complicates this is unique record identifier is lost because of aggregation. Have to use other fields from the aggregate query to provide unique identifier. Consider:
SELECT * FROM Query1 WHERE CustomerID & ProductName IN
(SELECT TOP 1 CustomerID & ProductName FROM Query1 AS Dupe
WHERE Dupe.CustomerID = Query1.CustomerID
ORDER BY Dupe.CustomerID, Dupe.CountOfProductCode DESC);
Overall what I'm trying to achieve is a query that shows the most ordered item from a customer in a database.
This answers your question. It does not modify your query which is only tangentially related.
SELECT s.CustomerCode, sq.ProductCode, SUM(sq.quantity) as qty
FROM Sales as s INNER JOIN
SalesQuantity as sq
ON s.SalesCode = sq.SalesCode
GROUP BY s.CustomerCode, sq.ProductCode;
To get the most ordered items, you can use this twice:
SELECT s.CustomerCode, sq.ProductCode, SUM(sq.quantity) as qty
FROM Sales as s INNER JOIN
SalesQuantity as sq
ON s.SalesCode = sq.SalesCode
GROUP BY s.CustomerCode, sq.ProductCode
HAVING sq.ProductCode IN (SELECT TOP 1 sq2.ProductCode
FROM Sales as s2 INNER JOIN
SalesQuantity as sq2
ON s2.SalesCode = sq2.SalesCode
WHERE s2.CustomerCode = s.CustomerCode
GROUP BY sq2.ProductCode
);
In almost any other database, this would be simpler, because you would be able to use window functions.

Using two tables to get one report in SQL Server

I have two tables, product and download as follows.
product (product_id (pk), product_name)
download (download_date(pk), download_version(pk), product_id(pk,fk))
I need a report to show how many downloaded, form which version of what product took place in each month.
SELECT
[product_name],
[version],
MONTH(download_date) AS [Month],
COUNT(MONTH(download_date)) AS [Count]
FROM
product
INNER JOIN
download ON product.product_id = download.product_id
GROUP BY
MONTH(download_date)
and I get this error
Column 'product.product_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Use alias names for the tables for better readability.
Mention the aliasname.columnname in the SELECT to avoid fetching the wrong values.
You missed the other columns except the aggregate values in the GROUP BY.
So the query below will return the result.
SELECT P.[product_name],
P.[version],
MONTH(D.download_date) AS [Month],
COUNT(MONTH(D.download_date)) AS [Count]
FROM product P
INNER JOIN download D ON D.product_id = P.product_id
GROUP BY P.[product_name], P.[version], MONTH(D.download_date)
You have some issue with tables and primary key.
create the table like this.
product(product_id(PK),name,verion)
download(date,product_id)
and run this query
SELECT product.name,product.version,COUNT(download.product_id)
FROM product INNER JOIN download ON product.product_id=download.download_id
Group BY(download._productid);
i think this is what you want if not post replay i will answer when i get back to stack.

SQL Inventory Count wont return item name

I am working with our inventory database and I created a query (access 2002 format citrix run server) that counts all items of a type meeting a serviceability status and it runs as I had expected but when I try to display the item name Equipment.Model in the other table which is already left joined via the ID I get the error Tried to execute a query that does not include the specified expression 'model' as part of an aggregate function. I think I would expect to get this if I tried to list something like an asset ID but this is related to the items I am counting. The trouble piece is quoted. The tables are quite large but they are basically the below
Equipment
|EquipmentID|CompanyID|Model|Description|TypeID|...
Inventory
|InventoryID|EquipmentID|Serial|Status|...
SELECT Inventory.EquipmentID, "Equipment.Model", COUNT(*) AS Count
FROM (Inventory LEFT JOIN Equipment ON Inventory.EquipmentID=Equipment.EquipmentID)
WHERE Equipment.TypeID = 14
AND Inventory.Status NOT IN (4,5,6,8)
GROUP BY Inventory.EquipmentID;
SELECT Inventory.EquipmentID, Equipment.Model, COUNT(*) AS Count
FROM Inventory
LEFT JOIN Equipment ON Inventory.EquipmentID=Equipment.EquipmentID
WHERE Equipment.TypeID = 14
AND Inventory.Status NOT IN (4,5,6,8)
GROUP BY Inventory.EquipmentID, Equipment.Model;
You have to use all non aggregate columns in the GROUP BY clause.
You need to include equipment.Model in your group by clause

Column is not contained in either an aggregate function or the GROUP BY clause

I have to tables:
DeviceInstace and Device
They are connected with Foreign Key on DeviceInstance.DeviceId=Device.Id
I have SQL query:
select d.CatalogNo,d.Manufacturer,d.Name, sum(quantity)
from DeviceInstance di
full join Device d
on d.Id=di.DeviceId
group by di.DeviceId
with which I need to make little summary that consist of:
catalog number
manufacturer
device name
number of devices (sum of values in quntity columns for these deviceIds)
But I'm facing some Grouping by issuses.
All I try refuse me error like this one:
Column 'Device.CatalogNo' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I know I can group by CatalogNo but then ten sum of quantity will return wrong numbers.
Can anyone suggest me how to repair it?
Just modify GROUP BY clause:
group by d.DeviceId, d.CatalogNo,d.Manufacturer,d.Name
You need to Add d.CatalogNo,d.Manufacturer in Group By Clause
Try this
select d.CatalogNo,d.Manufacturer,d.Name, sum(quantity)
from DeviceInstance di
full join Device d
on d.Id=di.DeviceId
group by d.Name, d.CatalogNo,d.Manufacturer