Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi ...
i want join 3 table in procedure ...
butt error in group by !?

this query :

SQL
USE [AnbarDB]GO
/****** Object:  StoredProcedure [dbo].[Kardex]    Script Date: 03/09/2015 10:52:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[Kardex]
@Kala nvarchar(50)
 
 
AS
BEGIN
select       
       dbo.TAnbar.Anbar as anbar,
       dbo.TAnbar.Kalatype as noekala,
       dbo.TKala.Cod as cod,
       dbo.TKala.Kala as kala,
       dbo.TKala.Vahed as vahed,
       dbo.TKala.Toz as tozi,
       dbo.TKala.Mojodi as mojodi,
       dbo.TKala.Pic as pict,
       COUNT(dbo.TBuy.Kala) as tedadkala,
       SUM(dbo.TBuy.Andaze) as andaz,
       SUM(dbo.TBuy.Kol) as kalakol,
       COUNT(dbo.TSel.Kala) as tedadkala2,
       SUM(dbo.TSel.Andaze) as andaz2,
       SUM(dbo.TSel.Kol) as kalakol2       
       From dbo.TAnbar inner join dbo.TKala on dbo.TAnbar.Anbar = dbo.TKala.Anbar
       inner join dbo.TBuy on dbo.TKala.Kala = dbo.TBuy.Kala
       inner join dbo.TSel on dbo.TKala.Kala = dbo.TSel.Kala
       where  dbo.TKala.Kala = @Kala
       group by dbo.TKala.Kala
END


and this error :

C#
Msg 8120, Level 16, State 1, Procedure Kardex, Line 7
Column 'dbo.TAnbar.Anbar' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.


and this tables :

pic[^]
Posted
Comments
_Asif_ 9-Mar-15 5:48am    
Why are you grouping the result over dbo.TKala.Kala? The query will work if you remove the whole group by clause
CHill60 9-Mar-15 6:04am    
Anything in the SELECT clause must also appear in the GROUP BY clause - it might be easier if you explain what you are trying to do as the counts here don't look right
Herman<T>.Instance 9-Mar-15 6:26am    
set it as answer!
CHill60 9-Mar-15 6:34am    
:) I'll give the OP an opportunity to come back to me with their actual problem first ... I'm thinking of guiding them towards using CTE for the for counts and sums then joining to the other tables for the other data
Herman<T>.Instance 9-Mar-15 6:54am    
ok!

Anything in the GROUP BY clause must also appear in the SELECT clause.

There are several ways of rearranging this with sub-queries etc but below is a solution using CTEs (Common Table Expressions[^])

-- Get the grouped data from TBuy into CTE T1...
With T1(Kala, tedadkala, andaz, kalakol) 
AS
(
    SELECT Kala, Count(Kala), SUM(Andaze), SUM(Kol)
    from TBuy GROUP BY Kala
),

-- Get the grouped data from TSel into CTE T2...
T2(Kala, tedadkala2, andaz2, kalakol2)
AS
(
    SELECT Kala, Count(Kala), SUM(Andaze), SUM(Kol)
    from TSel GROUP BY Kala
)
-- Use the CTEs in the overall query
select       
       dbo.TAnbar.Anbar as anbar,
       dbo.TAnbar.Kalatype as noekala,
       dbo.TKala.Cod as cod,
       dbo.TKala.Kala as kala,
       dbo.TKala.Vahed as vahed,
       dbo.TKala.Toz as tozi,
       dbo.TKala.Mojodi as mojodi,
       dbo.TKala.Pic as pict,
	tedadkala,
    andaz,
    kalakol,
    tedadkala2,
    andaz2,
    kalakol2 
 
From dbo.TAnbar 
inner join dbo.TKala on dbo.TAnbar.Anbar = dbo.TKala.Anbar
inner join T1 on T1.Kala = TKala.Kala
inner join T2 on TKala.Kala = T2.Kala
where  dbo.TKala.Kala = @kala
--NB NO group by here
 
Share this answer
 
thanks guys ...
very much !
and dear CHill60 ...
 
Share this answer
 
Comments
Deepu S Nair 10-Mar-15 3:28am    
Don't post your comment as solution.Use 'Have a Question or Comment?' link.
Rajesh waran 10-Mar-15 3:29am    
Try to post it as comment, not as an Answer.

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