Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select Query with Join and Sum Per Identity

Author  Topic 

StacyOW
Yak Posting Veteran

93 Posts

Posted - 2012-12-20 : 09:31:41
I am trying to make a select query that will join with a child table and then sum field QtyOrd for each parent Identity field returned in select. Not sure if that make sense?

Basically I have a parent table that I want to join with a child table. When I select records in the parent table I want to then have the query sum up the QtyOrd field in the child table for each PO# (identity field).

So I want to return:
PO# VendorName Date DateRequired POTotal StatusName StatusID PartsOrd
1 Viking 02/07/2012 02/08/2012 200.75 Closed 2 5

PartsOrd would be the sum of the field QtyOrd in the child (Items) table for each PO#. I'm thinking I need to use a GroupBy but I'm not sure how to do the sum part, examples I've found aren't exactly what I am trying to do so they aren't very helpful.

Not sure exactly how to explain this, so I understand if you aren't getting what I am trying do.
Thanks,
Stacy

P.S. This is what my query looks like right now but it's returning a PONum record for each item in the items table. I need to have it return just one PO# with the sum of all the items (QtyOrd) in that PartsOrd field.

USE [MT]
GO
/****** Object: StoredProcedure [dbo].[POStatusReport] Script Date: 12/20/2012 10:00:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROC [dbo].[POStatusReport]
(
@zStatus varchar(150),
@StartDate varchar(30),
@StopDate varchar(30)
)
AS
BEGIN
SET NOCOUNT ON
SELECT DISTINCT porders.PONum, vendors.VendorName, porders.Date, porders.DateRequired, porders.POTotal, postatus.StatusName, porders.StatusID,
items.QtyOrd AS PartsOrd
FROM porders INNER JOIN
vendors ON porders.VendorID = vendors.VendorID INNER JOIN
postatus ON porders.StatusID = postatus.StatusID INNER JOIN
items ON porders.PONum = items.PONum
WHERE (porders.Date BETWEEN @StartDate AND @StopDate) AND ','+ @zStatus + ',' LIKE '%,' + CAST(postatus.StatusName AS varchar(50)) + ',%'
END

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-12-20 : 11:04:01
sounds like this

SELECT porders.PONum, vendors.VendorName, porders.Date, porders.DateRequired, porders.POTotal, postatus.StatusName, porders.StatusID,
i.PartsOrd
FROM porders INNER JOIN
vendors ON porders.VendorID = vendors.VendorID INNER JOIN
postatus ON porders.StatusID = postatus.StatusID INNER JOIN
(SELECT PONum,SUM(QtyOrd) AS PartsOrd FROM items GROUP BY PONum)i ON porders.PONum = i.PONum
WHERE (porders.Date BETWEEN @StartDate AND @StopDate) AND (postatus.StatusName = @zStatus)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

StacyOW
Yak Posting Veteran

93 Posts

Posted - 2012-12-20 : 11:18:46
Got it working! Must of had to refresh or something cuz it is working now!

Thanks anyway!
Stacy
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-12-20 : 11:23:38
hmm...but your posted query didnt have any GROUPBY then how did you get aggregates?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

StacyOW
Yak Posting Veteran

93 Posts

Posted - 2012-12-20 : 11:42:23
This is what I have right now and it is working perfectly. I'm not that great with sql so I'm not exactly sure how it's working.
USE [MT]
GO
/****** Object: StoredProcedure [dbo].[POStatusReport] Script Date: 12/20/2012 10:40:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROC [dbo].[POStatusReport]
(
@zStatus varchar(150),
@StartDate varchar(30),
@StopDate varchar(30)
)
AS
BEGIN
SET NOCOUNT ON
SELECT DISTINCT porders.PONum, porders.Date, porders.DateRequired, porders.POTotal, porders.StatusID,
(SELECT SUM(QtyOrd) AS Expr1
FROM items AS tt
WHERE (PONum = porders.PONum)) AS PartsOrd, postatus.StatusName, vendors.VendorName
FROM porders INNER JOIN
postatus ON porders.StatusID = postatus.StatusID INNER JOIN
vendors ON porders.VendorID = vendors.VendorID
WHERE (porders.Date BETWEEN @StartDate AND @StopDate) AND ','+ @zStatus + ',' LIKE '%,' + CAST(postatus.StatusName AS varchar(50)) + ',%'
END

I just needed it to sum up the qtyord field in Items table (child) for each PO I'm selecting in the parent Porders table.

Stacy

PONum is PK in parent and FK in items, does that make a difference. I have checked the PO's and it is summing up all of the QtyOrd column in the items for each PO.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-12-20 : 11:45:06
ok...so you're using subquery. Then its fine. Your first posted suggestion didnt have this hence i asked

I prefer joins over subqueries in scenarios like this

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

StacyOW
Yak Posting Veteran

93 Posts

Posted - 2012-12-20 : 11:46:41
quote:
Originally posted by visakh16

ok...so you're using subquery. Then its fine. Your first posted suggestion didnt have this hence i asked

I prefer joins over subqueries in scenarios like this

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/




Ok I will change it to your way then if you think that's better! I trust your judgement.
Thanks again.
Stacy
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-12-20 : 11:59:53
why not do a test and see for yourself

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

StacyOW
Yak Posting Veteran

93 Posts

Posted - 2012-12-20 : 12:31:04
Will do!

quote:
Originally posted by visakh16

why not do a test and see for yourself

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-12-20 : 12:32:16
tnx...please post back results too

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -