1

我的第一个表是 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中得到了空值,所以我需要相同的recordId高于一个..

4

2 回答 2

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
于 2012-11-03T11:27:05.303 回答
0

您正在选择N.RecordId,它来自CustValue.RecordIdLEFT JOIN来自表的字段ProjectCustomFieldValues

记录 1、2 或 3 没有薪水条目,ProjectCustomFieldValues这就是为什么它NULL在 a 之后返回LEFT JOIN

您可能在选择方面取得了更大的成功,M.RecordId但您的整个陈述可能需要进行一些认真的重构。

于 2012-11-03T10:57:37.247 回答