Re: Using a variable as a view name in a select

From: Hilary Forbes <hforbes(at)dmr(dot)co(dot)uk>
To: "Wilkinson, Jim" <Jim(dot)Wilkinson(at)cra-arc(dot)gc(dot)ca>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Using a variable as a view name in a select
Date: 2007-04-03 16:45:00
Message-ID: 200704031638.l33GcPe3025200@tamar.dmr.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

<html>
<body>
Jim<br><br>
So let's suppose you have a &quot;master&quot; table of
incidents<br><br>
incident_no (serial)<br>
incident_date (timestamp)<br>
other fields<br><br>
My understanding is that you now want to eg count the incidents starting
in a given month and going forwards for 12 months, grouping the results
by month.&nbsp; Have I understood the problem?<br><br>
If so here goes:<br><br>
Set up a table hftest<br><br>
incident serial<br>
incdate timestamp<br><br>
SELECT * from hftest;<br>
incident |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; incdate<br>
----------+---------------------<br>
&nbsp;&nbsp;&nbsp;&nbsp; 1000 | 2006-05-03 00:00:00<br>
&nbsp;&nbsp;&nbsp;&nbsp; 1001 | 2006-04-03 00:00:00<br>
&nbsp;&nbsp;&nbsp;&nbsp; 1002 | 2006-04-01 00:00:00<br>
&nbsp;&nbsp;&nbsp;&nbsp; 1003 | 2006-12-08 00:00:00<br>
&nbsp;&nbsp;&nbsp;&nbsp; 1004 | 2007-02-28 00:00:00<br>
&nbsp;&nbsp;&nbsp;&nbsp; 1005 | 2007-08-03 00:00:00<br><br>
Now:<br>
SELECT max(to_char(incdate,'Mon')) ,count(incident) from hftest WHERE
date_trunc('month',incdate) &gt;='2006/04/01' AND
date_trunc('month',incdate)&lt;=date_trunc('month',date '2006/04/01' +
interval '12 months') GROUP BY date_trunc('month',incdate) ORDER BY
date_trunc('month',incdate);<br>
&nbsp;max | count<br>
-----+-------<br>
&nbsp;Apr |&nbsp;&nbsp;&nbsp;&nbsp; 2<br>
&nbsp;May |&nbsp;&nbsp;&nbsp;&nbsp; 1<br>
&nbsp;Dec |&nbsp;&nbsp;&nbsp;&nbsp; 1<br>
&nbsp;Feb |&nbsp;&nbsp;&nbsp;&nbsp; 1<br>
&nbsp;<br>
&nbsp;which is almost what you want.&nbsp; To get the missing months with
zeroes, I think you probably need a table of months and to use a left
outer join but you may have found a better way by now!<br><br>
&nbsp;Now I have NO idea on the efficiency of this as I rather suspect
all those date_trunc functions may have an adverse effect!<br><br>
Best regards<br>
Hilary<br>
&nbsp;<br><br>
<br><br>
<br><br>
<br>
At 16:44 03/04/2007, you wrote:<br><br>
<blockquote type=cite class=cite cite=""><font size=2 color="#000080">Hi
Hilary, <br>
I am trying to produce reports where the user can select a different
fiscal year starting month.&nbsp; From this I would select the correct
table view to produce the reports in the correct month order by
column<br>
&nbsp;<br>
Select * from table_view;<br>
&nbsp;<br>
Incident&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
April&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; May&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
June&nbsp;&nbsp;&nbsp;&nbsp; July&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Aug&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ….<br>
===============================================<br>
Falls&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0<br>
.<br>
.<br>
.<br>
.<br>
&nbsp;<br>
Can you think of another way to do this ?<br>
&nbsp;<br>
&nbsp;<br>
<hr>
<div align="center"></font></div>
<font face="Tahoma" size=2><b>From:</b> Hilary Forbes
[<a href="mailto:hforbes(at)dmr(dot)co(dot)uk" eudora="autourl">
mailto:hforbes(at)dmr(dot)co(dot)uk</a>] <br>
<b>Sent:</b> April 3, 2007 10:14 AM<br>
<b>To:</b> Wilkinson, Jim; pgsql-sql(at)postgresql(dot)org<br>
<b>Subject:</b> Re: [SQL] Using a variable as a view name in a
select<br>
</font><font face="Times New Roman, Times">&nbsp;<br>
Jim<br><br>
My initial reaction is what are you trying to achieve?&nbsp; Surely you
could have one underlying table with dates in it and<br><br>
SELECT * from mytable WHERE date1&gt;='2007/04/01' AND
date2&lt;='2007/05/01';<br><br>
but otherwise, like John, I would use an external scripting language to
create the table name.<br><br>
Hilary<br><br>
At 14:04 03/04/2007, Wilkinson, Jim wrote:<br><br>
<br>
</font><font face="Times New Roman, Times" size=2 color="#000080">I have
created a view, called april_may.&nbsp;&nbsp; I need to select this view
by combineing to fields in the database to create the view name etc
…<br>
&nbsp;<br>
Create view as select * from table_X;<br>
&nbsp;<br>
I need to do something like this … <br>
&nbsp;<br>
Select * from (select table.start_month||_||table.end_month);<br>
==================<br>
Start_month&nbsp; = april<br>
End_month = May<br>
&nbsp;<br>
What I what to pass to the select is the combination of the 2 fields as
the view name.<br>
&nbsp;<br>
Any ideas ?<br>
</font><br>
<font face="Times New Roman, Times">Hilary Forbes<br>
DMR Limited (UK registration 01134804) <br>
A DMR Information and Technology Group company (
<a href="http://www.dmr.co.uk/" eudora="autourl">www.dmr.co.uk</a>) <br>
Direct tel 01689 889950 Fax 01689 860330 <br>
DMR is a UK registered trade mark of DMR Limited<br>
**********************************************************</font>
</blockquote>
<x-sigsep><p></x-sigsep>
Hilary Forbes<br>
DMR Limited (UK registration 01134804) <br>
A DMR Information and Technology Group company
(<a href="http://www.dmr.co.uk/" eudora="autourl"><font color="#0000FF">
<u>www.dmr.co.uk</a></u></font>) <br>
Direct tel 01689 889950 Fax 01689 860330 <br>
DMR is a UK registered trade mark of DMR Limited<br>
**********************************************************</body>
</html>

Attachment Content-Type Size
unknown_filename text/html 5.1 KB

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Wilkinson, Jim 2007-04-03 17:18:02 Re: Using a variable as a view name in a select
Previous Message Andrew Sullivan 2007-04-03 16:16:06 Re: Update problem.