简体   繁体   English

联合查询以合并3个表的结果

[英]Union query to combine results of 3 tables

I am relatively new to coding so please have patience. 我是编码的新手,请耐心等待。

I am trying to combine data from 3 tables. 我正在尝试合并3个表中的数据。 I have managed to get some data back but it isn't what i need. 我设法找回了一些数据,但这不是我所需要的。 Please see my example below. 请在下面查看我的示例。

select oid, rrnhs, idnam, idfnam, dte1, ta  
  as 'access type' from person
left join 
  (select fk_oid, min(dte), dte1, ta
   from 
     ((Select fk_oid,min(accessdate) as dte, accessdate1 as dte1, accesstype as ta 
        from vascularpdaccess 
        where isnull(accesstype)=false group by fk_oid)
union
 (Select fk_oid, min(hpdate) as dte, hpdate as dte1, HPACCE as ta 
   from hdtreatment 
   where isnull(hptype)=false group by fk_oid)) as bla
 group by fk_oid) as access
 on person.oid=access.fk_oid
 where person.rrnhs in (1000010000, 2000020000, 3000030000)

My understanding with a union is that the columns have to be of the same data type but i have two problems. 我对联合的理解是,列必须具有相同的数据类型,但是我有两个问题。 The first is that accesstype and hpacce combine in to a the same column as expected, but i dont want to actually see the hpacce data (dont know if this is even possible). 第一个是accesstypehpacce合并到同一列中,如预期的那样,但我不想实际看到hpacce数据(不知道这是否可能)。 Secondly, the idea of the query is to pull back a patients 'accesstype' date at the first date of hpdate . 其次,查询的想法是在第一次约会给拉了回来一个病人的接入类型“日期hpdate

I dont know if this even makes sens to you guys but hoping someone can help..y'all are usually pretty nifty! 我不知道这是否对你们都有意义,但希望有人能提供帮助。.你们通常都很漂亮!

Thanks in advance! 提前致谢!

Mikey 米奇

All queries need to have the same number of columns in the SELECT statement. 所有查询在SELECT语句中必须具有相同的列数。 It looks like you first query has the max number of columns, so you will need to "pad" the other to have the same number of columns. 看起来您的第一个查询具有最大的列数,因此您将需要“填充”另一个以具有相同的列数。 You can use NULL as col to create the column with all null values. 您可以使用NULL as col来创建具有所有空值的列。

To answer the question (I think) you were asking... for a UNION or UNION ALL set operation, you are correct: the number of columns and the datatypes of the columns returned must match. 要回答这个问题(我认为),您是……对于UNIONUNION ALL设置操作,您是正确的:列数和返回的列的数据类型必须匹配。

But it is possible to return a literal as an expression in the SELECT list. 但是可以在SELECT列表中将文字作为表达式返回。 For example, if you don't want to return the value of HPACCE column, you can replace that with a literal or a NULL. 例如,如果您不想返回HPACCE列的值, HPACCE可以将其替换为文字或NULL。 (If that column is character datatype (we can't tell from the information provided in the question), you could use (for example) a literal empty string '' AS ta in place of HPACCE AS ta . (如果该列是字符数据类型(我们不能从问题中提供的信息中看出),则可以使用(例如)文字空字符串'' AS ta代替HPACCE AS ta

                     SELECT fk_oid
                          , MIN(HPDATE)   AS dte
                          , hpdate        AS dte1
                          , NULL          AS ta
    -- -------------------- ^^^^
                       FROM hdtreatment

Some other notes: 其他注意事项:

The predicate ISNULL(foo)=FALSE can be more simply expressed as foo IS NOT NULL . 谓词ISNULL(foo)=FALSE可以更简单地表示为foo IS NOT NULL

The UNION set operator will remove duplicate rows. UNION集运算符将删除重复的行。 If that's not necessary, you could use a UNION ALL set operator. 如果没有必要,可以使用UNION ALL set运算符。

The subsequent GROUP BY fk_oid operation on the inline view bla is going to collapse rows; 内联视图bla上的后续GROUP BY fk_oid操作将折叠行; but it's indeterminate which row the values from dte1 and ta will be from. 但它是不确定的,从价值哪一dte1ta将从。 (ie there is no guarantee those values will be from the row that had the "minimum" value of dte .) Other databases will throw an exception/error with this statement, along the lines of "non-aggregate in SELECT list not in GROUP BY". (即,不能保证那些值将来自具有dte的“最小”值的行。)其他数据库将使用此语句,沿着“ SELECT列表中未聚合,不在GROUP中的非聚合”行抛出异常/错误。通过”。 But this is allowed (without error or warning) by a MySQL specific extension to GROUP BY behavior. 但这是MySQL特定于GROUP BY行为的扩展所允许的(没有错误或警告)。 (We can get MySQL to behave like other databases and throw an error of we specify a value for sql_mode that includes ONLY_FULL_GROUP_BY (?).) (我们可以使MySQL像其他数据库一样工作,并抛出错误,即为sql_mode指定一个包含ONLY_FULL_GROUP_BY(?)的值。

The predicate on the outer query doesn't get pushed down into the inline view bla . 外部查询的谓词不会被下推到内联视图bla The view bla is going to materialized for every fk_oid , and that could be a performance issue on large sets. 对于每个fk_oid ,视图bla都将fk_oid ,这在大型集合上可能是性能问题。

Also, qualifying all column references would make the statement easier to read. 此外,限定所有列引用的内容将使该语句更易于阅读。 And, that will also insulate the statement from throwing an "ambiguous column" error in the future, when a column named (eg) ta or dte1 is added to the person table. 而且,当将名为(例如) tadte1的列添加到person表时,这也可以防止语句将来引发“歧义列”错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM