Combining multiple selected records into a single record

Combining multiple selected records into a single record

Post by Lee V. Duh » Thu, 10 Jun 1999 04:00:00



Is there a way that I can run a query, with a group by and return the values
in a single record?

Example

    This query may return 4 records
    Select Field1, Count(*)
        From Table1
        Group By Field 1

        Field1:        Count(*)
           1                  25
           2                 10
           3                  23
           4                    4

I want to take those results and return a SINGLE record query with a colume
for each record
   Example:
     Colume Name -->        1        2        3        4
     Record Values -->       25     10      24       4

Is there a way to perform this function?

Any info would be appreciated!

Thanks
Lee

 
 
 

Combining multiple selected records into a single record

Post by Chris Behren » Thu, 10 Jun 1999 04:00:00


We seem to be working on nearly the same problem. I've been working on a
cursor solution to this, but have run into a roadblock. The code goes
something like this:

SELECT szContent, szTitle FROM tblActionData

OPEN csrActionData



Close..etc.

First of all, not dynamic. I suppose there may be some array solution to
this though... the real problem is that apparently you can't use a variable
as a column alias.

Any ideas anyone?

--
Chris Behrens

http://www.deltav.net



Quote:> Is there a way that I can run a query, with a group by and return the
values
> in a single record?

> Example

>     This query may return 4 records
>     Select Field1, Count(*)
>         From Table1
>         Group By Field 1

>         Field1:        Count(*)
>            1                  25
>            2                 10
>            3                  23
>            4                    4

> I want to take those results and return a SINGLE record query with a
colume
> for each record
>    Example:
>      Colume Name -->        1        2        3        4
>      Record Values -->       25     10      24       4

> Is there a way to perform this function?

> Any info would be appreciated!

> Thanks
> Lee


 
 
 

Combining multiple selected records into a single record

Post by BPMargoli » Thu, 10 Jun 1999 04:00:00


Lee,

Assuming you know in advance the maximum number of rows that will be
returned using the GROUP BY (and assuming of course that the maximum number
of rows is a manageable number), you might consider the following:

Select Column1 = sum (case when Field1 = 1 then cnt else 0 end),
            Column2 = sum (case when Field1 = 2 then cnt else 0 end),
            Column3 = sum (case when Field1 = 3 then cnt else 0 end),
            Column4 = sum (case when Field1 = 4 then cnt else 0 end)

From (
     Select Field1, cnt = Count(*)
         From Table1
         Group By Field1) as dtable



Quote:> Is there a way that I can run a query, with a group by and return the
values
> in a single record?

> Example

>     This query may return 4 records
>     Select Field1, Count(*)
>         From Table1
>         Group By Field 1

>         Field1:        Count(*)
>            1                  25
>            2                 10
>            3                  23
>            4                    4

> I want to take those results and return a SINGLE record query with a
colume
> for each record
>    Example:
>      Colume Name -->        1        2        3        4
>      Record Values -->       25     10      24       4

> Is there a way to perform this function?

> Any info would be appreciated!

> Thanks
> Lee

 
 
 

Combining multiple selected records into a single record

Post by Lee V. Duh » Fri, 11 Jun 1999 04:00:00


BP:

I believe this is what I tried, but I'll try it again to make sure!

Thanks for the information


> Lee,

> Assuming you know in advance the maximum number of rows that will be
> returned using the GROUP BY (and assuming of course that the maximum
number
> of rows is a manageable number), you might consider the following:

> Select Column1 = sum (case when Field1 = 1 then cnt else 0 end),
>             Column2 = sum (case when Field1 = 2 then cnt else 0 end),
>             Column3 = sum (case when Field1 = 3 then cnt else 0 end),
>             Column4 = sum (case when Field1 = 4 then cnt else 0 end)

> From (
>      Select Field1, cnt = Count(*)
>          From Table1
>          Group By Field1) as dtable



> > Is there a way that I can run a query, with a group by and return the
> values
> > in a single record?

> > Example

> >     This query may return 4 records
> >     Select Field1, Count(*)
> >         From Table1
> >         Group By Field 1

> >         Field1:        Count(*)
> >            1                  25
> >            2                 10
> >            3                  23
> >            4                    4

> > I want to take those results and return a SINGLE record query with a
> colume
> > for each record
> >    Example:
> >      Colume Name -->        1        2        3        4
> >      Record Values -->       25     10      24       4

> > Is there a way to perform this function?

> > Any info would be appreciated!

> > Thanks
> > Lee

 
 
 

1. Select multiple top 5 record from single record set

Hi there,  We have run into a problem with querying the top 5 records of a
database. What our database consist of is ports in our network that have had
stats collected on them for the day, every 15 min, so 96 samples per port.
The data looks like this:
Source        Cells            Sample#
port1            15                    1
.
.
port1            22                   96
port2            111                   1
.
.
port2            245                  96

So, if there is 50 ports, were looking at 4800 records in the database.
What we have been trying to do is query the top 5 Cells for each port.  I
have tried subqueries, cursurs, you name it!

Help would be much appreciated!

Mike

2. Joining UDF's on ROLLUP fields

3. Combining fields from multiple records into a single field

4. 17917-NJ-Hackensack-C-UNIX-SQL-ORACLE-JR Applications programmer

5. Combining multiple records of same types into one record

6. ESQL vs. database APIs

7. VB6 ADO and Multiple Records/Single Records

8. Select stmt w/Outer join

9. Module-level multiple-record recordset vs temp single-record recordset

10. SQL Question: splitting single record into multiple records.

11. Link multiple datagrids to a single record (Master\Detail Form with multiple details)

12. How to combine multiple records into one beyond a simple union query

13. combining records and determining record count