Skip to content

Tag: postgresql

Subquery on SELECT with conditional WHERE clause after it

So I have three tables, news : topics : and news_topics : And I want to return it like these : I succeed on returning that value using this query But there is one flaw to that query, on the above result there are one row that contains null topics. When I add AND topics IS NOT NULL after it

PostgreSQL select record with multiple condition

I am trying to select the record but not getting the record I wanted. here is my sqlfiddle http://sqlfiddle.com/#!17/5296a/1 so when I selects the record using below query it gives me result with the title DDDDD. it should give me GGGGGGGG. I wanted to check if there is any record with author_id=3 and greater…

Define enum as the column names of table PostgreSQL

Is there a way to define an enum using a table’s column names? I have a users table defined as follows: And then I want to define a required_profile_fields column in another table to be an enum containing only the column names in that public.users table: Is there a way that I can define that profile_fie…

Postgresql SELECT sample with FOR loop

I have a task where I need to create a 1000 sample row limit query for each attribute selected. So for example if I have: I would need to turn this in to: Problem is that I have a lot of these attributes and that I might be adding more attributes later. Is there a better way of writing this?

Group by month and name SQL

I need some help with SQL. I have Table1 with columns Id, Date1 and Date2 Table2 with columns Table1Id and Table2Id Table3 with columns Id and Name Here is my try: I need to take Count(date2)/Count(date1) grouped by monthes and name. I have no idea how to do that, as there is no table with monthes. DB –…