SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


complex query


complex query

Author
Message
BeginnerBug
BeginnerBug
SSC Veteran
SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)

Group: General Forum Members
Points: 297 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?


Steve Hall
Steve Hall
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2835 Visits: 11570
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.

Steve Hall
Linkedin
Blog Site
BeginnerBug
BeginnerBug
SSC Veteran
SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)

Group: General Forum Members
Points: 297 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.


Steve Hall
Steve Hall
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2835 Visits: 11570
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

Steve Hall
Linkedin
Blog Site
andrewd.smith
andrewd.smith
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1564 Visits: 3232
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)


BeginnerBug
BeginnerBug
SSC Veteran
SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)

Group: General Forum Members
Points: 297 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?
BeginnerBug
BeginnerBug
SSC Veteran
SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)SSC Veteran (297 reputation)

Group: General Forum Members
Points: 297 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....
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search