1

抱歉,如果我的标题无论如何都具有误导性。试图为我遇到的问题想出一个准确的描述性标题时遇到了麻烦。

我有一些数据需要从我的 SQL Server 2008 R2 中提取。我只是想使用 C# ASP.Net 将此数据输出到 GridView。

我只是想看看是否有一种方法可以利用单个查询来提取我需要的所有数据。我会尽力解释我的设置并向您展示我的意思:

我有 2 个表,示例结构和数据如下。

表:费率

int  nvarchar         nvarchar         decimal nvarchar                  nvarchar                    nvarchar
ID   Location_Code_A  Location_Code_B  Rate    EffectiveRateDate_YYMMDD  TerminationRateDate_YYMMDD  Classificaton
54   123456           456789           400     110801                    999999                      C
55   123456           456789           885.3   110801                    999999                      D
56   123456           456789           901.75  110801                    999999                      T
57   123456           456789           407.5   120501                    120630                      C
58   123456           456789           850     120501                    120630                      D
59   123456           456789           898     120501                    120630                      T
60   123456           987654           325     110801                    999999                      C
61   123456           987654           345.5   120501                    120630                      C

表:跟踪

int nvarchar           nvarchar         nvarchar         nvarchar              nvarchar
ID  TrackCode          Location_Code_A  Location_Code_B  DepartureDate_YYMMDD  Classification
7   124FWDSE48W621543  123456           456789           111025                C
8   124FWDSE48W621544  123456           456789           111025                C
9   124FWDSE48W621545  123456           456789           111025                C
10  124FWDSE48W621546  123456           456789           111025                D
11  124FWDSE48W621547  123456           456789           111025                D
12  124FWDSE48W621548  123456           456789           111028                T
13  124FWDSE48W621549  123456           456789           111025                C
14  124FWDSE48W621550  123456           456789           111125                C
15  124FWDSE48W621551  123456           456789           120525                D
16  124FWDSE48W621552  123456           456789           120526                C
17  124FWDSE48W621553  123456           987654           110927                C
18  124FWDSE48W621554  123456           987654           111027                C
19  124FWDSE48W621555  123456           987654           111027                C
20  124FWDSE48W621556  123456           987654           120514                C
21  124FWDSE48W621557  123456           987654           120515                C
22  124FWDSE48W621558  123456           987654           120515                C

我想要实现的是一个分组查询,它提取Tracking.TrackCode, 乘以Tracking.Location_Code(s) 的计数,然后乘以Rates.Rate得到总数以及Tracking.DepartureDate_YYMMDD费率的有效/终止日期之间的位置。分类确实在确保使用正确的速率方面发挥了作用,但在实际的最终 GridView 中却没有。

所以我的 GridView 结果看起来像:

Location_A  Location_B  TotalTrackCodes  RateTotal
123456      456789      10               5522.35
123456      987654      6                2011.5

您可以看到,当费率的离开日期介于其Rates.EffectiveRateDate&的 2 个不同记录之间时,就会出现我的问题Rates.TerminationRateDate。发生这种情况时,我想获取最新的费率信息,基本上是具有更新 Rates.EffectiveRateDate 的信息(如果转换为 int,则为最高 EffectiveRateDate)。

由于 EffectiveRateDates 不是日期格式,我只是在查询中将它们转换为 int 并将它们用作一种范围而不是进行检查等。似乎以这种方式工作得很好。

是否有可能做一个查询,我可以将它作为 DataSet 传递给我的 GridView 来给我正确的结果?

请注意,我没有构建这些表/格式等。它们已经由第 3 方填充,我只是想查询这些表,因此无法自行调整表。

非常感谢您的帮助。

为了节省人们的时间和帮助,请参阅下面的每个表及其数据内容的 SQL 转储:

