Group by query error in multiple joins - sql

I need to fetch each select item and its count in single query.Here is my query and expecting the output as single array.
select ordetable.order_id, count(ordetable.order_id), order_table.name,
count(order_table.na me), orderitem_table.itemname,
count(orderitem_table.itemname)
from order_table
left join orderitem_table
on order_table.order_id = orderitem_table.order_id
group by ordetable.order_id
Am getting this error:
[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Column
'orderitem_table.itemname' is invalid in the select list because it is
not contained in either an aggregate function or the GROUP BY clause.

The issue is evident from the error message, isn't it?
Modify your query like below.
select ordetable.order_id, count(ordetable.order_id), order_table.name,
count(order_table.name), orderitem_table.itemname,
count(orderitem_table.itemname)
from order_table
left join orderitem_table
on order_table.order_id = orderitem_table.order_id
group by ordetable.order_id, order_table.name, orderitem_table.itemname

If you not like to group order_table.name and orderitem_table.itemname, then you can use 'min' condition.
select ordetable.order_id, count(ordetable.order_id), min(order_table.name),
count(order_table.name), min(orderitem_table.itemname),
count(orderitem_table.itemname)
from order_table
left join orderitem_table
on order_table.order_id = orderitem_table.order_id
group by ordetable.order_id

Your wording and your query are not in sync. Each item and its count (over all the orders) will be
select orderitem_table.itemname, orderitem_table.itemnumber, count(*)
from order_table
join orderitem_table
on order_table.order_id = orderitem_table.order_id
group by orderitem_table.itemname, orderitem_table.itemnumber

Related

SUM value wrong show from two tables

SELECT
SUM(fabricinventory.sqty) AS shipped,
SUM(fabricinventory.rcvd) AS received
FROM
(SELECT
SUM(requisition.issue) AS issue
FROM
requisition
INNER JOIN
fabricinventory ON fabricinventory.id = requisition.fab_id
GROUP BY
fabricinventory.buyer_id,
fabricinventory.file_id);
When I input subquery, I get a syntax error.
Can you please help me with the correct syntax?
try my code :
SELECT
SUM(shipped) AS shipped,
SUM(received) AS received
FROM
(
SELECT
fabricinventory.sqty AS shipped,
fabricinventory.rcvd AS received,
SUM(requisition.issue) AS issue
FROM
requisition
INNER JOIN fabricinventory ON fabricinventory.id = requisition.fab_id
GROUP BY
fabricinventory.sqty,
fabricinventory.rcvd) sub_query
;
When you use subquery behind FROM you must add an alias to the query

SQL Count uses info from join

I need to count the amount of times InternalMenuLinkItemNumber appears per sitenumber and per order mode. Then i need to show MenuItemID and i do that with a inner join using item numbers, but when i add this join it skews the QTY result. I've tried using distinct in the COUNT but then all the QTY is 1. Please assist.
Query and result where QTY result is 100% correct but no MenuItemID.
SELECT ST_Sites.BusinessUnit,[ST_SalesMixTransactions_RealTimeFeed].SiteNumber,InternalMenuLinkItemNumber,[ST_SalesMix].MenuItemID,OrderMode,SellingPrice,COUNT(ST_SalesMixTransactions_RealTimeFeed.InternalMenuLinkItemNumber) as QTY
FROM ST_AlohaSalesMixTransactions_RealTimeFeed
inner join ST_Sites on ST_Sites.SiteNumber= [ST_SalesMixTransactions_RealTimeFeed].SiteNumber
where [ST_SalesMixTransactions_RealTimeFeed].BusinessDate between'2017-06-27'and'2017-07-03' and [ST_SalesMixTransactions_RealTimeFeed].SiteNumber = '1001006'
group by InternalMenuLinkItemNumber,[ST_SalesMixTransactions_RealTimeFeed].SiteNumber,OrderMode,SellingPrice,ST_Sites.BusinessUnit,[ST_SalesMix].MenuItemID
order by InternalMenuLinkItemNumber
Result where QTY comes out as expected:
If I add the inner join to get MenuItemID:
Query:
SELECT ST_Sites.BusinessUnit,[ST_SalesMixTransactions_RealTimeFeed].SiteNumber,InternalMenuLinkItemNumber,[ST_SalesMix].MenuItemID,OrderMode,SellingPrice,COUNT(ST_SalesMixTransactions_RealTimeFeed.InternalMenuLinkItemNumber) as QTY
FROM ST_AlohaSalesMixTransactions_RealTimeFeed
inner join ST_SalesMix on [ST_AlohaSalesMixTransactions_RealTimeFeed].InternalMenuLinkItemNumber= ST_SalesMix.ItemNumber
inner join ST_Sites on ST_Sites.SiteNumber= [ST_SalesMixTransactions_RealTimeFeed].SiteNumber
where [ST_SalesMixTransactions_RealTimeFeed].BusinessDate between'2017-06-27'and'2017-07-03' and [ST_SalesMixTransactions_RealTimeFeed].SiteNumber = '1001006'
group by InternalMenuLinkItemNumber,[ST_SalesMixTransactions_RealTimeFeed].SiteNumber,OrderMode,SellingPrice,ST_Sites.BusinessUnit,[ST_SalesMix].MenuItemID
order by InternalMenuLinkItemNumber
Result where QTY is now way off:
If I use distinct:
Query:
SELECT ST_Sites.BusinessUnit,[ST_SalesMixTransactions_RealTimeFeed].SiteNumber,InternalMenuLinkItemNumber,[ST_SalesMix].MenuItemID,OrderMode,SellingPrice,COUNT(distinct ST_SalesMixTransactions_RealTimeFeed.InternalMenuLinkItemNumber) as QTY
FROM ST_AlohaSalesMixTransactions_RealTimeFeed
inner join ST_SalesMix on [ST_AlohaSalesMixTransactions_RealTimeFeed].InternalMenuLinkItemNumber= ST_SalesMix.ItemNumber
inner join ST_Sites on ST_Sites.SiteNumber= [ST_SalesMixTransactions_RealTimeFeed].SiteNumber
where [ST_SalesMixTransactions_RealTimeFeed].BusinessDate between'2017-06-27'and'2017-07-03' and [ST_SalesMixTransactions_RealTimeFeed].SiteNumber = '1001006'
group by InternalMenuLinkItemNumber,[ST_SalesMixTransactions_RealTimeFeed].SiteNumber,OrderMode,SellingPrice,ST_Sites.BusinessUnit,[ST_SalesMix].MenuItemID
order by InternalMenuLinkItemNumber
Result for QTY is now all 1:
If I understand correctly, you want something like
SELECT SiteNumber, OrderMode, count([DISTINCT?] InternalMenuLinkItemNumber)
...
GROUP BY SiteNumber, OrderMode
You want to count the InternalMenuLinkItemNumber, so InternalMenuLinkItemNumber must not occur in the GROUP BY clause.
EDIT:
When using GROUP BY, the SELECT list may only contain columns also mentioned in the GROUP BY clause, or aggregate functions (on arbitrary columns).
Try this:
SELECT a.InternalMenuLinkItemNumber, a.SiteNumber, a.OrderMode, a.SellingPrice, a.BusinessUnit, a.MenuItemID, a.QTY, CASE WHEN MAX(b.MenuItemID) = MIN(b.MenuItemID) THEN MAX(b.MenuItemID) ELSE -1 END AS MenuItemID
FROM
(SELECT ST_Sites.BusinessUnit, [ST_SalesMixTransactions_RealTimeFeed].SiteNumber, InternalMenuLinkItemNumber, [ST_SalesMix].MenuItemID, OrderMode, SellingPrice, COUNT(ST_SalesMixTransactions_RealTimeFeed.InternalMenuLinkItemNumber) as QTY
FROM ST_AlohaSalesMixTransactions_RealTimeFeed
INNER JOIN ST_Sites on ST_Sites.SiteNumber = [ST_SalesMixTransactions_RealTimeFeed].SiteNumber
WHERE [ST_SalesMixTransactions_RealTimeFeed].BusinessDate between'2017-06-27'and'2017-07-03' and [ST_SalesMixTransactions_RealTimeFeed].SiteNumber = '1001006'
GROUP BY InternalMenuLinkItemNumber, [ST_SalesMixTransactions_RealTimeFeed].SiteNumber, OrderMode, SellingPrice, ST_Sites.BusinessUnit, [ST_SalesMix].MenuItemID
) a
INNER JOIN ST_SalesMix b ON a.InternalMenuLinkItemNumber = b.ItemNumber
GROUP BY a.InternalMenuLinkItemNumber, a.SiteNumber, a.OrderMode, a.SellingPrice, a.BusinessUnit, a.MenuItemID, a.QTY
ORDER BY a.InternalMenuLinkItemNumber
It works on the theory that your first query gives good counts, so keep that as it is (it's now the inner query) and then do the problematic join outside of it. Obviously there are many rows from ST_SalesMix for each properly counted row in the first query, so I'm grouping on the original group list but that means that you might get multiple MenuItemIDs. I'm checking for that in the CASE statement by testing the MAX and MIN MenuItemIDs - if they are the same return MAX(MenuItemID) otherwise I'm returning -1 as an error flag to indicate that there were multiple MenuItemIDs associated with this group. It might not be the most efficient method but I didn't have much to go on.
I hope this helps.
all is sorted now. Thanks to everyone.
#jwolf your suggested query was the answer.

Calculation from two queries in SQL

I have 2 queries that give me counts. I want to divide one count by the other - I currently have the below SQL but throws out errors immediately:
SELECT tbl_CEAR_Name.CEAR_Team,
Q_Manager_Backlog_Total.Backlogs,
Q_Manager_CHS_Total.CHS_Total,
[Q_manager_CHS_Total].[CHS_Total]/[q_managers_backlog_total].[backlogs] AS Expr1
FROM Q_Manager_CHS_Total
INNER JOIN
(
Q_Manager_Backlog_Total
INNER JOIN tbl_CEAR_Name
ON Q_Manager_Backlog_Total.CEAR_Team = tbl_CEAR_Name.CEAR_Team
)
ON Q_Manager_CHS_Total.CEAR_Team = tbl_CEAR_Name.CEAR_Team
ORDER BY Q_Manager_Backlog_Total.Backlogs DESC;
You totally banjaxed your joins... Try this
SELECT tbl_CEAR_Name.CEAR_Team,
Q_Manager_Backlog_Total.Backlogs,
Q_Manager_CHS_Total.CHS_Total,
[Q_manager_CHS_Total].[CHS_Total]/[q_managers_backlog_total].[backlogs] AS Expr1
FROM Q_Manager_CHS_Total
INNER JOIN
(
select Q_Manager_Backlog_Total.Backlogs, tbl_CEAR_Name.CEAR_Team
FROM Q_Manager_Backlog_Total
INNER JOIN tbl_CEAR_Name
ON Q_Manager_Backlog_Total.CEAR_Team = tbl_CEAR_Name.CEAR_Team
) T1
ON Q_Manager_CHS_Total.CEAR_Team = T1.CEAR_Team
ORDER BY Q_Manager_Backlog_Total.Backlogs DESC;
I suspect the count field [backlogs] in query [q_managers_backlog_total] has 0 as a result somewhere. If you then try to divide by 0 you will evidently get an error. Catch a 0 value in the underlying query and you're good.

Sub query brain freeze

Its been a while since I've done sub queries and for the life of me I cant see whats wrong with my query.
The error message I get when executing is:
ORA-00904: "SUB"."PRO_REFNO": invalid identifier
This is my query. I'm obviously doing something wrong but I just cant see it.
SELECT
prop.PRO_ADR_1_LINE,
ele.POE_START_DATE,
ele.POE_ELEMENT_DESCR,
ele.POE_VALUE,
ele.POE_ATTRIBUTE,
ele.POE_FURTHER_ATTRIBUTE,
ele.POE_FURTHER_ATTRIBUTE_DESCR,
prop.PRO_SCHEME,
prop.PRO_SCHEME_DESCR,
GEO.GEO_BUS_UNIT,
GEO.GEO_REGION,
GEO.GEO_REGION_DESCR,
prop.PRO_NEIGHBOURHOOD_DESCR
--sub.pro_refno
FROM property prop
--inner join
left join GEO on prop.PRO_GEO_PATCH=GEO.GEO_PATCH
left join PROPERTY_OTHER_ELEMENT ele on ele.POE_PRO_REFNO =prop.PRO_REFNO
inner join(
SELECT
property.PRO_SCHEME,
count(distinct property.PRO_REFNO)
FROM
PROPERTY
WHERE
property.pro_type = 'P'
GROUP BY
property.PRO_SCHEME
)sub
on sub.pro_refno = prop.PRO_REFNO
where
ele.POE_START_DATE BETWEEN '01-APR-2016' AND sysdate
AND
ele.POE_ELEMENT LIKE 'EST%'
AND
ele.POE_ELEMENT_DESCR <> 'Estate Walkabout - Would you live in this neighbourhood ?'
AND
ele.POE_VALUE IN ( '1','2','3','4','5','6','7','8','9','10' )
Both the outer query and sub query run fine separately. Like I said its been a while so I'm guessing its something stupid I've done/not done.
Thanks
Adam
You didn't give a name to the aggregate column:
inner join(
SELECT
property.PRO_SCHEME,
count(distinct property.PRO_REFNO) -- No name!!!
FROM
PROPERTY
WHERE
property.pro_type = 'P'
GROUP BY
property.PRO_SCHEME
)sub
on sub.pro_refno = prop.PRO_REFNO
Change that to:
inner join(
SELECT
property.PRO_SCHEME,
count(distinct property.PRO_REFNO) As PRO_REFNO
FROM
PROPERTY
WHERE
property.pro_type = 'P'
GROUP BY
property.PRO_SCHEME
)sub
on sub.pro_refno = prop.PRO_REFNO
Your subquery doesn't select PRO_REFNO, so the outer query cannot match it in the JOIN predicate. Try this for the subquery:
SELECT
property.PRO_SCHEME,
property.PRO_REFNO,
count(distinct property.PRO_REFNO)
FROM PROPERTY
WHERE property.pro_type = 'P'
GROUP BY
property.PRO_SCHEME,
property.PRO_REFNO
Also, your COUNT(DISTINCT ...) isn't given an alias, which you'll need if it's ever going to be used in the outer select.

How to use DISTINCT in ms access?

I have the following query:
SELECT DISTINCT AwardDescriptions.aID, CostCentres.cCC
FROM AwardDescriptions
INNER JOIN CostCentres ON AwardDescriptions.aID = CostCentres.cNumber
ORDER BY CostCentres.cCC;
For some reason, when I run my query, it still shows all duplicate values.
Any suggestions?
You can use group by and get either the minimum or maximum value:
SELECT MAX(AwardDescriptions.aID) as aID, CostCentres.cCC
FROM AwardDescriptions INNER JOIN
CostCentres
ON AwardDescriptions.aID = CostCentres.cNumber
GROUP BY CostCentres.cCC
ORDER BY CostCentres.cCC;