SQL Server - Complicated Query HOW2 Question

Asked By WANNABE
16-Jun-07 03:25 PM
I would like to direct this to someone who helped me on a previous query, but I
am not sure if that violates some kind of etiquette, Anyway, I Hope you know who
you are, You were a big help, thanks again!!
I would REALLY appreciate any help.  I hope that with just a little more help I
should be able to work through the many more I need to complete.

How can the data in a relational table be parsed into a flat file table??
For instance; table1 has an id for each entry and an sid and name, table2 has an
sid and name1 name2 name3...456.
I have both tables in the same database and want to import the data from table1
into table2.  The records in table1 with identical sid's should create only one
record in table2.  Here is a sample of the tables, and what I have started
with>>
CREATE DATABASE SAMPLE002

GO

USE SAMPLE002

CREATE TABLE table1
(
sID int NOT NULL,
Name1 varchar(50) NULL,
Name2 varchar(50) NULL,
Name3 varchar(50) NULL,
Name4 varchar(50) NULL,
Name5 varchar(50) NULL,
Name6 varchar(50) NULL
)

GO

INSERT INTO table1 (sID, Name1, Name2, Name3, Name4, Name5, Name6) VALUES (1,
'Hal', 'Jenny', 'Hank', 'Mary', NULL, NULL)
INSERT INTO table1 (sID, Name1, Name2, Name3, Name4, Name5, Name6) VALUES (2,
'Leroy', 'Anne', 'Curtis', NULL, NULL, NULL)

GO

CREATE TABLE table2
(
pID int IDENTITY (1, 1) NOT NULL,
sID int NOT NULL,
Name varchar(50) NOT NULL
)

GO

INSERT INTO table2 (sID, Name) VALUES (3, 'Robert')
INSERT INTO table2 (sID, Name) VALUES (3, 'Roger')
INSERT INTO table2 (sID, Name) VALUES (3, 'Edgar')
INSERT INTO table2 (sID, Name) VALUES (3, 'Mitch')
INSERT INTO table2 (sID, Name) VALUES (4, 'Julie')
INSERT INTO table2 (sID, Name) VALUES (4, 'Wendy')
INSERT INTO table2 (sID, Name) VALUES (4, 'Roberto')

--View the tables as they are
select * from table1
select * from table2

--I tried this first but of course it duplicates the sid in table 2 and that
can't happen.  I expect there needs to be a looping process on each sid but I am
not sure how to write that
insert into table1 (sid,Name1)
select sid,Name from table2

--Use this to clean out the mess made by the previous insert
delete from table1
where sid >2
THANK YOU !!!
SQL Server 2005
(1)
SQL Server
(1)
CREATE DATABASE
(1)
CREATE TABLE
(1)
NOT NULL
(1)
Varchar
(1)
Database
(1)
BSc
(1)
  Tom Moreau replied...
17-Jun-07 11:56 AM
Thanx you for supplying DDL and data.  Here's a solution, using a technique
known as "unpivoting", which will work in SQL 2000 as well as SQL 2005:

insert table2 (sID, Name)
select
sID
, Name
from
(
select
t1.sID
, case x.pos
when 1 then t1.Name1
when 2 then t1.Name2
when 3 then t1.Name3
when 4 then t1.Name4
when 5 then t1.Name5
when 6 then t1.Name6
end Name
from
(
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6
) as x (pos)
cross join
table1 t1
) y
where
y.Name is not null

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON   Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


I would like to direct this to someone who helped me on a previous query,
but I
am not sure if that violates some kind of etiquette, Anyway, I Hope you know
who
you are, You were a big help, thanks again!!
I would REALLY appreciate any help.  I hope that with just a little more
help I
should be able to work through the many more I need to complete.

How can the data in a relational table be parsed into a flat file table??
For instance; table1 has an id for each entry and an sid and name, table2
has an
sid and name1 name2 name3...456.
I have both tables in the same database and want to import the data from
table1
into table2.  The records in table1 with identical sid's should create only
one
record in table2.  Here is a sample of the tables, and what I have started
with>>
CREATE DATABASE SAMPLE002

GO

USE SAMPLE002

CREATE TABLE table1
(
sID int NOT NULL,
Name1 varchar(50) NULL,
Name2 varchar(50) NULL,
Name3 varchar(50) NULL,
Name4 varchar(50) NULL,
Name5 varchar(50) NULL,
Name6 varchar(50) NULL
)

GO

