3

这是我的表:

[Member]: 
{ [Id], [UserId], [UserName], [Email], [Status_Id], [MemberType_Id] }

[CustomerProfile] :
{ [Id], [Member_Id], [FirstName], [LastName], [SSN], [Gender], [PostalCode],[City_Id], [Address]}


[DealerProfile]:
{ [Id], [Member_Id], [FirstName], [LastName], [SSN], [Gender], [StoreName], [PostalCode], [City_Id], [Address] }


[ManagerProfile]
{ [Id], [Member_Id], [FirstName], [LastName], [SSN], [Gender] }

[City]:
{ [Id], [Title], [Province_Id] }

所以我需要创建所有成员和相关列的完整视图,我将从以下选择查询开始:

SELECT     
[ME].[Id] AS [MemberId],
[ME].[UserId],
[ME].[UserName],
[ME].[Email],
[ME].[Status_Id],
[ST].[Title] AS [Status],
[ME].[MemberType_Id],
[MT].[Title] AS [MemberType],

CASE
    WHEN [ME].[MemberType_Id] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Customer') THEN [CUP].[Id] 
    WHEN [ME].[MemberType_Id] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Dealer') THEN [DEP].[Id]
    WHEN [ME].[MemberType_Id] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Manager') THEN [MAP].[Id] 
    ELSE 0 END AS [Profile_Id],
CASE
    WHEN [ME].[MemberType_Id] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Customer') THEN [CUP].[FirstName]
    WHEN [ME].[MemberType_Id] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Dealer') THEN [DEP].[FirstName]
    WHEN [ME].[MemberType_Id] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Manager') THEN [MAP].[FirstName]
    ELSE 'Unknown' END AS [FirstName],
CASE
    WHEN [ME].[MemberType_Id] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Customer') THEN [CUP].[LastName]
    WHEN [ME].[MemberType_Id] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Dealer') THEN [DEP].[LastName]
    WHEN [ME].[MemberType_Id] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Manager') THEN [MAP].[LastName]
    ELSE 'Unknown' END AS [LastName],
CASE
    WHEN [ME].[MemberType_Id] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Customer') THEN [CUP].[SSN]
    WHEN [ME].[MemberType_Id] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Dealer') THEN [DEP].[SSN]
    WHEN [ME].[MemberType_Id] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Manager') THEN [MAP].[SSN]
    ELSE 'Unknown' END AS [SSN],
CASE
    WHEN [ME].[MemberType_Id] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Customer') THEN [CUP].[Address]
    WHEN [ME].[MemberType_Id] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Dealer') THEN [DEP].[Address]
    WHEN [ME].[MemberType_Id] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Manager') THEN ''
    ELSE 'Unknown' END AS [Address],
CASE
    WHEN [ME].[MemberType_Id] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Customer') THEN [CUP].[PostalCode]
    WHEN [ME].[MemberType_Id] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Dealer') THEN [DEP].[PostalCode]
    WHEN [ME].[MemberType_Id] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Manager') THEN ''
    ELSE 'Unknown' END AS [PostalCode],
CASE
    WHEN [ME].[MemberType_Id] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Customer') THEN [CUP].[Gender]
    WHEN [ME].[MemberType_Id] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Dealer') THEN [DEP].[Gender]
    WHEN [ME].[MemberType_Id] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Manager') THEN [MAP].[Gender]
    ELSE 'Unknown' END AS [Gender],
CASE
    WHEN [ME].[MemberType_Id] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Customer') THEN [CUP].[City_Id]
    WHEN [ME].[MemberType_Id] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Dealer') THEN [DEP].[City_Id]
    WHEN [ME].[MemberType_Id] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Manager') THEN 0
    ELSE 0 END AS [CityId]

FROM [Members].[Member] AS [ME]
INNER JOIN [Members].[MemberType] AS [MT] ON [ME].[MemberType_Id] = [MT].[Id]
INNER JOIN [Members].[Status] AS [ST] ON [ME].[Status_Id]=[ST].[Id]
INNER JOIN [Members].[ManagerProfile] AS [MAP] ON [ME].[Id]= [MAP].[Member_Id]
INNER JOIN [Members].[CustomerProfile] AS [CUP] ON [ME].[Id]=[CUP].[Member_Id]
INNER JOIN [Members].[DealerProfile] AS [DEP] ON [ME].[Id]=[DEP].[Member_Id];
GO

如您所见,重复 Case When 以查找成员类型并决定从哪个表中选择值。创建像我的视图这样的视图是个好主意吗?有没有更好的方法来检查会员类型?我应该提到表中的 Id 列MemberType可以更改,所以我不想使用这样的东西: WHEN [ME].[MemberType_Id] = 1 Then ''你的建议是什么?

