Welcome to dbForumz.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Roll up results

 
   Database Forums (Home) -> General Discussions RSS
Next:  sessions not working  
Author Message
john.wb

External


Since: Nov 27, 2008
Posts: 1



(Msg. 1) Posted: Thu Nov 27, 2008 8:10 pm
Post subject: Roll up results
Archived from groups: comp>databases>ms-sqlserver (more info?)

Hi everyone,

5 samples are analyzed for lead in three different tests. Not every
sample is necessarily analyzed in each test, resulting in gaps in the
table.

create table results
(
testnbr int not null,
sampleid char(Cool not null,
result decimal(5,1),
primary key (testnbr, sampleid)
)

insert into results(testnbr, sampleid, result) values (1, 'sample 1',
1.1)
insert into results(testnbr, sampleid, result) values (1, 'sample 2',
null)
insert into results(testnbr, sampleid, result) values (1, 'sample 3',
null)
insert into results(testnbr, sampleid, result) values (1, 'sample 4',
4.Cool
insert into results(testnbr, sampleid, result) values (1, 'sample 5',
null)

insert into results(testnbr, sampleid, result) values (2, 'sample 1',
1.7)
insert into results(testnbr, sampleid, result) values (2, 'sample 2',
2.4)
insert into results(testnbr, sampleid, result) values (2, 'sample 3',
null)
insert into results(testnbr, sampleid, result) values (2, 'sample 4',
null)
insert into results(testnbr, sampleid, result) values (2, 'sample 5',
5.5)

insert into results(testnbr, sampleid, result) values (3, 'sample 1',
null)
insert into results(testnbr, sampleid, result) values (3, 'sample 2',
2.6)
insert into results(testnbr, sampleid, result) values (3, 'sample 3',
1.9)
insert into results(testnbr, sampleid, result) values (3, 'sample 4',
null)
insert into results(testnbr, sampleid, result) values (3, 'sample 5',
5.3)


Given the above I'm trying to return a table where the individual
results are 'rolled-up' towards the top of the list to fill in the
blanks. The output should look like this:

1 sample 1 1.1
1 sample 2 2.4
1 sample 3 1.9
1 sample 4 4.8
1 sample 5 5.5
2 sample 1 1.7
2 sample 2 2.6
2 sample 3 null
2 sample 4 null
2 sample 5 5.3
3 sample 1 null
3 sample 2 null
3 sample 3 null
3 sample 4 null
3 sample 5 null

so that nulls are pushed down to the bottom of the list while numeric
results are pushed to the top. I can't find a pure SQL solution for
this? Does one even exit? Any help would be appreciated.

 >> Stay informed about: Roll up results 
Back to top
Login to vote
John WB

External


Since: Nov 27, 2008
Posts: 2



(Msg. 2) Posted: Thu Nov 27, 2008 9:10 pm
Post subject: Re: Roll up results [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Nov 28, 12:00 am, Plamen Ratchev <Pla... DeleteThis @SQLStudio.com> wrote:
> You can adjust the test number sequence based on the result values. Here
> is one method to accomplish this (requires SQL Server 2005/2008):
>
> SELECT ROW_NUMBER() OVER(PARTITION BY sampleid
>                           ORDER BY CASE
>                                      WHEN result IS NULL
>                                      THEN 2147483647
>                                      ELSE testnbr
>                                    END) AS testnbr,
>         sampleid, result
> FROM results;
>
> --
> Plamen Ratchevhttp://www.SQLStudio.com

Hi Plamen, many thanks for your help. Yes, that looks like it will
work. Since I posted I think I've come up with a solution that will
also work in SQL 2000:

select B.testnbr, B.sampleid, A.result
from
(
select r.sampleid, r.result,
(
select count(*) from results r2
where r2.sampleid = r.sampleid
and r2.result is not null
and r2.testnbr <= r.testnbr
) as testnbr
from results r
where r.result is not null
) as A right join
(
select r.sampleid, r.testnbr
from results r
) as B on A.sampleid = B.sampleid
and A.testnbr = B.testNbr

 >> Stay informed about: Roll up results 
Back to top
Login to vote
Plamen Ratchev

External


Since: Aug 20, 2008
Posts: 182



(Msg. 3) Posted: Fri Nov 28, 2008 12:00 am
Post subject: Re: Roll up results [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

You can adjust the test number sequence based on the result values. Here
is one method to accomplish this (requires SQL Server 2005/2008):

SELECT ROW_NUMBER() OVER(PARTITION BY sampleid
ORDER BY CASE
WHEN result IS NULL
THEN 2147483647
ELSE testnbr
END) AS testnbr,
sampleid, result
FROM results;

--
Plamen Ratchev
http://www.SQLStudio.com
 >> Stay informed about: Roll up results 
Back to top
Login to vote
Plamen Ratchev

External


Since: Aug 20, 2008
Posts: 182



(Msg. 4) Posted: Fri Nov 28, 2008 10:22 am
Post subject: Re: Roll up results [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Yes, on SQL Server 2000 you can use the subquery approach. Have in mind
with large result set performance will not be good because of the
multiple scans needed to calculate the new test number. The solution
with ROW_NUMBER will be a lot more efficient.

--
Plamen Ratchev
http://www.SQLStudio.com
 >> Stay informed about: Roll up results 
Back to top
Login to vote
--CELKO--

External


Since: Apr 17, 2007
Posts: 400



(Msg. 5) Posted: Fri Nov 28, 2008 10:41 am
Post subject: Re: Roll up results [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Can you explain this problem a bit more? The DDL is good and the
sample data is fine. I just do not unde
 >> Stay informed about: Roll up results 
Back to top
Login to vote
--CELKO--

External


Since: Apr 17, 2007
Posts: 400



(Msg. 6) Posted: Fri Nov 28, 2008 1:19 pm
Post subject: Re: Roll up results [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Can you explain this problem a bit more? I like the DDL and the
sample, since so many posters don;t bother any more. But I don't
understand what you want. You talk about a list (no such thing in
RDBMS) and a "top" and "bottom", pushing and blanks -- Can you
explain it in logical terms instead?

You seem to be destroying data, not summarizing it. For example, all
of the results for (test_nbr = 3) are set to NULL in your desired
results. But your own data says that there were results for some of
the samples in that test series.

I can understand that if a sample in test #n has NULLs, you would run
another test #(n+1) until you get a non-NULL result for all samples.
I can see keeping the first non-NULL result with COALESCE () and
little re-design of the schema
 >> Stay informed about: Roll up results 
Back to top
Login to vote
John WB

External


Since: Nov 27, 2008
Posts: 2



(Msg. 7) Posted: Sat Nov 29, 2008 12:38 pm
Post subject: Re: Roll up results [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Nov 28, 4:19 pm, --CELKO-- <jcelko....DeleteThis@earthlink.net> wrote:
> Can you explain this problem a bit more?  I like the DDL and the
> sample, since so many posters don;t bother any more.  But I don't
> understand what you want.  You talk about a list (no such thing in
> RDBMS) and a "top" and "bottom", pushing  and blanks -- Can you
> explain it in logical terms instead?
>
> You seem to be destroying data, not summarizing it.  For example, all
> of the results for (test_nbr = 3) are set to NULL in your desired
> results.  But your own data says that there were results for some of
> the samples in that test series.
>
> I can understand that if a sample in test #n has NULLs, you would run
> another test #(n+1) until you get a non-NULL result for all samples.
> I can see keeping the first non-NULL result with  COALESCE () and
> little re-design of the schema

Hello Joe, the table is used as the recordsource for a crosstab
report. Normally the crosstab looks like this:

test1 test2 test3
sample 1 1.1 1.7 NULL
sample 2 NULL 2.4 2.6
sample 3 NULL NULL 1.9
sample 4 4.8 NULL NULL
sample 5 NULL 5.5 5.3


After moving the results around a bit, we want the crosstab to look
like this:

sample 1 1.1 1.7 NULL
sample 2 2.4 2.6 NULL
sample 3 1.9 NULL NULL
sample 4 4.8 NULL NULL
sample 5 5.5 5.3 NULL
 >> Stay informed about: Roll up results 
Back to top
Login to vote
steve

External


Since: Nov 07, 2007
Posts: 15



(Msg. 8) Posted: Sat Nov 29, 2008 5:32 pm
Post subject: Re: Roll up results [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Nov 29, 1:38 pm, John WB <john... RemoveThis @assaynet.com> wrote:
> On Nov 28, 4:19 pm, --CELKO-- <jcelko... RemoveThis @earthlink.net> wrote:
>

> Hello Joe, the table is used as the recordsource for a crosstab
> report. Normally the crosstab looks like this:
>
>                 test1        test2         test3
> sample 1        1.1             1.7             NULL
> sample 2        NULL    2.4             2.6
> sample 3        NULL    NULL    1.9
> sample 4        4.8             NULL    NULL
> sample 5        NULL    5.5             5.3
>
> After moving the results around a bit, we want the crosstab to look
> like this:
>
> sample 1        1.1             1.7             NULL
> sample 2        2.4             2.6             NULL
> sample 3        1.9             NULL    NULL
> sample 4        4.8             NULL    NULL
> sample 5        5.5             5.3             NULL

So much for mighty joe old:)

The RAC utility will give you the report you want easily.
The rank of testnbr is pivoted except when result is a null in which
case it's sorted last. This way you get the order you want. The rank
reflects the testnbr value (in a sorted order) where the testnbr is
put last if result null.

Exec Rac
@transform='Max(result) as MR',
@rows='sampleid',
@pvtcol='testnbr',
@pvtsort='(case when Max(result) is null then 100000 else testnbr
end)',
@from='results',
@rank='test', @nullcell='null',
@defaults1='y',@racheck='y',@shell='n'

sampleid test1 test2 test3
-------- ----- ----- -----
sample 1 1.1 1.7 null
sample 2 2.4 2.6 null
sample 3 1.9 null null
sample 4 4.8 null null
sample 5 5.5 5.3 null

You can even eliminate the #'s from test# (the rank of testnbr) so you
have:

Exec Rac
@transform='Max(result) as MR',
@rows='sampleid',
@pvtcol='testnbr',
@pvtsort='(case when Max(result) is null then 100000 else testnbr
end)',
@from='##results',
@rank='test', @nullcell='null',
@defaults1='y',@racheck='y',@shell='n',
@replacepvtcols='{[value] for [select left(~[name]~,4)]}',
@select='select sampleid,_pvtcols_
from rac
order by rd'

sampleid test test test
-------- ---- ---- ----
sample 1 1.1 1.7 null
sample 2 2.4 2.6 null
sample 3 1.9 null null
sample 4 4.8 null null
sample 5 5.5 5.3 null

I wrote RAC to allow you to solve problems like this (dynamic pivots)
without getting buried in sql code. Just a little sql and knowing RACs
parameters will let you be much more productive (and kewl:). Works on
S2k+ (RAC is written in t-sql only and does not use cursors:)
Visit RAC @
www.rac4sql.net

best,
steve dassin
www.beyondsql.blogspot.com
 >> Stay informed about: Roll up results 
Back to top
Login to vote
steve11228

External


Since: Nov 19, 2008
Posts: 2



(Msg. 9) Posted: Sun Nov 30, 2008 2:44 pm
Post subject: Re: Roll up results [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Nov 30, 2:18 pm, Hugo Kornelis
>.
> Rac4SQL has not been updated for SQL Server 2005 and SQL Server 2008,....

RAC plays nicely with S2005. Should do the same on S2008 Smile
 >> Stay informed about: Roll up results 
Back to top
Login to vote
Hugo Kornelis

External


Since: Jan 31, 2006
Posts: 330



(Msg. 10) Posted: Sun Nov 30, 2008 4:25 pm
Post subject: Re: Roll up results [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Thu, 27 Nov 2008 20:10:07 -0800 (PST), john.wb RemoveThis @assaynet.com wrote:

>Hi everyone,
>
>5 samples are analyzed for lead in three different tests. Not every
>sample is necessarily analyzed in each test, resulting in gaps in the
>table.
(snip)

Hi John,

I don't usually recommmend products I have no personal experience with,
but if you're on SQL Server 7.0 or SQL Server 2000, then I guess that
the Rac4SQL utility recommended by Steve (the author) will probably be
the best tool for the job. But do download and try a trial edition
before actually using it!

Rac4SQL has not been updated for SQL Server 2005 and SQL Server 2008,
but fortunately the ROW_NUMBER() function introduced in SQL Server 2005
makes it easier to roll this yourself. The query below should give you
the results you need.

WITH TheCTE AS
(SELECT testnbr, sampleid, result,
ROW_NUMBER() OVER (PARTITION BY sampleid
ORDER BY CASE WHEN result IS NULL
THEN 2 ELSE 1 END,
testnbr) AS rn
FROM results)
SELECT rn, sampleid, result
FROM TheCTE
ORDER BY rn, sampleid;

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
 >> Stay informed about: Roll up results 
Back to top
Login to vote
Display posts from previous:   
   Database Forums (Home) -> General Discussions All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
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



[ Contact us | Terms of Service/Privacy Policy ]