INSERT INTO table1 (sID, Name1, Name2, Name3, Name4, Name5, Name6) VALUES
(1,
'Hal', 'Jenny', 'Hank', 'Mary', NULL, NULL)
INSERT INTO table1 (sID, Name1, Name2, Name3, Name4, Name5, Name6) VALUES
(2,
'Leroy', 'Anne', 'Curtis', NULL, NULL, NULL)

GO

CREATE TABLE table2
(
pID int IDENTITY (1, 1) NOT NULL,
sID int NOT NULL,
Name varchar(50) NOT NULL
)

GO

INSERT INTO table2 (sID, Name) VALUES (3, 'Robert')
INSERT INTO table2 (sID, Name) VALUES (3, 'Roger')
INSERT INTO table2 (sID, Name) VALUES (3, 'Edgar')
INSERT INTO table2 (sID, Name) VALUES (3, 'Mitch')
INSERT INTO table2 (sID, Name) VALUES (4, 'Julie')
INSERT INTO table2 (sID, Name) VALUES (4, 'Wendy')
INSERT INTO table2 (sID, Name) VALUES (4, 'Roberto')

--View the tables as they are
select * from table1
select * from table2

--I tried this first but of course it duplicates the sid in table 2 and that
can't happen.  I expect there needs to be a looping process on each sid but
I am
not sure how to write that
insert into table1 (sid,Name1)
select sid,Name from table2

--Use this to clean out the mess made by the previous insert
delete from table1
where sid >2
THANK YOU !!!
  WANNABE replied...
17-Jun-07 10:41 PM
Thanks Tom, But this inserts data from Flat file into Relational File and I am
trying to parse the data from the relational table into the flat file table,
sorry if my explanation wasn't clear.
=======================================================
Thanx you for supplying DDL and data.  Here's a solution, using a technique
known as "unpivoting", which will work in SQL 2000 as well as SQL 2005:

insert table2 (sID, Name)
select
sID
, Name
from
(
select
t1.sID
, case x.pos
when 1 then t1.Name1
when 2 then t1.Name2
when 3 then t1.Name3
when 4 then t1.Name4
when 5 then t1.Name5
when 6 then t1.Name6
end Name
from
(
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6
) as x (pos)
cross join
table1 t1
) y
where
y.Name is not null

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON   Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


I would like to direct this to someone who helped me on a previous query,
but I
am not sure if that violates some kind of etiquette, Anyway, I Hope you know
who
you are, You were a big help, thanks again!!
I would REALLY appreciate any help.  I hope that with just a little more
help I
should be able to work through the many more I need to complete.

How can the data in a relational table be parsed into a flat file table??
For instance; table1 has an id for each entry and an sid and name, table2
has an
sid and name1 name2 name3...456.
I have both tables in the same database and want to import the data from
table1
into table2.  The records in table1 with identical sid's should create only
one
record in table2.  Here is a sample of the tables, and what I have started
with>>
CREATE DATABASE SAMPLE002

GO

USE SAMPLE002

CREATE TABLE table1
(
sID int NOT NULL,
Name1 varchar(50) NULL,
Name2 varchar(50) NULL,
Name3 varchar(50) NULL,
Name4 varchar(50) NULL,
Name5 varchar(50) NULL,
Name6 varchar(50) NULL
)

GO

INSERT INTO table1 (sID, Name1, Name2, Name3, Name4, Name5, Name6) VALUES
(1,
'Hal', 'Jenny', 'Hank', 'Mary', NULL, NULL)
INSERT INTO table1 (sID, Name1, Name2, Name3, Name4, Name5, Name6) VALUES
(2,
'Leroy', 'Anne', 'Curtis', NULL, NULL, NULL)

GO

CREATE TABLE table2
(
pID int IDENTITY (1, 1) NOT NULL,
sID int NOT NULL,
Name varchar(50) NOT NULL
)

GO

INSERT INTO table2 (sID, Name) VALUES (3, 'Robert')
INSERT INTO table2 (sID, Name) VALUES (3, 'Roger')
INSERT INTO table2 (sID, Name) VALUES (3, 'Edgar')
INSERT INTO table2 (sID, Name) VALUES (3, 'Mitch')
INSERT INTO table2 (sID, Name) VALUES (4, 'Julie')
INSERT INTO table2 (sID, Name) VALUES (4, 'Wendy')
INSERT INTO table2 (sID, Name) VALUES (4, 'Roberto')

--View the tables as they are
select * from table1
select * from table2

--I tried this first but of course it duplicates the sid in table 2 and that
can't happen.  I expect there needs to be a looping process on each sid but
I am
not sure how to write that
insert into table1 (sid,Name1)
select sid,Name from table2

--Use this to clean out the mess made by the previous insert
delete from table1
where sid >2
THANK YOU !!!
  Tom Moreau replied...
18-Jun-07 07:38 AM
Here's a SQL 2005 solution:

with x
as
(
select
sID
, Name
, row_number () over (partition by sID order by sID, Name) rw
from
table2
)
insert table1
select
sID
, min (case when rw = 1 then Name end)
, min (case when rw = 2 then Name end)
, min (case when rw = 3 then Name end)
, min (case when rw = 4 then Name end)
, min (case when rw = 5 then Name end)
, min (case when rw = 6 then Name end)
from
x
group by
sID

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON   Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


Thanks Tom, But this inserts data from Flat file into Relational File and I
am
trying to parse the data from the relational table into the flat file table,
sorry if my explanation wasn't clear.
=======================================================
Thanx you for supplying DDL and data.  Here's a solution, using a technique
known as "unpivoting", which will work in SQL 2000 as well as SQL 2005:

insert table2 (sID, Name)
select
sID
, Name
from
(
select
t1.sID
, case x.pos
when 1 then t1.Name1
when 2 then t1.Name2
when 3 then t1.Name3
when 4 then t1.Name4
when 5 then t1.Name5
when 6 then t1.Name6
end Name
from
(
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6
) as x (pos)
cross join
table1 t1
) y
where
y.Name is not null

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON   Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


I would like to direct this to someone who helped me on a previous query,
but I
am not sure if that violates some kind of etiquette, Anyway, I Hope you know
who
you are, You were a big help, thanks again!!
I would REALLY appreciate any help.  I hope that with just a little more
help I
should be able to work through the many more I need to complete.

How can the data in a relational table be parsed into a flat file table??
For instance; table1 has an id for each entry and an sid and name, table2
has an
sid and name1 name2 name3...456.
I have both tables in the same database and want to import the data from
table1
into table2.  The records in table1 with identical sid's should create only
one
record in table2.  Here is a sample of the tables, and what I have started
with>>
CREATE DATABASE SAMPLE002

GO

USE SAMPLE002

CREATE TABLE table1
(
sID int NOT NULL,
Name1 varchar(50) NULL,
Name2 varchar(50) NULL,
Name3 varchar(50) NULL,
Name4 varchar(50) NULL,
Name5 varchar(50) NULL,
Name6 varchar(50) NULL
)

GO

INSERT INTO table1 (sID, Name1, Name2, Name3, Name4, Name5, Name6) VALUES
(1,
'Hal', 'Jenny', 'Hank', 'Mary', NULL, NULL)
INSERT INTO table1 (sID, Name1, Name2, Name3, Name4, Name5, Name6) VALUES
(2,
'Leroy', 'Anne', 'Curtis', NULL, NULL, NULL)

GO

CREATE TABLE table2
(
pID int IDENTITY (1, 1) NOT NULL,
sID int NOT NULL,
Name varchar(50) NOT NULL
)

GO

INSERT INTO table2 (sID, Name) VALUES (3, 'Robert')
INSERT INTO table2 (sID, Name) VALUES (3, 'Roger')
INSERT INTO table2 (sID, Name) VALUES (3, 'Edgar')
INSERT INTO table2 (sID, Name) VALUES (3, 'Mitch')
INSERT INTO table2 (sID, Name) VALUES (4, 'Julie')
INSERT INTO table2 (sID, Name) VALUES (4, 'Wendy')
INSERT INTO table2 (sID, Name) VALUES (4, 'Roberto')

--View the tables as they are
select * from table1
select * from table2

--I tried this first but of course it duplicates the sid in table 2 and that
can't happen.  I expect there needs to be a looping process on each sid but
I am
not sure how to write that
insert into table1 (sid,Name1)
select sid,Name from table2

--Use this to clean out the mess made by the previous insert
delete from table1
where sid >2
THANK YOU !!!
  WANNABE replied...
18-Jun-07 10:51 AM
WOW!!!  Thanks SO MUCH Tom.  That worked great!!!
I do always have questions though, because I try so hard to understand.
Why does it produce the
message above the
(2 row(s) affected)
When I run a select statement on both tables I still see the NULL values where
they should be.
ALSO, As I read through and study this query to try to understand it and how it
works, can you provide any further guidance on some of  commands you used, such
as ROW_NUMBER() OVER(PARTITION BY SID ORDER BY SID, NAME) RW, the SQL2005 help
is not very detailed
AND,  each CASE statement is a simple statement, could these be IF statements ??
Please don't be offended by my stupidity!!  I truly appreciate all your
assistance !!!
Thank You!!!
=====================================
Here's a SQL 2005 solution:

