How to get required information based on 3 tables in SQL Server

时间:2015-08-15 07:27:36

标签: sql-server sql-server-2008 sql-server-2008-r2 sql-server-2012

How to solve this issue in SQL Server?

Table1:

Pid   |     Pname  | prosubcatgoryid
1     |     pen    |  100
2     |     Rin    |  101
3     |     Cap    |  100

Table2:

prosubcatgoryid      |   prosubcatgoryidDescription
          100        |   powerofwords
          101        |   wearingitem

Table3 :

Pid  | Address           |  City | datetime
1    | Homeless          |  Chen | 2014-11-13  09:32:14.000
1    | 3913 W. Strong    |  Chen | 2011-03-044 19:04:10.000 
1    | 1100 W MALLON     |  Chen | 2012-11-13  09:32:14.000
2    | 610 W GARLAND #3  |  Hyd  | 2013-11-13  09:32:14.000

Table1 and Table2 have a common column prosubcatgoryid and Table1 and Table3 have a common column Pid.

We need get prosubcatgoryid related information taken from table2 and pid related address and city information taken from table3 and we need to consider latest datetime (highest datetime value based on pid) corresponding address and city information from table3

Based on above 3 tables I want output like below

pid    |Pname  | prosubcatgoryid |prosubcatgoryidDescription|  Address        | City      | datetime 
1      | Pen   |  100            | powerofwords             |  Homeless       | Chen      |2014-11-13 09:32:14.000
2      | Rin   |  101            | wearingitem              |610 W GARLAND #3 | Hyd       |2013-11-13 09:32:14.000
3      | Cap   |  100            | powerofwords             |notavilable      |notavilable|1900-01-01(pass any default values)

I tried this query for table1 and table2

select 
    a.pid, a.pname, a.prosubcatgoryid, b.prosubcatgoryidDescription 
from 
    table1 
left join 
    table2 on table1.prosubcatgoryid = table2.prosubcatgoryid

for table3 information

SELECT 
    mp.id, mp.address, mp.city, mp.state, mp.datetime
FROM
    (SELECT 
         MAX(datetime) AS MaxP, id 
     FROM 
         [test].[dbo].[table3] 
     GROUP BY 
         id) MaxP 
JOIN
    [test].[dbo].[table3] MP ON MaxP.id = MP.id AND MaxP.MaxP = MP.timestamp

Finally I did not get any idea how to merge above 2 queries and get above result.

Please tell me how to write query to achieve this task in SQL Server

2 个答案:

答案 0 :(得分:1)

你可以使用外部申请和前1来完成,如下所示:

select 
    a.pid, a.pname, a.prosubcatgoryid, 
    b.prosubcatgoryidDescription,
    c.Address, c.City, c.datetime
from 
    table1 a
    left join table2 b 
        on a.prosubcatgoryid = b.prosubcatgoryid
    outer apply (
      select top 1 Address, City, datetime
      from table3 c
      where c.Pid = a.Pid
      order by datetime desc
    ) c

SQL Fiddle

中的示例

您可以在select中使用isnull获取所需的地址默认值,例如isnull(c.datetime, '19000101')如果您不想使用空值。

答案 1 :(得分:0)

您可以使用row_number()为每个id组的每一行编号。使用rn = 1,您只能选择最新的行:

select  *
from    table1 a
left join
        table2 b 
on      a.prosubcatgoryid = b.prosubcatgoryid
left join 
        (
        select  row_number() over (
                  partition by pid
                  order by [datetime] desc) rn
        ,       *
        from    table3
        ) c
on      c.Pid = a.Pid
        and c.rn = 1 -- Only latest row

Example at SQL Fiddle.