Select rows with multiple columns of the same value

With a table table1 with columns like: name, dept, col1, col2, col3, col4, col5 I need to find rows with same values across multiple columns - e.g. col2, col3, col4, col5 While something like below would work, I need an approach that would work even

Check the unique data for multiple lines in PostgreSQL

I have, lets say user name unique and I need to insert many users at one query (It could be thousands of users). What would be the best approach to check for each user if the username already exists? Using PostgreSql.You can use on conflict clause, e

Rails: Displaying data in a crosstab

I'm building an expenditures diary and therefore need a crosstab in my view. The app has an expenditures table. Every record in that table is a single expenditure holding (amongst others) "Value" (as integer), "Day" (as date) and "

Monthly balance Postgres

I'm using postgreSQL 9.2 I have a table with transactions that affect an account startbalance and I want to create a query that generates the monthly balances. It seems simple, but I'm stuck and the closest thing I can find might be this Calculate Mo

Nearest day in the sql table

I'm trying to find a record and order it by the closets day based on the current day. Let me try to illustrate with an example. Say that John wants to find out when he has to teach next. John teaches the following days (days are converted to numbers,

Active record - Where IN with multiple columns

I have a query that needs to fetch from a table that meet two columns requirements exactly. So if I have users table with columns, age and score. SELECT * FROM users where (age, score) IN ((5,6), (9,12), (22,44)..) In my web app I am getting this pai

how to cook in a number of records in a sql query

I have a query that looks like this: select id, extension, count(distinct(id)) from publicids group by id,extension; This is what the results looks like: id | extension | count -------------+-------------------------+------- 18459154909 | 12333 | 1 1

Postgresql substring using regex

I need to run a postgresql query to get names from database but I need to sort these names alphabetically. The names that I am gettign from database are as follows: (123) Jone Lee (22) Hans Hee 2 Dean Alloni Alen Khan I need to output to be Alen Khan

Update column in another table with combined columns

So I have two tables: surveys and survey_ids, where I'm essentially migrating some of the data from survey_ids into surveys. 'surveys' has one column called dist_string which should ideally be the combination of the columns 'dist_admin' and 'dist_use

How can I link a Google Spreadsheet to PostgreSQL?

How can I link a Google spreadsheet to PostgreSQL? I googled and got some sample code for MySQL, and a couple of products which do it. As per this link ,support exists for a few databases.You can also use our self service reporting tool - Kloudio to

Could not load postgresql on Mac OS startup

I have installed postgresql via HomeBrew. It worked but today I find that postgresql server does not run when computer start. I try to use launchctl to load it again with below command: $ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql

gem install pg does not work during bundle / RoR installation

edit What worked for me was running: brew doctor I then had to chown a few files and delete a few things it asked me to. I then ran sudo gem install pg and it worked. Not sure why it only worked with 'sudo' but when I tried before without it failed I

How are composite keys evaluated?

Are composite keys guaranteed to be unique as long as the individual values of the columns it consists of are unique (as in the column values are separately evaluated), or is it the resulting value (as in a concatenation of the column values) that ma

update the table data automatically every year

I have a data base table containing a id, description, start_date, end_date there are 12 entries for every month i just want to increment year 2012 to 2013 in every start_date, end_date field when this year end is it possible when a super user login

A good strategy for writing a database abstraction layer in C

I need to write a database abstraction layer in C for the database access API of PostgreSQL (libpq) and possibly some other relational database management system. I am considering writing my own abstraction of the existing functions in pqsql and poss

Failed to connect to the postgresql Rails database

I'm very new to Ruby and postgres. Below is my database.yml development: adapter: postgresql database: test_database username: postgresql password: mypassword host: localhost encoding: utf8 The user exists and I'm, able to login using same credential

PostgreSQL error that I can not understand

I have this SQL Query which should find the rank of a certain cook. I should compute the number of recipes this cook has made, compute the number of other cooks recipes, and the final goal is to get our cook ranking, which is the number of cooks who

Postgresql, Using the index with between

I have index on a column, let's say ID (bigint). If I have a query with something like this: SELECT * FROM table WHERE id = 12345 ...it will use index. But when I'm using query like... SELECT * FROM table WHERE id >= 12345 AND id <= 12366 It use seq