spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

SQL return a default value if a row is not found [PostgreSQL]


The outer join will already take care of what you want.

As t1 is the "left table" of the join, all rows from t1 will be returned. Columns from the "right table" (t2 in your example) will then have a null value. So you only need to convert that null to a 0:

select t1.tag_id, coalesce(t2.cnt, 0)
from T1 t1 
    left join T2 t2 on t1.tag_Id=t2.tag_id 
and t1.place_id = 1;

SQLFiddle example: http://sqlfiddle.com/#!12/ed7bf/1


Unrelated but:

Using count as a column name is a really bad idea, because it will require you to always enclose the column name in double quotes: t2."count" because it is a reserved word. Plus it doesn't really document the purpose of the column. You should find a better name for that.


Categories : SQL

Related to : SQL return a default value if a row is not found [PostgreSQL]
MySQL - Way to set a default return value when NULL is found?
Use COALESCE() function like below, it will return the first non NULL value provided in list COALESCE(col_name, 'default_value') For your case, do COALESCE(region_id, c.name) I think, you are specifically talking about the part concat( r.name, ', ', c.name ) as location You can modify this using CASE expression as well case when r.name is not null and c.name is not null then concat( r

Categories : Mysql
Which way would you suggest to return default value that is not null?
Split the method into two cases: string TryGetImageName() { if (GetImageNameFunc != null) { string imageName = GetImageNameFunc(); if (!string.IsNullOrEmpty(imageName)) { return imageName; } } return null; //no default! } string GetImageNameOrDefault() { return TryGetImageName() ?? "default.png"; } Your method was doing two things at

Categories : C#
sbt scalatest returning '0 test class found in package '''
I had the same problem a while ago. I got a fix by running 'sbt test' from the command line for the first go (in the project directory ofcourse). After this the IntelliJ didn't have any problem in identifying the package and ran the test.

Categories : Scala
Couchapp - default template not found when running `couchapp generate ...` on Windows
OK, I got it working - all that was needed was to include an environment variable for couchapp.exe. So, if you are installing a standalone version of Couchapp from the downloads section ( https://github.com/downloads/couchapp/couchapp/couchapp-1.0.0-win.zip, e.g. ) you NEED TO MAKE SURE that you first of all execute the installer and then point you environment variables Path to it, that's all. Hop

Categories : Misc
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console
Changing the last pathelement to: <pathelement location="${src.dir}/" /> will work, but in practice you should probably have this file somewhere else. In an ant build it's normal to copy these files into the classes directory so it gets included in the jar file.

Categories : Java
Recently Add
Use REGEXP_REPLACE to replace variable number of characters with same number of constant characters
How to subtract a data with nano-second in a same group
listagg alternative in Oracle
How to join 3 tables to a single table in SQL
Postgresql: Trying to Understand the Information Schema Table Constraints Info for a NOT NULL Contstraint
Data Frame in R use like SQL, possibly using sqldf()
How can I group by a table by the date?
Query slows down in Conditional IF
Getting the daily sales report given the date
Retrieve rows with unique values in a column
case sensitive sql search in vb.net
Oracle multi dimensional query
Update column when datediff is greater then other column in the same table
add value from different tables and insert total value into another table
How to prevent transaction locking in sql server for all the connections?
Check the query efficiency
how can I run this select statement inside a select statement without any problems?
Add amount once on duplicate entries
SQL Server 2012 - Unique Priority Column
Query: employees who do not have the same name
Trigger to not allow a member to rent if unpaid balance exceeds $50
Anyway way to change this Oracle SQL statment to use regs?
If you set a field name as an Alias, can you utilize that name within SQL code?
Increment next month with dayOfTheMonth SQL sybase
Update Table Set From is not working as expected
How do I find one matching strings in two txt files
SQL help needed (oracle application express)
How to Roll Up Weekending Data in SQL
How do you call a variable (that is a list of values) in a conditional statement?
Oracle INSTR backward in Oracle SQL
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.