4

4 回答 4

1

我想这会对你有所帮助

SELECT     
[ME].[Id] AS [MemberId],
[ME].[UserId],
[ME].[UserName],
[ME].[Email],
[ME].[Status_Id],
[ST].[Title] AS [Status],
[ME].[MemberType_Id],
[MT].[Title] AS [MemberType],
[X].[Id] AS [Profile_Id], 
[X].[FirstName],
[X].[LastName],
[X].[SSN],
[X].[Address],
[X].[PostalCode],
[X].[Gender],
[X].[City_Id]
FROM [Members].[Member] AS [ME]
INNER JOIN [Members].[MemberType] AS [MT] ON [ME].[MemberType_Id] = [MT].[Id]
INNER JOIN [Members].[Status] AS [ST] ON [ME].[Status_Id]=[ST].[Id]
INNER JOIN 
    (Select [Member_Id], [Id], [FirstName], [LastName], [SSN], [Address]='', [PostalCode]='', [Gender], [City_Id]=0,
        [TYPE] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Customer') 
        From [Members].[ManagerProfile] AS [MAP] Where [MAP].[Member_Id] = [ME].[Id]
    Union All
    Select [Member_Id], [Id], [FirstName], [LastName], [SSN], [Address], [PostalCode], [Gender], [City_Id], 
        [TYPE] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Manager') 
    From [Members].[CustomerProfile] AS [CUP] Where [CUP].[Member_Id] = [ME].[Id]
    Union All
    Select [Member_Id], [Id], [FirstName], [LastName], [SSN], [Address], [PostalCode], [Gender], [City_Id], 
        [TYPE] = (SELECT TOP(1) [TMT].[Id] FROM [Members].[MemberType] AS [TMT] WHERE [TMT].[Title]='Dealer') 
    From [Members].[DealerProfile] AS [DEP] Where [DEP].[Member_Id] = [ME].[Id])
     AS [X] On [ME].[Id] = [X].[Member_Id] AND [ME].[MemberType_Id] = [X].[TYPE]
 GO
于 2012-04-12T07:28:05.027 回答
1

如果您假设经理、客户和经销商在同一张表中,事情会变得更简单:

SELECT     
    [ME].[Id] AS [MemberId],
    [ME].[UserId],
    [ME].[UserName],
    [ME].[Email],
    [ME].[Status_Id],
    [ST].[Title] AS [Status],
    [ME].[MemberType_Id],
    [MT].[Title] AS [MemberType],
    a.ID,
    a.FirstName,
    a.LastName,
    a.SSN,
    a.Address,
    a.PostalCode,
    a.Gender,
    a.City_Id
FROM [Members].[Member] AS [ME]
INNER JOIN [Members].[MemberType] AS [MT] ON [ME].[MemberType_Id] = [MT].[Id]
INNER JOIN [Members].[Status] AS [ST] ON [ME].[Status_Id]=[ST].[Id]
INNER JOIN 
(
   select 'Manager' MemberType, Member_id, Firstname, LastName, SSN, 
          Address, PostalCode, Gender, City_id
     from ManagerProfile
   union all
   select 'Customer', Member_id, Firstname, LastName, SSN, 
          Address, PostalCode, Gender, City_id
     from CustomerProfile
   union all
   select 'Dealer', Member_id, Firstname, LastName, SSN, 
          '', '', Gender, 0
     from DealerProfile
)   
   on me.id = a.member_id
  and MT.TYPE = a.MemberType

这清楚地表明您的所有经销商、经理和客户都属于同一个表,或者可能属于每个类型的表,具有用于公共数据的公共表。MemberTypeId 将作为鉴别器从成员移动到新表。

于 2012-04-12T08:02:28.317 回答
0

您也可以CROSS JOIN使用类型。您仍将比较typid's 和case语句将更容易阅读。像这样:

SELECT     
[ME].[Id] AS [MemberId],
[ME].[UserId],
[ME].[UserName],
[ME].[Email],
[ME].[Status_Id],
[ST].[Title] AS [Status],
[ME].[MemberType_Id],
[MT].[Title] AS [MemberType],

CASE
    WHEN [ME].[MemberType_Id] = tblTypes.CustomerId THEN [CUP].[Id] 
    WHEN [ME].[MemberType_Id] = tblTypes.DealerId THEN [DEP].[Id]
    WHEN [ME].[MemberType_Id] = tblTypes.ManagerId THEN [MAP].[Id] 
    ELSE 0 END AS [Profile_Id],
