PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 17th, 2003, 01:38 AM
wanaka wanaka is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 40 wanaka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 7 sec
Reputation Power: 6
postgres concat two field

CASE WHEN contact_list.group_role = '1' THEN 'Main Contact' WHEN contact_list.group_role = 'developer' THEN 'Developer Contact' ELSE '' END AS contact_group,
contact_list.group_desc,

I have contact list select statement that I want to join contact_group with contact_list.group_desc after the case statement. Just wondering is this possible? I have tried the following but it doesn't seem to work.

contact_group || ' ' || group.desc AS complate_group_title

Reply With Quote
  #2  
Old June 17th, 2003, 03:21 AM
bcyde's Avatar
bcyde bcyde is offline
Me likey breadsticks...
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2003
Location: Los Angeles
Posts: 1,189 bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 1 h 12 m 58 sec
Reputation Power: 12
Send a message via AIM to bcyde Send a message via Yahoo to bcyde
What's the exact error you're getting? I usually don't have problems with string concatenations, although I haven't had to use them after a case.

Can you print out the complete query and the error?

-b
__________________
PostgreSQL, it's what's for dinner...

Reply With Quote
  #3  
Old June 17th, 2003, 04:51 AM
swampBoogie swampBoogie is online now
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,807 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 9 h 12 m 57 sec
Reputation Power: 48
You can't use an alias other than in order by.

You could write is a derived table

Code:
select contact_group || ' ' || g.desc AS complate_group_title from
(select CASE WHEN contact_list.group_role = '1' THEN 'Main Contact' WHEN contact_list.group_role = 'developer' THEN 'Developer Contact' ELSE '' END AS contact_group, contact_list.group_desc
from contact_list , ... )  as g


Btw, it's not a case statement, it's a case expression.

Reply With Quote
  #4  
Old June 18th, 2003, 03:55 AM
wanaka wanaka is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 40 wanaka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 7 sec
Reputation Power: 6
Hi Thank for the example.

This is my complete sql

SELECT project.project_name,
contact_group ||' ' || g.group_desc AS complate_group_title from
(Select CASE WHEN contact_list.group_role = '1' THEN 'Main Contact' WHEN contact_list.group_role = 'developer' THEN 'Developer Contact' ELSE '' END AS contact_group, contact_list.group_desc
from contact_list) as g,
CASE WHEN (contact_list.priority = 1) THEN 'Primary Contact' WHEN (contact_list.priority = 2) THEN 'Secondary Contact' ELSE 'Management Contact' END AS contact_priority,
contact_person.lastname || ', ' || contact_person.firstname AS name,
contact_person.mobileph,
contact_person.workph,
contact_person.workext,
contact_person.fax,
contact_person.pager,
contact_person.otherph,
contact_person.email,
contact_person.comments,
contact_person.title,
contact_person.ic,
contact_person.cpid,
contact_person.modifydate,
contact_list.pid FROM project,
contact_list,
contact_person WHERE ((project.pid = contact_list.pid) AND (contact_person.cpid = contact_list.cpid)) ORDER BY project.project_name,
contact_list.group_role,
contact_list.group_desc,
contact_list.priority;

The error message I got is parse error near CASE

Reply With Quote
  #5  
Old June 18th, 2003, 04:01 AM
wanaka wanaka is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 40 wanaka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 7 sec
Reputation Power: 6
I forgot to post my table as well.

CREATE TABLE "contact_person" (
"cpid" int4 DEFAULT nextval('public.contact_person_cpid_seq') NOT NULL ,
"modifydate" timestamp without time zone,
"firstname" varchar(50),
"lastname" varchar(50),
"pager" varchar(15),
"workph" varchar(15),
"mobileph" varchar(15),
"otherph" varchar(15),
"email" varchar(50),
"comments" text,
"fax" varchar(15),
"title" varchar(50),
"ic" varchar(15),
"workext" varchar(5),
CONSTRAINT "contact_person_pkey" PRIMARY KEY ("cpid")
);

CREATE TABLE "contact_list" (
"cl_id" int4 DEFAULT nextval('public.contact_list_cl_id_seq') NOT NULL ,
"pid" int4 NOT NULL ,
"cpid" int4 NOT NULL ,
"priority" int2,
"group_desc" varchar(30),
"group_role" varchar(15),
CONSTRAINT "cpid_delete" FOREIGN KEY ("cpid") REFERENCES contact_person(cpid) MATCH FULL ,
CONSTRAINT "pid_delete" FOREIGN KEY ("pid") REFERENCES project(pid) MATCH FULL
);

CREATE TABLE "project" (
"pid" int4 DEFAULT nextval('public.project_pid_seq') NOT NULL ,
"project_name" varchar(50),
CONSTRAINT "project_pkey" PRIMARY KEY ("pid")
);


I want to join contact_list.group_role with contact_list.group_desc but group_role I need to replace the following value

case 1
Main Contact
Case 2
Developer Contact
Case 3
Null

Reply With Quote
  #6  
Old June 18th, 2003, 11:24 AM
bcyde's Avatar
bcyde bcyde is offline
Me likey breadsticks...
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2003
Location: Los Angeles
Posts: 1,189 bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 1 h 12 m 58 sec
Reputation Power: 12
Send a message via AIM to bcyde Send a message via Yahoo to bcyde
Thx for posting your full query and table setup. Try this query to see if it's what you want:

SELECT project.project_name,
g.contact_group ||' ' || g.group_desc AS complate_group_title,
contact_person.lastname || ', ' || contact_person.firstname AS name,
CASE WHEN (contact_list.priority = 1) THEN 'Primary Contact' WHEN (contact_list.priority = 2) THEN 'Secondary Contact' ELSE 'Management Contact' END AS contact_priority,
contact_person.mobileph,
contact_person.workph,
contact_person.workext,
contact_person.fax,
contact_person.pager,
contact_person.otherph,
contact_person.email,
contact_person.comments,
contact_person.title,
contact_person.ic,
contact_person.cpid,
contact_person.modifydate,
contact_list.pid
FROM project,
(SELECT (CASE WHEN contact_list.group_role = '1' THEN 'Main Contact' WHEN contact_list.group_role = 'developer' THEN 'Developer Contact' ELSE '' END) AS contact_group, contact_list.group_desc FROM contact_list) as g,
contact_list,
contact_person WHERE project.pid = contact_list.pid AND contact_person.cpid = contact_list.cpid
ORDER BY project.project_name,
contact_list.group_role,
contact_list.group_desc,
contact_list.priority

The query runs in a db I created from your table setup, but since I don't have any data I'm not exactly sure if it's what you want. If it's not exactly what you were looking for, hopefully it'll get you moving in the right direction, your original query had 2 FROM clauses.

HTH
-b

Reply With Quote
  #7  
Old June 18th, 2003, 10:34 PM
wanaka wanaka is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 40 wanaka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 7 sec
Reputation Power: 6
Error: ERROR: current transaction is aborted, queries ignored until end of transaction block

This is very interesting error. I tried to figure out

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > postgres concat two field


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT