将两个表中的数据联接到1个表中的单个条目

时间:2019-03-05 11:16:22

标签: sql tsql sql-server-2008

早上全部

希望您能提供帮助。我已经设法在Access中实现了这一目标,但在SQL中却似乎失败了。

我目前正在做什么

在Access中,我创建了具有2个表的1个DB:

  • 数据库名称“所有投资组合-总价值”
  • 列名:ID,value_date,external_ref,Portfolio_name,value
  • 数据库名称“ VAL_File_Concatenate”
  • 列名:value_date,external_ref,wrapper_name,model,value

在Access中如何工作是将一个文本文件导入VAL_File_Concatenate,然后执行以下代码:

INSERT INTO [All Portfolios - Total Values] ( value_date, external_ref, portfolio_name, [value] ) IN 'PATH TO DB'

SELECT [Val_File_Concatenate].value_date, [Val_File_Concatenate].external_ref, [Val_File_Concatenate] [Wrapper_Name] & " - " & [Model] AS portfolio_name, Sum([Val_File_Concatenate].value) AS SumOfvalue
FROM [Val_File_Concatenate]

GROUP BY [Val_File_Concatenate].value_date, [Val_File_Concatenate].external_ref, [Wrapper_Name] & " - " & [Model]

HAVING ((([Val_File_Concatenate].value_date) Is Not Null));

这会将VAL_File_Concatenate中的值复制到All_Portfolios-总值中的相关字段中,但将Wrapper_Name和模型合并到“ All Portfolio-总值”表中的“ Portfolio_Name”字段中,并以“-”作为分隔符。

作为参考,VAL_File_Concatenate看起来像这样:

Value_Date     External_Ref       Wrapper_Name       Model         Value
2019-03-01      Xxxxxxx/x          GIA - Cash       Liquidity      0.165

“全部投资组合-总价值”数据库中的Access输出为:

Value_Date     External_Ref      Portfolio_Name            Value
2019-03-01      Xxxxxxx/x       GIA – Cash - Liquidity     0.165

我要实现的目标

我已将数据库迁移到SQL Server 2008,即具有2个表的单个DB。我正在苦苦挣扎的是查询的一部分,它通过合并VAL_File_Concatenate中的Wrapper_Name和Model字段在“所有投资组合-总价值”中创建Portfolio_Name。

非常感谢您提供的任何帮助。我尝试了各种方法,但似乎无法克服这些错误。

迈克

3 个答案:

答案 0 :(得分:0)

在标准SQL中,查询看起来更像这样:

INSERT INTO <path to db>.[All Portfolios - Total Values] ( value_date, external_ref, portfolio_name, value )
    SELECT c.value_date, 
           c.external_ref,
           CONCAT(c.[Wrapper_Name], '-', [Model]) AS portfolio_name,
            Sum(c.value) AS SumOfvalue
    FROM Val_File_Concatenate c
    WHERE c.value_date Is Not Null
    GROUP BY c.value_date, c.external_ref, CONCAT(c.[Wrapper_Name], '-', [Model]);

MS Access使用非标准语法。您的最终查询应如下所示。

答案 1 :(得分:0)

在sql server中,您可以使用+代替&

    INSERT INTO [All Portfolios - Total Values] ( value_date, external_ref, portfolio_name, [value] ) IN 'PATH TO DB'

    SELECT [Val_File_Concatenate].value_date
        , [Val_File_Concatenate].external_ref
        , [Val_File_Concatenate].[Wrapper_Name] + ' - ' + [Model] AS portfolio_name
        , Sum([Val_File_Concatenate].value) AS SumOfvalue
    FROM [Val_File_Concatenate]

    GROUP BY [Val_File_Concatenate].value_date, [Val_File_Concatenate].external_ref, [Wrapper_Name] + " - " +  [Model]

    HAVING ((([Val_File_Concatenate].value_date) Is Not Null));

答案 2 :(得分:0)

You can try this.

INSERT INTO [All Portfolios - Total Values] ( value_date, external_ref, portfolio_name, [value] ) 
SELECT 
    [Val_File_Concatenate].value_date, 
    [Val_File_Concatenate].external_ref,
    [Val_File_Concatenate].[Wrapper_Name] + ' - ' + [Val_File_Concatenate].[Model] AS portfolio_name, 
    Sum([Val_File_Concatenate].value) AS SumOfvalue
FROM 
    [Val_File_Concatenate]
WHERE 
    [Val_File_Concatenate].value_date Is Not Null
GROUP BY 
    [Val_File_Concatenate].value_date, 
    [Val_File_Concatenate].external_ref, 
    [Val_File_Concatenate].[Wrapper_Name] + ' - ' + [Val_File_Concatenate].[Model]