Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hello frnds i have a problem in joining ...
SQL
alter PROCEDURE [dbo].[sp_GatherCenterInfoForSpecialpaymentsTypes]      
(      
    @company_u_id int,      
    @UnitId int      
)      
AS      
BEGIN      
    SET NOCOUNT ON;  
 
    SELECT t1.Center_Name, t1.Center_Code, t1.Center_Id,t1.Center_Id_Format  
    FROM DayOpenDayCloseInfo 
    INNER JOIN (  
        (SELECT CenterInfo.Center_Name, CenterInfo.Center_Code, 
         CenterInfo.Center_Id,CenterInfo.Center_Id_Format, 
         MAX(DayOpenDayCloseInfo.DayOPenCloseId)     
         FROM CenterInfo 
         INNER JOIN DayOpenDayCloseInfo
         ON CenterInfo.Center_Id = DayOpenDayCloseInfo.Center_Id   
         WHERE (CenterInfo.Unit_Id = @UnitId) AND (CenterInfo.IsActive = 1) 
             AND (CenterInfo.company_u_id=@company_u_id)
         GROUP BY CenterInfo.Center_Name, CenterInfo.Center_Code, 
             CenterInfo.Center_Id,CenterInfo.Center_Id_Format)
    ) t1
    ON t1.Center_Id = DayOpenDayCloseInfo.Center_Id
    WHERE DayOpenDayCloseInfo.IsActive = 0
END
RETURN

when it give error on execute that ...
"Msg 8155, Level 16, State 2, Procedure sp_GatherCenterInfoForSpecialpaymentsTypes, Line 21
No column name was specified for column 5 of 't1'."

i am unable to understand what the problem is ../

in this i am joining a result set with an table .... as u see here ..

pls rply ..Thnks in advance
Posted
Updated 23-Jul-13 23:15pm
v2

Hello,

Have you tried rewriting your proc as shown below.
SQL
ALTER PROCEDURE [dbo].[sp_GatherCenterInfoForSpecialpaymentsTypes]      
(      
    @company_u_id int,      
    @UnitId int      
)      
AS      
BEGIN      
    SET NOCOUNT ON;  
 
    SELECT t1.Center_Name, t1.Center_Code, t1.Center_Id, t1.Center_Id_Format  
    FROM DayOpenDayCloseInfo 
    INNER JOIN (  
        (SELECT CenterInfo.Center_Name, CenterInfo.Center_Code, 
         CenterInfo.Center_Id,CenterInfo.Center_Id_Format, 
         MAX(DayOpenDayCloseInfo.DayOPenCloseId) AS DayOPenCloseId
         FROM CenterInfo 
         INNER JOIN DayOpenDayCloseInfo
         ON CenterInfo.Center_Id = DayOpenDayCloseInfo.Center_Id   
         WHERE (CenterInfo.Unit_Id = @UnitId) AND (CenterInfo.IsActive = 1) 
             AND (CenterInfo.company_u_id=@company_u_id)
         GROUP BY CenterInfo.Center_Name, CenterInfo.Center_Code, 
             CenterInfo.Center_Id, CenterInfo.Center_Id_Format)
    ) t1
    ON t1.Center_Id = DayOpenDayCloseInfo.Center_Id
    WHERE DayOpenDayCloseInfo.IsActive = 0
END
RETURN

Regards,
 
Share this answer
 
Comments
GDdixit 24-Jul-13 5:46am    
no ..its not giving the desire result ...
Prasad Khandekar 24-Jul-13 6:23am    
Hello,

If you look carefully at the solution you will observe that I have only added an alias for aggregate column MAX(DayOpenDayCloseInfo.DayOPenCloseId). Which should help you get rid of the error you were getting earlier.

Regards
To be hones the query is pretty messy...

But try providing an alias to MAX(DayOpenDayCloseInfo.DayOPenCloseId) in your subquery that should help
something like : MAX(DayOpenDayCloseInfo.DayOPenCloseId) as MaxOpenDay
 
Share this answer
 
v2
Comments
GDdixit 24-Jul-13 5:39am    
it work but not giving the desired result

actually the query result which is inside inner joining brackets

i.e. (SELECT CenterInfo.Center_Name, CenterInfo.Center_Code,
CenterInfo.Center_Id,CenterInfo.Center_Id_Format,
MAX(DayOpenDayCloseInfo.DayOPenCloseId)
FROM CenterInfo
INNER JOIN DayOpenDayCloseInfo
ON CenterInfo.Center_Id = DayOpenDayCloseInfo.Center_Id
WHERE (CenterInfo.Unit_Id = @UnitId) AND (CenterInfo.IsActive = 1)
AND (CenterInfo.company_u_id=@company_u_id)
GROUP BY CenterInfo.Center_Name, CenterInfo.Center_Code,
CenterInfo.Center_Id,CenterInfo.Center_Id_Format)
)

give a result set and a single row for each center_id

but when i join this with a table than i only want to select those results which which satisfy this condition ... (DayOpenDayCloseInfo.IsActive = 0)

there is multiple records in DayOpenDayCloseInfo for each center_id but i have to slect only those results which satisfy this condition e.i. (DayOpenDayCloseInfo.IsActive = 0) and "MAX(DayOpenDayCloseInfo.DayOPenCloseId) " for each center_id
Guirec 24-Jul-13 5:45am    
Yes you first problem is fixed... that was for sure.
Now as I told your query is pretty messy and I am not surprised it is not bringing the result you expect.

If you need some help regarding this then you'd better describe your tables and the expected output...

Statements like : "give a result set and a single row for each center_id" does not mean much. In SQL a select always return a result set and nothing else. a result set period.

I suggest you open a second question.
GDdixit 24-Jul-13 6:04am    
ok .. i am opening a new question now
Put a column name alias for your column of 'MAX(DayOpenDayCloseInfo.DayOPenCloseId)'
 
Share this answer
 
ALTER PROCEDURE [dbo].[sp_GatherCenterInfoForSpecialpaymentsTypes]
(
@company_u_id int,
@UnitId int
)
AS
BEGIN
SET NOCOUNT ON;

SELECT t1.Center_Name, t1.Center_Code, t1.Center_Id, t1.Center_Id_Format
FROM DayOpenDayCloseInfo
INNER JOIN (
(SELECT CenterInfo.Center_Name, CenterInfo.Center_Code,
CenterInfo.Center_Id,CenterInfo.Center_Id_Format,
MAX(DayOpenDayCloseInfo.DayOPenCloseId) AS DayOPenCloseId
FROM CenterInfo
INNER JOIN DayOpenDayCloseInfo
ON CenterInfo.Center_Id = DayOpenDayCloseInfo.Center_Id
WHERE (CenterInfo.Unit_Id = @UnitId) AND (CenterInfo.IsActive = 1)
AND (CenterInfo.company_u_id=@company_u_id)
GROUP BY CenterInfo.Center_Name, CenterInfo.Center_Code,
CenterInfo.Center_Id, CenterInfo.Center_Id_Format)
) t1
ON t1.Center_Id = DayOpenDayCloseInfo.Center_Id
WHERE DayOpenDayCloseInfo.IsActive = 0
END
RETURN
 
Share this answer
 
provide alias to MAX(DayOpenDayCloseInfo.DayOPenCloseId) in sub query

example

MAX(DayOpenDayCloseInfo.DayOPenCloseId) AS DayOPenCloseId
 
Share this 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