/****** Object:  Table [dbo].[Tracking]    Script Date: 08/21/2012 14:03:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Tracking](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [TrackCode] [nvarchar](50) NULL,
    [Location_Code_A] [nvarchar](50) NULL,
    [Location_Code_B] [nvarchar](50) NULL,
    [DepartureDate_YYMMDD] [nvarchar](50) NULL,
    [Classification] [nvarchar](50) NULL,
 CONSTRAINT [PK_Tracking] PRIMARY KEY CLUSTERED 
(
    [ID] 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
SET IDENTITY_INSERT [dbo].[Tracking] ON
INSERT [dbo].[Tracking] ([ID], [TrackCode], [Location_Code_A], [Location_Code_B], [DepartureDate_YYMMDD], [Classification]) VALUES (1, N'124FWDSE48W621543', N'123456', N'456789', N'111025', N'C')
INSERT [dbo].[Tracking] ([ID], [TrackCode], [Location_Code_A], [Location_Code_B], [DepartureDate_YYMMDD], [Classification]) VALUES (2, N'124FWDSE48W621544', N'123456', N'456789', N'111025', N'C')
INSERT [dbo].[Tracking] ([ID], [TrackCode], [Location_Code_A], [Location_Code_B], [DepartureDate_YYMMDD], [Classification]) VALUES (3, N'124FWDSE48W621545', N'123456', N'456789', N'111025', N'C')
INSERT [dbo].[Tracking] ([ID], [TrackCode], [Location_Code_A], [Location_Code_B], [DepartureDate_YYMMDD], [Classification]) VALUES (4, N'124FWDSE48W621546', N'123456', N'456789', N'111025', N'D')
INSERT [dbo].[Tracking] ([ID], [TrackCode], [Location_Code_A], [Location_Code_B], [DepartureDate_YYMMDD], [Classification]) VALUES (5, N'124FWDSE48W621547', N'123456', N'456789', N'111025', N'D')
INSERT [dbo].[Tracking] ([ID], [TrackCode], [Location_Code_A], [Location_Code_B], [DepartureDate_YYMMDD], [Classification]) VALUES (6, N'124FWDSE48W621548', N'123456', N'456789', N'111028', N'T')
INSERT [dbo].[Tracking] ([ID], [TrackCode], [Location_Code_A], [Location_Code_B], [DepartureDate_YYMMDD], [Classification]) VALUES (7, N'124FWDSE48W621549', N'123456', N'456789', N'111025', N'C')
INSERT [dbo].[Tracking] ([ID], [TrackCode], [Location_Code_A], [Location_Code_B], [DepartureDate_YYMMDD], [Classification]) VALUES (8, N'124FWDSE48W621550', N'123456', N'456789', N'111025', N'C')
INSERT [dbo].[Tracking] ([ID], [TrackCode], [Location_Code_A], [Location_Code_B], [DepartureDate_YYMMDD], [Classification]) VALUES (9, N'124FWDSE48W621551', N'123456', N'456789', N'120525', N'D')
INSERT [dbo].[Tracking] ([ID], [TrackCode], [Location_Code_A], [Location_Code_B], [DepartureDate_YYMMDD], [Classification]) VALUES (10, N'124FWDSE48W621552', N'123456', N'456789', N'120526', N'C')
INSERT [dbo].[Tracking] ([ID], [TrackCode], [Location_Code_A], [Location_Code_B], [DepartureDate_YYMMDD], [Classification]) VALUES (11, N'124FWDSE48W621553', N'123456', N'987654', N'110927', N'C')
INSERT [dbo].[Tracking] ([ID], [TrackCode], [Location_Code_A], [Location_Code_B], [DepartureDate_YYMMDD], [Classification]) VALUES (12, N'124FWDSE48W621554', N'123456', N'987654', N'111027', N'C')
INSERT [dbo].[Tracking] ([ID], [TrackCode], [Location_Code_A], [Location_Code_B], [DepartureDate_YYMMDD], [Classification]) VALUES (13, N'124FWDSE48W621555', N'123456', N'987654', N'111027', N'C')
INSERT [dbo].[Tracking] ([ID], [TrackCode], [Location_Code_A], [Location_Code_B], [DepartureDate_YYMMDD], [Classification]) VALUES (14, N'124FWDSE48W621556', N'123456', N'987654', N'120514', N'C')
INSERT [dbo].[Tracking] ([ID], [TrackCode], [Location_Code_A], [Location_Code_B], [DepartureDate_YYMMDD], [Classification]) VALUES (15, N'124FWDSE48W621557', N'123456', N'987654', N'120515', N'C')
INSERT [dbo].[Tracking] ([ID], [TrackCode], [Location_Code_A], [Location_Code_B], [DepartureDate_YYMMDD], [Classification]) VALUES (16, N'124FWDSE48W621558', N'123456', N'987654', N'120515', N'C')
SET IDENTITY_INSERT [dbo].[Tracking] OFF
/****** Object:  Table [dbo].[Rates]    Script Date: 08/21/2012 14:03:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Rates](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Location_Code_A] [nvarchar](50) NULL,
    [Location_Code_B] [nvarchar](50) NULL,
    [Rate] [nvarchar](50) NULL,
    [EffectiveRateDate_YYMMDD] [nvarchar](50) NULL,
    [TerminationRateDate_YYMMDD] [nvarchar](50) NULL,
    [Classification] [nvarchar](50) NULL,
 CONSTRAINT [PK_Rates] PRIMARY KEY CLUSTERED 
(
    [ID] 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
SET IDENTITY_INSERT [dbo].[Rates] ON
INSERT [dbo].[Rates] ([ID], [Location_Code_A], [Location_Code_B], [Rate], [EffectiveRateDate_YYMMDD], [TerminationRateDate_YYMMDD], [Classification]) VALUES (1, N'123456', N'456789', N'400', N'110801', N'999999', N'C')
INSERT [dbo].[Rates] ([ID], [Location_Code_A], [Location_Code_B], [Rate], [EffectiveRateDate_YYMMDD], [TerminationRateDate_YYMMDD], [Classification]) VALUES (2, N'123456', N'456789', N'885.3', N'110801', N'999999', N'D')
INSERT [dbo].[Rates] ([ID], [Location_Code_A], [Location_Code_B], [Rate], [EffectiveRateDate_YYMMDD], [TerminationRateDate_YYMMDD], [Classification]) VALUES (3, N'123456', N'456789', N'901.75', N'110801', N'999999', N'T')
INSERT [dbo].[Rates] ([ID], [Location_Code_A], [Location_Code_B], [Rate], [EffectiveRateDate_YYMMDD], [TerminationRateDate_YYMMDD], [Classification]) VALUES (4, N'123456', N'456789', N'407.5', N'120501', N'120630', N'C')
INSERT [dbo].[Rates] ([ID], [Location_Code_A], [Location_Code_B], [Rate], [EffectiveRateDate_YYMMDD], [TerminationRateDate_YYMMDD], [Classification]) VALUES (5, N'123456', N'456789', N'850', N'120501', N'120630', N'D')
INSERT [dbo].[Rates] ([ID], [Location_Code_A], [Location_Code_B], [Rate], [EffectiveRateDate_YYMMDD], [TerminationRateDate_YYMMDD], [Classification]) VALUES (6, N'123456', N'456789', N'898', N'120501', N'120630', N'T')
INSERT [dbo].[Rates] ([ID], [Location_Code_A], [Location_Code_B], [Rate], [EffectiveRateDate_YYMMDD], [TerminationRateDate_YYMMDD], [Classification]) VALUES (7, N'123456', N'987654', N'325', N'110801', N'999999', N'C')
INSERT [dbo].[Rates] ([ID], [Location_Code_A], [Location_Code_B], [Rate], [EffectiveRateDate_YYMMDD], [TerminationRateDate_YYMMDD], [Classification]) VALUES (8, N'123456', N'987654', N'345.5', N'120501', N'120630', N'C')
SET IDENTITY_INSERT [dbo].[Rates] OFF
4

1 回答 1

1
select
    Location_Code_A,
    Location_Code_B,
    COUNT(*),
    SUM(rate)
from
(    
    select 
    ROW_NUMBER() over (partition by tracking.id order by rates.effectiveratedate_yymmdd desc) rn,
    tracking.*, 
    convert(decimal(9,5),rates.Rate) rate
from 
    tracking
        inner join  
    rates
        on tracking.Location_Code_A = rates.Location_Code_A
        and tracking.Location_Code_B = rates.Location_Code_B
        and tracking.Classification = rates.Classification
        and tracking.DepartureDate_YYMMDD>=rates.EffectiveRateDate_YYMMDD
) v
where rn=1  
    group by Location_Code_A, Location_Code_B
    order by Location_Code_A, Location_Code_B

生产

123456   456789 10  5929.85000
123456   987654 6   2011.50000
于 2012-08-21T13:32:20.510 回答