Unix Technical Forum

Need help building SQL query

This is a discussion on Need help building SQL query within the pgsql Novice forums, part of the PostgreSQL category; --> Hello, I have a problem as to which I do not know how to solve. My tables are built ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Novice

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-17-2008, 10:20 PM
Robert Blixt
 
Posts: n/a
Default Need help building SQL query



Hello,

I have a problem as to which I do not know
how to solve. My tables are built like this:

Timereportbreak consists of four breaks for
each timereport. They are ordered by the
indexnumber (0-3). They are connected to the
timereport table using the column timereportid.

Code:
CREATE TABLE timereportbreak
(
timereportid int4 NOT NULL DEFAULT 0,
starttime time NOT NULL DEFAULT '00:00:00'::time without time zone,
endtime time NOT NULL DEFAULT '00:00:00'::time without time zone,
indexnumber int4 NOT NULL DEFAULT 0
)
WITH OIDS;
ALTER TABLE timereportbreak OWNER TO postgres;
Timereport consists of the following columns..

Code:
CREATE TABLE timereport
(
employeesignature varchar(6) NOT NULL DEFAULT ''::character varying,
startdate date,
workedtimetotal time NOT NULL DEFAULT '00:00:00'::time without time zone,
starttime time NOT NULL DEFAULT '00:00:00'::time without time zone,
endtime time NOT NULL DEFAULT '00:00:00'::time without time zone,
statuscode int2 NOT NULL DEFAULT 0,
note text NOT NULL DEFAULT ''::text,
internalinfo varchar(80) NOT NULL DEFAULT ''::character varying,
id int4 NOT NULL DEFAULT nextval('timereport_id_seq'::text),
CONSTRAINT "TimeReport_pkey" PRIMARY KEY (id)
)
WITH OIDS;
ALTER TABLE timereport OWNER TO postgres;

What I would like to retrieve is all columns from timereport
and the four timereportbreaks connected to the timereport table.
However, since each break is not a separate column I am not
sure how to solve this. And since this database already has data
in it. I can't remodel the table. This is the output I want:

timereport.*, timereportbreak0start, timereportbreak0end,
timereportbreak1start, timereportbreak1end, ...



I did try something like this but using LIMIT makes it faulty and errornous:

Code:
CREATE OR REPLACE FUNCTION timereport_getbydateandemployee("varchar", date,
date, "varchar")
RETURNS SETOF timereportandbreaks AS
$BODY$
SELECT DISTINCT
timereport.employeesignature,
timereport.startdate,
timereport.workedtimetotal,
timereport.starttime,
timereport.endtime,
timereport.statuscode,
timereport.note,
timereport.internalinfo,
timereport.id
,(SELECT timereportbreak.starttime FROM timereportbreak, timereport,
employee
WHERE
(timereport.startdate BETWEEN $2 AND $3)
AND
(upper(timereport.employeesignature) = upper($4))
AND
upper(timereport.employeesignature) = upper(employee.signature)
AND
employee.companyid = $1
AND
timereport.id = timereportbreak.timereportid
AND
timereportbreak.indexnumber = '0'
LIMIT '1'
) AS break1start
,(SELECT timereportbreak.endtime FROM timereportbreak, timereport,
employee
WHERE
(timereport.startdate BETWEEN $2 AND $3)
AND
(upper(timereport.employeesignature) = upper($4))
AND
upper(timereport.employeesignature) = upper(employee.signature)
AND
employee.companyid = $1
AND
timereport.id = timereportbreak.timereportid
AND
timereportbreak.indexnumber = '0'
LIMIT '1'
) AS break1end
,(SELECT timereportbreak.starttime FROM timereportbreak, timereport,
employee
WHERE
(timereport.startdate BETWEEN $2 AND $3)
AND
(upper(timereport.employeesignature) = upper($4))
AND
upper(timereport.employeesignature) = upper(employee.signature)
AND
employee.companyid = $1
AND
timereport.id = timereportbreak.timereportid
AND
timereportbreak.indexnumber = '1'
LIMIT '1'
) AS break2start
,(SELECT timereportbreak.endtime FROM timereportbreak, timereport,
employee
WHERE
(timereport.startdate BETWEEN $2 AND $3)
AND
(upper(timereport.employeesignature) = upper($4))
AND
upper(timereport.employeesignature) = upper(employee.signature)
AND
employee.companyid = $1
AND
timereport.id = timereportbreak.timereportid
AND
timereportbreak.indexnumber = '1'
LIMIT '1'
) AS break2end
,(SELECT timereportbreak.starttime FROM timereportbreak, timereport,
employee
WHERE
(timereport.startdate BETWEEN $2 AND $3)
AND
(upper(timereport.employeesignature) = upper($4))
AND
upper(timereport.employeesignature) = upper(employee.signature)
AND
employee.companyid = $1
AND
timereport.id = timereportbreak.timereportid
AND
timereportbreak.indexnumber = '2'
LIMIT '1'
) AS break3start
,(SELECT timereportbreak.endtime FROM timereportbreak, timereport,
employee
WHERE
(timereport.startdate BETWEEN $2 AND $3)
AND
(upper(timereport.employeesignature) = upper($4))
AND
upper(timereport.employeesignature) = upper(employee.signature)
AND
employee.companyid = $1
AND
timereport.id = timereportbreak.timereportid
AND
timereportbreak.indexnumber = '2'
LIMIT '1'
) AS break3end
,(SELECT timereportbreak.starttime FROM timereportbreak, timereport,
employee
WHERE
(timereport.startdate BETWEEN $2 AND $3)
AND
(upper(timereport.employeesignature) = upper($4))
AND
upper(timereport.employeesignature) = upper(employee.signature)
AND
employee.companyid = $1
AND
timereport.id = timereportbreak.timereportid
AND
timereportbreak.indexnumber = '3'
LIMIT '1'
) AS break4start
,(SELECT timereportbreak.endtime FROM timereportbreak, timereport,
employee
WHERE
(timereport.startdate BETWEEN $2 AND $3)
AND
(upper(timereport.employeesignature) = upper($4))
AND
upper(timereport.employeesignature) = upper(employee.signature)
AND
employee.companyid = $1
AND
timereport.id = timereportbreak.timereportid
AND
timereportbreak.indexnumber = '3'
LIMIT '1'
) AS break4end
FROM
timereport, timereportbreak, employee
WHERE
(timereport.startdate BETWEEN $2 AND $3)
AND
(upper(timereport.employeesignature) = upper($4))
AND
(upper(timereport.employeesignature) = upper(employee.signature))
AND
(employee.companyid = $1)
AND
(timereportbreak.timereportid = timereport.id)
ORDER BY
timereport.startdate,
timereport.starttime
$BODY$
LANGUAGE 'sql' VOLATILE;
ALTER FUNCTION timereport_getbydateandemployee("varchar", date, date,
"varchar") OWNER TO postgres;


