在A或B或C上加入表格

时间:2012-02-23 22:51:28

标签: sql sql-server sql-server-2008

我有一张桌子:

docname, specialtycode1, specialtycode2, specialtycode3

我有另一张桌子

    specialtycode1, specialtycode1desc
specialtycode2, specialtrycode2desc
specialtycode3, specialtycode3desc

我想要的结果是:

docname, specialtycode1, secialtycode1desc, 
  specialtycode2, specialtycode2desc, 
  specialtycode3, specialtycode3desc

我试着这样做一个查询:

join dbo.SpecialtyMapping as f on (e.specialtycode1=f.specialtyabbrev
                                or e.specialtycode2=f.specialtyabbrev
                                or e.specialtycode3=f.specialtyabbrev)

我不认为这是正确的解决方案,但不知道从哪里开始。请帮忙!

这是完整的查询:

WITH cte(rejected) AS (SELECT DISTINCT [Specimen ID]
                                                 FROM         dbo.QuickLabDump
                                                 WHERE     (Outcome = 'REJECTED') AND ([Specimen ID] IS NOT NULL))
    SELECT     CONVERT(VARCHAR(8), a.[Date Entered], 1) AS [Full Date], DATEPART(yy, a.[Date Entered]) AS [Year Entered], LEFT(DATENAME(MONTH, a.[Date Entered]), 3) 
                            AS [Month Entered], DATEPART(dd, a.[Date Entered]) AS [Day Entered], CASE WHEN DATEPART(WEEKDAY, [DATE entered]) 
                            = 1 THEN 'Sun' WHEN DATEPART(WEEKDAY, [DATE entered]) = 2 THEN 'Mon' WHEN DATEPART(WEEKDAY, [DATE entered]) 
                            = 3 THEN 'Tus' WHEN DATEPART(WEEKDAY, [DATE entered]) = 4 THEN 'Wed' WHEN DATEPART(WEEKDAY, [DATE entered]) 
                            = 5 THEN 'Thu' WHEN DATEPART(WEEKDAY, [DATE entered]) = 6 THEN 'Fri' WHEN DATEPART(WEEKDAY, [DATE entered]) = 7 THEN 'Sat' END AS DOW, 
                            CONVERT(VARCHAR(8), DATEADD(D, - (1 * DATEPART(dw, a.[Date Entered])) + 6, a.[Date Entered]), 1) AS [Week Ending], COUNT(DISTINCT a.[Specimen ID]) 
                            AS CountAccns, c.SALESREP AS [Sales Rep], c.NPI AS MLNPI, e.NPIb AS IMSNPI, e.SpecialtyPrimaryCodeb AS [IMS Specialty Primary Code],
                            f.specialtydesc,
                            e.SpecialtySecondaryCodeb AS [IMS Specialty Secondary Code], e.SpecialtyTertiaryCodeb AS [IMS Specialty Tertiary Code], 
                            e.ProfessionalID1b AS [IMS Professional ID 1], a.[Requesting Physician] AS Physician, a.[Practice Code], b.[MLIS Practice ID] AS [MLIS Code], a.[Practice Name], 
                            c.DATEESTABLISHED AS [Date Established], c.PRACTICEADDRESS1 AS Address, c.PRACTICEADDRESS2 AS Address2, c.PRACTICECITY AS City, 
                            c.PRACTICESTATE AS State, d.[MLIS Status] AS Status, MAX(a.[Order Count]) AS [order count]
     FROM         dbo.QuickLabDump AS a LEFT OUTER JOIN
                            dbo.qlmlismapping AS b ON b.[Quicklab ID] = a.[Practice Code] LEFT OUTER JOIN
                            dbo.PracticeandPhysician AS c ON a.[Requesting Physician] = c.DOCTORFIRSTNAME + ' ' + c.DOCTORLASTNAME AND 
                            a.[Practice Code] = c.PRACTICECODE LEFT OUTER JOIN
                            dbo.IMSData AS e ON c.NPI = e.NPIb LEFT OUTER JOIN
                            dbo.QLMLISInfo AS d ON b.[MLIS Practice ID] = d.[MLIS Practice ID]
                            join dbo.SpecialtyMapping as f on (e.specialtyprimarycodeb=f.specialtyabbrev
                                or e.specialtysecondarycodeb=f.specialtyabbrev
                                or e.specialtytertiarycodeb=f.specialtyabbrev)
     WHERE     (a.[Date Entered] > '20110101') AND (NOT EXISTS
                                (SELECT     1 AS Expr1
                                  FROM          cte AS cte_1
                                  WHERE      (rejected = a.[Specimen ID]))) AND (NOT EXISTS
                                (SELECT     1 AS Expr1
                                  FROM          dbo.PendingSpecimens
                                  WHERE      ([Specimen ID] = a.[Specimen ID])))
     GROUP BY a.[Date Entered], c.SALESREP, c.NPI, e.NPIb, e.SpecialtyPrimaryCodeb, e.SpecialtySecondaryCodeb, e.SpecialtyTertiaryCodeb, e.ProfessionalID1b, 
                            a.[Requesting Physician], a.[Practice Code], b.[MLIS Practice ID], a.[Practice Name], c.DATEESTABLISHED, c.PRACTICEADDRESS1, c.PRACTICEADDRESS2, 
                            c.PRACTICECITY, c.PRACTICESTATE, d.[MLIS Status],f.specialtydesc

2 个答案:

答案 0 :(得分:1)

如果在第一个表中有三个“专业代码”作为列,则需要三个JOIN,或者您可以从docName表中取消数据并执行单个JOIN。我将使用不需要对原始查询进行过多修改的解决方案:

SELECT
    ims.docname
    ,ims.specialtycode1, sm1.specialtycode1desc
    ,ims.specialtycode2, sm2.specialtycode2desc
    ,ims.specialtycode3, sm3.specialtycode3desc
FROM
    dbo.IMSData ims
LEFT JOIN
    dbo.SpecialtyMapping sm1 ON ims.specialtycode1 = sm1.specialtyabbrev
LEFT JOIN
    dbo.SpecialtyMapping sm2 ON ims.specialtycode2 = sm2.specialtyabbrev
LEFT JOIN
    dbo.SpecialtyMapping sm3 ON ims.specialtycode3 = sm3.specialtyabbrev

只要您的SpecialtyMapping表的行是唯一的,这应该可行。

编辑 - 根据评论中的建议更新为LEFT JOIN。我还注意到列名称与您查询中的列名称不完全匹配,但您将获得要点。

答案 1 :(得分:1)

有很多东西可以用于长时间运行的查询。

从哪里开始寻找。

  1. 多少数据?如果您拥有大量数据和大量数据,那么无论您的查询有多好,您的查询都会花费很长时间。然而,大多数数据库可以采取相当大的数量,所以我们假设这不是问题。

  2. 您要加入的列的表索引吗?数据库需要找到您要加入的列,如果它们没有编入索引,则此过程可能需要很长时间,索引您要加入的列

    在Your_TableName(Column1,Column2等)上创建聚簇索引IX_Your_IndexName

  3. 了解如何阅读执行计划,这实际上会告诉您查询运行缓慢的原因。开始的好地方! Here is an article让你开始。

  4. 祝你好运。