bitasd
5/7/2018 - 5:55 PM

sql_commands.sql

## update column of a table:
update matract_homes_07595_sum set pop100_sum_ =
   
      pop100_sum / NULLIF (
         numb_homes,0);
         
## query with multiple conditions
```sql
create table as select * from table_name where "age">=1 and "year"<1996;
```
## choosing random lines from a table in postgresql :https://stackoverflow.com/questions/580639/how-to-randomly-select-rows-in-sql
```sql 
SELECT id,cnt FROM table ORDER BY RANDOM() LIMIT 100;

## fast estimate og rows number
SELECT reltuples AS approximate_row_count FROM pg_class WHERE relname = 'table_name';

## Indexing
```sql
create index idx_id on houston(id);
```

## exporting a table to a csv with headers:
\COPY products_273 TO '/tmp/products_199.csv' DELIMITER ',' CSV HEADER;

### functions on a chunk of data:
select sc.number_visit, sc.dist, (sc.number_visit * (sc.dist)^2) as dist2xn from (select * from cluster30_home_dist limit 5) sc;

### Rg
create table houston_cluster30_rg as (select sc.user_id,(sum(sc.dist2xn)/sum(sc.number_visit))^.5 as rg from (select * from cluster30_home_dist) sc where sc.di
st>10 group by sc.user_id);