Hi
I have a table that has fromNode, toNode and idGroup columns
I want to return the TOP 3 toNodes for each IDGroup that the FromNode =
1000
E.G. The query would return
fromNode, toNode, idGroup
1000,2001,1
1000,2002,1
1000,2003,1
1000,2004,2
1000,2005,2
1000,2006,2
1000,2013,3
1000,2014,3
1000,2016,4
But I am having trouble comming up with such a query. Can any one help.
SQL code and inserts below.
CREATE TABLE [dbo].[memberPathsGroups](
[fromNode] [int] NOT NULL,
[toNode] [int] NOT NULL,
[idGroup] [int] NOT NULL,
CONSTRAINT [PK_memberPathsGroups] PRIMARY KEY CLUSTERED
(
[fromNode] ASC,
[toNode] ASC,
[idGroup] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
insert into memberPathsGroups values (1000,2001,1)
insert into memberPathsGroups values (1000,2002,1)
insert into memberPathsGroups values (1000,2003,1)
insert into memberPathsGroups values (1000,2004,2)
insert into memberPathsGroups values (1000,2005,2)
insert into memberPathsGroups values (1000,2006,2)
insert into memberPathsGroups values (1000,2007,2)
insert into memberPathsGroups values (1000,2008,2)
insert into memberPathsGroups values (1000,2009,2)
insert into memberPathsGroups values (1000,2010,2)
insert into memberPathsGroups values (1000,2012,2)
insert into memberPathsGroups values (1000,2013,3)
insert into memberPathsGroups values (1000,2014,3)
insert into memberPathsGroups values (1000,2015,2)
insert into memberPathsGroups values (1000,2016,4)
insert into memberPathsGroups values (1001,2001,1)
insert into memberPathsGroups values (1001,2010,1)
insert into memberPathsGroups values (1001,2012,1)
insert into memberPathsGroups values (1001,2016,2)Here's one way:
SELECT
fromNode,
toNode,
idGroup
FROM dbo.memberPathsGroups mpg
WHERE
mpg.toNode IN
(
SELECT TOP 3
mpg1.toNode
FROM dbo.memberPathsGroups mpg1
WHERE
mpg1.fromNode = mpg.fromNode
AND mpg1.idGroup = mpg.idGroup
ORDER BY mpg1.toNode
)
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
--
"Rippo" <info@.rippo.co.uk> wrote in message
news:1131373532.924245.290810@.o13g2000cwo.googlegroups.com...
> Hi
> I have a table that has fromNode, toNode and idGroup columns
> I want to return the TOP 3 toNodes for each IDGroup that the FromNode =
> 1000
> E.G. The query would return
> fromNode, toNode, idGroup
> 1000,2001,1
> 1000,2002,1
> 1000,2003,1
> 1000,2004,2
> 1000,2005,2
> 1000,2006,2
> 1000,2013,3
> 1000,2014,3
> 1000,2016,4
> But I am having trouble comming up with such a query. Can any one help.
> SQL code and inserts below.
>
> CREATE TABLE [dbo].[memberPathsGroups](
> [fromNode] [int] NOT NULL,
> [toNode] [int] NOT NULL,
> [idGroup] [int] NOT NULL,
> CONSTRAINT [PK_memberPathsGroups] PRIMARY KEY CLUSTERED
> (
> [fromNode] ASC,
> [toNode] ASC,
> [idGroup] ASC
> )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
> ) ON [PRIMARY]
> GO
> insert into memberPathsGroups values (1000,2001,1)
> insert into memberPathsGroups values (1000,2002,1)
> insert into memberPathsGroups values (1000,2003,1)
> insert into memberPathsGroups values (1000,2004,2)
> insert into memberPathsGroups values (1000,2005,2)
> insert into memberPathsGroups values (1000,2006,2)
> insert into memberPathsGroups values (1000,2007,2)
> insert into memberPathsGroups values (1000,2008,2)
> insert into memberPathsGroups values (1000,2009,2)
> insert into memberPathsGroups values (1000,2010,2)
> insert into memberPathsGroups values (1000,2012,2)
> insert into memberPathsGroups values (1000,2013,3)
> insert into memberPathsGroups values (1000,2014,3)
> insert into memberPathsGroups values (1000,2015,2)
> insert into memberPathsGroups values (1000,2016,4)
> insert into memberPathsGroups values (1001,2001,1)
> insert into memberPathsGroups values (1001,2010,1)
> insert into memberPathsGroups values (1001,2012,1)
> insert into memberPathsGroups values (1001,2016,2)
>
Showing posts with label fro. Show all posts
Showing posts with label fro. Show all posts
Tuesday, March 27, 2012
Wednesday, March 7, 2012
Generate SQL insert statement fro SQL Server database
Good day,
I have seen in Enterprise manager there is a toll that can script the all tables in a database, but nothing that can generate the insert statements for all the rows in each table in a secified database.
Does any one know of a application, plug in, script that can generate the insert statments for all the tables in a database?
Please someone help, this is driving me insane.
ThanksFound the answer, great sacript.
SQL Insert generator script - http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=1052&lngWId=5|||I wrote this a while back for my own use
Call this sp in a loop over SELECT TABLE_NAME FROM INFORMATION_SCHEMA.tables
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE spGenerateInsertStatement
(
@.TableName varchar(255),
@.SQL_INSERT varchar(8000) OUTPUT
)
AS
DECLARE @.current_column varchar(255)
DECLARE @.current_ordinal int
DECLARE @.max_ordinal int
--pre assign some values for safety
SET @.current_column = ''
SET @.current_ordinal = 0
SET @.max_ordinal = 0
SET @.SQL_INSERT = 'INSERT INTO [' + @.TableName + '] ('
--get the max ordinal for the table you're inserting into
SELECT @.max_ordinal = MAX(ORDINAL_POSITION)
FROM INFORMATION_SCHEMA.columns
WHERE TABLE_NAME = @.TableName
--create a cursor of column names and ordinals
DECLARE cInsert CURSOR
FOR
SELECT COLUMN_NAME,
ORDINAL_POSITION
FROM INFORMATION_SCHEMA.columns
WHERE TABLE_NAME = @.TableName
ORDER BY ORDINAL_POSITION
OPEN cInsert
--fetch the next record
FETCH NEXT FROM cInsert INTO @.current_column,@.current_ordinal
WHILE(@.@.FETCH_STATUS = 0)
BEGIN
IF(@.current_ordinal != @.max_ordinal)
BEGIN
--append "column," to the insert statement
SET @.SQL_INSERT = @.SQL_INSERT + '[' +@.current_column + '],' + CHAR(13) + CHAR(10)
END
ELSE
BEGIN
--append "column)" to the insert statement
SET @.SQL_INSERT = @.SQL_INSERT + '[' + @.current_column + '])' + CHAR(13) + CHAR(10)
END
--fetch the next record
FETCH NEXT FROM cInsert INTO @.current_column,@.current_ordinal
END
--cleanup cursor
CLOSE cInsert
DEALLOCATE cInsert
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO|||Hi! I'm on your side now!|||Hi! I just joined the forum to learn more about sql server 2000 and make friends too!|||Raski, what do you mean you are on our side? Where you from?
I have seen in Enterprise manager there is a toll that can script the all tables in a database, but nothing that can generate the insert statements for all the rows in each table in a secified database.
Does any one know of a application, plug in, script that can generate the insert statments for all the tables in a database?
Please someone help, this is driving me insane.
ThanksFound the answer, great sacript.
SQL Insert generator script - http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=1052&lngWId=5|||I wrote this a while back for my own use
Call this sp in a loop over SELECT TABLE_NAME FROM INFORMATION_SCHEMA.tables
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE spGenerateInsertStatement
(
@.TableName varchar(255),
@.SQL_INSERT varchar(8000) OUTPUT
)
AS
DECLARE @.current_column varchar(255)
DECLARE @.current_ordinal int
DECLARE @.max_ordinal int
--pre assign some values for safety
SET @.current_column = ''
SET @.current_ordinal = 0
SET @.max_ordinal = 0
SET @.SQL_INSERT = 'INSERT INTO [' + @.TableName + '] ('
--get the max ordinal for the table you're inserting into
SELECT @.max_ordinal = MAX(ORDINAL_POSITION)
FROM INFORMATION_SCHEMA.columns
WHERE TABLE_NAME = @.TableName
--create a cursor of column names and ordinals
DECLARE cInsert CURSOR
FOR
SELECT COLUMN_NAME,
ORDINAL_POSITION
FROM INFORMATION_SCHEMA.columns
WHERE TABLE_NAME = @.TableName
ORDER BY ORDINAL_POSITION
OPEN cInsert
--fetch the next record
FETCH NEXT FROM cInsert INTO @.current_column,@.current_ordinal
WHILE(@.@.FETCH_STATUS = 0)
BEGIN
IF(@.current_ordinal != @.max_ordinal)
BEGIN
--append "column," to the insert statement
SET @.SQL_INSERT = @.SQL_INSERT + '[' +@.current_column + '],' + CHAR(13) + CHAR(10)
END
ELSE
BEGIN
--append "column)" to the insert statement
SET @.SQL_INSERT = @.SQL_INSERT + '[' + @.current_column + '])' + CHAR(13) + CHAR(10)
END
--fetch the next record
FETCH NEXT FROM cInsert INTO @.current_column,@.current_ordinal
END
--cleanup cursor
CLOSE cInsert
DEALLOCATE cInsert
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO|||Hi! I'm on your side now!|||Hi! I just joined the forum to learn more about sql server 2000 and make friends too!|||Raski, what do you mean you are on our side? Where you from?
Subscribe to:
Posts (Atom)