with x
as
(
select
sID
, Name
, row_number () over (partition by sID order by sID, Name) rw
from
table2
)
insert table1
select
sID
, min (case when rw = 1 then Name end)
, min (case when rw = 2 then Name end)
, min (case when rw = 3 then Name end)
, min (case when rw = 4 then Name end)
, min (case when rw = 5 then Name end)
, min (case when rw = 6 then Name end)
from
x
group by
sID

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON   Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


Thanks Tom, But this inserts data from Flat file into Relational File and I
am
trying to parse the data from the relational table into the flat file table,
sorry if my explanation wasn't clear.
=======================================================
Thanx you for supplying DDL and data.  Here's a solution, using a technique
known as "unpivoting", which will work in SQL 2000 as well as SQL 2005:

insert table2 (sID, Name)
select
sID
, Name
from
(
select
t1.sID
, case x.pos
when 1 then t1.Name1
when 2 then t1.Name2
when 3 then t1.Name3
when 4 then t1.Name4
when 5 then t1.Name5
when 6 then t1.Name6
end Name
from
(
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6
) as x (pos)
cross join
table1 t1
) y
where
y.Name is not null

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON   Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


I would like to direct this to someone who helped me on a previous query,
but I
am not sure if that violates some kind of etiquette, Anyway, I Hope you know
who
you are, You were a big help, thanks again!!
I would REALLY appreciate any help.  I hope that with just a little more
help I
should be able to work through the many more I need to complete.

How can the data in a relational table be parsed into a flat file table??
For instance; table1 has an id for each entry and an sid and name, table2
has an
sid and name1 name2 name3...456.
I have both tables in the same database and want to import the data from
table1
into table2.  The records in table1 with identical sid's should create only
one
record in table2.  Here is a sample of the tables, and what I have started
with>>
CREATE DATABASE SAMPLE002

GO

USE SAMPLE002

CREATE TABLE table1
(
sID int NOT NULL,
Name1 varchar(50) NULL,
Name2 varchar(50) NULL,
Name3 varchar(50) NULL,
Name4 varchar(50) NULL,
Name5 varchar(50) NULL,
Name6 varchar(50) NULL
)

GO

INSERT INTO table1 (sID, Name1, Name2, Name3, Name4, Name5, Name6) VALUES
(1,
'Hal', 'Jenny', 'Hank', 'Mary', NULL, NULL)
INSERT INTO table1 (sID, Name1, Name2, Name3, Name4, Name5, Name6) VALUES
(2,
'Leroy', 'Anne', 'Curtis', NULL, NULL, NULL)

GO

CREATE TABLE table2
(
pID int IDENTITY (1, 1) NOT NULL,
sID int NOT NULL,
Name varchar(50) NOT NULL
)

GO

INSERT INTO table2 (sID, Name) VALUES (3, 'Robert')
INSERT INTO table2 (sID, Name) VALUES (3, 'Roger')
INSERT INTO table2 (sID, Name) VALUES (3, 'Edgar')
INSERT INTO table2 (sID, Name) VALUES (3, 'Mitch')
INSERT INTO table2 (sID, Name) VALUES (4, 'Julie')
INSERT INTO table2 (sID, Name) VALUES (4, 'Wendy')
INSERT INTO table2 (sID, Name) VALUES (4, 'Roberto')

--View the tables as they are
select * from table1
select * from table2

--I tried this first but of course it duplicates the sid in table 2 and that
can't happen.  I expect there needs to be a looping process on each sid but
I am
not sure how to write that
insert into table1 (sid,Name1)
select sid,Name from table2

--Use this to clean out the mess made by the previous insert
delete from table1
where sid >2
THANK YOU !!!
  Hugo Kornelis replied...
18-Jun-07 04:57 PM
That's because these lines:


all produce a vlaue of either Name or NULL (if there is no ELSE in a
CASE expression, it defaults to ELSE NULL), The MIN() function removes
these values again, but according to ANSI specifications, a warning must
be given that NULL values were omitted in an aggregate.

Or the short answer - that is expected behaviour for this query.



What this does (logically - the physical implementation might be
different as long as the results are the same) is:
1. Group rows in the result set on the SID column. I.e., for each SID
value, you'll get a group with all the rows with that SID value..
2. Within each group, order the rows by SID and Name. Of course, since
SID is always equal within each group, Tom could also have used
(PARTITION BY SID ORDER BY NAME)
3. Number the rows sequentially, respecting the order, and restarting
from 1 for each SID group.
4. Make this logical row number available to the outer query under the
alias RW (Tom omitted the optional keyword AS before the alias RW, which
I always include for extra clarity)



