基于列状态的特定于案例的计算

时间:2016-11-27 00:45:13

标签: sql-server

这是我的费用表。它为我提供了当月的费用详情。如果我执行SELECT * from FeeTest,则会在使用GETDATE()时生成当月费用。默认情况下,IsPaid设置为false。

CREATE TABLE [dbo].[FeeTest](
    [ClassId] [int] NOT NULL,
    [HeaderId] [int] NOT NULL,
    [FeeAmount] [money] NOT NULL,
    [FeePeriod]  AS ((datename(month,getdate())+', ')+datename(year,getdate())),
    [DueDate]  AS (CONVERT([date],dateadd(month,(-1),dateadd(month,datediff(month,(0),getdate())+(1),(9))))),
    [FeeBillGeneratedOn]  AS (CONVERT([date],dateadd(month,(-1),dateadd(month,datediff(month,(0),getdate())+(1),(0))))),
    [IsPaid] [bit] NOT NULL DEFAULT ((0)),
 CONSTRAINT [UNIQ_feeHeadersTest] UNIQUE CLUSTERED 
(
    [ClassId] ASC,
    [HeaderId] 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

ALTER TABLE [dbo].[FeeTest]  WITH CHECK ADD FOREIGN KEY([ClassId])
REFERENCES [dbo].[Classes] ([ClassId])
GO

ALTER TABLE [dbo].[FeeTest]  WITH CHECK ADD FOREIGN KEY([HeaderId])
REFERENCES [dbo].[FeeHeaders] ([HeaderId])
GO

这就是我获取所有学生的费用详情

select Students.StudentId, Students.StudentName, Students.RegistrationNumber, FeeTest.FeePeriod, SUM(FeeTest.FeeAmount) AS TotalFee, FeeTest.DueDate, FeeTest.IsPaid
 from Students inner join FeeTest on Students.ClassId = FeeTest.ClassId group by Students.StudentId, Students.StudentName, Students.RegistrationNumber, FeeTest.DueDate,
 FeeTest.IsPaid, FeeTest.FeePeriod

这是我保留所有学生的月度记录的地方,无论IsPaid是0还是1。

CREATE TABLE FeePaymentStatus
(
    studentId int not null primary key,
    studentName nvarchar(255) not null,
    registrationNumber nvarchar(9) not null,
    FeePeriod nvarchar(62) not null,
    FeeAmount money not null,
    dueDate date not null,
    IsPaid bit not null,
    CONSTRAINT [UNIQ_FeePeriod] UNIQUE CLUSTERED
    (
        [StudentId],
        [FeePeriod]
    )
);

INSERT INTO FeePaymentStatus
 select Students.StudentId, Students.StudentName, Students.RegistrationNumber, FeeTest.FeePeriod, SUM(FeeTest.FeeAmount) AS TotalFee, FeeTest.DueDate, FeeTest.IsPaid
 from Students inner join FeeTest on Students.ClassId = FeeTest.ClassId group by Students.StudentId, Students.StudentName, Students.RegistrationNumber, FeeTest.DueDate,
 FeeTest.FeePeriod, FeeTest.IsPaid

接下来我要做的是为12月份再次运行时仍然将IsPaid设为0的学生加费。

select Students.StudentId, Students.StudentName, Students.RegistrationNumber, FeeTest.FeePeriod, SUM(FeeTest.FeeAmount) AS TotalFee, FeeTest.DueDate, FeeTest.IsPaid
 from Students inner join FeeTest on Students.ClassId = FeeTest.ClassId group by Students.StudentId, Students.StudentName, Students.RegistrationNumber, FeeTest.DueDate,
 FeeTest.IsPaid, FeeTest.FeePeriod

我如何实现这一目标?第一段代码中的HeaderId确实有一个标题,用于读取以前的费用,默认值为0.我使用的是SQL Server 2014。

1 个答案:

答案 0 :(得分:0)

使用类似的东西

SELECT   ProductNumber, Name, "Price Range" =   
  CASE   
     WHEN ListPrice =  0 THEN 'Mfg item - not for resale'  
     WHEN ListPrice < 50 THEN 'Under $50'  
     WHEN ListPrice >= 50 and ListPrice < 250 THEN 'Under $250'  
     WHEN ListPrice >= 250 and ListPrice < 1000 THEN 'Under $1000'  
     ELSE 'Over $1000'  
  END  
FROM Production.Product