简体   繁体   English

如何使用别名联接到表

[英]How to join to a table using aliases

Thanks in advance for any help. 在此先感谢您的帮助。

I'm wondering how to join two SQL statements I have, based on the fact that I've given the columns aliases. 我想知道如何结合我拥有的两个SQL语句,因为我已经给列指定了别名。

The source table looks like this (please bear in mind that changing the layout of this table is enormously expensive due to other requirements): 源表如下所示(请记住,由于其他要求,更改此表的布局非常昂贵):

ColumnID    RowIdx    Val
1           0         "Mr A"
1           1         "Mr B"
1           2         "Mr C"
2           0         "M40 2TB"
2           1         "G23 XYN"
2           2         "HJ 23N"

I use two sql statements to create views on the table, which I think join. 我使用两个sql语句在表上创建视图,我认为这些视图是联接的。 This looks something like this: 看起来像这样:

-- Statement #1
CREATE TEMP TABLE nameTable AS (SELECT 
    max(CASE WHEN ColumnID=1 THEN Val ELSE null END) AS Name, 
    row_number() over (ORDER BY RowIdx) AS rowindex 

FROM myTable 

WHERE (ColumnID=1) 

GROUP BY RowIdx
ORDER BY RowIdx);

-- Statement #2
CREATE TEMP TABLE postcodeTable AS (SELECT 
    max(CASE WHEN ColumnID=2 THEN Val ELSE null END) AS PostCode, 
    row_number() over (ORDER BY RowIdx) AS rowindex 

FROM myTable 

WHERE (ColumnID=2) 

GROUP BY RowIdx
ORDER BY RowIdx);

-- Statement #3
SELECT * FROM nameTable
INNER JOIN postcodeTable ON nameTable.rowIndex=postcodeTable.rowIndex

The result is the following: 结果如下:

Name    PostCode    RowIndex
Mr A    M40 2TB     0
Mr B    G23 XYN     1
Mr C    HJ 23N      2

I would like to combine these into a single statement (as it makes generating the statement simpler for my program. 我想将它们组合成一条语句(因为这使我的程序的生成语句更简单。

I have chosen to omit some logic from statements 1 and 2 as it is needlessly complicated for my example, but what I have left out prohibits me doing this: 我选择从语句1和2中省略一些逻辑,因为对于我的示例而言,它不必要地复杂,但是我遗漏的内容禁止我这样做:

SELECT 
    max(CASE WHEN ColumnID=1 THEN Val ELSE null END) AS Name, 
    max(CASE WHEN ColumnID=2 THEN Val ELSE null END) AS PostCode, 
    row_number() over (ORDER BY RowIdx) AS rowindex 

FROM myTable 

WHERE (ColumnID=1 OR ColumnID=2) 

GROUP BY RowIdx
ORDER BY RowIdx

I'm now considering doing something like this, but I'm not sure if it's possible: 我现在正在考虑做这样的事情,但不确定是否可行:

SELECT 
    max(CASE WHEN ColumnID=1 THEN Val ELSE null END) AS Name, 
    row_number() over (ORDER BY RowIdx) AS rowindex 

FROM myTable 

INNER JOIN (SELECT 
        max(CASE WHEN ColumnID=2 THEN Val ELSE null END) AS PostCode, 
        row_number() over (ORDER BY RowIdx) AS rowindex 

    FROM myTable 

    WHERE (ColumnID=2) 

    GROUP BY RowIdx
    ORDER BY RowIdx) AS postcodeTable ON rowIndex=postcodeTable.rowIndex

WHERE (ColumnID=1) 

GROUP BY RowIdx
ORDER BY RowIdx);

Is this something I should even be considering doing? 这是我什至应该考虑做的事情吗? Or should I just stick with temporary tables? 还是我应该坚持使用临时餐桌? If it is, how do I get this to work? 如果是这样,我如何使它工作? The compile issue in on the rowIndex=postcodeTable.rowIndex rowIndex doesn't exist. rowIndex=postcodeTable.rowIndex rowIndex上的编译问题不存在。

I know the original schema is formatted weirdly, but there are all sorts of external reasons for that. 我知道原始模式的格式很奇怪,但是有各种各样的外部原因。 Please let me know if you need any more info. 如果您需要更多信息,请告诉我。

I may be missing something, but I don't understand the need for the complexity in your example queries. 我可能会遗漏一些东西,但我不理解示例查询中对复杂性的需求。 Would something like the following (untested) query work? 类似于以下(未经测试)的查询是否可行?

SELECT Name, PostCode, A.RowIdx AS RowIdx 
FROM
    (SELECT Val AS Name, RowIdx FROM myTable WHERE ColumnID=1) A
    INNER JOIN
    (SELECT Val AS PostCode, RowIdx FROM myTable WHERE ColumnID=2) B
ON A.RowIdx = B.RowIdx;

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

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