【问题标题】:Comparing columns of different datatypes while concatenating in T-SQL在 T-SQL 中连接时比较不同数据类型的列
【发布时间】:2015-10-09 16:48:49
【问题描述】:

我在tableA 的列Period(数据类型:Varchar)中有值,如下所示:

表A

Period
------
3M
124W
200D
2Y

在表 B 中,列 Frequency(数据类型:numeric)在同一表中的 Period_Code(数据类型:Varchar)列中具有 4.0000、154.00000、100.00000、4.00000 等值及其对应的期间代码表 B 为 M、W、D 和 Y。

表 B

Frequency      |    Period_Code
-------------------------------
4.0000         |          M
154.00000      |          W
100.00000      |          D
4.00000        |          Y

我可以在order_id 上加入两个表。

我想比较TableB中记录的FrequencyPeriod_Code的组合在TableA的Period列中是否存在。

我该怎么做?

它是 SQL Server 2008 或更高版本。

请帮忙。

谢谢。

【问题讨论】:

  • 我刚刚意识到我的答案并没有真正回答你的问题,但我会在删除之前暂时将其留在那里,以防它对你有所帮助。对于您的示例数据,您可能需要编辑这些值,以便在 2 个表之间至少存在 1 条记录,因为目前 A 中不存在来自 B 的组合。
  • 制革商。我很抱歉没有正确地提出这个问题。这两个表可以在订单 ID 上连接。并且两个表中有许多相同的 order_id。
  • 这听起来像是您可能想要添加到问题和示例数据中的重要信息。如果现在对你没有任何用处,我会删除我的帖子吗?
  • 我还是不明白,频率是什么类型... sql server 中的数字类型会给你 4、154、100、4。
  • 你还应该给出一个想要的输出示例......

标签: sql-server string comparison concatenation


【解决方案1】:

以下代码匹配所需的输出:

    DECLARE @tableA TABLE
    (
        [Period] VARCHAR(50)
    )

    DECLARE  @tableB TABLE
    (
        [Frequency] INT
    ,   [Period_Code]   VARCHAR(50)
    )

    INSERT INTO @tableA
    VALUES('3M'), ('124W'), ('200D') , ('2Y')

    INSERT INTO @tableB(Frequency, Period_Code)
    VALUES(4, 'M')
    , (154, 'W')
    ,   (100, 'D')
    , (4, 'Y')

    SELECT * FROM @tableA
    SELECT * FROM @tableB

    SELECT * FROM @tableA a
    INNER JOIN
    @tableB b
    ON a.Period = CONVERT(VARCHAR, b.Frequency) + b.Period_Code

【讨论】:

  • 您可以将频率设置为数字。结果会和小数点一样只有0
  • 他有数据,所以不需要创建表。他还显示表中有 4.0000
  • 在这种情况下,只有最后一个查询有用
【解决方案2】:

一个问题......你怎么有 4.0000 作为 INT ?

所以你有这样的东西:

    select * from tableB 
     where substring(convert(varchar,Frequency),0,charindex('.',Frequency))+Period_Code  
         in (
             Select Period
             from tableA
             )

【讨论】:

    【解决方案3】:

    如果您想连接 tableB 中的两个字段,您可以使用 CONCAT 函数(SQL Server 2012+)或仅使用 +(旧版本)。然后,要查找连接字段是否匹配回 tableA 中的句点列,您可以直接执行 join 或使用 IN 或仅使用 EXISTS 来完成您想要的输出:

    加入方法

    SELECT distinct b.*
    FROM tableA a
    INNER JOIN (
        SELECT order_id,
               frequency,
               period_code,
               convert(VARCHAR(255), cast(frequency as int)) + period_code AS period
        FROM tableB
        ) b ON a.order_id = b.order_id and a.period = b.period;
    

    EXISTS 方法

    SELECT distinct b.*
    FROM tableB b
    WHERE EXISTS (
            SELECT 1
            FROM tableA a
            WHERE a.order_id = b.order_id 
            AND a.period = convert(VARCHAR(255), cast(frequency as int)) + b.period_code
            );
    

    输入法

    SELECT DISTINCT b.*
    FROM tableB b
    WHERE convert(VARCHAR(255), cast(frequency as int)) + period_code IN (
            SELECT period
            FROM tableA a
            WHERE a.order_id = b.order_id
            );
    

    SQL Fiddle Demo

    相反,如果你想输出匹配失败的所有order_ids',你可以使用NOT EXISTSNOT IN这样做:

    不存在方法

    SELECT distinct b.order_id
    FROM tableB b
    WHERE NOT EXISTS (
            SELECT 1
            FROM tableA a
            WHERE a.order_id = b.order_id 
            AND a.period = convert(VARCHAR(255), b.frequency) + b.period_code
            );
    

    不在方法中

    SELECT distinct b.order_id
    FROM tableB b
    WHERE convert(VARCHAR(255), frequency) + period_code NOT IN (
            SELECT period
            FROM tableA a
            WHERE a.order_id = b.order_id
            );
    

    SQL Fiddle Demo2

    【讨论】:

      【解决方案4】:

      选择 A.order_id、B.order_id、A.Period、B.Period_Code、B.Frequency

      从表 B B

      在 a.order_id = b.order_id 上左连接 tableA A

      其中 cast(substring (A.period,1,(len(A.period)-1)) AS numeric(14,6))
      B.频率或 上(右(A.period,1)) B.Period_Code

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-22
        • 2022-11-16
        • 1970-01-01
        • 2015-06-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多