 |
|
 |
|
Next: Default sorting order on View in SSIS is differen..
|
| Author |
Message |
External

Since: Nov 03, 2008 Posts: 3
|
(Msg. 1) Posted: Mon Nov 03, 2008 12:37 pm
Post subject: combining multiple rows into one row Archived from groups: microsoft>public>sqlserver>mseq (more info?)
|
|
|
Hello Everyone,
This seems simple, but I am finding it hard to do it without a temp table or
a cursor. I need to do it without both.
The values of PRTHASH below need to become a single row
select TOP(5) CUST_ID,
hashbytes('SHA1', CAST(CUST_ID AS VARCHAR) +
',' + NAME1 +
',' + CAST(CUST_TIMESTAMP AS VARCHAR) )
AS 'PRTHASH'
from cust
where Cust_ID > @start
order by Cust_ID
I can do the following if I store CUST_ID and PRTHASH in a TEMP table
select @part ='';
select @part= @part + cast(PRTHASH AS VARCHAR)+ ',' from TEMP
But without a temp table or cursor, I am lost. I believe a correlated
subquery or replace statements with CASE can be used, but can't figure out
how. Even if I do use them, wouldn't this require writing many select
statements, if I select TOP 100 rows.
Please give me some hint if there exists one. >> Stay informed about: combining multiple rows into one row |
|
| Back to top |
|
 |  |
External

