Can this be done with SQL?

Can this be done with SQL?

Post by Vadim Nagorny » Thu, 12 Oct 2000 04:00:00



Hi,
I have the following table:

Field1  Field2  Field3
----------------------
  A       5     aaa
  A       5     bbb
  B       8     ccc
  B       8     ddd
  C       2     eee
  C       2     fff
  C       4     ggg
----------------------
I need a query that would return:

  A       5     aaa   2
  A       5     bbb   2
  B       8     ccc   2
  B       8     ddd   2
  C       2     eee   3
  C       2     fff   3
  C       4     ggg   3

i.e. all rows are selected, plus extra column
added that shows how many rows with certain
value of Field1 are in the table.
Can this be done with SQL, please?
Thanks,
Vadim.

 
 
 

Can this be done with SQL?

Post by Stefan » Thu, 12 Oct 2000 04:00:00


Vadim,

Considering your tablename as test, the query would look like this:

select t.field1, t.field2, t.field3, q.cnt from test t join
(select Field1, count(*) as Cnt from test group by field1) as q
on t.field1 = q.field1


  Hi,
  I have the following table:

  Field1  Field2  Field3
  ----------------------
    A       5     aaa
    A       5     bbb
    B       8     ccc
    B       8     ddd
    C       2     eee
    C       2     fff
    C       4     ggg
  ----------------------
  I need a query that would return:

    A       5     aaa   2
    A       5     bbb   2
    B       8     ccc   2
    B       8     ddd   2
    C       2     eee   3
    C       2     fff   3
    C       4     ggg   3

  i.e. all rows are selected, plus extra column
  added that shows how many rows with certain
  value of Field1 are in the table.
  Can this be done with SQL, please?
  Thanks,
  Vadim.

 
 
 

Can this be done with SQL?

Post by Vadim Nagorny » Fri, 13 Oct 2000 04:00:00


Stefan,
it works perfect!
Thanks a lot for teaching me a good piece of SQL.
Regards,
Vadim.

Vadim,

Considering your tablename as test, the query would look like this:

select t.field1, t.field2, t.field3, q.cnt from test t join
(select Field1, count(*) as Cnt from test group by field1) as q
on t.field1 = q.field1


    Hi,
    I have the following table:

    Field1  Field2  Field3
    ----------------------
      A       5     aaa
      A       5     bbb
      B       8     ccc
      B       8     ddd
      C       2     eee
      C       2     fff
      C       4     ggg
    ----------------------
    I need a query that would return:

      A       5     aaa   2
      A       5     bbb   2
      B       8     ccc   2
      B       8     ddd   2
      C       2     eee   3
      C       2     fff   3
      C       4     ggg   3

    i.e. all rows are selected, plus extra column
    added that shows how many rows with certain
    value of Field1 are in the table.
    Can this be done with SQL, please?
    Thanks,
    Vadim.

 
 
 

1. ADO and SQL: what am I doing wrong ?

Hello,

I'm in trouble with an SQL query, in which the OLEDB Jet engine does not
find 1 parameter. My problem is that in the query there are no parameters,
and Access does recognize the query very well.
The query is:

SELECT * FROM PREZZI WHERE ((PREZZI.PIANO = 0) AND (PREZZI.STANZA = "01")
AND (PREZZI.DATAPREZZO <= #03/16/1999 12.00.00 PM#)) ORDER BY
PREZZI.DATAPREZZO

2. Export data from a cube

3. What am I doing wrong (RTRIM, PL/SQL)

4. Java and JDBC in SGI environment

5. SQL problem, what am I doing wong?

6. Inserting and retrieving images and documents into SQL Server

7. SQL What am I doing wrong ?

8. Inserting Fields (During

9. Am I doing this correctly?

10. What am I doing wrong...

11. What am I doing wrong

12. Why am I doing a Table Scan?

13. Query performance issue - What Am I doing wrong?