How to multi JOIN in SQL Server - sql-server

I have a query in SQL Server as:
SELECT MD.Author_ID, MD.CoAuthor_ID, MD.Venue_ID, A2P.pid [Paper_ID],
P.abstract_research_area_category_id [Paper_Category_ID],
MD.Year
FROM Merged_Data MD
JOIN sub_aminer_author2paper A2P ON MD.Author_ID = A2P.aid AND
MD.Year = A2P.p_year AND
MD.Venue_ID = A2P.p_venue_vid
JOIN sub_aminer_paper P ON MD.Venue_ID = P.p_venue_vid AND
MD.Year = P.p_year
WHERE MD.Author_ID = 677
Whereas I'm unable to get desired results because unable to join A2P.pid with sub_aminer_paper for extracting [Paper_Category_ID].
How can I join A2P.pid with sub_aminer_paper to have a pid match and extract [Paper_Category_ID] whereas sub_aminer_paper has the field pid?

As per the comment it solved the issue. So the same, I provide the answer for the question:
SELECT MD.Author_ID, MD.CoAuthor_ID, MD.Venue_ID, A2P.pid [Paper_ID],
P.abstract_research_area_category_id [Paper_Category_ID],
MD.Year
FROM Merged_Data MD
JOIN sub_aminer_author2paper A2P ON MD.Author_ID = A2P.aid AND
MD.Year = A2P.p_year AND
MD.Venue_ID = A2P.p_venue_vid
JOIN sub_aminer_paper P ON MD.Venue_ID = P.p_venue_vid AND
MD.Year = P.p_year
WHERE MD.Author_ID = 677 AND A2P.pid = P.pid
Adding the A2P.pid = P.pid in the WHERE clause will solve the problem.

Try This,
SELECT MD.Author_ID, MD.CoAuthor_ID, MD.Venue_ID, A2P.pid [Paper_ID],
P.abstract_research_area_category_id [Paper_Category_ID],
MD.Year
FROM Merged_Data MD
INNER JOIN sub_aminer_author2paper A2P
ON (MD.Author_ID = A2P.aid AND
MD.Year = A2P.p_year AND
MD.Venue_ID = A2P.p_venue_vid)
INNER JOIN sub_aminer_paper P
ON (A2P.pid = P.pid
MD.Venue_ID = P.p_venue_vid AND
MD.Year = P.p_year)
WHERE MD.Author_ID = 677

Related

Records are fetched very slow when joining view inside view in SQL

I want to fetch Records from a View but there is a other view which is left joined in my query which is causing to fetch the records very slowly.
Its taking 5 minutes to fetch 13000 records.
My view is as below -
CREATE View [dbo].[VIEW_IQ_PURCHASE_ANALYSIS]
As
SELECT
ISNULL(MstOrganization.Name,'') as [Organization Name] ,
ItemLedger.DateDelivered AS [Date Delivered],
Characteristics.*
FROM VIEW_ITEM_STOCK_LEDGER as ItemLedger
LEFT JOIN **VIEW_PRODUCT_WITH_CHARACTERISTIC_COLUMN_DATA** as Characteristics on Characteristics.Code = ItemLedger.ItemCode
LEFT JOIN MstGroup ON MstGroup.Code = ItemLedger.GroupCode
LEFT JOIN MstOrganization ON MstOrganization.Code = ItemLedger.OrganizationCode
LEFT JOIN MstCurrency As BaseCurrency On BaseCurrency.Code = MstOrganization.CurrencyCode
LEFT JOIN stmVoucherType ON stmVoucherType.Code = ItemLedger.VoucherTypeCode
LEFT JOIN MstCurrency As DocumentCurrency ON DocumentCurrency.Code = ItemLedger.DocumentCurrencyCode
LEFT JOIN MstUser ON MstUser.Code = ItemLedger.UserCode
LEFT JOIN MstBusinessPartner ON MstBusinessPartner.Code = ItemLedger.PartyCode
LEFT JOIN MstWalkinCustomer ON MstWalkinCustomer.Code = ItemLedger.PartyCode
LEFT JOIN MstAddressDetail As BusinessPartnerAddressDetail ON BusinessPartnerAddressDetail.ObjectCode = MstBusinessPartner.Code
AND BusinessPartnerAddressDetail.IsDefault = 1
LEFT JOIN MstWalkInCustomerAddressDetail As WalkInCustomerAddressDetail ON WalkInCustomerAddressDetail.ObjectCode = ItemLedger.PartyCode
AND WalkInCustomerAddressDetail.IsDefault = 1
LEFT JOIN MstAddressType As BusinessPartnerAddressType ON BusinessPartnerAddressType.Code = BusinessPartnerAddressDetail.AddressTypeCode
LEFT JOIN MstCity As BusinessPartnerCity ON BusinessPartnerCity.Code = BusinessPartnerAddressDetail.CityCode
LEFT JOIN MstCountry As BusinessPartnerCountry ON BusinessPartnerCountry.Code = BusinessPartnerAddressDetail.CountryCode
LEFT JOIN MstState As BusinessPartnerState ON BusinessPartnerState.Code = BusinessPartnerAddressDetail.StateCode
LEFT JOIN MstCurrency As BusinessPartnerCurrency ON BusinessPartnerCurrency.Code = MstBusinessPartner.CurrencyCode
LEFT JOIN MstUOM As DetailUOM ON DetailUOM.Code = ItemLedger.DetailUOMCode
LEFT JOIN stmItem ON stmItem.Code = ItemLedger.ItemCode
LEFT JOIN MstUOM As ItemUOM ON ItemUOM.Code = stmItem.UOMCode
LEFT JOIN mstProductCategory ON mstProductCategory.Code = stmItem.ProductCategoryCode
LEFT JOIN mstTax ON mstTax.Code = ItemLedger.TaxCode
LEFT JOIN MstStockPoint ON MstStockPoint.Code = ItemLedger.StockPointCode
LEFT JOIN MstBusinessPartner As SalesmanBuyer ON SalesmanBuyer.Code = ItemLedger.SalesmanBuyerCode
LEFT JOIN MstShipper ON MstShipper.Code = ItemLedger.ShipperCode
WHERE (ItemLedger.vouchertypecode=204 OR (ItemLedger.VoucherTypeCode=402 and ItemLedger.DocumentTypeCode=2))
There is another View in my above view called - VIEW_PRODUCT_WITH_CHARACTERISTIC_COLUMN_DATA which i am joining with my view.
VIEW_PRODUCT_WITH_CHARACTERISTIC_COLUMN_DATA is as below -
CREATE VIEW VIEW_PRODUCT_WITH_CHARACTERISTIC_COLUMN_DATA
AS
SELECT mstProduct.Code,
(SELECT ISNULL((SELECT mstProductCharacteristicListDetail.ListValue
FROM mstProductCharacteristicListDetail WHERE
mstProductCharacteristicListDetail.HeaderCode = mstProductCharacteristicDetail.ProductCharacteristicCode
AND mstProductCharacteristicListDetail.SequenceNo = mstProductCharacteristicDetail.CharacteristicValue),'')
FROM mstProductCharacteristicDetail WHERE mstProductCharacteristicDetail.ItemCode = mstProduct.Code
AND mstProductCharacteristicDetail.ProductCharacteristicCode = 10011786 )
As [Sub Category],
(SELECT ISNULL((SELECT mstProductCharacteristicListDetail.ListValue
FROM mstProductCharacteristicListDetail
WHERE mstProductCharacteristicListDetail.HeaderCode = mstProductCharacteristicDetail.ProductCharacteristicCode
AND mstProductCharacteristicListDetail.SequenceNo = mstProductCharacteristicDetail.CharacteristicValue),'')
FROM mstProductCharacteristicDetail
WHERE mstProductCharacteristicDetail.ItemCode = mstProduct.Code
AND mstProductCharacteristicDetail.ProductCharacteristicCode = 10011787 ) As [Brand],
(SELECT ISNULL((SELECT mstProductCharacteristicListDetail.ListValue
FROM mstProductCharacteristicListDetail
WHERE mstProductCharacteristicListDetail.HeaderCode = mstProductCharacteristicDetail.ProductCharacteristicCode
AND mstProductCharacteristicListDetail.SequenceNo = mstProductCharacteristicDetail.CharacteristicValue),'')
FROM mstProductCharacteristicDetail
WHERE mstProductCharacteristicDetail.ItemCode = mstProduct.Code
AND mstProductCharacteristicDetail.ProductCharacteristicCode = 10011788 ) As [Color],
(SELECT ISNULL((SELECT mstProductCharacteristicListDetail.ListValue
FROM mstProductCharacteristicListDetail
WHERE mstProductCharacteristicListDetail.HeaderCode = mstProductCharacteristicDetail.ProductCharacteristicCode
AND mstProductCharacteristicListDetail.SequenceNo = mstProductCharacteristicDetail.CharacteristicValue),'')
FROM mstProductCharacteristicDetail
WHERE mstProductCharacteristicDetail.ItemCode = mstProduct.Code
AND mstProductCharacteristicDetail.ProductCharacteristicCode = 10011789 ) As [Size],
(SELECT ISNULL((SELECT mstProductCharacteristicListDetail.ListValue
FROM mstProductCharacteristicListDetail
WHERE mstProductCharacteristicListDetail.HeaderCode = mstProductCharacteristicDetail.ProductCharacteristicCode
AND mstProductCharacteristicListDetail.SequenceNo = mstProductCharacteristicDetail.CharacteristicValue),'')
FROM mstProductCharacteristicDetail
WHERE mstProductCharacteristicDetail.ItemCode = mstProduct.Code
AND mstProductCharacteristicDetail.ProductCharacteristicCode = 10011790 ) As [Style],
(SELECT mstProductCharacteristicDetail.CharacteristicValue
FROM mstProductCharacteristicDetail
WHERE mstProductCharacteristicDetail.ItemCode = mstProduct.Code
AND mstProductCharacteristicDetail.ProductCharacteristicCode = 10011791 ) As [Barcode],
(SELECT mstProductCharacteristicDetail.CharacteristicValue
FROM mstProductCharacteristicDetail
WHERE mstProductCharacteristicDetail.ItemCode = mstProduct.Code
AND mstProductCharacteristicDetail.ProductCharacteristicCode = 10011792 ) As [Item Name Arabic]
FROM mstProduct
When i execute VIEW_PRODUCT_WITH_CHARACTERISTIC_COLUMN_DATA - it fetches me record very fast (3sec) whats causing trouble is joining that view.
Any help would be appreciated.
I'm not doing the whole query here, it's messy and awful to read (sorry, but those object names are huge, and with no aliasing it's completely bloated out).
I suspect that the following logic is going to work for your first column [Sub Category], and so you'll be able to apply the same logic (which will just be more CASE expression, no more subqueries) to get the rest of the values:
SELECT P.Code,
CASE PCD.ProductCharacteristicCode WHEN 10011786 THEN ISNULL(PCLD.ListValue,'') END AS [Sub Category] --This should achieve the same thing as the subquery with a subquery
CASE PCD.ProductCharacteristicCode WHEN 10011787 THEN ISNULL(PCLD.ListValue,'') END AS [Brand]
FROM mstProduct P
LEFT JOIN mstProductCharacteristicDetail PCD ON P.Code = PCD.ItemCode
LEFT JOIN mstProductCharacteristicListDetail PCLD ON PCD.ProductCharacteristicCode = PCLD.HeaderCode
AND PCD.CharacteristicValue = SequenceNo;
Note: Without sample data this is completely untested.

alternative to sub query in join

I have a SQL query that I'm trying to optimize.
Is there a better way to avoid using subquery here?
Got a suggestion on using Row_number(),
posting this with some corrections
DECLARE #curdate DATETIME
SET #curdate = GETDATE()
SELECT DISTINCT
SIS.StudentID, StudentCoverage.StudentCoverageDataID,
Student.FirstName, Student.LastName,
Student.DateOfBirth, Student.Gender,
ASMT.AssessmentDate
FROM
SIS (NOLOCK)
INNER JOIN
SISMaster (NOLOCK) ON SISMaster.SISID = SIS.SISID
INNER JOIN
Assessment ASMT ON SIS.StudentID = ASMT.StudentId
INNER JOIN
StudentCoverage (NOLOCK) ON StudentCoverage.StudentID = SIS.StudentID
INNER JOIN
Organization (NOLOCK) ON StudentCoverage.OrgID = Organization.OrganizationID
INNER JOIN
Student (NOLOCK) ON Student.StudentID = SIS.StudentID
INNER JOIN
StudentCoverageData (NOLOCK) ON StudentCoverageData.StudentCoverageID = StudentCoverage.StudentCoverageID
AND StudentCoverageData.StudentCoverageDataID = (SELECT TOP 1 StudentCoverageData.StudentCoverageDataID
FROM StudentCoverage
INNER JOIN StudentCoverageData ON StudentCoverageData.StudentCoverageID = StudentCoverage.StudentCoverageID
WHERE StudentCoverage.StudentId = SIS.StudentID
AND StudentCoverageData.Active = 1
AND StudentCoverageData.EffectiveDate <= #curdate
AND (StudentCoverageData.ExitDate IS NULL OR StudentCoverageData.ExitDate > #curdate)
ORDER BY StudentCoverageData.AsOfDate DESC)
All Tables in your subquery is exists in inner join clause, so you could rewrite your query like this:
;WITH temps AS
(
DECLARE #curdate DATETIME = GETDATE()
SELECT
SIS.StudentID, StudentCoverage.StudentCoverageDataID,
Student.FirstName, Student.LastName,
Student.DateOfBirth, Student.Gender,
ASMT.AssessmentDate,
ROW_NUMBER() OVER (PARTITION BY StudentCoverageData.StudentCoverageDataID ORDER BY StudentCoverageData.AsOfDate) AS RowIndex
FROM
SIS (NOLOCK)
INNER JOIN
SISMaster (NOLOCK) ON SISMaster.SISID = SIS.SISID
INNER JOIN
StudentCoverage (NOLOCK) ON StudentCoverage.StudentID = SIS.StudentID
INNER JOIN
Organization (NOLOCK) ON StudentCoverage.OrgID = Organization.OrganizationID
INNER JOIN
Student (NOLOCK) ON Student.StudentID = SIS.StudentID
INNER JOIN
StudentCoverageData (NOLOCK) ON StudentCoverageData.StudentCoverageID = StudentCoverage.StudentCoverageID
WHERE StudentCoverageData.Active = 1
AND StudentCoverageData.EffectiveDate <= #curdate
AND (StudentCoverageData.ExitDate IS NULL OR StudentCoverageData.ExitDate > #curdate)
)
SELECT * FROM temps t
WHERE t.RowIndex = 1

How can I call teo columns from a tablar function in a SQL Server stored procedure at a same time

This is my stored procedure:
ALTER PROCEDURE [dbo].[PaymentStatusProviderDetailView]
(#POId INT ,
#ProviderId INT ,
#PrrId INT)
AS
BEGIN
SELECT
SDID,
dbo.GetServiceDetailString(SDID) AS ServiceName,
POID,
AmountPaid, AmountHeld,
ProviderName,
(SELECT ReimbursementAmount
FROM dbo.GetReimbursementBilledAmounts(#POId, SDID, #ProviderId, #PrrId)) AS ReimbursementAmount,
(SELECT ServiceMonth
FROM dbo.GetReimbursementBilledAmounts(#POId, SDID, #ProviderId, #PrrId)) AS ServiceMonth
FROM
(SELECT
SD.Id AS SDID, PO.Id AS POID,
SUM(PD.PaymentAmount) AS AmountPaid,
PH.HoldAmount AS AmountHeld,
PROV.ContractorName AS ProviderName
FROM
[dbo].[PaymentDetail] PD
JOIN
PurchaseOrder PO ON PO.Id = PD.PO_Id
JOIN
fBusinessUnit BU ON BU.id = PD.BU_Id
LEFT JOIN
Reimbursement_EBSUtilization REU ON REU.Id = PD.REU_Id
LEFT JOIN
Reimbursement_CDSUtilization RCU ON RCU.Id = PD.RCU_Id
LEFT JOIN
PaymentHold PH ON PH.PO_Id = PO.Id
AND (PH.RCU_Id = PD.RCU_Id OR PH.REU_Id = PD.REU_Id)
LEFT JOIN
ProviderReimbursementRequest PRR ON (PRR.Id = REU.PRR_Id OR PRR.Id = RCU.PRR_Id)
LEFT JOIN
fContractor PROV ON PROV.Id = PRR.Contractor_Id
LEFT JOIN
CDSUtilization CDS ON CDS.Id = RCU.CDSU_Id
LEFT JOIN
fServiceDetail SD ON SD.Id = REU.SD_Id OR SD.Id = CDS.ServiceDetail_Id
WHERE
Po.Id = #POId
AND PRR.Contractor_Id = #ProviderId
AND PRR.Id = #PrrId
GROUP BY
SD.Id, PO.Id, PH.HoldAmount, PROV.ContractorName
UNION
SELECT
SD.Id AS SDID, PO.Id AS POID,
NULL,
SUM(PH.HoldAmount) AS AmountHeld,
PROV.ContractorName AS ProviderName
FROM
PurchaseOrder PO
JOIN
PaymentHold PH ON PH.PO_Id = PO.Id
LEFT JOIN
Reimbursement_EBSUtilization REU ON REU.Id = PH.REU_Id
LEFT JOIN
Reimbursement_CDSUtilization RCU ON RCU.Id = PH.RCU_Id
LEFT JOIN
CDSUtilization CDS ON CDS.Id = RCU.CDSU_Id
JOIN
ProviderReimbursementRequest PRR ON PRR.Id = REU.PRR_Id OR RCU.PRR_Id = PRR.Id
JOIN
fContractor PROV ON PROV.Id = PRR.Contractor_Id
JOIN
fServiceDetail SD ON SD.Id = REU.SD_Id OR SD.Id = CDS.ServiceDetail_Id
WHERE
Po.Id = #POId
AND PRR.Contractor_Id = #ProviderId
AND PRR.Id = #PrrId
AND NOT EXISTS (SELECT 1
FROM PaymentDetail PD
WHERE PH.PO_Id = PD.PO_Id
AND (PH.RCU_Id = PD.RCU_Id OR PH.REU_Id = PD.REU_Id))
GROUP BY
SD.Id, PO.Id, PROV.ContractorName) DT
END
In the above stored procedure, in the particular code I am calling the same tablar function twice for two columns
(SELECT ReimbursementAmount
FROM dbo.GetReimbursementBilledAmounts(#POId, SDID, #ProviderId, #PrrId)) AS ReimbursementAmount,
(SELECT ServiceMonth
FROM dbo.GetReimbursementBilledAmounts(#POId, SDID, #ProviderId, #PrrId)) AS ServiceMonth
How can I call the above function in the same stored procedure only once and obtain two column names at the same time?
Say for example like below
(SELECT ReimbursementAmount, ServiceMonth
FROM dbo.GetReimbursementBilledAmounts(#POId, SDID, #ProviderId, #PrrId)) AS ReimbursementAmount and ServiceMonth
Can anyone please help me on this?
Does this work?
SELECT .......
........
RI.ReimbursementAmount,
RI.ServiceMonth,
...
FROM
.....
....
CROSS APPLY dbo.GetReimbursementBilledAmounts(#POId, SDID, #ProviderId, #PrrId) RI
WHERE
Po.Id = #POId
AND PRR.Contractor_Id = #ProviderId
AND PRR.Id = #PrrId
....
..
this looks like a limitation in sql server . we should only use a column function to retrieve or display a single column.

Sql recursive query error

WITH BomTree (
ITEMNO
,[DESC]
,BOMNO
,BUILDQTY
,UNIT
,COMPONENT
,[Comp Desc]
,[Comp-Qty]
,COMPBOMNO
,Depth
)
AS (
SELECT bomh.ITEMNO
,itm1.DESC]
,bomh.BOMNO
,bomh.BUILDQTY
,bomh.UNIT
,bomd.COMPONENT
,itm.[DESC] [Comp Desc]
,bomd.QTY [Comp-Qty]
,bomd.COMPBOMNO
,0 AS Depth
FROM ICBOMH bomh
INNER JOIN ICBOMD bomd ON bomh.BOMNO = bomd.BOMNO
AND bomh.ITEMNO = bomd.ITEMNO
INNER JOIN ICITEM itm1 ON bomd.ITEMNO = itm1.ITEMNO
INNER JOIN ICITEM itm ON bomd.COMPONENT = itm.ITEMNO
WHERE bomd.BOMNO = '01'
AND bomd.ITEMNO = '300060397'
UNION ALL
SELECT bomh.ITEMNO
,itm1.DESC]
,bomh.BOMNO
,bomh.BUILDQTY
,bomh.UNIT
,bomd.COMPONENT
,itm.[DESC] [Comp Desc]
,(t.[Comp-Qty] * bomd.QTY) AS [Comp-Qty]
,bomd.COMPBOMNO
,t.Depth + 1 AS Depth
FROM ICBOMH bomh
INNER JOIN ICBOMD bomd ON bomh.BOMNO = bomd.BOMNO
AND bomh.ITEMNO = bomd.ITEMNO
INNER JOIN ICITEM itm1 ON bomd.ITEMNO = itm1.ITEMNO
INNER JOIN ICITEM itm ON bomd.COMPONENT = itm.ITEMNO
INNER JOIN BomTree AS t ON bomd.ITEMNO = t.COMPONENT
)
SELECT ITEMNO
,[DESC]
,BOMNO
,BUILDQTY
,UNIT
,COMPONENT
,[Comp Desc]
,[Comp-Qty]
,COMPBOMNO
,Depth
FROM BomTree
GROUP BY ITEMNO
,[DESC]
,BOMNO
,BUILDQTY
,UNIT
,COMPONENT
,[Comp Desc]
,[Comp-Qty]
,COMPBOMNO
,Depth;
error:Types don't match between the anchor and the recursive part in
column "Comp-Qty" of recursive query "BomTree".
First of all, this error means there is mismatch in datatype between the same columns in two union queries. In order to fix this you could rewrite your query something like this:
WITH BomTree
AS (
SELECT bomh.ITEMNO
,itm1.[DESC]
,bomh.BOMNO
,bomh.BUILDQTY
,bomh.UNIT
,bomd.COMPONENT
,itm.[DESC] [Comp Desc]
,cast(bomd.QTY as int) [Comp-Qty] --cast as integer
,bomd.COMPBOMNO
,0 AS Depth
FROM ICBOMH bomh
INNER JOIN ICBOMD bomd ON bomh.BOMNO = bomd.BOMNO
AND bomh.ITEMNO = bomd.ITEMNO
INNER JOIN ICITEM itm1 ON bomd.ITEMNO = itm1.ITEMNO
INNER JOIN ICITEM itm ON bomd.COMPONENT = itm.ITEMNO
WHERE bomd.BOMNO = '01'
AND bomd.ITEMNO = '300060397'
UNION ALL
SELECT bomh.ITEMNO
,itm1.DESC]
,bomh.BOMNO
,bomh.BUILDQTY
,bomh.UNIT
,bomd.COMPONENT
,itm.[DESC] [Comp Desc]
,cast((t.[Comp-Qty] * bomd.QTY) as int) AS [Comp-Qty] --cast as integer
,bomd.COMPBOMNO
,t.Depth + 1 AS Depth
FROM ICBOMH bomh
INNER JOIN ICBOMD bomd ON bomh.BOMNO = bomd.BOMNO
AND bomh.ITEMNO = bomd.ITEMNO
INNER JOIN ICITEM itm1 ON bomd.ITEMNO = itm1.ITEMNO
INNER JOIN ICITEM itm ON bomd.COMPONENT = itm.ITEMNO
INNER JOIN BomTree AS t ON bomd.ITEMNO = t.COMPONENT
)
SELECT DISTINCT ITEMNO
,[DESC]
,BOMNO
,BUILDQTY
,UNIT
,COMPONENT
,[Comp Desc]
,[Comp-Qty]
,COMPBOMNO
,Depth
FROM BomTree;

How to use Count and Sum in the Same Query?

I have written the following query using the Aggregate function "SUM".
SELECT
D.PODetailID,
SUM(D.AcceptedQty)
FROM STR_MRVDetail D
INNER JOIN STR_MRVHeader H ON H.MRVID = D.MRVID
INNER JOIN PUR_POHeader PH ON PH.POID = H.POID
INNER JOIN PUR_PODetail PD ON PD.PODetailID = D.PODetailID
WHERE H.StatusID = 4
AND PH.POID = 839
AND (SELECT
SUM(AcceptedQty)
FROM
STR_MRVDetail
WHERE
STR_MRVDetail.PODetailID = PD.PODetailID) =
(SELECT POQuantity FROM PUR_PODetail
WHERE PUR_PODetail.PODetailID = PD.PODetailID)
GROUP BY D.PODetailID
Currently this Query returns 2 rows. I want to retrieve the count of this query. How to count the rows of the above query?
You can add ##ROWCOUNT to the query, which also gives you the final result set as well as the count of all the rows in the result set.
SELECT D.PODetailID, SUM(D.AcceptedQty), ##ROWCOUNT FROM STR_MRVDetail D
INNER JOIN STR_MRVHeader H ON H.MRVID = D.MRVID
INNER JOIN PUR_POHeader PH ON PH.POID = H.POID
INNER JOIN PUR_PODetail PD ON PD.PODetailID = D.PODetailID
WHERE H.StatusID = 4
AND PH.POID = 839
AND (SELECT SUM(AcceptedQty) FROM STR_MRVDetail
WHERE STR_MRVDetail.PODetailID = PD.PODetailID) =
(SELECT POQuantity FROM PUR_PODetail
WHERE PUR_PODetail.PODetailID = PD.PODetailID)
GROUP BY D.PODetailID
SELECT COUNT(*) as Total_Rows
FROM
(
SELECT D.PODetailID as PODetailID, SUM(D.AcceptedQty) as Total_AcceptedQty
FROM STR_MRVDetail D
INNER JOIN STR_MRVHeader H ON H.MRVID = D.MRVID
INNER JOIN PUR_POHeader PH ON PH.POID = H.POID
INNER JOIN PUR_PODetail PD ON PD.PODetailID = D.PODetailID
WHERE H.StatusID = 4
AND PH.POID = 839
AND (SELECT SUM(AcceptedQty) FROM STR_MRVDetail
WHERE STR_MRVDetail.PODetailID = PD.PODetailID) =
(SELECT POQuantity FROM PUR_PODetail
WHERE PUR_PODetail.PODetailID = PD.PODetailID)
GROUP BY D.PODetailID
) as t

Resources