In SQL Server, CASE is an expression, not a statement. That means that
you can't execute it by itself, but can include it in a query. It will
be evaluated for each row, and the value of the expression depends on
that evaluation.



My motto is that there are no stupid questions. Not asking a question,
that is stupid. <g>

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
  WANNABE replied...
20-Jun-07 10:33 AM
Thank you all for all your help, and guidance!!
Someday I hope that I can be as helpful to someone else, as you have all been to
me!
WANNABE
===================================


That's because these lines:


all produce a vlaue of either Name or NULL (if there is no ELSE in a
CASE expression, it defaults to ELSE NULL), The MIN() function removes
these values again, but according to ANSI specifications, a warning must
be given that NULL values were omitted in an aggregate.

Or the short answer - that is expected behaviour for this query.



What this does (logically - the physical implementation might be
different as long as the results are the same) is:
1. Group rows in the result set on the SID column. I.e., for each SID
value, you'll get a group with all the rows with that SID value..
2. Within each group, order the rows by SID and Name. Of course, since
SID is always equal within each group, Tom could also have used
(PARTITION BY SID ORDER BY NAME)
3. Number the rows sequentially, respecting the order, and restarting
from 1 for each SID group.
4. Make this logical row number available to the outer query under the
alias RW (Tom omitted the optional keyword AS before the alias RW, which
I always include for extra clarity)



In SQL Server, CASE is an expression, not a statement. That means that
you can't execute it by itself, but can include it in a query. It will
be evaluated for each row, and the value of the expression depends on
that evaluation.



My motto is that there are no stupid questions. Not asking a question,
that is stupid. <g>

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Create New Account
help
SQL Server Database Engine Tuning Advisor I am trying to use the Database Engine Tuning Advisor. I have the developer edition of SQL2005 and SQL2008 installed with several no issues with anything I have tried to do until I attempted to run the Database Engine Tuning Advisor. When I start the Database Engine Tuning Advisor for either version of SQL I get the same message when I click Connect using either the sa account or a Windows account with the proper credentials: Database Engine Tuning Advisor Failed to open a new connection. Additional information: Could not find stored
SQL Server Why_can’t_recursive_queries_contain. . .? hi I hope I didn = 92t put too many questions 1) Why can such as this is already a strong case to simply disallow the option. - - Hugo Kornelis, SQL Server MVP My SQL Server blog: http: / / sqlblog.com / blogs / hugo_kornelis MS SQL Server is a minimal subset of the ANSI / ISO Standards. In Standard SQL, the WITH clause
SQL Server Problem with DROP CONSTRAINT When I issue the following T-SQL 2008: ALTER TABLE ALIS.mc.LOCApp DROP CONSTRAINT LOCApp_Created_df I get the following errors: Msg 1781, Level 16 a DEFAULT bound to it. Msg 1750, Level 16, State 0, Line 1 Could not create constraint. See previous errors. I can see the constraint exists in Object Explorer. If I Object Explorer the message I get is: The Default 'LOCApp_Created_df' does not exist on the server. (SQL Manager UI) What is going on? Patrick = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = Patrick Jackman Vancouver, BC 604-874-5774 e r can you post DDL for the table? USE [ALIS] GO / * ** ** * Object: Table [mc].[LOCApp] Script Date: 12 / 19 / 2008 16:16:03
SQL Server Bulk Insert Help Hi Guys, Just trying to complete a Bulk Insert and am getting over. The first field of each following records will be empty and discarded. - - Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se Links for SQL Server Books Online: SQL 2008: http: / / msdn.microsoft.com / en-us / sqlserver / cc514207.aspx SQL 2005: http: / / msdn.microsoft.com / en-us / sqlserver / bb895970.aspx SQL 2000: http: / / www.microsoft
SQL Server Stored procedure with IN clause I have a stored procedure that I want to send vw_LitigationStatus WHERE (FileNumber IN(@filenumbers)) END END RETURN http: / / www.aspfaq.com / 2248 - - Aaron Bertrand SQL Server MVP Read my take here: http: / / www.sqlservercentral.com / columnists / sholliday / thezerotonparameterproblem.asp Yes, there has been quite a bit written about this. I show how to create CLR (and TSQL) functions to handle arrays of parameters passed to stored procedures in my book. The trick is to change the array (usually a delimited list) to a Table-typed variable and pass it to the IN clause via a SELECT. WHERE X IN to get more information on my latest book: Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition) and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook) - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- There are