Showing posts with label rows. Show all posts
Showing posts with label rows. Show all posts

Thursday, March 29, 2012

Copy just new rows

Hi,
I need to copy just the new rows from a Oracle DB to a MS-SQL table. I
created a DTS and I can get the information with no problems the issues is
that I only want to insert the new rows or records created.
Any tipsHow do you expect to define "new rows"?
Please post DDL, sample data and desired results.
See http://www.aspfaq.com/5006 for info.
"julio delgado" <jdelgado89@.hotmail.com> wrote in message
news:u5z3eMiLFHA.2120@.TK2MSFTNGP10.phx.gbl...
> Hi,
> I need to copy just the new rows from a Oracle DB to a MS-SQL table. I
> created a DTS and I can get the information with no problems the issues is
> that I only want to insert the new rows or records created.
> Any tips
>|||If you're talking about rows with keys that do not exist in the target, you
can use a linked server as follows:
INSERT INTO Target(column_list)
SELECT <column_list> FROM Source AS S
WHERE NOT EXISTS
(SELECT * FROM Target AS T
WHERE T.key = S.key)
Just specify instead of source or target (depending on where you're
connected to) the full four part name (server.db.owner.object).
BG, SQL Server MVP
www.SolidQualityLearning.com
"julio delgado" <jdelgado89@.hotmail.com> wrote in message
news:u5z3eMiLFHA.2120@.TK2MSFTNGP10.phx.gbl...
> Hi,
> I need to copy just the new rows from a Oracle DB to a MS-SQL table. I
> created a DTS and I can get the information with no problems the issues is
> that I only want to insert the new rows or records created.
> Any tips
>|||Thanks
"Itzik Ben-Gan" <itzik@.REMOVETHIS.SolidQualityLearning.com> wrote in message
news:O%23M548iLFHA.1392@.TK2MSFTNGP10.phx.gbl...
> If you're talking about rows with keys that do not exist in the target,
you
> can use a linked server as follows:
> INSERT INTO Target(column_list)
> SELECT <column_list> FROM Source AS S
> WHERE NOT EXISTS
> (SELECT * FROM Target AS T
> WHERE T.key = S.key)
> Just specify instead of source or target (depending on where you're
> connected to) the full four part name (server.db.owner.object).
> --
> BG, SQL Server MVP
> www.SolidQualityLearning.com
>
> "julio delgado" <jdelgado89@.hotmail.com> wrote in message
> news:u5z3eMiLFHA.2120@.TK2MSFTNGP10.phx.gbl...
is
>sqlsql

Thursday, March 8, 2012

Copy Data Issue

I need to create a new field and copy over an existing field from the same table. Since this table is about 3 million rows. It will take about 1 hour 30 minutes to complete.

Any good way around?

Thanks!

-J8

-------------
UPDATE Customer SET ColB = ColA WHERE ColA IS NOT NULLWhat (if any) point is there to this exercise?

-PatP|||Check this (http://www.dbforums.com/showthread.php?&p=3687532) thread out.|||The New column ColB will become a key column for sorting and also accept new kind of data while orig. ColA column continually receive data as it was. the historical data need to be copy over for sorting ...

Friday, February 24, 2012

Copy 60GB table?

I'm trying to copy all 440 million rows from one table in my SQL Server
2005 db to another table with a different clustering scheme. After a
few test inserts that were successful (up to a million rows in 20
seconds with no problem), I crossed my fingers and went whole-hog with
the naive "insert into dest_table (column1, column2, ...) select
column1, column2, ... from source_table" but of course it ran out of
space in *both* the db log file (at about 130 GB) and the tempdb log
file (at about 54GB) and rolled it back, wrapping up 10 hours after I
kicked it off.

I think it would work if I could insert in batches and/or disable
logging, like with a bulk insert, so I tried exporting the source table
to a flat file in the hopes of bulk-importing it back into the
destination table, but the export just halted for no reason after 176
million rows, using all the CPU and not doing anything at all, just
sitting there. (The fact that this operation not only failed, but
brought down the whole server - any operation on any database in the
instance timed out, no clients could connect, the whole deal - I find
very disappointing for what MS claimed was a scalable, robust product
that could rival Oracle.)

What I'm wondering is, does anyone know of a way to copy a table that
is apparently just too large for SQL Server 2005 to handle?

Thanks,
Sethsql_server_user (kaioptera@.gmail.com) writes:
> I'm trying to copy all 440 million rows from one table in my SQL Server
> 2005 db to another table with a different clustering scheme. After a
> few test inserts that were successful (up to a million rows in 20
> seconds with no problem), I crossed my fingers and went whole-hog with
> the naive "insert into dest_table (column1, column2, ...) select
> column1, column2, ... from source_table" but of course it ran out of
> space in *both* the db log file (at about 130 GB) and the tempdb log
> file (at about 54GB) and rolled it back, wrapping up 10 hours after I
> kicked it off.

That operation is definitely not a walk in a park.

I think I would attempt this strategy:

1) Set recovery for the database to simple recovery.
2) Create the clustered index on the target table with a relatively low
fill factor, say 30%. (Of course, no NC indexes would be in place.)
3) Insert batchwise from the old table, where batches are defined by
intervals of the clustered index of the source table. For batchsize,
I would use 100000 to 500000 rows, depending on wide the table is.
4) I would make the control loop so that it is easy to stop it. For
instance on each iteration read from a control table. Possibly also
a table the defines the batches, so you can keep track of what has
been copied. But the table should not be joined with the INSERT;
read interval into variales.
5) Occassionally stop the process and monitor fragmentation in
target table. It should first decrease, as holes are filled in,
but then it will increase again. (But you will probably see this
from the fact that the time for iteration increases.)
6) Once everything is done, switch to full recovery and backup the
database.

My thinking here is that creating the clustered index on this monster
is going require a lot of log and tempdb - you can guess what happens.
So let's have the clustered index in place from the start, even if
that will take longer time.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Thanks, Erland - I'm gonna have to pay you a consulting fee soon.

The recovery model is set to simple, there are no nonclustered indexes
on the table, and I agree that clustering on insert is the way to go,
since I think trying to recluster the new table with all the data in it
would just blow up the logs and tempdb like my initial insert attempt.
I like the idea of the batched insert with the low fill factor
clustered index, however, I ran a bunch of tests and the amount of log
and tempdb space required seems to be a function of the size of the
destination table, not the number of rows in the batch: I would insert
a batch (10 million rows), shrink the log and tempdb, insert another
batch, and so on, and every time, the log and tempdb would require more
space, roughly linearly as my very approximate data below indicate:

after 1st batch: log = 1GB, tempdb = 2GB
after shrinking and 2nd batch: log = 2GB, tempdb = 4GB
after shrinking and 3rd batch: log = 3 GB, tempdb = 6GB

So I'm worried about the batch approach for the full insert.

I'm currently trying an SSIS copy from one table to the other, and
since it's doing a bulk insert it doesn't seem to be touching the db
log. However, tempdb is getting pretty fat, 12GB and going strong, but
I've cleared a lot of room for it this time - it has about 179GB to go,
so hopefully that will be enough. If this doesn't work I'll definitely
try your batched insert suggestion.

Thanks again,
Seth

Erland Sommarskog wrote:
> sql_server_user (kaioptera@.gmail.com) writes:
> > I'm trying to copy all 440 million rows from one table in my SQL Server
> > 2005 db to another table with a different clustering scheme. After a
> > few test inserts that were successful (up to a million rows in 20
> > seconds with no problem), I crossed my fingers and went whole-hog with
> > the naive "insert into dest_table (column1, column2, ...) select
> > column1, column2, ... from source_table" but of course it ran out of
> > space in *both* the db log file (at about 130 GB) and the tempdb log
> > file (at about 54GB) and rolled it back, wrapping up 10 hours after I
> > kicked it off.
> That operation is definitely not a walk in a park.
> I think I would attempt this strategy:
> 1) Set recovery for the database to simple recovery.
> 2) Create the clustered index on the target table with a relatively low
> fill factor, say 30%. (Of course, no NC indexes would be in place.)
> 3) Insert batchwise from the old table, where batches are defined by
> intervals of the clustered index of the source table. For batchsize,
> I would use 100000 to 500000 rows, depending on wide the table is.
> 4) I would make the control loop so that it is easy to stop it. For
> instance on each iteration read from a control table. Possibly also
> a table the defines the batches, so you can keep track of what has
> been copied. But the table should not be joined with the INSERT;
> read interval into variales.
> 5) Occassionally stop the process and monitor fragmentation in
> target table. It should first decrease, as holes are filled in,
> but then it will increase again. (But you will probably see this
> from the fact that the time for iteration increases.)
> 6) Once everything is done, switch to full recovery and backup the
> database.
> My thinking here is that creating the clustered index on this monster
> is going require a lot of log and tempdb - you can guess what happens.
> So let's have the clustered index in place from the start, even if
> that will take longer time.
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/pr...oads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodin...ions/books.mspx|||"sql_server_user" <kaioptera@.gmail.com> wrote in message
news:1147299426.561430.312200@.u72g2000cwu.googlegr oups.com...
> Thanks, Erland - I'm gonna have to pay you a consulting fee soon.

BTW, might be worth looking at using BCP to dump it out and then insert it
into the new table.

Then build your indices.

> The recovery model is set to simple, there are no nonclustered indexes
> on the table, and I agree that clustering on insert is the way to go,
> since I think trying to recluster the new table with all the data in it
> would just blow up the logs and tempdb like my initial insert attempt.
> I like the idea of the batched insert with the low fill factor
> clustered index, however, I ran a bunch of tests and the amount of log
> and tempdb space required seems to be a function of the size of the
> destination table, not the number of rows in the batch: I would insert
> a batch (10 million rows), shrink the log and tempdb, insert another
> batch, and so on, and every time, the log and tempdb would require more
> space, roughly linearly as my very approximate data below indicate:
> after 1st batch: log = 1GB, tempdb = 2GB
> after shrinking and 2nd batch: log = 2GB, tempdb = 4GB
> after shrinking and 3rd batch: log = 3 GB, tempdb = 6GB
> So I'm worried about the batch approach for the full insert.
> I'm currently trying an SSIS copy from one table to the other, and
> since it's doing a bulk insert it doesn't seem to be touching the db
> log. However, tempdb is getting pretty fat, 12GB and going strong, but
> I've cleared a lot of room for it this time - it has about 179GB to go,
> so hopefully that will be enough. If this doesn't work I'll definitely
> try your batched insert suggestion.
> Thanks again,
> Seth
> Erland Sommarskog wrote:
> > sql_server_user (kaioptera@.gmail.com) writes:
> > > I'm trying to copy all 440 million rows from one table in my SQL
Server
> > > 2005 db to another table with a different clustering scheme. After a
> > > few test inserts that were successful (up to a million rows in 20
> > > seconds with no problem), I crossed my fingers and went whole-hog with
> > > the naive "insert into dest_table (column1, column2, ...) select
> > > column1, column2, ... from source_table" but of course it ran out of
> > > space in *both* the db log file (at about 130 GB) and the tempdb log
> > > file (at about 54GB) and rolled it back, wrapping up 10 hours after I
> > > kicked it off.
> > That operation is definitely not a walk in a park.
> > I think I would attempt this strategy:
> > 1) Set recovery for the database to simple recovery.
> > 2) Create the clustered index on the target table with a relatively low
> > fill factor, say 30%. (Of course, no NC indexes would be in place.)
> > 3) Insert batchwise from the old table, where batches are defined by
> > intervals of the clustered index of the source table. For batchsize,
> > I would use 100000 to 500000 rows, depending on wide the table is.
> > 4) I would make the control loop so that it is easy to stop it. For
> > instance on each iteration read from a control table. Possibly also
> > a table the defines the batches, so you can keep track of what has
> > been copied. But the table should not be joined with the INSERT;
> > read interval into variales.
> > 5) Occassionally stop the process and monitor fragmentation in
> > target table. It should first decrease, as holes are filled in,
> > but then it will increase again. (But you will probably see this
> > from the fact that the time for iteration increases.)
> > 6) Once everything is done, switch to full recovery and backup the
> > database.
> > My thinking here is that creating the clustered index on this monster
> > is going require a lot of log and tempdb - you can guess what happens.
> > So let's have the clustered index in place from the start, even if
> > that will take longer time.
> > --
> > Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> > Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
> > Books Online for SQL Server 2000 at
> > http://www.microsoft.com/sql/prodin...ions/books.mspx|||> 2) Create the clustered index on the target table with a relatively low
> fill factor, say 30%. (Of course, no NC indexes would be in place.)

As the table is empty there is no point putting on the fillfactor because
its not enforced for new pages.

Only good for creating indexes on existing data.

I agree though - clustered index should be in place before you start.

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials

"Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
news:Xns97BFF29DB95D8Yazorman@.127.0.0.1...
> sql_server_user (kaioptera@.gmail.com) writes:
>> I'm trying to copy all 440 million rows from one table in my SQL Server
>> 2005 db to another table with a different clustering scheme. After a
>> few test inserts that were successful (up to a million rows in 20
>> seconds with no problem), I crossed my fingers and went whole-hog with
>> the naive "insert into dest_table (column1, column2, ...) select
>> column1, column2, ... from source_table" but of course it ran out of
>> space in *both* the db log file (at about 130 GB) and the tempdb log
>> file (at about 54GB) and rolled it back, wrapping up 10 hours after I
>> kicked it off.
> That operation is definitely not a walk in a park.
> I think I would attempt this strategy:
> 1) Set recovery for the database to simple recovery.
> 2) Create the clustered index on the target table with a relatively low
> fill factor, say 30%. (Of course, no NC indexes would be in place.)
> 3) Insert batchwise from the old table, where batches are defined by
> intervals of the clustered index of the source table. For batchsize,
> I would use 100000 to 500000 rows, depending on wide the table is.
> 4) I would make the control loop so that it is easy to stop it. For
> instance on each iteration read from a control table. Possibly also
> a table the defines the batches, so you can keep track of what has
> been copied. But the table should not be joined with the INSERT;
> read interval into variales.
> 5) Occassionally stop the process and monitor fragmentation in
> target table. It should first decrease, as holes are filled in,
> but then it will increase again. (But you will probably see this
> from the fact that the time for iteration increases.)
> 6) Once everything is done, switch to full recovery and backup the
> database.
> My thinking here is that creating the clustered index on this monster
> is going require a lot of log and tempdb - you can guess what happens.
> So let's have the clustered index in place from the start, even if
> that will take longer time.
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/pr...oads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodin...ions/books.mspx|||Tony Rogerson (tonyrogerson@.sqlserverfaq.com) writes:
>> 2) Create the clustered index on the target table with a relatively low
>> fill factor, say 30%. (Of course, no NC indexes would be in place.)
> As the table is empty there is no point putting on the fillfactor because
> its not enforced for new pages.
> Only good for creating indexes on existing data.
> I agree though - clustered index should be in place before you start.

Eh, I guess that I will have to stick with my general lame excuse that
the hour was late.

However, it could maybe be idea to reindex every now and then with a low
fill-factor.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||SSIS worked. It looks like it just put the *entire* table in tempdb -
to sort it into the new clustering order, I guess. The key was
eliminating logging with the bulk insert. Thanks for the help,
everybody.

Sunday, February 12, 2012

Converting Rows to Columns in SQL7

I do a:
SELECT * FROM xxx

And Get:
Date Place Sum
A M 1
A O 3
A P 2
B O 5
B M 4
B P 2

And I want it to look like:

Date M O P
A 1 3 2
B 4 5 2

Can you think of an EASY way to do this?
I can do it with a cursor that constructs a SQL statement, which I EXEC, but the 8000 character limit may prove to be a limiting factor.
sp_execsql is somewhat messy for the nature of this issue.
Any input is appreciated.

Thanks in advance."Brad Joss" <bradjoss@.hotmail.com> wrote in message news:<bgq7p7$m03$1@.news01.intel.com>...
> I do a:
> SELECT * FROM xxx
> And Get:
> Date Place Sum
> A M 1
> A O 3
> A P 2
> B O 5
> B M 4
> B P 2
> And I want it to look like:
> Date M O P
> A 1 3 2
> B 4 5 2
> Can you think of an EASY way to do this?
> I can do it with a cursor that constructs a SQL statement, which I EXEC,
> but the 8000 character limit may prove to be a limiting factor.
> sp execsql is somewhat messy for the nature of this issue.
> Any input is appreciated.
> Thanks in advance.
> --

Assuming that (Date, Place) is a unique combination, then this should work:

select
Date,
sum(case when Place = 'M' then Sum else NULL end) as 'M',
sum(case when Place = 'O' then Sum else NULL end) as 'O',
sum(case when Place = 'P' then Sum else NULL end) as 'P'
from
xxx
group by
Date

Simon|||i tried something but couldnt do anything about it. only if you send
me the solution you have it will help me(though with the limiting
factor)

Else i think at any point of time the 8000 character limit will be a
factor that will not allow you to do this always......
RVG

"Brad Joss" <bradjoss@.hotmail.com> wrote in message news:<bgq7p7$m03$1@.news01.intel.com>...
> I do a:
> SELECT * FROM xxx
> And Get:
> Date Place Sum
> A M 1
> A O 3
> A P 2
> B O 5
> B M 4
> B P 2
> And I want it to look like:
> Date M O P
> A 1 3 2
> B 4 5 2
> Can you think of an EASY way to do this?
> I can do it with a cursor that constructs a SQL statement, which I EXEC,
> but the 8000 character limit may prove to be a limiting factor.
> sp execsql is somewhat messy for the nature of this issue.
> Any input is appreciated.
> Thanks in advance.
> --

converting rows to columns

have a urgent requirement. Please somebody help me.

I have a table departinfo with following records

begin_time end_time Name Pieces

10:00 10:15 PopCorn 3
10:15 10:30 Biscuits 5
10:30 10:45 PopCorn 2

Now I need to run a sql query and the output should be as below :

begin_time end_time PopCorn Biscuits

10:00 10:15 3 0
10:15 10:30 0 5
10:30 10:45 2 0

Please note that only one column i.e. PopCorn is created in spite of
having multiple records in the table. Similarly the records are not
fixed. I mean that
there can be n number of records and the columns should be uniquely
created.

Can somebody help me out
PLZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZHere's one solution:

CREATE TABLE MyTable
(
begin_time smalldatetime NOT NULL,
end_time smalldatetime NOT NULL,
Name varchar(10) NOT NULL,
Pieces int NOT NULL
CONSTRAINT PK_MyTable PRIMARY KEY
(
begin_time,
end_time,
Name
)
)

INSERT INTO MyTable
SELECT '10:00', '10:15', 'PopCorn', 3
UNION ALL SELECT '10:15', '10:30', 'Biscuits', 5
UNION ALL SELECT '10:30', '10:45', 'PopCorn', 2

SELECT
CONVERT(char(5), a.begin_time, 108) AS begin_time,
CONVERT(char(5), a.end_time, 108) AS end_time,
ISNULL(SUM(b.Pieces), 0) AS PopCorn,
ISNULL(SUM(c.Pieces), 0) AS Biscuits
FROM
(
SELECT DISTINCT
begin_time,
end_time
FROM MyTable
) AS a
LEFT JOIN MyTable b ON
b.begin_time = a.begin_time AND
b.end_time = a.end_time AND
b.Name = 'PopCorn'
LEFT JOIN MyTable c ON
c.begin_time = a.begin_time AND
c.end_time = a.end_time AND
c.Name = 'Biscuits'
GROUP BY
a.begin_time,
a.end_time
ORDER BY
a.begin_time
GO

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Pooj" <poojaahirrao@.hotmail.com> wrote in message
news:94541780.0401100526.5d3178ae@.posting.google.c om...
> have a urgent requirement. Please somebody help me.
> I have a table departinfo with following records
> begin_time end_time Name Pieces
> 10:00 10:15 PopCorn 3
> 10:15 10:30 Biscuits 5
> 10:30 10:45 PopCorn 2
> Now I need to run a sql query and the output should be as below :
> begin_time end_time PopCorn Biscuits
> 10:00 10:15 3 0
> 10:15 10:30 0 5
> 10:30 10:45 2 0
> Please note that only one column i.e. PopCorn is created in spite of
> having multiple records in the table. Similarly the records are not
> fixed. I mean that
> there can be n number of records and the columns should be uniquely
> created.
>
> Can somebody help me out
PLZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ ZZZZZZZZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ ZZZZZZ|||Hi

Check out a crosstab query that will transform your rows to columns:

http://support.microsoft.com/defaul...b;EN-US;q175574

There are many posts about pivot tables or crosstab queries.. Search google
for more.

John

"Pooj" <poojaahirrao@.hotmail.com> wrote in message
news:94541780.0401100526.5d3178ae@.posting.google.c om...
> have a urgent requirement. Please somebody help me.
> I have a table departinfo with following records
> begin_time end_time Name Pieces
> 10:00 10:15 PopCorn 3
> 10:15 10:30 Biscuits 5
> 10:30 10:45 PopCorn 2
> Now I need to run a sql query and the output should be as below :
> begin_time end_time PopCorn Biscuits
> 10:00 10:15 3 0
> 10:15 10:30 0 5
> 10:30 10:45 2 0
> Please note that only one column i.e. PopCorn is created in spite of
> having multiple records in the table. Similarly the records are not
> fixed. I mean that
> there can be n number of records and the columns should be uniquely
> created.
>
> Can somebody help me out
PLZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ ZZZZZZZZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ ZZZZZZ|||"John Bell" <jbellnewsposts@.hotmail.com> wrote in message news:<btp2ft$h0i$1@.titan.btinternet.com>...
> Hi
> Check out a crosstab query that will transform your rows to columns:
> http://support.microsoft.com/defaul...b;EN-US;q175574
> There are many posts about pivot tables or crosstab queries.. Search google
> for more.
> John
> "Pooj" <poojaahirrao@.hotmail.com> wrote in message
> news:94541780.0401100526.5d3178ae@.posting.google.c om...
> > have a urgent requirement. Please somebody help me.
> > I have a table departinfo with following records
> > begin_time end_time Name Pieces
> > 10:00 10:15 PopCorn 3
> > 10:15 10:30 Biscuits 5
> > 10:30 10:45 PopCorn 2
> > Now I need to run a sql query and the output should be as below :
> > begin_time end_time PopCorn Biscuits
> > 10:00 10:15 3 0
> > 10:15 10:30 0 5
> > 10:30 10:45 2 0
> > Please note that only one column i.e. PopCorn is created in spite of
> > having multiple records in the table. Similarly the records are not
> > fixed. I mean that
> > there can be n number of records and the columns should be uniquely
> > created.
> > Can somebody help me out
> PLZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ ZZZZZZZZZZZZZZZZZZZZZZZZZZ
> ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ ZZZZZZ

Another solution would be using a union statement

select * from departinfo where popcorn = 0
union select * from departinfo where bisuits = 0
order by begin_time|||Hello Dan,
Thanks for the quick response !
The solution you have provided wil not solve my problem due to
following reason :

The departinfo table records are not fixed as shown below. The time
difference can vary also the remainig data. The table will be
populated in the following format. Only the format is fixed the
records are not.

begin_datetime end_datetime Name Pieces

So my requirement is to convert the Name field into unique distinct
columns and match the pieces.
for example if the Name field contains items like Biscuits, Pizza,
PopCorn
then these many columns should be created and matched with the pieces.
Please help !
Thanks
Pooj

"Dan Guzman" <danguzman@.nospam-earthlink.net> wrote in message news:<8HTLb.1542$i4.11@.newsread1.news.atl.earthlink.net>...
> Here's one solution:
> CREATE TABLE MyTable
> (
> begin_time smalldatetime NOT NULL,
> end_time smalldatetime NOT NULL,
> Name varchar(10) NOT NULL,
> Pieces int NOT NULL
> CONSTRAINT PK_MyTable PRIMARY KEY
> (
> begin_time,
> end_time,
> Name
> )
> )
> INSERT INTO MyTable
> SELECT '10:00', '10:15', 'PopCorn', 3
> UNION ALL SELECT '10:15', '10:30', 'Biscuits', 5
> UNION ALL SELECT '10:30', '10:45', 'PopCorn', 2
> SELECT
> CONVERT(char(5), a.begin_time, 108) AS begin_time,
> CONVERT(char(5), a.end_time, 108) AS end_time,
> ISNULL(SUM(b.Pieces), 0) AS PopCorn,
> ISNULL(SUM(c.Pieces), 0) AS Biscuits
> FROM
> (
> SELECT DISTINCT
> begin_time,
> end_time
> FROM MyTable
> ) AS a
> LEFT JOIN MyTable b ON
> b.begin_time = a.begin_time AND
> b.end_time = a.end_time AND
> b.Name = 'PopCorn'
> LEFT JOIN MyTable c ON
> c.begin_time = a.begin_time AND
> c.end_time = a.end_time AND
> c.Name = 'Biscuits'
> GROUP BY
> a.begin_time,
> a.end_time
> ORDER BY
> a.begin_time
> GO
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "Pooj" <poojaahirrao@.hotmail.com> wrote in message
> news:94541780.0401100526.5d3178ae@.posting.google.c om...
> > have a urgent requirement. Please somebody help me.
> > I have a table departinfo with following records
> > begin_time end_time Name Pieces
> > 10:00 10:15 PopCorn 3
> > 10:15 10:30 Biscuits 5
> > 10:30 10:45 PopCorn 2
> > Now I need to run a sql query and the output should be as below :
> > begin_time end_time PopCorn Biscuits
> > 10:00 10:15 3 0
> > 10:15 10:30 0 5
> > 10:30 10:45 2 0
> > Please note that only one column i.e. PopCorn is created in spite of
> > having multiple records in the table. Similarly the records are not
> > fixed. I mean that
> > there can be n number of records and the columns should be uniquely
> > created.
> > Can somebody help me out
> PLZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ ZZZZZZZZZZZZZZZZZZZZZZZZZZ
> ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ ZZZZZZ|||Hi John,

Thanks for the quick reply.I checked the link u have provided but it
seems it
won't solve my problem b'coz DaypartInfo table records are not fixed.
They can vary E.g.my table format is as below:
begin_time end_time Name Pieces

10:00 10:15 PopCorn 3
10:15 10:30 Biscuits 5
10:30 10:45 PopCorn 2

In this table I want to convert the Nae field records as columns i.e.
Name field may contain PoCorn ,Pizza ,Biscuits,Cheese etc . Basically
these records can vary and those should be converted as columns at
runtime...

Please help...

Thanks,
Pooj.

"John Bell" <jbellnewsposts@.hotmail.com> wrote in message news:<btp2ft$h0i$1@.titan.btinternet.com>...
> Hi
> Check out a crosstab query that will transform your rows to columns:
> http://support.microsoft.com/defaul...b;EN-US;q175574
> There are many posts about pivot tables or crosstab queries.. Search google
> for more.
> John
> "Pooj" <poojaahirrao@.hotmail.com> wrote in message
> news:94541780.0401100526.5d3178ae@.posting.google.c om...
> > have a urgent requirement. Please somebody help me.
> > I have a table departinfo with following records
> > begin_time end_time Name Pieces
> > 10:00 10:15 PopCorn 3
> > 10:15 10:30 Biscuits 5
> > 10:30 10:45 PopCorn 2
> > Now I need to run a sql query and the output should be as below :
> > begin_time end_time PopCorn Biscuits
> > 10:00 10:15 3 0
> > 10:15 10:30 0 5
> > 10:30 10:45 2 0
> > Please note that only one column i.e. PopCorn is created in spite of
> > having multiple records in the table. Similarly the records are not
> > fixed. I mean that
> > there can be n number of records and the columns should be uniquely
> > created.
> > Can somebody help me out
> PLZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ ZZZZZZZZZZZZZZZZZZZZZZZZZZ
> ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ ZZZZZZ|||Hi

This may help...

http://www.sqlteam.com/item.asp?ItemID=2955

John|||You can do this very easily and without any
sql coding with the RAC utility for S2k.
It can be used to generate dynamic crosstabs
and solve all types of problems in an easy way.
It's similar in concept to Access crosstab but
much more powerful.

RAC v2.2 and QALite @.
www.rac4sql.net

Converting Rows to Columns

Hi,
I have this resultset :
Mem_id Specialization
19001 Internal medicine
19001 Endoscopy
19001 Oncology

I need to output this like below--(in 1 row)

19001 Internal medicine Endoscopy Oncology

I am using SQL2005. Can anyone please help asap.

ThanksUse Pivot function on SQL2005.

converting rows into columns when the count is not known

i have 2 rows with 3 columns each. ( each rows has a colun called ScoreType which contains different data like Performancelevel,scaledscore

Subject Marks ScoreType
A 2 Performancelevel
A 123 scaledscore
B 4 Performancelevel
B 678 scaledscore

i want this data as

Subject PerformanceLevel ScaledScore
A 123 2
B 678 4

how can i acheive this, Please not that the number of scoretypes is not constant, could be more also

Quote:

Originally Posted by praneethraj

i have 2 rows with 3 columns each. ( each rows has a colun called ScoreType which contains different data like Performancelevel,scaledscore

how can i acheive this, Please not that the number of scoretypes is not constant, could be more also


you didn't point your Database versions. anyway, you can use Cross Tab query.

Converting Rows into Columns MS SQL 2K

I have a SP that returns the information I want but it returns it in 2
separate queries.

Example:
Query 1
Name, Number, Class
Row 1- Mike Phillips, 154AA, AA
and
Query 2
Time, Manual
Row 1 -12:45:22,0
Row 2 -13:04:56,0

What I want it to look like is:
Name, Number, Class, Time 1, Manual 1, Time 2, Manual 2
Row 1- Mike Phillips, 154AA, AA, 12:45:22, 0, 13:04:56, 0

Here is the query I'm using:
DECLARE Class cursor

FOR
--here we get a list of distinct classes to pass to the Class cursor
select Distinct(class_ID) from kt_member_lap
where Race_ID = 83
order by Class_ID;

OPEN Class;

DECLARE @.RaceID int
DECLARE@.RacerCount int
DECLARE @.ClassID char(50)
DECLARE @.classcount
DECLARE @.Racer char(50)
DECLARE @.i int

SET @.RaceID = 83
--this is where we loop through the classes
FETCH NEXT FROM Class INTO @.ClassID
WHILE (@.@.FETCH_STATUS <> -1)
BEGIN

IF (@.@.FETCH_STATUS <> -2)

DECLARE Lap cursor

FOR
Select DISTINCT(Member_ID) from KT_MEMBER_LAP
Where class_ID = @.classID and race_id = @.RaceID

OPEN Lap;

--this is to begin counting from the first lap
SET @.i = 1;

FETCH NEXT FROM Lap INTO @.Racer
WHILE (@.@.FETCH_STATUS <> -1)
BEGIN

IF (@.@.FETCH_STATUS <> -2)
SELECT KT_MEMBER.MEMBER_FNAME + ' ' +
KT_MEMBER.MEMBER_LNAME As MemberName,
CONVERT(nvarchar(3),
KT_MEMBER_CLASS.MEMBER_CLASS_BIKE_NUM) + KT_CLASS.CLASS_LETTER As
BikeNumber,
KT_CLASS.CLASS_DESC
FROM KT_CLASS INNER JOIN
KT_MEMBER_CLASS ON KT_CLASS.CLASS_ID =
KT_MEMBER_CLASS.CLASS_ID INNER JOIN
KT_MEMBER ON KT_MEMBER_CLASS.MEMBER_ID =
KT_MEMBER.MEMBER_ID
WHERE KT_MEMBER.MEMBER_ID = @.Racer and KT_CLASS.CLASS_ID =
@.ClassID

--SELECT @.Racer, @.ClassID

Select MEMBER_LAP_TIME_REAL, member_lap_manual from KT_MEMBER_LAP
Where Member_ID = @.Racer and class_ID = @.classID and race_id =
@.RaceID
ORDER BY MEMBER_LAP_TIME_REAL

--here I count up for the next lap
SET @.i = @.i + 1;

FETCH NEXT FROM Lap INTO @.Racer
END

CLOSE Lap;
DEALLOCATE Lap;

FETCH NEXT FROM Class INTO @.ClassID
END

CLOSE Class;
DEALLOCATE Class;

Any help would be appreciated.[posted and mailed, please reply in news]

dare197 (daniel.white@.perceptivetech.com) writes:
> I have a SP that returns the information I want but it returns it in 2
> separate queries.
> Example:
> Query 1
> Name, Number, Class
> Row 1- Mike Phillips, 154AA, AA
> and
> Query 2
> Time, Manual
> Row 1 -12:45:22,0
> Row 2 -13:04:56,0
> What I want it to look like is:
> Name, Number, Class, Time 1, Manual 1, Time 2, Manual 2
> Row 1- Mike Phillips, 154AA, AA, 12:45:22, 0, 13:04:56, 0

Could there be any number of Time, Manual rows or is there never more
than two? I will assume that you always have two. Then you can try
this query:

SELECT m.MEMBER_FNAME + ' ' + m.MEMBER_LNAME As MemberName,
CONVERT(nvarchar(3), mc.MEMBER_CLASS_BIKE_NUM) +
c.CLASS_LETTER As BikeNumber,
c.CLASS_DESC,
ml1.MEMBER_LAP_TIME_REAL AS "Time 1",
ml1.member_lap_manual AS "Manual 1",
ml2.MEMBER_LAP_TIME_REAL AS "Time 2",
ml2.member_lap_manual AS "Manual 2",
FROM KT_CLASS c
JOIN KT_MEMBER_CLASS mc ON c.CLASS_ID = mc.CLASS_ID
JOIN KT_MEMBER m ON mc.MEMBER_ID = m.MEMBER_ID
JOIN KT_MEMBER_LAP ml1 ON ml1.MEMBER_ID = m.MEMBER_ID
AND mll.CLASS_ID = mc.CLASS_ID
JOIN KT_MEMBER_LAP ml2 ON ml2.MEMBER_ID = m.MEMBER_ID
AND ml2.CLASS_ID = mc.CLASS_ID
AND ml2.member_lap_manual > ml1.member_lap_manual
WHERE m.MEMBER_ID = @.Racer
AND c.CLASS_ID = @.ClassID
ORDER BY c.CLASS_ID, m.MEMBER_ID

Here I have collapsed everything into one query, without any cursor, as
I could see no need for a cursor. Cursors can be a magnitude slower than
set-based statements, so there all reasons to avoid them.

--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp