简体   繁体   English

从现有表创建新的 SQL Server 表

[英]Creating a new SQL Server table from an existing table

I have the job of updating a database table by removing the month column and recreating that table grouping on column fields and summing the hours column.我的工作是通过删除月份列并在列字段上重新创建该表分组并对小时列求和来更新数据库表。

Here are my instructions: "Create a new PS_StaffHours table without the Month column. Migrate the existing PS_StaffHours data to the new table by grouping on (PSScnID/PSEmpID/Prd/WBSID/RatePrd) and summing the hours."以下是我的说明:“创建一个没有 Month 列的新 PS_StaffHours 表。通过对 (PSScnID/PSEmpID/Prd/WBSID/RatePrd) 进行分组并对小时数求和,将现有的 PS_StaffHours 数据迁移到新表。”

Now here is the table I scripted out and have to re-create without the Month column and summing the hour column:现在这是我编写的表格,必须在没有 Month 列的情况下重新创建并对小时列求和:

CREATE TABLE [dbo].[PS_StaffHours]
(
    [PSScnID] [int] NOT NULL,
    [PSEmpID] [int] NOT NULL,
    [Prd] [int] NOT NULL,
    [WBSID] [int] NOT NULL,
    [RatePrd] [int] NOT NULL,
    [Month] [date] NOT NULL,
    [Hours] [decimal](16, 8) NOT NULL,

   CONSTRAINT [PK_PS_StaffHours] 
       PRIMARY KEY CLUSTERED ([PSScnID] ASC, [PSEmpID] ASC, [Prd] ASC,
                              [WBSID] ASC, [RatePrd] ASC, [Month] 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].[PS_StaffHours] WITH CHECK 
    ADD CONSTRAINT [FK_PS_StaffHours_PS_Scenarios] 
        FOREIGN KEY([PSScnID]) REFERENCES [dbo].[PS_Scenarios] ([PSScnID])
GO

ALTER TABLE [dbo].[PS_StaffHours] CHECK CONSTRAINT [FK_PS_StaffHours_PS_Scenarios]
GO

ALTER TABLE [dbo].[PS_StaffHours] WITH CHECK 
    ADD CONSTRAINT [FK_PS_StaffHours_PS_Staff] 
        FOREIGN KEY([PSScnID], [PSEmpID]) REFERENCES [dbo].[PS_Staff] ([PSScnID], [PSEmpID])
GO

ALTER TABLE [dbo].[PS_StaffHours] CHECK CONSTRAINT [FK_PS_StaffHours_PS_Staff]
GO

ALTER TABLE [dbo].[PS_StaffHours] WITH CHECK 
    ADD CONSTRAINT [FK_PS_StaffHours_PS_WBS] 
        FOREIGN KEY([PSScnID], [WBSID]) REFERENCES [dbo].[PS_WBS] ([PSScnID], [WBSID])
GO

ALTER TABLE [dbo].[PS_StaffHours] CHECK CONSTRAINT [FK_PS_StaffHours_PS_WBS]
GO

ALTER TABLE [dbo].[PS_StaffHours] WITH CHECK 
    ADD CONSTRAINT [CK_PS_StaffHours] CHECK ((DATEPART(day, [Month]) = (1)))
GO

ALTER TABLE [dbo].[PS_StaffHours] CHECK CONSTRAINT [CK_PS_StaffHours]
GO

EXEC sys.sp_addextendedproperty 
         @name = N'MS_Description', 
         @value = N'Restricts Month field to the 1st of every month.' , 
         @level0type = N'SCHEMA', @level0name = N'dbo',  
         @level1type = N'TABLE', @level1name = N'PS_StaffHours',  
         @level2type = N'CONSTRAINT', @level2name = N'CK_PS_StaffHours'
GO

First, to create the new table I think all I have to do is rename the table name in the CREATE portion of the script and just remove all references of the Month column in the CREATE statement.首先,要创建新表,我认为我所要做的就是在脚本的 CREATE 部分重命名表名,然后删除 CREATE 语句中 Month 列的所有引用。 Am I correct in my thinking about the CREATE script?我对 CREATE 脚本的想法是否正确?

Now, here is the script I'm going to use to populate the table without the Month field but I'm not sure if I have the grouping correct because I am getting an error because I'm not grouping on the Hours column with the rest.现在,这是我将用来在没有 Month 字段的情况下填充表格的脚本,但我不确定我的分组是否正确,因为我收到错误,因为我没有在 hours 列上grouping休息。 Also, I'm unsure about how to go about summing the hours in the query.另外,我不确定如何summing查询中的小时数。

My code:我的代码:

SELECT c.PSScnID, c.PSEmpID, c.Prd, c.WBSID, c.RatePrd, c.Hours  
INTO dbo.NewPS_StaffHours 
FROM PS_StaffHours AS c  
GROUP BY c.PSScnID, c.PSEmpID, c.Prd, c.WBSID, c.RatePrd  
GO

Your first assumption is correct.你的第一个假设是正确的。 To group by your columns and sum up the hours - try this:要按列分组并总结小时数 - 试试这个:

SELECT c.PSScnID, c.PSEmpID, c.Prd, c.WBSID, c.RatePrd, SUM(c.Hours) AS Hours
INTO dbo.NewPS_StaffHours 
FROM PS_StaffHours AS c  
GROUP BY c.PSScnID, c.PSEmpID, c.Prd, c.WBSID, c.RatePrd  
GO

Sounds like you're going to be creating your table before running your select.听起来您要在运行选择之前创建表。 So get rid of the INTO dbo.NewPS_StaffHours .所以摆脱INTO dbo.NewPS_StaffHours Run a create table statement and then and INSERT INTO newTable VALUES... SELECT运行 create table 语句,然后INSERT INTO newTable VALUES... SELECT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM