简体   繁体   English

SQL Server:联接两行。 第一列的所有列和第二列的第一列使用别名

[英]SQL Server : Join two rows. All columns from the 1st and 1 column from the second using an alias

This marks the first time I ask a question on stack overflow, and probably the 5000th time i've visited the site. 这标志着我第一次问堆栈溢出问题,大概是我访问该站点的第5000次。 So first off, thanks for all your hard work! 首先,感谢您的辛勤工作!

So I have a basic select query on a single table that returns two rows of similar data and are linked via a shared PK. 因此,我在单个表上有一个基本的选择查询,该查询返回两行相似的数据,并通过共享的PK进行链接。

I want to retrieve all fields from the first row, and only one of the columns from the second under an alias. 我想从第一行中检索所有字段,并从第二条中检索别名下的仅一列。

Basically flattening the two records into one but only using one of the columns from the second row. 基本上将两个记录拼合为一个记录,但仅使用第二行中的一列。

OK Here is a screenshot. 确定这是屏幕截图。

http://www.flickr.com/photos/imagevault/8581053528/ http://www.flickr.com/photos/imagevault/8581053528/

Looking at the first results window I want the Second "Comp" value to show up as an additional column on the first row as a "RentalComp". 查看第一个结果窗口,我希望第二个“ Comp”值作为“ RentalComp”显示在第一行的另一列中。 IF there is only one row returned for a given propertyid then it can just be null. 如果给定的propertyid仅返回一行,则该行可以为null。

Thanks! 谢谢!

.. I'm at a loss of what to google for so here i am. ..我不知道该向Google寻求什么,所以我在这里。

SELECT a.*, b.Comp AS RentalComp
FROM dbo.vwComps AS a LEFT OUTER JOIN dbo.vwComps AS b ON a.PropertyID = b.PropertyID 
AND b.ConfigurationUsed = 2
WHERE (a.ConfigurationUsed = 1)

The key was specifying multiple conditions in the 'ON' statement. 关键是在“ ON”语句中指定多个条件。 THen doing a basic filter in the where clause. 然后在where子句中进行基本过滤。

I kept trying to do everything in the where an it was filtering everything out. 我一直试图在过滤掉所有内容的地方做所有事情。

Is this what you're looking for? 这是您要找的东西吗?

SELECT t1.*, 
       t2.col
FROM   table1 t1
       JOIN table2 t2
       ON t1.key = t2.key

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

相关问题 SQL Server:从同一列中具有匹配值的第一行之后的列中选择所有行 - SQL Server : select all rows from a column after the 1st row with matching value from the same column 如何连接两个SQL表,将第二个表中的值放入第一个表中的列? - How can I join two SQL tables, putting a value from the 2nd table into a column in the 1st? 如何从SQL Server中的列中删除第一个字符 - How to remove the 1st character from a column in SQL Server 联接2个表并从第一个表中获取一些列,并从第二个表中获取最大时间戳值 - join 2 tables and get some columns from 1st table and max timestamp value from second table SQL将第二个表中的行作为列连接 - SQL join rows from second table as columns 如何从第一个表中返回所有列,而从第二个表中只返回一列 - How to return all columns from 1st table and only 1 column from 2nd table SQL连接2个表,从第2个条件查询第1个表 - SQL join 2 tables, query 1st with condition from 2nd 将所有其他行的值添加到每一行。 SQL - Add to each row values from all other rows. sql 使用别名从两个不同的表中左连接特定列 - Left Join Specific Columns From Two Different Tables Using Alias 第二个表上的 SQL 连接所有不同的行显示两个表中的一列 - SQL join on second table all distinct rows showing a column from both tables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM