SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select Query with Join and Sum Per Identity
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

StacyOW
Yak Posting Veteran

USA
93 Posts

Posted - 12/20/2012 :  09:31:41  Show Profile  Reply with Quote
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

Edited by - StacyOW on 12/20/2012 11:03:01

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 12/20/2012 :  11:04:01  Show Profile  Reply with Quote
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

USA
93 Posts

Posted - 12/20/2012 :  11:18:46  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 12/20/2012 :  11:23:38  Show Profile  Reply with Quote
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

USA
93 Posts

Posted - 12/20/2012 :  11:42:23  Show Profile  Reply with Quote
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.

Edited by - StacyOW on 12/20/2012 11:45:39
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 12/20/2012 :  11:45:06  Show Profile  Reply with Quote
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

USA
93 Posts

Posted - 12/20/2012 :  11:46:41  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 12/20/2012 :  11:59:53  Show Profile  Reply with Quote
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

USA
93 Posts

Posted - 12/20/2012 :  12:31:04  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 12/20/2012 :  12:32:16  Show Profile  Reply with Quote
tnx...please post back results too

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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.1 seconds. Powered By: Snitz Forums 2000