在多个条件下联接两个表-SQL Server

时间:2019-05-05 04:15:46

标签: sql sql-server

我需要根据某些条件联接两个表。我日常输入中的src故事。我需要根据以下一些标准将src表与mstr表连接起来。

  1. 如果我通过memberid,fname和lname在两个表之间建立联接,那么我需要从mstr表中获取dob。
  2. 如果在上述基础上我没有获得匹配的条件,那么我只需要按会员ID参加,然后参加任务。如果按成员ID在mstr中有多个记录,那么我需要选择记录中具有较早日期的记录。
  3. 如果我也没有通过memberid进行匹配,则需要随机创建一个fname,它是xx + 5个随机数,lname是ZZ + 5个随机数,而dob是今天的日期-110年。

这是我尝试过的方法,可以帮助您加快解决速度。我正在提供两个表的示例数据

declare @src table (memberids int, fnames varchar(25), lnames varchar(25));
insert into @src values
(1, 'Ankit','Bansal'),
(2, 'Dinesh','Bansal'),
(3, 'Sushil','Dania'),
(4, '',''),
(5, Null ,Null),
(10,Null,Null)

select * from @src
declare @mstr table (memberid int, fname varchar(25), lname varchar(25),dob date);
insert into @mstr values
(1, 'Ankit','Bansal','2010-06-24'),
(2, 'Dinesh','Bansal','2009-06-24'),
(3, 'Sushil','Dania','2000-06-24'),
(4, 'Sunil','Wadh','2011-06-24'),
(5, 'Suresh','Bansal','2000-06-24'),
(5, 'Animesh','Bajaj','2001-06-24'),
(6, 'Dhiresh','Jain','2001-06-24');


select * from @mstr;

这是我编写的查询。

 select  memberids, fnames, lnames, a.dob
 from @src
 outer apply
  (select dob
         from @mstr where memberids = memberid and fnames = fname 
         and lnames = lname) a ; 

我得到的当前结果是

memberids   fnames  lnames  dob
1   Ankit   Bansal  2010-06-24
2   Dinesh  Bansal  2009-06-24
3   Sushil  Dania   2000-06-24
4           NULL
5   NULL    NULL    NULL
10  NULL    NULL    NULL

但是输出应如下所示

memberids   fnames  lnames  dob
1   Ankit   Bansal  24-06-2010
2   Dinesh  Bansal  24-06-2009
3   Sushil  Dania   24-06-2000
4   Sunil   Wadh    24-06-2011
5   Suresh  Bansal  24-06-2000
10  XX12345 ZZ123456    Today's Date - 110 years

2 个答案:

答案 0 :(得分:2)

我相信可以做到以下几点(在SQL Fiddle上进行了测试):

SELECT @src.memberids
     , CASE WHEN a.memberid IS NOT NULL THEN @src.fnames
            WHEN b.memberid IS NOT NULL THEN b.fname
            ELSE 'XX' + FORMAT(ABS(CAST(CHECKSUM(NewId()) AS BIGINT)) % 100000, '00000')
       END AS fnames
     , CASE WHEN a.memberid IS NOT NULL THEN @src.lnames
            WHEN b.memberid IS NOT NULL THEN b.lname
            ELSE 'ZZ' + FORMAT(ABS(CAST(CHECKSUM(NewId()) AS BIGINT)) % 100000, '00000')
       END AS lnames
     , CASE WHEN a.memberid IS NOT NULL THEN a.dob
            WHEN b.memberid IS NOT NULL THEN b.dob
            ELSE DATEADD(year, -110, CAST(GETDATE() AS DATE))
       END AS dob
  FROM @src
  LEFT JOIN @mstr a ON a.memberid = @src.memberids
                   AND a.fname = @src.fnames
                   AND a.lname = @src.lnames
  OUTER APPLY (
     SELECT TOP 1 b.memberid, b.fname, b.lname, b.dob
       FROM @mstr b
      WHERE b.memberid = @src.memberids
      ORDER BY b.dob
  ) b

答案 1 :(得分:0)

请检查此查询。这应该可行,但我必须说表格设计存在缺陷。如果这是必需的,请考虑以下查询。但是,如果您有范围,则可以重新考虑先重新设计表格。

declare @src table (memberids int, fnames varchar(25), lnames varchar(25));
insert into @src values
(1, 'Ankit','Bansal'),
(2, 'Dinesh','Bansal'),
(3, 'Sushil','Dania'),
(4, '',''),
(5, Null ,Null),
(10,Null,Null)

declare @mstr table (memberid int, fname varchar(25), lname varchar(25),dob date);
insert into @mstr values
(1, 'Ankit','Bansal','2010-06-24'),
(2, 'Dinesh','Bansal','2009-06-24'),
(3, 'Sushil','Dania','2000-06-24'),
(4, 'Sunil','Wadh','2011-06-24'),
(5, 'Suresh','Bansal','2000-06-24'),
(5, 'Animesh','Bajaj','2001-06-24'),
(6, 'Dhiresh','Jain','2001-06-24');

SELECT M.memberid,M.fname,M.lname,M.dob
FROM (
    SELECT M.memberid, MIN(M.dob) dob
    FROM @src S 
    INNER JOIN @mstr M ON S.memberids = M.memberid
        AND S.fnames = M.fname
        AND S.lnames = M.lname 
    GROUP BY M.memberid
    )B
INNER JOIN @mstr M ON B.memberid = M.memberid AND B.dob = M.dob

UNION

SELECT M.memberid,M.fname,M.lname,M.dob
FROM (
    SELECT M.memberid, MIN(M.dob) dob
    FROM @src S 
    INNER JOIN @mstr M ON S.memberids = M.memberid
    AND (S.fnames IS NULL OR S.fnames = '')
    GROUP BY M.memberid
    )B
INNER JOIN @mstr M ON B.memberid = M.memberid AND B.dob = M.dob

UNION

SELECT C.smid  memberids, 'XX'+CAST ((convert(numeric(5,0),rand() * 10000) + 9999)AS VARCHAR(5)) fname, 
'ZZ'+CAST ((convert(numeric(5,0),rand() * 10000) + 9999)AS VARCHAR(5)) lnames, 
DATEADD(YY,-110, GETDATE()) dob
FROM
(
SELECT S.memberids smid,M.memberid mmid
FROM @src S 
LEFT JOIN @mstr M ON S.memberids = M.memberid
)C WHERE 
C.smid IS NOT NULL
AND C.mmid IS NULL