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?
 
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
 
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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
 
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
 
Annette Wilson, MSISSr. Programmer AnalystAuthor Commented:
Thank you!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.