Group by BASE PART

How can I get a sum total of labor hours for each part and operation for each BASE part?

I do have BASE parts available in another table if this will help.

    SELECT  
          ct.[BASE]
          ,ct.[BASEDescription]
          
          ,ct.[PART_NO]
          ,ct.[M]
          ,ct.[OP]
          ,ct.[PIECE]
          ,ct.[QTY]
          ,ct.[PART_ID]
          ,ct.[DESCRIPTION]
         
          ,tt.LABOR_HOURS
          ,tt.LABOR_COST
      FROM [DATABASE].[dbo].[CarrieTbl] ct inner join DATABASE2.dbo.TT_HOUR_DETAILS tt
    ON ct.[PART_NO]=tt.PART_ID

Open in new window

Annette Wilson, MSISSr. Programmer AnalystAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bcnagelCommented:
Hi technette.

If each PART_NO corresponds to one and only one BASE, how about trying something like this:

;with cte_Totals as
	(
	select ct.[BASE], ct.[PART_NO], sum(tt.LABOR_HOURS) as TotalLaborHours, null as TotalOP
		from [DATABASE].[dbo].[CarrieTbl] ct
		inner join DATABASE2.dbo.TT_HOUR_DETAILS tt
		on	ct.[PART_NO] = tt.PART_ID;
		group by ct.[BASE], ct.[PART_NO]

	union

	select ct.[BASE], null as PART_NO, null as TotalLaborHours, sum(ct.[OP]) as TotalOP
		from [DATABASE].[dbo].[CarrieTbl] ct
		inner join DATABASE2.dbo.TT_HOUR_DETAILS tt
		on	ct.[PART_NO] = tt.PART_ID;
		group by ct.[BASE]
	)

select *
	from cte_Totals t
	order by BASE, PART_NO

Open in new window


You might have to adjust things to get it working, but hopefully the general idea makes sense.
0
Mark ElySenior Coldfusion DeveloperCommented:
 SELECT  ct.[PART_NO]
		  ,ct.[BASE]
          ,ct.[OP]
          ,ct.[PART_ID]
          ,SUM(tt.LABOR_HOURS) AS Labor_Hours
      FROM [DATABASE].[dbo].[CarrieTbl] ct 
		inner join DATABASE2.dbo.TT_HOUR_DETAILS tt
		 ON ct.[PART_NO]=tt.PART_ID
		GROUP BY ct.Part_No, ct.OP, ct.BASE

Open in new window

0
Annette Wilson, MSISSr. Programmer AnalystAuthor Commented:
Mark,
Thank you!  The query works well.  There no way to get the descriptions in this?

If not, I can create a temp table, add a description column and update it.
0
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

Annette Wilson, MSISSr. Programmer AnalystAuthor Commented:
Thank you for responding bcnagel.

I'm getting an error on the second part of the query.

Operand data type varchar is invalid for sum operator   "sum(ct.[OP])" - this is varchar.
0
bcnagelCommented:
Yeah, I misunderstood your question, technette. I thought you were looking for the sum of OP for each BASE.

Given that you have only one description per BASE, you can add that field into the grouping that Mark suggested:

SELECT  ct.[PART_NO]
		  ,ct.[BASE]
		  ,ct.[BASEDescription]
          ,ct.[OP]
          ,ct.[PART_ID]
          ,SUM(tt.LABOR_HOURS) AS Labor_Hours
      FROM [DATABASE].[dbo].[CarrieTbl] ct 
		inner join DATABASE2.dbo.TT_HOUR_DETAILS tt
		 ON ct.[PART_NO]=tt.PART_ID
		GROUP BY ct.Part_No, ct.OP, ct.BASE, ct.[BASEDescription]

Open in new window


What does the ct.[DESCRIPTION] field describe? The PART_NO? If so, you could throw that into your summary, too, as long as there is only one ct.[DESCRIPTION] per PART_NO.
0
Annette Wilson, MSISSr. Programmer AnalystAuthor Commented:
What if each part has multiple base?

So, for each base, the part may be in one base and be in another base.
0
bcnagelCommented:
The query above should work fine under those conditions, because you will get unique groups for every combination of part and base. Does that make sense?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Annette Wilson, MSISSr. Programmer AnalystAuthor Commented:
Thank you!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.