473,734 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SELECT based on function result

I have a function that tells me if a record is positive and negative
based on several field values. I use it in select statements:

ohc=> SELECT sample_id, is_wipe_positiv e(tblleadwipe.s ample_id) AS
positive FROM tblleadwipe WHERE hud_building_id IS NOT NULL;
sample_id | positive
-----------+----------
73 | f
81 | t
(2 rows)

I see that I cannot change my WHERE statement to WHERE positive = 't'
because the column positive does not exist. Now I am looking for the
best way to return all records that are found positive or negative using
a query. Can anyone offer any guidance to how I can return all the
positives (or negatvies)? Or do I need to write another function that
does that?

--
Robert
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #1
2 1304
Robert Fitzpatrick wrote:
I have a function that tells me if a record is positive and negative
based on several field values. I use it in select statements:

ohc=> SELECT sample_id, is_wipe_positiv e(tblleadwipe.s ample_id) AS
positive FROM tblleadwipe WHERE hud_building_id IS NOT NULL;
sample_id | positive
-----------+----------
73 | f
81 | t
(2 rows)

I see that I cannot change my WHERE statement to WHERE positive = 't'
because the column positive does not exist. Now I am looking for the
best way to return all records that are found positive or negative using
a query. Can anyone offer any guidance to how I can return all the
positives (or negatvies)? Or do I need to write another function that
does that?

--
Robert

Any reason why:

SELECT sample_id, positive
FROM (SELECT sample_id,
is_wipe_positiv e(tblleadwipe.s ample_id) AS positive
FROM tblleadwipe
WHERE hud_building_id IS NOT NULL) foo
WHERE positive = 't';

won't work?

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match


Nov 23 '05 #2
Robert Fitzpatrick <ro****@webtent .com> writes:
SELECT sample_id, is_wipe_positiv e(tblleadwipe.s ample_id) AS
positive FROM tblleadwipe WHERE hud_building_id IS NOT NULL; I see that I cannot change my WHERE statement to WHERE positive = 't'
because the column positive does not exist.


What have you got against

SELECT sample_id, is_wipe_positiv e(tblleadwipe.s ample_id) AS positive
FROM tblleadwipe WHERE is_wipe_positiv e(tblleadwipe.s ample_id) = 't';

If you're concerned about the notational overhead of writing the
expression twice, you could use a sub-select:

SELECT sample_id, positive FROM
(SELECT sample_id, is_wipe_positiv e(tblleadwipe.s ample_id) AS positive
FROM tblleadwipe) AS sub
WHERE positive = 't';

Bear in mind though that the planner is quite likely to flatten this
into the same case as above; so this is only a notational device and
not a way to save any execution time.

If you are trying to avoid two evaluations of is_wipe_positiv e() per
row, there isn't any real clean way to do that, because standard SQL
doesn't have any concept of avoiding duplicate evaluations. There
are various ways you can fake out the Postgres planner --- at the moment
I'd suggest an OFFSET 0, viz

SELECT sample_id, positive FROM
(SELECT sample_id, is_wipe_positiv e(tblleadwipe.s ample_id) AS positive
FROM tblleadwipe OFFSET 0) sub
WHERE positive = 't';

to prevent the sub-select from being flattened into the outer query.
But realize that what you are doing here is disabling macro-optimization
in favor of your own ideas about micro-optimization. For example, if
you had an index on is_wipe_positiv e(tblleadwipe.s ample_id) then the
second form would very possibly perform far worse than the first,
because it could not use the index.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6447
by: point | last post by:
Hello there... I'm a PHP programmer and starting to learn JS... I have a following problem.... I have 3 select boxes! one is hotel one is destination and one is country... if someone clicks selects the country then the destination select box shows the destinations in that country and further if he chooses destination all
15
2739
by: grunar | last post by:
After some thought on what I need in a Python ORM (multiple primary keys, complex joins, case statements etc.), and after having built these libraries for other un-named languages, I decided to start at the bottom. What seems to plague many ORM systems is the syntactic confusion and string-manipulation required to build the SQL Statements. If you want to do a Left Outer Join, support nested functions, and a nested conditional clause, you'd...
4
1885
by: Alex | last post by:
Hi, I have a bit string , 7 bits, every bit representing a day of the week. e.g. 1110011. Is there and easy way where I can translate/format that string in a query. I want to give the string back with a '-' for every 0 and the first char of the Day for every '1'. example 1100111 = SM--TFS. thanks for any suggestions
6
26544
by: Terentius Neo | last post by:
Is it possible to combine (in DB2 UDB 8.1) a stored procedure and a select statement? I mean something like this: Select c.number, call procedure( c.number ) as list from table c With best regards
4
2827
by: Terencetrent | last post by:
I having been using Access '97/2002 for about 4 years now and have never really had the need or the time to learn visual basic. Well, I think the time has finally come. I need help with Visual Basic code that will examine numeric value for a particular field in a query, and assign a new numeric vaule to that field. There are over 21 possible values and I am told that IIF statement will only handle 9 of the possibilities and that I need...
2
14747
by: Zlatko Matiæ | last post by:
Hello. How to reference selected values from a multi-select list box, as a criteria in a query ? Is it possible at all? Regards, Zlatko
4
2475
by: Matt Ratliff | last post by:
Hello, I would appreciate any assistance you have with the following problem: I have (as an example) an array of values as follows: arrayvalues=new Array("0001","0003","0005") where each is the value of an option in a select statement: <select id="usertypes" multiple="multiple"> <option value="0033">data1</option>
1
6527
by: vj | last post by:
How i can populate all fileds dynamically in jsp page based on contents found in xml file? I have written jsp servlets and java class file. i transferred automatic data from jsp to servlet then to java class which creates a xml file based on values entered in dynamic jsp page. Now i want to read all those values entered to xml in my other jsp page. I am able to call values from file in my jsp page. But as dynamic values can be any in no...
0
4070
by: vijendra | last post by:
How i can populate all fileds dynamically in jsp page based on contents found in xml file?I have written jsp servlets and java class file. i transferred automatic data from jsp to servlet then to java class which creates a xml file based on values entered in dynamic jsp page. Now i want to read all those values entered to xml in my other jsp page.But as dynamic values can be any in no i don't know how could i populate all those in my jsp...
0
8777
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9237
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9184
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6737
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4813
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.