Click here to Skip to main content
15,848,157 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I have two table as A,B .Table A holds my category names and table B holds
my fk_tbl_A ,time ,machine name.

I want to calculate the time ,which I have used for each machine and grouped by fk_abl_A
I wrote the below code in SQL 2012:
SQL
select sum(cast(a.during_time as int)) from dbo.sabt_bazdid as a
join dbo.ability as b
on a.fk_operator_ability = b.id
Group by a.fk_operator_ability  


The code working well here but when I want to show the names of the category in table A ,
Sql shows error.

SQL
select b.name , sum(cast(a.during_time as int)) from dbo.sabt_bazdid as a
join dbo.ability as b
on a.fk_operator_ability = b.id
Group by a.fk_operator_ability 



Please help me to solve problem
Thanks.
Posted
Updated 26-Jan-14 8:55am
v2
Comments
Peter Leow 26-Jan-14 10:00am    
Not clear. Can you show full schema of tables A and B.
sorosh04 26-Jan-14 10:17am    
Table B :
[em_number]
,[fk_applier_id]
,[fk_ability_id]
,[fk_machine_id]
,[start_date]
,[start_time]
,[end_date]
,[end_time]
,[during_time]
,[fault_reason]
,[result]
,[description]

Table A:
[id]
,[description]
,[category_id]
,[name]
sorosh04 26-Jan-14 10:18am    
I want to calculate the during_time from tbl B for each name from tble A and group by fk_ability_id

1 solution

You would have encountered error like this:
Column 'b.name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

Try change this:
Group by a.fk_operator_ability 

to
Group by a.fk_operator_ability, b.name

Hope it helps.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900