CASE
    WHEN [ME].[MemberType_Id] = tblTypes.CustomerId THEN [CUP].[FirstName]
    WHEN [ME].[MemberType_Id] = (tblTypes.DealerId  THEN [DEP].[FirstName]
    WHEN [ME].[MemberType_Id] = tblTypes.ManagerId THEN [MAP].[FirstName]
    ELSE 'Unknown' END AS [FirstName],
CASE
    WHEN [ME].[MemberType_Id] = tblTypes.CustomerId THEN [CUP].[LastName]
    WHEN [ME].[MemberType_Id] = tblTypes.DealerId  THEN [DEP].[LastName]
    WHEN [ME].[MemberType_Id] = tblTypes.ManagerId THEN [MAP].[LastName]
    ELSE 'Unknown' END AS [LastName],
CASE
    WHEN [ME].[MemberType_Id] = tblTypes.CustomerId THEN [CUP].[SSN]
    WHEN [ME].[MemberType_Id] = tblTypes.DealerId  THEN [DEP].[SSN]
    WHEN [ME].[MemberType_Id] = tblTypes.ManagerId THEN [MAP].[SSN]
    ELSE 'Unknown' END AS [SSN],
CASE
    WHEN [ME].[MemberType_Id] = tblTypes.CustomerId THEN [CUP].[Address]
    WHEN [ME].[MemberType_Id] = tblTypes.DealerId  THEN [DEP].[Address]
    WHEN [ME].[MemberType_Id] = tblTypes.ManagerId THEN ''
    ELSE 'Unknown' END AS [Address],
CASE
    WHEN [ME].[MemberType_Id] = tblTypes.CustomerId THEN [CUP].[PostalCode]
    WHEN [ME].[MemberType_Id] = tblTypes.DealerId  THEN [DEP].[PostalCode]
    WHEN [ME].[MemberType_Id] = tblTypes.ManagerId THEN ''
    ELSE 'Unknown' END AS [PostalCode],
CASE
    WHEN [ME].[MemberType_Id] = tblTypes.CustomerId THEN [CUP].[Gender]
    WHEN [ME].[MemberType_Id] = tblTypes.DealerId  THEN [DEP].[Gender]
    WHEN [ME].[MemberType_Id] = tblTypes.ManagerId THEN [MAP].[Gender]
    ELSE 'Unknown' END AS [Gender],
CASE
    WHEN [ME].[MemberType_Id] = tblTypes.CustomerId THEN [CUP].[City_Id]
    WHEN [ME].[MemberType_Id] = tblTypes.DealerId  THEN [DEP].[City_Id]
    WHEN [ME].[MemberType_Id] = tblTypes.ManagerId THEN 0
    ELSE 0 END AS [CityId]

FROM [Members].[Member] AS [ME]
INNER JOIN [Members].[MemberType] AS [MT] ON [ME].[MemberType_Id] = [MT].[Id]
INNER JOIN [Members].[Status] AS [ST] ON [ME].[Status_Id]=[ST].[Id]
INNER JOIN [Members].[ManagerProfile] AS [MAP] ON [ME].[Id]= [MAP].[Member_Id]
INNER JOIN [Members].[CustomerProfile] AS [CUP] ON [ME].[Id]=[CUP].[Member_Id]
INNER JOIN [Members].[DealerProfile] AS [DEP] ON [ME].[Id]=[DEP].[Member_Id]
CROSS JOIN
    (
        SELECT
            CustomerId,
            DealerId,
            ManagerId
        FROM
            (
                SELECT 1 AS nbr
            ) AS tblType
        LEFT JOIN
            (
                SELECT TOP(1) 
                    [TMT].[Id]  AS CustomerId
                FROM 
                    [Members].[MemberType] AS [TMT] 
                WHERE 
                    [TMT].[Title]='Customer'
            ) AS Customer ON 1=1
        LEFT JOIN
            (
                SELECT TOP(1) 
                    [TMT].[Id]  AS DealerId
                FROM 
                    [Members].[MemberType] AS [TMT] 
                WHERE 
                    [TMT].[Title]='Dealer'
            ) AS Dealer ON 1=1
        LEFT JOIN
            (
                SELECT TOP(1) 
                    [TMT].[Id]  AS ManagerId
                FROM 
                    [Members].[MemberType] AS [TMT] 
                WHERE 
                    [TMT].[Title]='Manager'
            ) AS Manager ON 1=1
    ) AS tblTypes
于 2012-04-12T06:56:54.793 回答
0

那这个呢:

CASE
    WHEN [MT].[Title]='Customer' THEN [CUP].[FirstName]
    WHEN [MT].[Title]='Dealer' THEN [DEP].[FirstName]
    WHEN [MT].[Title]='Manager' THEN [MAP].[FirstName]
    ELSE 'Unknown' END AS [FirstName]
于 2012-04-12T06:48:36.963 回答