Any help would be tremendously appretiated.

// Kind Regards Robert



---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-17-2008, 10:20 PM
Bruno Wolff III
 
Posts: n/a
Default Re: Need help building SQL query

On Mon, Dec 05, 2005 at 10:28:04 +0100,
Robert Blixt <robert.blixt@transpa.se> wrote:
>
> Timereportbreak consists of four breaks for
> each timereport. They are ordered by the
> indexnumber (0-3). They are connected to the
> timereport table using the column timereportid.
>
> What I would like to retrieve is all columns from timereport
> and the four timereportbreaks connected to the timereport table.
> However, since each break is not a separate column I am not
> sure how to solve this. And since this database already has data
> in it. I can't remodel the table. This is the output I want:


Normally you want to do this by joining to Timereportbreak once for each
break (i.e. 4 times) using a different table alias for each join. Then
you can use the a different condition on indexnumber for each alias to
get the appropiate break.

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-17-2008, 10:21 PM
Robert Blixt
 
Posts: n/a
Default Re: Need help building SQL query



> -----Ursprungligt meddelande-----
> Från: pgsql-novice-owner@postgresql.org [mailtogsql-novice-
> owner@postgresql.org] För Bruno Wolff III
> Skickat: den 5 december 2005 17:46
> Till: Robert Blixt
> Kopia: pgsql-novice@postgresql.org
> Ämne: Re: [NOVICE] Need help building SQL query
>
> On Mon, Dec 05, 2005 at 10:28:04 +0100,
> Robert Blixt <robert.blixt@transpa.se> wrote:
> >
> > Timereportbreak consists of four breaks for
> > each timereport. They are ordered by the
> > indexnumber (0-3). They are connected to the
> > timereport table using the column timereportid.
> >
> > What I would like to retrieve is all columns from timereport
> > and the four timereportbreaks connected to the timereport table.
> > However, since each break is not a separate column I am not
> > sure how to solve this. And since this database already has data
> > in it. I can't remodel the table. This is the output I want:

>
> Normally you want to do this by joining to Timereportbreak once for each
> break (i.e. 4 times) using a different table alias for each join. Then
> you can use the a different condition on indexnumber for each alias to
> get the appropiate break.
>


Thanks for your help, highly appretiated.


Regards,
Robert



---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:49 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com