Home Create dummy variables in PostgreSQL
Reply: 0

Create dummy variables in PostgreSQL

user1184
1#
user1184 Published in April 26, 2018, 1:43 pm

Is it possible to create a dummy variable when querying

For instance the query below will give me only the observations that satisfy the var1 conditions. I also want the remaining observations but with some kind of tag on it (0/1, indicator values would be sufficient)

SELECT distinct ON (id) id,var1,var2,var3 
FROM table 
where var2 = ANY('{blue,yellow}');

Have

    +-----+------+--------+------+
    | id  | Var1 |  Var2  | Var3 |
    +-----+------+--------+------+
    | 345 |   12 | Blue   | 3456 |
    | 345 |   12 | Red    | 2134 |
    | 346 |   45 | Blue   | 3451 |
    | 347 |   25 | yellow | 1526 |
    +-----+------+--------+------+

Want

+-----+------+--------+------+--------------------+
| id  | Var1 |  Var2  | Var3 | Indicator variable |
+-----+------+--------+------+--------------------+
| 345 |   12 | Blue   | 3456 |                  1 |
| 345 |   12 | Red    | 2134 |                  0 |
| 346 |   45 | Blue   | 3451 |                  1 |
| 347 |   25 | yellow | 1526 |                  1 |
+-----+------+--------+------+--------------------+
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.397592 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO