Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

complex query Expand / Collapse
Author
Message
Posted Friday, January 21, 2011 12:12 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, January 6, 2014 4:32 AM
Points: 127, Visits: 350
create table #mgr_tab(sno int,sname varchar(15), sid int,stype char(1))
create table #veg_tab(sid int, veg_name varchar(20))
create table #non_veg_tab(sid int, non_veg_name varchar(20))

insert into #mgr_tab values (1,'edible',10,'N')
insert into #mgr_tab values (2,'eating',20,'N')
insert into #mgr_tab values (3,'zebra',30,'V')
insert into #mgr_tab values (4,'zebra',40,'V')


insert into #veg_tab values(30,'carrot')
insert into #veg_tab values(40,'beans')


insert into #non_veg_tab values(10,'Deer')
insert into #non_veg_tab values(20,'Mutton')

based on stype in the mgr_tab table i need to get the values from either veg_tab or non_veg_tab.

Is it possible in single select statement?

select sno, sname, item_name,stype

this item_name can be wither veg or non-veg based on stype...

any help pls?

Post #1051355
Posted Friday, January 21, 2011 1:28 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 4:18 AM
Points: 1,539, Visits: 8,127
You can LEFT OUTER JOIN from #mgr_tab to #veg_tab and also LEFT OUTER JOIN from #mgr_tab to #non_veg_tab, both ON the sid for each table.
Then, within the SELECT statement use CASE to check the value of #mgr_tab to decide whether to use #veg_tab.veg_name or #non_veg_tab.non_veg_name.

BrainDonor.


BrainDonor
Linkedin
Blog Site
Post #1051372
Posted Friday, January 21, 2011 7:32 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, January 6, 2014 4:32 AM
Points: 127, Visits: 350
Hi,,

Thanks a lot..
As u said i followed.. but i am facing new problem..

select case com_owner.owner_type 
when 'Company' then com_header.trade_name
when 'Person' then per.first_name end
from hr_company_owner com_owner
left outer join
HR_PERSONS per
on com_owner.owner_id= per.person_id
left outer join
hr_company_header com_header
on com_owner.owner_id=com_header.company_id

this returns error as follows...

Msg 457, Level 16, State 1, Line 1
Implicit conversion of varchar value to varchar cannot be performed because the collation of the value is unresolved due to a collation conflict.


Post #1051526
Posted Friday, January 21, 2011 7:44 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 4:18 AM
Points: 1,539, Visits: 8,127
Post the definitions of all three tables. If you have some sample data with that too it would help. That is what made answering your first problem so easy.

BrainDonor


BrainDonor
Linkedin
Blog Site
Post #1051544
Posted Friday, January 21, 2011 8:00 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, October 24, 2012 2:12 PM
Points: 1,213, Visits: 3,232
Here are some alternative methods.

You could create a View from your #veg_tab and #non_veg_tab tables, that includes an additional column (stype) indicating the original source table of the row ('N' or 'V'), and join to this view on the sid and stype columns.

If you are not allowed/willing to create a View, you could join to a derived table or a CTE created from your #veg_tab and #non_veg_tab tables.

SELECT MGR.sno, MGR.sname, FOOD.item_name, FOOD.stype
FROM #mgr_tab MGR
JOIN (
SELECT 'V', sid, veg_name FROM #veg_tab
UNION ALL
SELECT 'N', sid, non_veg_name FROM #non_veg_tab
) FOOD(stype, sid, item_name)
ON (MGR.sid = FOOD.sid AND MGR.stype = FOOD.stype)

;WITH FOOD(stype, sid, item_name) AS (
SELECT 'V', sid, veg_name FROM #veg_tab
UNION ALL
SELECT 'N', sid, non_veg_name FROM #non_veg_tab
)
SELECT MGR.sno, MGR.sname, FOOD.item_name, FOOD.stype
FROM #mgr_tab MGR
JOIN FOOD ON (MGR.sid = FOOD.sid AND MGR.stype = FOOD.stype)

