![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I have an Excel spreadsheet that contains a history of football games which is in this format: Year | Week | Team | Away/Home | Various stats for each team. 1992 1 NO A 1992 1 PHI H 1992 1 DET A 1992 1 CHI H I would like to get it in a relational format to do analysis so that requires me to create two tables (Schedule and GameStats). These are the two tables in which I would like to import into: CREATE TABLE [dbo].[Schedule]( [ScheduleId] [int] IDENTITY(1,1) NOT NULL, [Year] [int] NULL, [Week] [int] NULL, [HTeam] [varchar](3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [ATeam] [varchar](3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, CONSTRAINT [PK_Schedule] PRIMARY KEY CLUSTERED ( [ScheduleId] ASC )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] CREATE TABLE [dbo].[GameStats]( [ScheduleId] [int] NOT NULL, [HFirstDowns] [int] NULL, [HRushes] [int] NULL, [HRushingYds] [int] NULL, [HPassingYds] [int] NULL, [HPassCompletions] [int] NULL, [HPassAttempts] [int] NULL, [HInts] [int] NULL, [HReturnYds] [int] NULL, [HSacksAllowed] [int] NULL, [HSacksYdsLost] [int] NULL, [HPunts] [int] NULL, [HAvgPunt] [int] NULL, [HFumbles] [int] NULL, [HFumblesLost] [int] NULL, [HPenalties] [int] NULL, [HPenaltyYdsLost] [int] NULL, [HTimeOfPossession] [varchar](8) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [AFirstDowns] [int] NULL, [ARushes] [int] NULL, [ARushingYds] [int] NULL, [APassingYds] [int] NULL, [APassCompletions] [int] NULL, [APassAttempts] [int] NULL, [AInts] [int] NULL, [AReturnYds] [int] NULL, [ASacksAllowed] [int] NULL, [ASacksYdsLost] [int] NULL, [APunts] [int] NULL, [AAvgPunt] [int] NULL, [AFumbles] [int] NULL, [AFumblesLost] [int] NULL, [APenalties] [int] NULL, [APenaltyYdsLost] [int] NULL, [ATimeOfPossession] [varchar](8) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, CONSTRAINT [PK_GameStats] PRIMARY KEY CLUSTERED ( [ScheduleId] ASC )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] I am having troubles with trying to create the one row in the Schedule table using the two rows in the Excel spreadsheet. I basically have to get the two rows in Excel and convert into one row in the table. I then have to use the newly inserted ScheduleId and do the same for the GameStats. Can someone help me with this (tutorial or other links)? |
#3
| |||
| |||
|
|
So can you identify the 2 rows that are linked? I would be inclined to use the Merge Join Transofrm to start with I would have two sources of the same Spreadsheet I would link on keys I would then only return the columns from the relevant side of the join If this does not do it for you then you can always look at a Script Component as a transform. -- Allan Mitchell Konesans Ltd T +44 7966 476 572 F +44 2071 008 479 http://www.konesans.com "Jason" <Jason (AT) discussions (DOT) microsoft.com> wrote in message news:4CC3FDF7-FF06-4CA5-B803-6DA9B6AD2121 (AT) microsoft (DOT) com... I have an Excel spreadsheet that contains a history of football games which is in this format: Year | Week | Team | Away/Home | Various stats for each team. 1992 1 NO A 1992 1 PHI H 1992 1 DET A 1992 1 CHI H I would like to get it in a relational format to do analysis so that requires me to create two tables (Schedule and GameStats). These are the two tables in which I would like to import into: CREATE TABLE [dbo].[Schedule]( [ScheduleId] [int] IDENTITY(1,1) NOT NULL, [Year] [int] NULL, [Week] [int] NULL, [HTeam] [varchar](3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [ATeam] [varchar](3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, CONSTRAINT [PK_Schedule] PRIMARY KEY CLUSTERED ( [ScheduleId] ASC )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] CREATE TABLE [dbo].[GameStats]( [ScheduleId] [int] NOT NULL, [HFirstDowns] [int] NULL, [HRushes] [int] NULL, [HRushingYds] [int] NULL, [HPassingYds] [int] NULL, [HPassCompletions] [int] NULL, [HPassAttempts] [int] NULL, [HInts] [int] NULL, [HReturnYds] [int] NULL, [HSacksAllowed] [int] NULL, [HSacksYdsLost] [int] NULL, [HPunts] [int] NULL, [HAvgPunt] [int] NULL, [HFumbles] [int] NULL, [HFumblesLost] [int] NULL, [HPenalties] [int] NULL, [HPenaltyYdsLost] [int] NULL, [HTimeOfPossession] [varchar](8) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [AFirstDowns] [int] NULL, [ARushes] [int] NULL, [ARushingYds] [int] NULL, [APassingYds] [int] NULL, [APassCompletions] [int] NULL, [APassAttempts] [int] NULL, [AInts] [int] NULL, [AReturnYds] [int] NULL, [ASacksAllowed] [int] NULL, [ASacksYdsLost] [int] NULL, [APunts] [int] NULL, [AAvgPunt] [int] NULL, [AFumbles] [int] NULL, [AFumblesLost] [int] NULL, [APenalties] [int] NULL, [APenaltyYdsLost] [int] NULL, [ATimeOfPossession] [varchar](8) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, CONSTRAINT [PK_GameStats] PRIMARY KEY CLUSTERED ( [ScheduleId] ASC )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] I am having troubles with trying to create the one row in the Schedule table using the two rows in the Excel spreadsheet. I basically have to get the two rows in Excel and convert into one row in the table. I then have to use the newly inserted ScheduleId and do the same for the GameStats. Can someone help me with this (tutorial or other links)? |
#4
| |||
| |||
|
|
The rows are sequential. The only way to identify the rows is to start from the top and grab the row with the A in the Away/Home column and then grab the row with the H. This makes up the one row in the table. After that then move to rows 3 & 4. "Allan Mitchell" wrote: So can you identify the 2 rows that are linked? I would be inclined to use the Merge Join Transofrm to start with I would have two sources of the same Spreadsheet I would link on keys I would then only return the columns from the relevant side of the join If this does not do it for you then you can always look at a Script Component as a transform. -- Allan Mitchell Konesans Ltd T +44 7966 476 572 F +44 2071 008 479 http://www.konesans.com "Jason" <Jason (AT) discussions (DOT) microsoft.com> wrote in message news:4CC3FDF7-FF06-4CA5-B803-6DA9B6AD2121 (AT) microsoft (DOT) com... I have an Excel spreadsheet that contains a history of football games which is in this format: Year | Week | Team | Away/Home | Various stats for each team. 1992 1 NO A 1992 1 PHI H 1992 1 DET A 1992 1 CHI H I would like to get it in a relational format to do analysis so that requires me to create two tables (Schedule and GameStats). These are the two tables in which I would like to import into: CREATE TABLE [dbo].[Schedule]( [ScheduleId] [int] IDENTITY(1,1) NOT NULL, [Year] [int] NULL, [Week] [int] NULL, [HTeam] [varchar](3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [ATeam] [varchar](3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, CONSTRAINT [PK_Schedule] PRIMARY KEY CLUSTERED ( [ScheduleId] ASC )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] CREATE TABLE [dbo].[GameStats]( [ScheduleId] [int] NOT NULL, [HFirstDowns] [int] NULL, [HRushes] [int] NULL, [HRushingYds] [int] NULL, [HPassingYds] [int] NULL, [HPassCompletions] [int] NULL, [HPassAttempts] [int] NULL, [HInts] [int] NULL, [HReturnYds] [int] NULL, [HSacksAllowed] [int] NULL, [HSacksYdsLost] [int] NULL, [HPunts] [int] NULL, [HAvgPunt] [int] NULL, [HFumbles] [int] NULL, [HFumblesLost] [int] NULL, [HPenalties] [int] NULL, [HPenaltyYdsLost] [int] NULL, [HTimeOfPossession] [varchar](8) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [AFirstDowns] [int] NULL, [ARushes] [int] NULL, [ARushingYds] [int] NULL, [APassingYds] [int] NULL, [APassCompletions] [int] NULL, [APassAttempts] [int] NULL, [AInts] [int] NULL, [AReturnYds] [int] NULL, [ASacksAllowed] [int] NULL, [ASacksYdsLost] [int] NULL, [APunts] [int] NULL, [AAvgPunt] [int] NULL, [AFumbles] [int] NULL, [AFumblesLost] [int] NULL, [APenalties] [int] NULL, [APenaltyYdsLost] [int] NULL, [ATimeOfPossession] [varchar](8) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, CONSTRAINT [PK_GameStats] PRIMARY KEY CLUSTERED ( [ScheduleId] ASC )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] I am having troubles with trying to create the one row in the Schedule table using the two rows in the Excel spreadsheet. I basically have to get the two rows in Excel and convert into one row in the table. I then have to use the newly inserted ScheduleId and do the same for the GameStats. Can someone help me with this (tutorial or other links)? |
#5
| |||
| |||
|
|
More than likely, you'll have to import the spreadsheet into a temp table, then loop through each row using a cursor. Each time you hit a row with an "A", you'll then grab the next row and do your insert. Hopefully this is a one time import as cursors run very slowly. If you haven't used them before, see books on line for some cursor examples. Here's some utterly untested code that assumes your rows will always be in the order A first, then H. Hopefully it gets you going. declare @AwayTeam varchar(100), @HomeTeam varchar(100), @RowType char(1), @TeamName varchar(100) declare test cursor for select * from spreadsheet open test fetch next from test into @RowType, @TeamName while @@fetch_status = 0 begin if @RowType = 'A' begin select @AwayTeam = @TeamName fetch next from test into @rowtype, @TeamName select @HomeTeam = @TeamName insert into TargetTable VALUES (@HomeTeam, @AwayTeam) END fetch next from test into @rowType, @TeamName END close test deallocate test "Jason" wrote: The rows are sequential. The only way to identify the rows is to start from the top and grab the row with the A in the Away/Home column and then grab the row with the H. This makes up the one row in the table. After that then move to rows 3 & 4. "Allan Mitchell" wrote: So can you identify the 2 rows that are linked? I would be inclined to use the Merge Join Transofrm to start with I would have two sources of the same Spreadsheet I would link on keys I would then only return the columns from the relevant side of the join If this does not do it for you then you can always look at a Script Component as a transform. -- Allan Mitchell Konesans Ltd T +44 7966 476 572 F +44 2071 008 479 http://www.konesans.com "Jason" <Jason (AT) discussions (DOT) microsoft.com> wrote in message news:4CC3FDF7-FF06-4CA5-B803-6DA9B6AD2121 (AT) microsoft (DOT) com... I have an Excel spreadsheet that contains a history of football games which is in this format: Year | Week | Team | Away/Home | Various stats for each team. 1992 1 NO A 1992 1 PHI H 1992 1 DET A 1992 1 CHI H I would like to get it in a relational format to do analysis so that requires me to create two tables (Schedule and GameStats). These are the two tables in which I would like to import into: CREATE TABLE [dbo].[Schedule]( [ScheduleId] [int] IDENTITY(1,1) NOT NULL, [Year] [int] NULL, [Week] [int] NULL, [HTeam] [varchar](3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [ATeam] [varchar](3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, CONSTRAINT [PK_Schedule] PRIMARY KEY CLUSTERED ( [ScheduleId] ASC )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] CREATE TABLE [dbo].[GameStats]( [ScheduleId] [int] NOT NULL, [HFirstDowns] [int] NULL, [HRushes] [int] NULL, [HRushingYds] [int] NULL, [HPassingYds] [int] NULL, [HPassCompletions] [int] NULL, [HPassAttempts] [int] NULL, [HInts] [int] NULL, [HReturnYds] [int] NULL, [HSacksAllowed] [int] NULL, [HSacksYdsLost] [int] NULL, [HPunts] [int] NULL, [HAvgPunt] [int] NULL, [HFumbles] [int] NULL, [HFumblesLost] [int] NULL, [HPenalties] [int] NULL, [HPenaltyYdsLost] [int] NULL, [HTimeOfPossession] [varchar](8) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [AFirstDowns] [int] NULL, [ARushes] [int] NULL, [ARushingYds] [int] NULL, [APassingYds] [int] NULL, [APassCompletions] [int] NULL, [APassAttempts] [int] NULL, [AInts] [int] NULL, [AReturnYds] [int] NULL, [ASacksAllowed] [int] NULL, [ASacksYdsLost] [int] NULL, [APunts] [int] NULL, [AAvgPunt] [int] NULL, [AFumbles] [int] NULL, [AFumblesLost] [int] NULL, [APenalties] [int] NULL, [APenaltyYdsLost] [int] NULL, [ATimeOfPossession] [varchar](8) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, CONSTRAINT [PK_GameStats] PRIMARY KEY CLUSTERED ( [ScheduleId] ASC )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] I am having troubles with trying to create the one row in the Schedule table using the two rows in the Excel spreadsheet. I basically have to get the two rows in Excel and convert into one row in the table. I then have to use the newly inserted ScheduleId and do the same for the GameStats. Can someone help me with this (tutorial or other links)? |
#6
| |||
| |||
|
|
More than likely, you'll have to import the spreadsheet into a temp table, then loop through each row using a cursor. Each time you hit a row with an "A", you'll then grab the next row and do your insert. Hopefully this is a one time import as cursors run very slowly. If you haven't used them before, see books on line for some cursor examples. Here's some utterly untested code that assumes your rows will always be in the order A first, then H. Hopefully it gets you going. declare @AwayTeam varchar(100), @HomeTeam varchar(100), @RowType char(1), @TeamName varchar(100) declare test cursor for select * from spreadsheet open test fetch next from test into @RowType, @TeamName while @@fetch_status = 0 begin if @RowType = 'A' begin select @AwayTeam = @TeamName fetch next from test into @rowtype, @TeamName select @HomeTeam = @TeamName insert into TargetTable VALUES (@HomeTeam, @AwayTeam) END fetch next from test into @rowType, @TeamName END close test deallocate test "Jason" wrote: The rows are sequential. The only way to identify the rows is to start from the top and grab the row with the A in the Away/Home column and then grab the row with the H. This makes up the one row in the table. After that then move to rows 3 & 4. "Allan Mitchell" wrote: So can you identify the 2 rows that are linked? I would be inclined to use the Merge Join Transofrm to start with I would have two sources of the same Spreadsheet I would link on keys I would then only return the columns from the relevant side of the join If this does not do it for you then you can always look at a Script Component as a transform. -- Allan Mitchell Konesans Ltd T +44 7966 476 572 F +44 2071 008 479 http://www.konesans.com "Jason" <Jason (AT) discussions (DOT) microsoft.com> wrote in message news:4CC3FDF7-FF06-4CA5-B803-6DA9B6AD2121 (AT) microsoft (DOT) com... I have an Excel spreadsheet that contains a history of football games which is in this format: Year | Week | Team | Away/Home | Various stats for each team. 1992 1 NO A 1992 1 PHI H 1992 1 DET A 1992 1 CHI H I would like to get it in a relational format to do analysis so that requires me to create two tables (Schedule and GameStats). These are the two tables in which I would like to import into: CREATE TABLE [dbo].[Schedule]( [ScheduleId] [int] IDENTITY(1,1) NOT NULL, [Year] [int] NULL, [Week] [int] NULL, [HTeam] [varchar](3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [ATeam] [varchar](3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, CONSTRAINT [PK_Schedule] PRIMARY KEY CLUSTERED ( [ScheduleId] ASC )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] CREATE TABLE [dbo].[GameStats]( [ScheduleId] [int] NOT NULL, [HFirstDowns] [int] NULL, [HRushes] [int] NULL, [HRushingYds] [int] NULL, [HPassingYds] [int] NULL, [HPassCompletions] [int] NULL, [HPassAttempts] [int] NULL, [HInts] [int] NULL, [HReturnYds] [int] NULL, [HSacksAllowed] [int] NULL, [HSacksYdsLost] [int] NULL, [HPunts] [int] NULL, [HAvgPunt] [int] NULL, [HFumbles] [int] NULL, [HFumblesLost] [int] NULL, [HPenalties] [int] NULL, [HPenaltyYdsLost] [int] NULL, [HTimeOfPossession] [varchar](8) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [AFirstDowns] [int] NULL, [ARushes] [int] NULL, [ARushingYds] [int] NULL, [APassingYds] [int] NULL, [APassCompletions] [int] NULL, [APassAttempts] [int] NULL, [AInts] [int] NULL, [AReturnYds] [int] NULL, [ASacksAllowed] [int] NULL, [ASacksYdsLost] [int] NULL, [APunts] [int] NULL, [AAvgPunt] [int] NULL, [AFumbles] [int] NULL, [AFumblesLost] [int] NULL, [APenalties] [int] NULL, [APenaltyYdsLost] [int] NULL, [ATimeOfPossession] [varchar](8) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, CONSTRAINT [PK_GameStats] PRIMARY KEY CLUSTERED ( [ScheduleId] ASC )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] I am having troubles with trying to create the one row in the Schedule table using the two rows in the Excel spreadsheet. I basically have to get the two rows in Excel and convert into one row in the table. I then have to use the newly inserted ScheduleId and do the same for the GameStats. Can someone help me with this (tutorial or other links)? |
#7
| |||
| |||
|
#8
| |||
| |||
|
|
Hi Jason, I'm researching on this issue and will give you reply later. Charles Wang Microsoft Online Community Support |
#9
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |