Avatar of Annette Wilson, MSIS
Annette Wilson, MSIS
Flag for United States of America asked on

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

Microsoft SQL Server 2005

Avatar of undefined
Last Comment
Annette Wilson, MSIS

8/22/2022 - Mon
bcnagel

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.
SOLUTION
Mark Ely

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Annette Wilson, MSIS

ASKER
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.
Annette Wilson, MSIS

ASKER
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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
SOLUTION
bcnagel

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Annette Wilson, MSIS

ASKER
What if each part has multiple base?

So, for each base, the part may be in one base and be in another base.
ASKER CERTIFIED SOLUTION
bcnagel

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Annette Wilson, MSIS

ASKER
Thank you!!