select with multiple occurences in same table

Поиск
Список
Период
Сортировка
Искать

select with multiple occurences in same table

От:
"William Herring" <wherring@mail.ifas.ufl.edu>
Дата:

      Table "inventorytrack"

 Attribute  |   Type    | Modifier

------------+-----------+----------

 tattoo     | text      |

 past_group | text      |

 ranch      | text      |

 ent_date   | timestamp |

 indate     | date      |

 

 

In the above table, I have multiple occurrences of ‘tattoo’.  However, some of the tattoo’s have null value in ‘indate’, some do not.  I would like to select those tattoos with a null indate, unless there is another identical tattoo in the table that has  a date value in  ‘indate’.  For example, I might have:

 

tattoo |   past_group   | ranch  |        ent_date        |   indate

--------+----------------+--------+------------------------+------------

 1039   | Barn2/Behind   | Iberia | 2000-03-16 11:03:32-08 |

 1039   | Across/Bubs    | Iberia | 2000-12-28 14:57:13-08 | 2000-12-28

 1832   | GardnerBlock#3 | Iberia | 2000-03-16 11:03:32-08 |

1832     | GardnerBlock#2 | Iberia | 2000-12-28 12:46:57-08 | 2000-12-28

1833     | Becker1            | Iberia | 2000-03-16 11:03:32-08 |

 

 

 

 

So the prospective SELECT would give back:

 

tattoo |   past_group   | ranch  |        ent_date        |   indate

1039   | Across/Bubs    | Iberia | 2000-12-28 14:57:13-08 | 2000-12-28

1832| GardnerBlock#2 | Iberia | 2000-12-28 12:46:57-08 | 2000-12-28

1833| Becker1            | Iberia | 2000-03-16 11:03:32-08 |

 

Any suggestions on how to do this?

Re: select with multiple occurences in same table

От:
"Josh Berkus" <josh@agliodbs.com>
Дата:
WIlliam,

>       Table "inventorytrack"
>  Attribute  |   Type    | Modifier
> ------------+-----------+----------
>  tattoo     | text      |
>  past_group | text      |
>  ranch      | text      |
>  ent_date   | timestamp |
>  indate     | date      |
> 

Are you sure that you're using the most efficient data types for this
table?  And where's your primary key?


> In the above table, I have multiple occurrences of ‘tattoo’.
> However, some
> of the tattoo’s have null value in ‘indate’, some do not.  I would
> like to
> select those tattoos with a null indate, unless there is another
> identical
> tattoo in the table that has  a date value in  ‘indate’.

THis one is easy.  However, I'm not sure exactly waht you want; the
sample return tuples you provided do not match what you asked for!

If you wanted to answer the question above:

SELECT * FROM inventorytrack
WHERE indate IS NULL
AND NOT EXISTS (SELECT tattoo FROM inventorytrack it2WHERE it2.tattoo = inventorytrack.tattoo	AND indate IS NOT NULL );

-Josh Berkus



______AGLIO DATABASE SOLUTIONS___________________________                                      Josh Berkus Complete information technology      josh@agliodbs.com  and data management solutions       (415) 565-7293 for law firms, small businesses        fax 621-2533   and non-profit organizations.      San Francisco

pl/pgSQL and escaping LIKE clauses

От:
"Robby Slaughter" <webmaster@robbyslaughter.com>
Дата:
I'm having trouble getting LIKE clauses to work correctly inside a plpgSQL 
function. 

Here's my table:

 id |  val
----+-------------
  1 | hello
  2 | there
  3 | everyone


Here's my function:

CREATE FUNCTION intable(char)
RETURNS INTEGER
AS
'
DECLARE 
  input ALIAS FOR $1;
  temp  INTEGER;
BEGIN
  SELECT INTO temp id FROM test WHERE val LIKE ''input%'';
  RAISE NOTICE ''Value of temp is %'',temp;
  RETURN temp;
END;
'
LANGUAGE 'plpgsql';

I should be able to SELECT('hello') and get back 1, correct?

No matter what I put in as a parameter, it always returns null.

If I change the LIKE clause to read "...LIKE ''hello%''" it does
in fact work. Or if I scrap the LIKE clause and have it 
read something such as ".... id = input" (if input is an integer)
it also works fine.

Any thoughts?

Thanks,
Robby
FAQ