Обсуждение: string not equal query, postgresql 9.4.4

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

string not equal query, postgresql 9.4.4

От
John Scalia
Дата:
Hi all,

Having a bit of a head-scratching problem. I'm writing a query where I want to output only distinct rows where one
char(4)field does not equal a specific value. Something like: 

select distinct testname where result <> 'PASS";      #i.e., only the FAIL or WARN tests

I've tried several different variants like "!~", "not like ('PASS')", "is distinct from 'PASS'", and so forth, but
obviouslyI'm missing something as every row is being output not 
just the ones which do not equal PASS. What would be the correct syntax for this in a 9.4.4 database?
--
Jay



Re: string not equal query, postgresql 9.4.4

От
"David G. Johnston"
Дата:
On Wed, Sep 16, 2015 at 9:14 PM, John Scalia <jayknowsunix@gmail.com> wrote:
Hi all,

Having a bit of a head-scratching problem. I'm writing a query where I want to output only distinct rows where one char(4) field does not equal a specific value. Something like:

select distinct testname where result <> 'PASS";      #i.e., only the FAIL or WARN tests

I've tried several different variants like "!~", "not like ('PASS')", "is distinct from 'PASS'", and so forth, but obviously I'm missing something as every row is being output not
just the ones which do not equal PASS. What would be the correct syntax for this in a 9.4.4 database?
--
Jay

WITH vals (v) AS (
VALUES ('PASS'::char(4)), ('FAIL'::char(4))
)
SELECT
​DISTINCT ​
*
FROM vals
WHERE v <> 'PASS'::char(4);

​Since this is basically what you did you apparently either do not understand your data completely or you have failed to convey necessary information to the audience whom you are asking for help.

David J.

Re: string not equal query, postgresql 9.4.4

От
Albe Laurenz
Дата:
John Scalia wrote:
> Having a bit of a head-scratching problem. I'm writing a query where I want to output only distinct
> rows where one char(4) field does not equal a specific value. Something like:
> 
> select distinct testname where result <> 'PASS";      #i.e., only the FAIL or WARN tests
> 
> I've tried several different variants like "!~", "not like ('PASS')", "is distinct from 'PASS'", and
> so forth, but obviously I'm missing something as every row is being output not
> just the ones which do not equal PASS. What would be the correct syntax for this in a 9.4.4 database?

I observe that your SQL query is syntactically wrong (closing double quote)
and the FROM clause is missing, but apart from that it should work fine.

Could it be that there are spaces or other invisible characters in the "result" attribute?

What do you get for
  SELECT DISTINCT '|' || result || '|' FROM <table> WHERE result <> 'PASS';

Yours,
Laurenz Albe

Re: string not equal query, postgresql 9.4.4

От
John Scalia
Дата:
I'm not in front of that server at the moment, so I can't test anything else, but I should have explained that the result field is char(4) not null, and all the values in it are length(4), so no padding should exist. I'll try some of these once I get back home today, but what had me concerned is if I entered result = 'PASS', as opposed to anything like not equal, the query worked as expected, and Albe, I believe the wrong syntax was just my typo from reentering it for the email. Also, David, the reason I was I searching for anything other than the value 'PASS' is the field has values of 'FAIL' or a 4 digit hex code produced by the instrumentation generating the data. I sure didn't want to have to list every possible 4 digit hex value that is a possibility. Sorry I wasn't very clear in my first posting. I had been struggling with this for some time, and my temper was a little short.

On Thu, Sep 17, 2015 at 3:21 AM, Albe Laurenz <laurenz.albe@wien.gv.at> wrote:
John Scalia wrote:
> Having a bit of a head-scratching problem. I'm writing a query where I want to output only distinct
> rows where one char(4) field does not equal a specific value. Something like:
>
> select distinct testname where result <> 'PASS";      #i.e., only the FAIL or WARN tests
>
> I've tried several different variants like "!~", "not like ('PASS')", "is distinct from 'PASS'", and
> so forth, but obviously I'm missing something as every row is being output not
> just the ones which do not equal PASS. What would be the correct syntax for this in a 9.4.4 database?

I observe that your SQL query is syntactically wrong (closing double quote)
and the FROM clause is missing, but apart from that it should work fine.

Could it be that there are spaces or other invisible characters in the "result" attribute?

What do you get for
  SELECT DISTINCT '|' || result || '|' FROM <table> WHERE result <> 'PASS';

Yours,
Laurenz Albe

Re: string not equal query, postgresql 9.4.4

От
John Scalia
Дата:
On 9/17/2015 7:54 AM, Albe Laurenz wrote:
> John Scalia wrote:
>>>> Having a bit of a head-scratching problem. I'm writing a query where I want to output only distinct
>>>> rows where one char(4) field does not equal a specific value. Something like:
>>>>
>>>> select distinct testname where result <> 'PASS";      #i.e., only the FAIL or WARN tests
>>>>
>>>> I've tried several different variants like "!~", "not like ('PASS')", "is distinct from 'PASS'", and
>>>> so forth, but obviously I'm missing something as every row is being output not
>>>> just the ones which do not equal PASS. What would be the correct syntax for this in a 9.4.4 database?
>>> Could it be that there are spaces or other invisible characters in the "result" attribute?
>> I'm not in front of that server at the moment, so I can't test anything else, but I should have
>> explained that the result field is char(4) not null, and all the values in it are length(4), so no
>> padding should exist. I'll try some of these once I get back home today, but what had me concerned is
>> if I entered result = 'PASS', as opposed to anything like not equal, the query worked as expected, and
>> Albe, I believe the wrong syntax was just my typo from reentering it for the email. Also, David, the
>> reason I was I searching for anything other than the value 'PASS' is the field has values of 'FAIL' or
>> a 4 digit hex code produced by the instrumentation generating the data. I sure didn't want to have to
>> list every possible 4 digit hex value that is a possibility. Sorry I wasn't very clear in my first
>> posting. I had been struggling with this for some time, and my temper was a little short.
> Well, it works for me:
>
> test=> CREATE TABLE test (id integer PRIMARY KEY, val character(4));
> CREATE TABLE
> test=> INSERT INTO test VALUES (1, 'PASS');
> INSERT 0 1
> test=> INSERT INTO test VALUES (2, 'FAIL');
> INSERT 0 1
> test=> INSERT INTO test VALUES (3, 'PASS');
> INSERT 0 1
> test=> INSERT INTO test VALUES (4, 'STHG');
> INSERT 0 1
> test=> SELECT * FROM test WHERE val <> 'PASS';
>  id | val
> ----+------
>   2 | FAIL
>   4 | STHG
> (2 rows)
>
> Please double check your data.
> Are they all ASCII? Any chance that there might be weird characters?
>
> Yours,
> Laurenz Albe
Thanks Laurenz,

I'm believing something here may be something incorrect with my setup, but here's the output from \d+ results

Table "public.results"
Column       |                 Type               | Modifiers | Storage     | Stats target | Description
instrument | character varying(10)  | not null     | extended |                       |
date             | character varying(15)  | not null     | extended |                       |
result           | character(4)                  | not null     | extended |                       |

The table has six rows:

Instrument |        date        | result
-----------------+-------------------+----------
visc100        | 01/01/2015 | PASS
visc60          | 01/01/2015 | FAIL
visc60pre    | 01/01/2015 | 6FB3
visc60post  | 01/01/2015 | 7F5A
density        | 01/01/2015 | PASS
base             | 01/01/2015 | PASS

The last query I ran was:

SELECT * FROM results where result <> 'PASS';

and it produced all the rows, not just the ones beginning with visc60 that I expected. Based on what you had written, I
shouldhave seen the correct output. So, any ideas? 
--
Jay



Re: string not equal query, postgresql 9.4.4

От
"David G. Johnston"
Дата:
On Thu, Sep 17, 2015 at 6:18 PM, John Scalia <jayknowsunix@gmail.com> wrote:

SELECT * FROM results where result <> 'PASS';

and it produced all the rows, not just the ones beginning with visc60 that I expected. Based on what you had written, I should have seen the correct output. So, any ideas?

​EXPLAIN ANALYZE SELECT * FROM results WHERE result <> 'PASS';

What happens when you run the following?  Do you see the same incorrect behavior?

WITH vals (v) AS (
VALUES ('PASS'::char(4)), ('FAIL'::char(4))
)
SELECT 
​DISTINCT ​
*
FROM vals
WHERE v <> 'PASS'::char(4);

David J.​
 

Re: string not equal query, postgresql 9.4.4

От
John Scalia
Дата:
On 9/17/2015 6:49 PM, David G. Johnston wrote:
On Thu, Sep 17, 2015 at 6:18 PM, John Scalia <jayknowsunix@gmail.com> wrote:

SELECT * FROM results where result <> 'PASS';

and it produced all the rows, not just the ones beginning with visc60 that I expected. Based on what you had written, I should have seen the correct output. So, any ideas?

​ EXPLAIN ANALYZE SELECT * FROM results WHERE result <> 'PASS';

What happens when you run the following?  Do you see the same incorrect behavior?

WITH vals (v) AS (
VALUES ('PASS'::char(4)), ('FAIL'::char(4))
)
SELECT 
​ DISTINCT ​
*
FROM vals
WHERE v <> 'PASS'::char(4);

David J.​
 
I tried your code above, and some additional queries prior to your message arriving, and I see now why I'm still getting multiple row beyond my original expecting. Carefully looking at the rows being produced showed that each instrument is performing a powerup test and all those passed. I guess I was just expecting too much from distinct and I understand why. I just need to add a second where clause if I can figure out how to make the powerup test clearly indicated as such in the table.

Thanks for everyone's help, I should have examined the automated data collection more closely.
--
Jay

Re: string not equal query, postgresql 9.4.4

От
Albe Laurenz
Дата:
John Scalia wrote:
> I'm believing something here may be something incorrect with my setup, but here's the output from \d+
> results
> 
> Table "public.results"
> Column       |                 Type               | Modifiers | Storage     | Stats target |
> Description
> instrument | character varying(10)  | not null     | extended |                       |
> date             | character varying(15)  | not null     | extended |                       |
> result           | character(4)                  | not null     | extended |                       |
> 
> The table has six rows:
> 
> Instrument |        date        | result
> -----------------+-------------------+----------
> visc100        | 01/01/2015 | PASS
> visc60          | 01/01/2015 | FAIL
> visc60pre    | 01/01/2015 | 6FB3
> visc60post  | 01/01/2015 | 7F5A
> density        | 01/01/2015 | PASS
> base             | 01/01/2015 | PASS
> 
> The last query I ran was:
> 
> SELECT * FROM results where result <> 'PASS';
> 
> and it produced all the rows, not just the ones beginning with visc60 that I expected. Based on what
> you had written, I should have seen the correct output. So, any ideas?

Not really...

What does the execution plan look like:
  EXPLAIN (VERBOSE, COSTS off) SELECT * FROM results where result <> 'PASS';

What do you get for
  SHOW lc_collate;
  SHOW lc_ctype;
Maybe some problem with your locale...

Yours,
Laurenz Albe