使用内部连接选择单行

时间:2015-05-20 07:12:47

标签: sql-server inner-join

我需要您提供有关Sql查询的帮助。我正在尝试实施两张牌的点数概念。所以我创建了一个名为MargePoints的参考表,并将CardIDs(CardDetails表的主键)保存为CardID和MargedID.After Points marged,我试图在一行中显示CardID和MargedID的所有信息。向我发送相关表格和查询的脚本。

SELECT MargePointsID,
       DOC,
       AdminName,
       MargedPoints,
       CardNumber,
       CustomerName,
       CardNumber2,
       CustomerName2 
FROM (

       SELECT   MargePoints.MargePointsID,  
                MargePoints.DOC, 
                AdminUsers.AdminName, 
                MargePoints.MargedPoints, 
                CardDetails.CardNumber, 
                CustomerDetails.CustomerName,
                '' AS CardNumber2 ,
                '' AS CustomerName2 

        FROM AdminUsers 
        INNER JOIN MargePoints ON AdminUsers.AdminUserID = MargePoints.AdminID 
        INNER JOIN CustomerDetails 
        INNER JOIN CardDetails ON CustomerDetails.CardID = CardDetails.CardID ON MargePoints.MargedID = CardDetails.CardID 

        UNION ALL 

        SELECT  MargePoints.MargePointsID,
                MargePoints.DOC, 
                AdminUsers.AdminName,
                MargePoints.MargedPoints,
                '' AS CardNumber ,
                '' AS CustomerName, 
                CardDetails.CardNumber AS CardNumber2,
                CustomerDetails.CustomerName AS CustomerName2 
         FROM AdminUsers 
         INNER JOIN MargePoints ON AdminUsers.AdminUserID = MargePoints.AdminID 
         INNER JOIN CustomerDetails 
         INNER JOIN CardDetails ON CustomerDetails.CardID = CardDetails.CardID ON MargePoints.CardID = CardDetails.CardID
     ) a

    /****** Object:  Table [dbo].[MargePoints]    Script Date: 05/20/2015 02:29:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[MargePoints](
    [MargePointsID] [int] IDENTITY(1,1) NOT NULL,
    [CardID] [int] NOT NULL,
    [MargedID] [int] NOT NULL,
    [MargedPoints] [float] NOT NULL,
    [DOC] [datetime] NOT NULL,
    [IsApproved] [bit] NOT NULL,
    [ApprovedDate] [datetime] NULL,
    [AdminID] [int] NOT NULL
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[MargePoints] ON
INSERT [dbo].[MargePoints] ([MargePointsID], [CardID], [MargedID], [MargedPoints], [DOC], [IsApproved], [ApprovedDate], [AdminID]) VALUES (2, 1, 9, 20, CAST(0x0000A49B0152F50B AS DateTime), 1, CAST(0x0000A49D00D0E7B0 AS DateTime), 2)
SET IDENTITY_INSERT [dbo].[MargePoints] OFF
/****** Object:  Table [dbo].[CustomerDetails]    Script Date: 05/20/2015 02:29:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[CustomerDetails](
    [CustomerID] [int] IDENTITY(1,1) NOT NULL,
    [CardID] [int] NOT NULL,
    [VehicleID] [int] NOT NULL,
    [FuelID] [int] NOT NULL,
    [AdminUserID] [int] NOT NULL,
    [CustomerName] [nvarchar](50) NOT NULL,
    [MobileNumber] [nvarchar](50) NOT NULL,
    [VehicleName] [nvarchar](50) NOT NULL,
    [VehicleNumber] [nvarchar](50) NOT NULL,
    [OpeningBalance] [int] NOT NULL,
    [TotalPoints] [int] NOT NULL,
    [IsDeleted] [bit] NOT NULL,
    [IsApproved] [int] NOT NULL,
    [IsLubricant] [bit] NOT NULL,
 CONSTRAINT [PK_UserDetails] PRIMARY KEY CLUSTERED 
(
    [CustomerID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'0-False, 1-True' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'CustomerDetails', @level2type=N'COLUMN',@level2name=N'IsDeleted'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'0-no lubricant,1-Lubricant' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'CustomerDetails', @level2type=N'COLUMN',@level2name=N'IsLubricant'
GO
SET IDENTITY_INSERT [dbo].[CustomerDetails] ON
INSERT [dbo].[CustomerDetails] ([CustomerID], [CardID], [VehicleID], [FuelID], [AdminUserID], [CustomerName], [MobileNumber], [VehicleName], [VehicleNumber], [OpeningBalance], [TotalPoints], [IsDeleted], [IsApproved], [IsLubricant]) VALUES (5, 1, 1, 1, 2, N'Minku Hazarika', N'123', N'1231', N'12341', 123456, 0, 0, 1, 0)
INSERT [dbo].[CustomerDetails] ([CustomerID], [CardID], [VehicleID], [FuelID], [AdminUserID], [CustomerName], [MobileNumber], [VehicleName], [VehicleNumber], [OpeningBalance], [TotalPoints], [IsDeleted], [IsApproved], [IsLubricant]) VALUES (6, 24, 1, 1, 2, N'qwe', N'qwe', N'weq', N'qwe', 0, 0, 0, 0, 0)
INSERT [dbo].[CustomerDetails] ([CustomerID], [CardID], [VehicleID], [FuelID], [AdminUserID], [CustomerName], [MobileNumber], [VehicleName], [VehicleNumber], [OpeningBalance], [TotalPoints], [IsDeleted], [IsApproved], [IsLubricant]) VALUES (9, 9, 1, 1, 2, N'minku', N'55555554444444', N'sdf', N'qw', 0, 0, 0, 1, 0)
SET IDENTITY_INSERT [dbo].[CustomerDetails] OFF
/****** Object:  Table [dbo].[CardDetails]    Script Date: 05/20/2015 02:29:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[CardDetails](
    [CardID] [int] IDENTITY(1,1) NOT NULL,
    [CardNumber] [nvarchar](50) NOT NULL,
    [IsCancelled] [int] NOT NULL,
 CONSTRAINT [PK_CardDetails] PRIMARY KEY CLUSTERED 
(
    [CardID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'0-False, 1-True' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'CardDetails', @level2type=N'COLUMN',@level2name=N'IsCancelled'
GO
SET IDENTITY_INSERT [dbo].[CardDetails] ON
INSERT [dbo].[CardDetails] ([CardID], [CardNumber], [IsCancelled]) VALUES (1, N'ASDUSF73444', 0)
INSERT [dbo].[CardDetails] ([CardID], [CardNumber], [IsCancelled]) VALUES (4, N'1234', 1)
INSERT [dbo].[CardDetails] ([CardID], [CardNumber], [IsCancelled]) VALUES (6, N';0000011119?', 1)
INSERT [dbo].[CardDetails] ([CardID], [CardNumber], [IsCancelled]) VALUES (9, N';0000011119?', 0)
INSERT [dbo].[CardDetails] ([CardID], [CardNumber], [IsCancelled]) VALUES (16, N';018262?', 1)
INSERT [dbo].[CardDetails] ([CardID], [CardNumber], [IsCancelled]) VALUES (23, N'www', 1)
INSERT [dbo].[CardDetails] ([CardID], [CardNumber], [IsCancelled]) VALUES (24, N'ww', 0)
INSERT [dbo].[CardDetails] ([CardID], [CardNumber], [IsCancelled]) VALUES (25, N'wwww', 0)
SET IDENTITY_INSERT [dbo].[CardDetails] OFF
/****** Object:  Table [dbo].[AdminUsers]    Script Date: 05/20/2015 02:29:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[AdminUsers](
    [AdminUserID] [int] IDENTITY(1,1) NOT NULL,
    [RoleID] [int] NOT NULL,
    [Email] [nvarchar](150) NOT NULL,
    [UserName] [nvarchar](100) NOT NULL,
    [Designation] [nvarchar](100) NULL,
    [AdminName] [nvarchar](50) NULL,
    [Password] [nvarchar](255) NOT NULL,
    [Address] [nvarchar](255) NULL,
    [PostCode] [nvarchar](10) NULL,
    [Mobile] [nvarchar](50) NULL,
    [IsDeleted] [int] NOT NULL,
    [DOC] [datetime] NULL,
    [Gender] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_AdminUsers_1] PRIMARY KEY CLUSTERED 
(
    [AdminUserID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'0-False, 1-True' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'AdminUsers', @level2type=N'COLUMN',@level2name=N'IsDeleted'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'AdminUsers', @level2type=N'COLUMN',@level2name=N'Gender'
GO
SET IDENTITY_INSERT [dbo].[AdminUsers] ON
INSERT [dbo].[AdminUsers] ([AdminUserID], [RoleID], [Email], [UserName], [Designation], [AdminName], [Password], [Address], [PostCode], [Mobile], [IsDeleted], [DOC], [Gender]) VALUES (2, 1, N'minku@gmail.com', N'admin', N'admin', N'admin', N'admin', N'9th Mile', N'123456', N'9876543210', 0, NULL, N'Male')
SET IDENTITY_INSERT [dbo].[AdminUsers] OFF
/****** Object:  Default [DF_CardDetails_IsCancelled]    Script Date: 05/20/2015 02:29:47 ******/
ALTER TABLE [dbo].[CardDetails] ADD  CONSTRAINT [DF_CardDetails_IsCancelled]  DEFAULT ((0)) FOR [IsCancelled]
GO
/****** Object:  Default [DF_CustomerDetails_OpeningBalance]    Script Date: 05/20/2015 02:29:47 ******/
ALTER TABLE [dbo].[CustomerDetails] ADD  CONSTRAINT [DF_CustomerDetails_OpeningBalance]  DEFAULT ((0)) FOR [OpeningBalance]
GO
/****** Object:  Default [DF_CustomerDetails_TotalPoints]    Script Date: 05/20/2015 02:29:47 ******/
ALTER TABLE [dbo].[CustomerDetails] ADD  CONSTRAINT [DF_CustomerDetails_TotalPoints]  DEFAULT ((0)) FOR [TotalPoints]
GO
/****** Object:  Default [DF_CustomerDetails_IsDeleted]    Script Date: 05/20/2015 02:29:47 ******/
ALTER TABLE [dbo].[CustomerDetails] ADD  CONSTRAINT [DF_CustomerDetails_IsDeleted]  DEFAULT ((0)) FOR [IsDeleted]
GO
/****** Object:  Default [DF_CustomerDetails_IsLubricant]    Script Date: 05/20/2015 02:29:47 ******/
ALTER TABLE [dbo].[CustomerDetails] ADD  CONSTRAINT [DF_CustomerDetails_IsLubricant]  DEFAULT ((0)) FOR [IsLubricant]
GO
/****** Object:  Default [DF_AdminUsers_DOC_1]    Script Date: 05/20/2015 02:29:47 ******/
ALTER TABLE [dbo].[AdminUsers] ADD  CONSTRAINT [DF_AdminUsers_DOC_1]  DEFAULT (getdate()) FOR [DOC]
GO
/****** Object:  Default [DF_MargePoints_DOC]    Script Date: 05/20/2015 02:29:47 ******/
ALTER TABLE [dbo].[MargePoints] ADD  CONSTRAINT [DF_MargePoints_DOC]  DEFAULT (getdate()) FOR [DOC]
GO
/****** Object:  Default [DF_MargePoints_IsApproved]    Script Date: 05/20/2015 02:29:47 ******/
ALTER TABLE [dbo].[MargePoints] ADD  CONSTRAINT [DF_MargePoints_IsApproved]  DEFAULT ((0)) FOR [IsApproved]
GO

0 个答案:

没有答案