Post #1051565
Posted Saturday, January 22, 2011 1:11 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, January 6, 2014 4:32 AM
Points: 127, Visits: 350
/****** Object:  Table [dbo].[HR_COMPANY_HEADER]    Script Date: 01/22/2011 12:59:50 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[HR_COMPANY_HEADER](
[COMPANY_ID] [int] IDENTITY(1,1) NOT NULL,
[TRADE_NAME] [varchar](50) NULL,
[COMMERCIAL_ACTIVITY] [varchar](50) NULL,
[GENERATE_RESERVATION_DOCUMENT] [char](1) NULL,
[LICENSE_NUMBER] [varchar](15) NULL,
[LICENSE_ISSUED_DATE] [datetime] NULL,
[LICENSE_EXPIRY_DATE] [datetime] NULL,
[VERIFIER_ID] [int] NULL,
[INFO_1] [varchar](50) NULL,
[INFO_2] [varchar](50) NULL,
[INFO_3] [varchar](50) NULL,
[INFO_4] [varchar](50) NULL,
[INFO_5] [varchar](50) NULL,

CONSTRAINT [PK_HR_COMPANY_HEADER] PRIMARY KEY CLUSTERED
(
[COMPANY_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
----------------------------------------


/****** Object: Table [dbo].[HR_COMPANY_OWNER] Script Date: 01/22/2011 13:05:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[HR_COMPANY_OWNER](
[COMPANY_OWNER_ID] [int] IDENTITY(1,1) NOT NULL,
[COMPANY_ID] [int] NULL,
[PARTNER_STATUS] [int] NULL,
[OWNER_TYPE] [varchar](50) NULL,
[OWNER_ID] [int] NOT NULL,
[INFO_1] [varchar](50) NULL,
[INFO_2] [varchar](50) NULL,
[INFO_3] [varchar](50) NULL,
[INFO_4] [varchar](50) NULL,
[INFO_5] [varchar](50) NULL,

CONSTRAINT [PK_HR_COMPANY_OWNER] PRIMARY KEY CLUSTERED
(
[COMPANY_OWNER_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[HR_COMPANY_OWNER] WITH CHECK ADD CONSTRAINT [FK_HR_COMPANY_OWNER_HR_COMPANY_HEADER] FOREIGN KEY([COMPANY_ID])
REFERENCES [dbo].[HR_COMPANY_HEADER] ([COMPANY_ID])
GO
ALTER TABLE [dbo].[HR_COMPANY_OWNER] CHECK CONSTRAINT [FK_HR_COMPANY_OWNER_HR_COMPANY_HEADER]
------------------------------------------------------------------------------


/****** Object: Table [dbo].[HR_PERSONS] Script Date: 01/22/2011 13:36:50 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[HR_PERSONS](
[FIRST_NAME] [varchar](150) NOT NULL,
[LAST_NAME] [varchar](150) NOT NULL,
[TITLE] [varchar](30) NULL,
[PREFIX] [varchar](30) NULL,
[SUFFIX] [varchar](30) NULL,
[MIDDLE_NAMES] [varchar](60) NULL,
[PERSON_TYPE] [varchar](30) NOT NULL,
[PERSON_ID] [int] NOT NULL,
[NATIONAL_IDENTIFIER] [varchar](30) NULL,
[EFFECTIVE_START_DATE] [datetime] NOT NULL,
[EFFECTIVE_END_DATE] [datetime] NOT NULL,
[DATE_EMPLOYEE_DATA_VERIFIED] [datetime] NULL,
[ON_MILITARY_SERVICE] [int] NULL,
[SECOND_PASSPORT_EXISTS] [int] NULL,

CONSTRAINT [PK_hr_persons] PRIMARY KEY CLUSTERED
(
[PERSON_ID] ASC,
[EFFECTIVE_START_DATE] ASC,
[EFFECTIVE_END_DATE] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF




Still i am facing same problem as collation conflict comes.. above tables script which i am using for this query....

When i right click the database and checked the properties .. collation is SQL_Latin1_General_CP1_CI_AS..... whts going wrong?.. can u suggest me anythg on this pls?
Post #1051925
Posted Saturday, January 22, 2011 4:04 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, January 6, 2014 4:32 AM
Points: 127, Visits: 350
it is solved...


it's the problem of having different collation in column level. i just dropped the table(after taking backup of data). created again.. now it works properly..

Thanks a lot all of you....

Post #1051937
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse