SUM value wrong show from two tables - sql

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

Related

Group BY clause to temporary table

I am beginner in SQL trying to group by a result
SELECT
API.PROJECTNUMBER, Project.ProjectName, Project.PROJMGRID, Staff.Email
INTO
#TEMPUnmatchedProjectWithSMoD
FROM
{ProjectsIncidentsIntegration} API WITH(NOLOCK)
JOIN
{Project} project WITH(NOLOCK) ON Project.ProjectNumber = API.PROJECTNUMBER
JOIN
{COMMON_STAFF} Staff WITH(NOLOCK) ON Staff.EmplId = Project.PROJMGRID
WHERE
NOT EXISTS (SELECT 1
FROM #DBName.DBO.NCompasS_TBL_SMoDIncident SMoD WITH(NOLOCK)
WHERE SMoD.ProjectCode = API.PROJECTNUMBER)
SELECT *
FROM #TEMPUnmatchedProjectWithSMoD
GROUP BY Project.PROJMGRID
I get an error:
Database returned the following error:
Error in advanced query UnmatchedProjectWithSMoD2: The multi-part identifier "Project.PROJMGRID" could not be bound.
The problem appears to be in this query:
SELECT *
FROM #TEMPUnmatchedProjectWithSMoD
GROUP BY Project.PROJMGRID ;
There are two major issues:
SELECT * with GROUP BY is not correct. What should be done with the columns that are not aggregation keys? You might have some idea. SQL generates an error.
Project is not defined.
It is quite unclear what this code is supposed to be doing, so I cannot suggest anything useful. You can just select all rows from the temporary table using:
SELECT p.*
FROM #TEMPUnmatchedProjectWithSMoD p;

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.

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.

Group by query error in multiple joins

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

SQL aggregate of calculated field

Using SQL Server 2005 I have a query that gets child records from bundles that has a calculated field for quantity. The query looks something like this:
SELECT TblB_1.fooID,
TblC.quantity * (TblA.quantity) AS Quantity,
TblB_1.name AS Name
FROM TblB AS TblB_1 INNER JOIN
TblC ON TblB_1.fooID = TblC.fooID RIGHT OUTER JOIN
TblB INNER JOIN
TblA ON TblB.fooID = TblA.fooID ON TblC.parentfooID = TblB.fooID
WHERE (TblB.isBundle = 1) AND (TblA.isDeleted = 0)
I need to be able to group by id and get a SUM of the quantity field. I've tried wrapping the quantity line with sum like this:
SUM(TblC.quantity * (TblA.quantity)) AS Quantity,
Then adding
GROUP BY TblB_1.fooID
after the WHERE clause, but that results in a "TblB_1.name is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause" error.
I also tried to get my head around using a subquery for this task, but I was not able to make that work either, nor could I find an example here or on the Web that I have been able to adapt. Thanks - Dan
You were close, you need to group by all the non aggregate fields. Like so
SELECT TblB_1.fooID,
SUM(TblC.quantity * (TblA.quantity)) AS Quantity,
TblB_1.name AS Name
FROM TblB AS TblB_1 INNER JOIN
TblC ON TblB_1.fooID = TblC.fooID RIGHT OUTER JOIN
TblB INNER JOIN
TblA ON TblB.fooID = TblA.fooID ON TblC.parentfooID = TblB.fooID
WHERE (TblB.isBundle = 1) AND (TblA.isDeleted = 0)
GROUP BY TblB_1.fooID, TblB_1.name