在SQL Server 2008中按ID增加连接字段

时间:2014-05-20 22:01:53

标签: sql-server tsql

我有下面的查询,我需要获取MetaName字段并增加由lst.LISTING_ID和lst.LISTING_SRC_ID字段分组的名称。

SELECT DISTINCT 
            lst.LISTING_ID,
            lst.LISTING_SRC_ID,
            rt.RLTN_TYP_NM + ' ' + t.PHONE_TYP_NM AS 'MetaName',
            ad.PHONE_NUM,
            lad.PHONE_EXT
FROM   dbo.int_aux_phone ad
   INNER JOIN dbo.int_aux_lst_phone lad
           ON ad.phone_id = lad.phone_id
              AND ad.PHONE_SRC_ID = lad.PHONE_SRC_ID
              AND ad.OWN_LIST_ID = lad.LISTING_ID
   INNER JOIN dbo.int_aux_listing lst
           ON lad.listing_id = lst.listing_id
              AND lad.listing_src_id = lst.listing_src_id
   INNER JOIN dbo.INT_RELATION_TYP rt
           ON rt.RLTN_TYP_ID = lad.RLTN_TYP_ID
   INNER JOIN dbo.INT_PHONE_TYP T
           ON t.PHONE_TYP_ID = ad.PHONE_TYP_ID

例如:

上面查询的输出如下所示。

 LISTING_ID LISTING_SRC_ID  MetaName               PHONE_NUM            PHONE_EXT
-------------------------------------------------------------------------------------------
4           1000071     Business Phone              (214) 692-5555       NULL
4           1000071         Business Phone              (281) 858-5555  NULL
4           1000071         Business Phone              (210) 344-5555  NULL
4           1000071         Alternate Business Phone    (210) 344-5555  NULL
4           1000071         Alternate Business Phone    (713) 223-5555  NULL
5           1000071         Alternate Business Phone    (713) 223-5555  NULL
5           1000071         Business Phone              (281) 858-5555  NULL
5           1000071         Business Phone              (210) 344-5555  NULL

我希望它看起来像这样

LISTING_ID  LISTING_SRC_ID  MetaName               PHONE_NUM            PHONE_EXT
-------------------------------------------------------------------------------------------
4           1000071     Business Phone              (214) 692-5555       NULL
4           1000071         Business Phone1             (281) 858-5555  NULL
4           1000071         Business Phone2             (210) 344-5555  NULL
4           1000071         Alternate Business Phone    (210) 344-5555  NULL
4           1000071         Alternate Business Phone1   (713) 223-5555  NULL
5           1000071         Alternate Business Phone    (713) 223-5555  NULL
5           1000071         Business Phone              (281) 858-5555  NULL
5           1000071         Business Phone1             (210) 344-5555  NULL

如果ContactA有三个Biz Phones值,请澄清上面的示例 商务电话 商务电话 商务电话

ContactB有三个商务电话价值 商务电话 商务电话 商务电话

脚本运行时需要输出

ContactA

商务电话 商务电话1 商务电话2

ContactB

商务电话 商务电话1 商务电话2

任何帮助都会很棒。我和一位朋友谈过,他推荐了一段时间循环,在你推荐之前还没有完成?

1 个答案:

答案 0 :(得分:1)

    SELECT DISTINCT 
    lst.LISTING_ID,
    lst.LISTING_SRC_ID,
    lst.RLTN_TYP_NM + ' ' + t.PHONE_TYPE_NM + replace(' ' + convert(varchar, ROW_NUMBER() over(partition by lst.LISTING_ID, rt.RLTN_TYP_NM + ' ' + t.PHONE_TYPE_NM order by rt.RLTN_TYP_NM, t.PHONE_TYPE_NM)-1), '0', '') MetaName,
    ad.PHONE_NUM,
    lad.PHONE_EXT
    FROM   dbo.int_aux_phone ad
       INNER JOIN dbo.int_aux_lst_phone lad
               ON ad.phone_id = lad.phone_id
                  AND ad.PHONE_SRC_ID = lad.PHONE_SRC_ID
                  AND ad.OWN_LIST_ID = lad.LISTING_ID
       INNER JOIN dbo.int_aux_listing lst
               ON lad.listing_id = lst.listing_id
                  AND lad.listing_src_id = lst.listing_src_id
       INNER JOIN dbo.INT_RELATION_TYP rt
               ON rt.RLTN_TYP_ID = lad.RLTN_TYP_ID
       INNER JOIN dbo.INT_PHONE_TYP T
               ON t.PHONE_TYP_ID = ad.PHONE_TYP_ID

此查询使Metanames从1开始递增:

SELECT DISTINCT 
        lst.LISTING_ID,
        lst.LISTING_SRC_ID,
        lst.RLTN_TYP_NM + ' ' + t.PHONE_TYPE_NM + convert(varchar, ROW_NUMBER() over(partition by lst.LISTING_ID, rt.RLTN_TYP_NM + ' ' + t.PHONE_TYPE_NM order by rt.RLTN_TYP_NM, t.PHONE_TYPE_NM)) MetaName,
        ad.PHONE_NUM,
        lad.PHONE_EXT
        FROM   dbo.int_aux_phone ad
           INNER JOIN dbo.int_aux_lst_phone lad
                   ON ad.phone_id = lad.phone_id
                      AND ad.PHONE_SRC_ID = lad.PHONE_SRC_ID
                      AND ad.OWN_LIST_ID = lad.LISTING_ID
           INNER JOIN dbo.int_aux_listing lst
                   ON lad.listing_id = lst.listing_id
                      AND lad.listing_src_id = lst.listing_src_id
           INNER JOIN dbo.INT_RELATION_TYP rt
                   ON rt.RLTN_TYP_ID = lad.RLTN_TYP_ID
           INNER JOIN dbo.INT_PHONE_TYP T
                   ON t.PHONE_TYP_ID = ad.PHONE_TYP_ID