coalesce

Concatenate multiple rows from multiple tables

痞子三分冷 提交于 2019-12-13 11:35:36
问题 I've reviewed many other posts on here and have become pretty familiar with the Coalesce function, but I haven't been able to figure out how to do this specific task. So, I have a Commissions table and a Categories table. I've created a gist here so you can see the exact data structure with some example data. Basically, the Commission table has a SalesRepID, LocationID, CategoryID, SurgeonID, and CommissionPercent column. Using a Coalesce function, I've been able to get something like this by

SQL Server: Combine multiple rows into one row

寵の児 提交于 2019-12-13 06:59:09
问题 I've looked at a few other similar questions, but none of them fits the particular situation I find myself in. I am a relative beginner at SQL. I am writing a query to create a report. I have read-only access to this DB. I am trying to combine three rows into one row. Any method that only requires read access will work. That being said, the three rows I have, were obtained by a very long sub-query. Here is the outer shell: SELECT Availability, Start_Date, End_Date FROM ( -- long subquery goes

DB2 Coalesce function is returning nulls

点点圈 提交于 2019-12-13 05:21:00
问题 I am using DB2 for IBM i V6R1, and I am trying to convert a string value which sometimes has a valid representation of a number in it into a number. What I came up with was this: select onorno, onivrf, coalesce(cast(substr(onivrf,1,5) as numeric),99999) as fred from oinvol sometimes the ONIVRF field has data like '00111-11', sometimes it has data like 'FREIGHT'. The documentation leads me to believe that for data like this: ONORNO ONIVRF 12 11010-11 13 FREIGHT 14 00125-22 I should get output

COALESCE all fields in different tables JOIN

余生长醉 提交于 2019-12-13 05:19:30
问题 I have 2 tables: Table 1 (with ENGLISH content): id (primary key, auto increment) title text ... Table 2 (with PORTUGUESE content): id (foreing Key to table 1 ID) title text … Contents example: Table 1 (with ENGLISH content): id: 4 title: Hello World! text: hello world, i need you! … : anothers fields Table 2 (with PORTUGUESE content): id: 4 title: Ola mundo text: NULL …: another fields with NULL values OR portuguese contents Perfect result: Result: id: 4 title: Ola mundo text: hello world, i

How to avoid NULL when using Value-Name Mapping in SQL

萝らか妹 提交于 2019-12-13 03:44:32
问题 I have a table like the following which is basically used to "give a name" to a value in a table (this table contains values for a bunch of other tables as well, not just for MYTABLE; I've omitted a few irrelevant fields from NAMEVALUEMAP): NAMEVALUEMAP Table --------------------- VALUE_ | NAME_ --------------------- 0 | ZERO 1 | ONE I didn't want to use JOINs so I thought of using Sub-Queries. Problem is when a value does not exist in the NAMEVALUEMAP table then NULL is shown. Instead of

Group by to create a vertical coalesce

旧城冷巷雨未停 提交于 2019-12-13 01:27:35
问题 I have this query. select transaction, bbk, sbk, obk, ibk from ( select transaction, case when "'BBK'" = 1 then country end bbk, case when "'SBK'" = 1 then country end sbk, case when "'OBK'" = 1 then country end obk, case when "'IBK'" = 1 then country end ibk from ( select regexp_substr("col_a", '[^~]+', 1, 1) as transaction, regexp_substr("col_a", '[^~]+', 1, 2) as code, regexp_substr("col_a", '[^~]+', 1, 3) as country from Table1 t) pivot ( --case when count(code) = 1 then count(code) for

ORDER BY with columns that are sometimes empty

跟風遠走 提交于 2019-12-12 07:59:59
问题 My SQL looks something like this: SELECT CompanyName , LastName , FirstName FROM ... JOIN ... ORDER BY CompanyName , LastName , FirstName Now the problem is that column A is sometimes empty (either as NULL or "" ), and I don't want all those results to turn up in the end. In the example, I'd like to have the fourth entry (which starts with a C) to be the third. But if I just ORDER BY, this happens: Avagax Bauer Frank Bele AG Smith John Mork AG Baggins Frodo Chen Jun In addition, I sometimes

Coalesce must take at least two expressions

五迷三道 提交于 2019-12-11 17:14:25
问题 I'm using Django and Python 3.7. I want to write a Coalesce expression to help me write a larger Django query. I have Coalesce( F("votes") - Subquery(relevant_hour_stats.values('votes_threshold')[:1]), output_field=models.FloatField()) Here is the expression in context ... qset = ( ArticleStat.objects .all() .annotate( shifted_article_create_hour=ExtractHour(ExpressionWrapper( F('article__created_on') + timedelta(seconds=avg_fp_time_in_seconds), output_field=models.DateTimeField() )) )

MySql - Retrieve correct values using COALESCE?

[亡魂溺海] 提交于 2019-12-11 10:22:00
问题 I have an sql fiddle here: http://www.sqlfiddle.com/#!2/fbd48/1 The colors should be red for flower and brown for bear, but it shows red for both. Not sure if COALESCE fits here, but was in the accepted answer for something similar here: MySQL - Retrieve row value from different table depending on value of row in a table 回答1: SELECT mem.member_name, g.* , coalesce(f.flower_color, b.bear_color) as color from members mem inner join general g on mem.member_id = g.member_id left join flowers f on

MySQL query - how to return placeholder value instead of NULL if no entry found

寵の児 提交于 2019-12-11 09:35:06
问题 Let's say I have a table called references, which has two fields: an id and a reference field. I want to create a query that will provide me with a reference number based upon an id. Like this: SELECT reference FROM references WHERE id = x (where x is some integer) However if the id is not found in the table I would like the query to show -1 instead of NULL. How can I do this? SELECT COALESCE(reference, -1) FROM references WHERE id = x doesn't work 回答1: Here are a few approaches: SELECT