0

データのクエリを実行しようとしていますが、同じレコードが 2 回表示されます。どうすれば区別できますか?

表1:UserBasics

user_Id , user_Fullname , user_Zip , user_Need
--------------------------------------------------------------------
10    Alberto Cesaro    98001    Sales, Marketing & Public Relations    

表 2:UserProfession

Prof_ID , Company , Designation
----------------------------------
10    Young's   Marketing Manager
10    Young's   Regional Manager

私の手順:

CREATE PROC P @Zip         VARCHAR(20)=NULL,
              @Company     VARCHAR(200)=NULL,
              @Designation VARCHAR(100)=NULL,
              @Interest    VARCHAR(200)=NULL,
              @CurrentID   VARCHAR(200)=NULL
--@JobFunc varchar(200)=NULL
AS
  BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

      SELECT ub.user_Id,
             ub.user_Fullname,
             up.Designation,
             up.Company
      FROM   UserBasics UB
             INNER JOIN UserProfession up
               ON ub.user_Id = up.Prof_ID
      WHERE  ( @Zip IS NULL
                OR ub.user_Zip LIKE '%' + @Zip + '%' )
             AND ( @Interest IS NULL
                    OR ub.user_Need LIKE '%' + @Interest + '%' )
             AND ( @Company IS NULL
                    OR up.Company LIKE '%' + @Company + '%' )
             AND ( ub.user_Id != @CurrentID )
             AND ( @Designation IS NULL
                    OR up.Designation LIKE '%' + @Designation + '%' )
  END 

上記のように、条件と変数を明確にするためだけにストアドプロシージャを使用しています

あなたの提案に対するすべてのユーザーデータの明確な希望を示す方法は?

ありがとう !

編集:

出力は似ているはずですが、

10  Alberto Cesaro          Marketing Manager,Regional Manager  Young's

2回目の編集:

私は会社名を使いましたが、ユーザーの職業テーブルの列でフィルターを使用したい場合、問題があります。どのように関連付けますか?? 私のクエリ、

    SELECT
     user_Id, user_Fullname,
     STUFF(
         (SELECT ', ' + Designation 
          FROM   UserProfession as up
          WHERE  Prof_ID = a.user_Id
          FOR XML PATH (''))
          , 1, 1, '') Designation,
      STUFF(
         (SELECT ', ' + Company 
          FROM   UserProfession
          WHERE  Prof_ID = a.user_Id
          FOR XML PATH (''))
          , 1, 1, '')  AS Company
FROM UserBasics AS a
where  (@Zip is null or a.user_Zip like '%'+@Zip+'%') and
    (@Interest is null or a.user_Need like '%'+@Interest+'%') and
    --  (@JobFunc is null or m.mentor_jobFunction= @JobFunc) and
    (@Company is null or up.Company like '%'+@Company+'%') and
    (a.user_Id != @CurrentID) and
    (@Designation is null or up.Designation like '%'+@Designation+'%')
--where   a.user_Zip like '%90005%'
-- WHERE clause here
GROUP BY user_Id, user_Fullname

-- where 句の Company と Designation でエラーが発生しているので、最後の提案を希望しますか ???

4

1 に答える 1

1
SELECT
     user_Id, user_Fullname,
     STUFF(
         (SELECT ', ' + Designation
          FROM   UserProfession
          WHERE  Prof_ID = a.user_Id
          FOR XML PATH (''))
          , 1, 1, '')  AS DesignationList
FROM UserBasics AS a
-- WHERE clause here
GROUP BY user_Id, user_Fullname
于 2013-01-28T09:54:41.360 に答える