在SQl中选择表时添加列

时间:2012-11-03 10:44:24

标签: sql-server sql-server-2008

我的第一张表是ProjectCustomFields

CustomFieldId   ProjectId   CustomFieldName CustomFieldRequired CustomFieldDataType
69  1   User Name   1   0
72  1   City    1   0
74  1   Email   0   0
82  1   Salary  1   2

我的第二张表是ProjectCustomFieldValues

CustomFieldValueId  ProjectId   CustomFieldId   CustomFieldValue    RecordId
35  1   69  kaliya  1
36  1   72  Bangalore   1
37  1   74  mm@gmail.com    1
41  1   69  Yohesh  2
42  1   72  Delhi   2
43  1   74      2
50  1   69  sss 3
51  1   72  Delhi   3
52  1   74  sss@icsportal.in    3
57  1   69  Sunil   4
58  1   72  Mumbai  4
59  1   74  sunil@icsportal.in  4
60  1   82  20000   4

我尝试了以下查询

Select M.CustomFieldName
,      N.CustomFieldValue
,      N.RecordId 
From
(
    Select G.CustomFieldName
    ,      H.RecordId 
    From
    (
        Select CustomFieldName 
        From ProjectCustomFields 
        Where ProjectId = 1
    ) G 
    Cross Join
    (
        Select Distinct RecordId 
        From ProjectCustomFieldValues
    ) H
) M
Left Join 
(
    Select CustFiled.CustomFieldName
    ,      CustValue.CustomFieldValue
    ,      CustValue.RecordId 
    From ProjectCustomFieldValues CustValue 
    Left Join ProjectCustomFields CustFiled  
        On CustValue.CustomFieldId = CustFiled.CustomFieldId 
    Where CustValue.AuctionId = 1
) N 
    On M.CustomFieldName = N.CustomFieldName 
    And M.RecordId       = N.RecordId

但我得到了下面的结果

#CustomFieldName#   CustomFieldValue    RecordId
User Name   kaliya  1
City    Bangalore   1
Email   mm@gmail.com    1
Salary  NULL    **NULL**
User Name   Yohesh  2
City    Delhi   2
Email       2
Salary  NULL    **NULL**
User Name   sss 3
City    Delhi   3
Email   sss@icsportal.in    3
Salary  NULL    **NULL**
User Name   NULL    **NULL**
City    NULL    **NULL**
Email   NULL    **NULL**
Salary  NULL    **NULL**
User Name   Sunil   4
City    Mumbai  4
Email   sunil@icsportal.in  4
Salary  20000   4

但预期结果是

CustomFieldName CustomFieldValue    RecordId
User Name   kaliya  1
City    Bangalore   1
Email   mm@gmail.com    1
Salary  NULL    **1**
User Name   Yohesh  2
City    Delhi   2
Email       2
Salary  NULL    **2**
User Name   sss 3
City    Delhi   3
Email   sss@icsportal.in    3
Salary  NULL    **3**
User Name   Sunil   4
City    Mumbai  4
Email   sunil@icsportal.in  4
Salary  20000   4

请指导我一些,我尝试了很多但是我在recordId中得到了null值,所以我需要同一个recordId以上...

2 个答案:

答案 0 :(得分:0)

您选择N.RecordIdCustValue.RecordId来自LEFT JOIN中来自表格ProjectCustomFieldValues的字段ProjectCustomFieldValues

NULL中的记录1,2或3的薪水没有条目,这就是LEFT JOIN之后返回M.RecordId的原因。

您可能在选择{{1}}方面取得更大成功,但您的整个陈述可以通过一些严肃的重构来完成。

答案 1 :(得分:0)

试试这个:

declare @ProjectCustomFields table
(
    CustomFieldId            bigint not null primary key clustered
    , ProjectId              int not null
    , CustomFieldName        nvarchar(64) not null
    , CustomFieldRequired    bit not null
    , CustomFieldDataType    int not null
)
insert @ProjectCustomFields
      select 69, 1, 'User Name', 1, 0
union select 72, 1, 'City'     , 1, 0
union select 74, 1, 'Email'    , 0, 0
union select 82, 1, 'Salary'   , 1, 2

declare @ProjectCustomFieldValues table
(
    CustomFieldValueId bigint not null primary key clustered 
    , ProjectId        int not null
    , CustomFieldId    bigint not null --foreign key references ProjectCustomFields(CustomFieldId)
    , CustomFieldValue nvarchar(max)
    , RecordId         bigint not null
)
insert @ProjectCustomFieldValues
      select 35, 1, 69, 'kaliya'            , 1
union select 36, 1, 72, 'Bangalore'         , 1
union select 37, 1, 74, 'mm@gmail.com'      , 1
union select 41, 1, 69, 'Yohesh'            , 2
union select 42, 1, 72, 'Delhi'             , 2
union select 43, 1, 74, ''                  , 2
union select 50, 1, 69, 'sss'               , 3
union select 51, 1, 72, 'Delhi'             , 3
union select 52, 1, 74, 'sss@icsportal.in'  , 3
union select 57, 1, 69, 'Sunil'             , 4
union select 58, 1, 72, 'Mumbai'            , 4
union select 59, 1, 74, 'sunil@icsportal.in', 4
union select 60, 1, 82, '20000'             , 4

select vf.CustomFieldName
, v2.CustomFieldValue
, vf.RecordId
from 
(
    select distinct 
      v.RecordId
    , f.CustomFieldName
    , f.CustomFieldId
    from @ProjectCustomFieldValues v
    cross join @ProjectCustomFields f
) vf
left outer join @ProjectCustomFieldValues v2
    on vf.RecordId = v2.RecordId
    and vf.CustomFieldId = v2.CustomFieldId
order by vf.RecordId, vf.CustomFieldId
相关问题