Since: Nov 03, 2008 Posts: 3
|
(Msg. 2) Posted: Mon Nov 03, 2008 2:56 pm
Post subject: RE: combining multiple rows into one row [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
I have seen some posts below that answer a similar question, but they either
use a function or save a table somewhere. I am not supposed to do that. I
also need to use a command that most major databases will understand. The
single row strings must fall out of select statements. Is that possible?
Thanks for your help!
"mayub" wrote:
> Hello Everyone,
>
> This seems simple, but I am finding it hard to do it without a temp table or
> a cursor. I need to do it without both.
>
> The values of PRTHASH below need to become a single row
>
> select TOP(5) CUST_ID,
> hashbytes('SHA1', CAST(CUST_ID AS VARCHAR) +
> ',' + NAME1 +
> ',' + CAST(CUST_TIMESTAMP AS VARCHAR) )
> AS 'PRTHASH'
> from cust
> where Cust_ID > @start
> order by Cust_ID
>
> I can do the following if I store CUST_ID and PRTHASH in a TEMP table
>
> select @part ='';
> select @part= @part + cast(PRTHASH AS VARCHAR)+ ',' from TEMP
>
> But without a temp table or cursor, I am lost. I believe a correlated
> subquery or replace statements with CASE can be used, but can't figure out
> how. Even if I do use them, wouldn't this require writing many select
> statements, if I select TOP 100 rows.
>
> Please give me some hint if there exists one.
>
>
> >> Stay informed about: combining multiple rows into one row |
|
| Back to top |
|
 |  |
External

Since: Nov 03, 2008 Posts: 3
|
(Msg. 3) Posted: Tue Nov 04, 2008 12:02 pm
Post subject: RE: combining multiple rows into one row [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Here is the answer to my question:
select @part='';
select TOP(5) @part= @part+ cast(hashbytes('SHA1', CAST(CUST_ID AS
VARCHAR) +
',' + NAME1 +
',' + CAST(CUST_TIMESTAMP AS VARCHAR) )+
AS VARCHAR)+ ','
from cust
where Cust_ID > @start
order by cust_ID;
"mayub" wrote:
> I have seen some posts below that answer a similar question, but they either
> use a function or save a table somewhere. I am not supposed to do that. I
> also need to use a command that most major databases will understand. The
> single row strings must fall out of select statements. Is that possible?
> Thanks for your help!
>
>
>
>
> "mayub" wrote:
>
> > Hello Everyone,
> >
> > This seems simple, but I am finding it hard to do it without a temp table or
> > a cursor. I need to do it without both.
> >
> > The values of PRTHASH below need to become a single row
> >
> > select TOP(5) CUST_ID,
> > hashbytes('SHA1', CAST(CUST_ID AS VARCHAR) +
> > ',' + NAME1 +
> > ',' + CAST(CUST_TIMESTAMP AS VARCHAR) )
> > AS 'PRTHASH'
> > from cust
> > where Cust_ID > @start
> > order by Cust_ID
> >
> > I can do the following if I store CUST_ID and PRTHASH in a TEMP table
> >
> > select @part ='';
> > select @part= @part + cast(PRTHASH AS VARCHAR)+ ',' from TEMP
> >
> > But without a temp table or cursor, I am lost. I believe a correlated
> > subquery or replace statements with CASE can be used, but can't figure out
> > how. Even if I do use them, wouldn't this require writing many select
> > statements, if I select TOP 100 rows.
> >
> > Please give me some hint if there exists one.
> >
> >
> > >> Stay informed about: combining multiple rows into one row |
|
| Back to top |
|
 |  |
External

Since: Jan 10, 2008 Posts: 608
|
(Msg. 4) Posted: Tue Nov 04, 2008 3:21 pm
Post subject: Re: combining multiple rows into one row [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
mayub,
Yes, that does appear to work in SQL Server, but you also said, " I also
need to use a command that most major databases will understand." I am
unsure that "most major databases" will process this the same way.
Also, I do not believe that the "SELECT TOP (5) ... ORDER BY Cust_ID" is
guaranteed to build your string in low to high order, if that is important
to you. The TOP(5) will be honored, but the optimizer does not need to sort
in order to honor that directive.
So, you can wind up with a strings that has your TOP (5) in an order such
as: 5,1,4,2,3
FWIW,
RLF
"mayub" wrote in message
> Here is the answer to my question:
>
> select @part='';
> select TOP(5) @part= @part+ cast(hashbytes('SHA1', CAST(CUST_ID AS
> VARCHAR) +
> ',' + NAME1 +
> ',' + CAST(CUST_TIMESTAMP AS
> VARCHAR) )+
> AS VARCHAR)+ ','
> from cust
> where Cust_ID > @start
> order by cust_ID;
>
>
>
>
> "mayub" wrote:
>
>> I have seen some posts below that answer a similar question, but they
>> either
>> use a function or save a table somewhere. I am not supposed to do that. I
>> also need to use a command that most major databases will understand. The
>> single row strings must fall out of select statements. Is that possible?
>> Thanks for your help!
>>
>>
>>
>>
>> "mayub" wrote:
>>
>> > Hello Everyone,
>> >
>> > This seems simple, but I am finding it hard to do it without a temp
>> > table or
>> > a cursor. I need to do it without both.
>> >
>> > The values of PRTHASH below need to become a single row
>> >
>> > select TOP(5) CUST_ID,
>> > hashbytes('SHA1', CAST(CUST_ID AS VARCHAR) +
>> > ',' + NAME1 +
>> > ',' + CAST(CUST_TIMESTAMP AS
>> > VARCHAR) )
>> > AS 'PRTHASH'
>> > from cust
>> > where Cust_ID > @start
>> > order by Cust_ID
>> >
>> > I can do the following if I store CUST_ID and PRTHASH in a TEMP table
>> >
>> > select @part ='';
>> > select @part= @part + cast(PRTHASH AS VARCHAR)+ ',' from TEMP
>> >
>> > But without a temp table or cursor, I am lost. I believe a correlated
>> > subquery or replace statements with CASE can be used, but can't figure
>> > out
>> > how. Even if I do use them, wouldn't this require writing many select
>> > statements, if I select TOP 100 rows.
>> >
>> > Please give me some hint if there exists one.
>> >
>> >
>> > >> Stay informed about: combining multiple rows into one row |
|
| Back to top |
|
 |  |
| Related Topics: | Set default for null values in unmatched rows of left join - Is there an easy way to set a default value for values in unmatched rows that return null I wouldlike to return 0 for fields that return null in a left join
update querie multiple tables - Hi I wish to create a stored procedure that updates multiple tables. I have a table called User CREATE TABLE [dbo].[Users]( [UserId] [int] NULL, [Active] [bit] NULL, [OrganisationId] [int] NULL, [Username] [varchar](20), [Password] [varchar](20),....
index on a view - Hello, I have a table (TAB) and A View with alias (VIEW) Table cod varchar 3 descr carchar 60 my view cod alias COd1 descr alias DES Now i need a index on view with key COD1 I can't create it. Can..
Creating an alias - I have an alias that can get very long because it maintains a information of where it came from. Is there a way to alias an alias? My first thought was: Declare @T1 varchar(128) Set T1 = 'MyAlias' .... Inner Join Table1 as @T1 This of course does no...
Formatting numeric fields in select-clause - This is propably a very simple question, but I can“t seem to find the answer to it in the documentation. I want to format a numeric field so the result is right justified and zero-filled. ex select 1 will give the result 01 How do I manage this.. |
|
You can post new topics in this forum You can reply to topics in this forum You can edit your posts in this forum You can delete your posts in this forum You can vote in polls in this forum
|
|
|
|
 |
|
|