Results 1 to 6 of 6

Thread: SQL COUNT(*) Function

  1. #1

    Thread Starter
    Addicted Member Genom's Avatar
    Join Date
    May 2006
    Posts
    186

    SQL COUNT(*) Function

    I am tring to make a query but I am a beginner in SQL commands. Example is following:

    There is the table which is my database and Query will look like that. I have added everything to picture.

    I am using MS SQL 2005
    (Sorry about my english)

    Note: The result of the query shall look like this:
    ID2___Count_____Count of ID2 with Option=1_____Count of ID2 withOption=2
    1_____5_________3___________________________2
    2_____4_________0___________________________0
    3_____6_________1___________________________2
    4_____4_________2___________________________1


    Thanks for every kind of help
    Dim Me As Coder

  2. #2
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Re: SQL COUNT(*) Function

    Check this crosstab query .You can modify it according to your need
    Code:
    SELECT ID2,
    count(case [Option] when 1 then 1 else null end) as [ID2-Option1],
    count(case [Option] when 2 then 1 else null end) as [ID2-Option2]
    FROM TEST where Option in(1,2)
    GROUP BY ID2

  3. #3

    Thread Starter
    Addicted Member Genom's Avatar
    Join Date
    May 2006
    Posts
    186

    Re: SQL COUNT(*) Function

    does it work in Access too?
    Dim Me As Coder

  4. #4
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Smile Re: SQL COUNT(*) Function

    For creating crosstab query in Access Check for TRANSFORM syntax.
    Check this
    Code:
    TRANSFORM Count(TEST.ID2) AS CountOfID2
    SELECT TEST.ID2
    FROM TEST
    GROUP BY TEST.ID2
    PIVOT "CountOfID2-" & TEST.Id2 In ("Option-1","Option-2","Option-3");
    Last edited by amrita; Apr 10th, 2007 at 06:24 AM.

  5. #5

    Thread Starter
    Addicted Member Genom's Avatar
    Join Date
    May 2006
    Posts
    186

    Re: SQL COUNT(*) Function

    Thanks for your help...
    I have seen that the columnname Option is being put in "[]" What does it mean? And how would the code if I would use another name like "IDOption"?
    And can you explain this code please:

    SELECT ID2,
    count(case [Option] when 1 then 1 else null end) as [ID2-Option1],
    count(case [Option] when 2 then 1 else null end) as [ID2-Option2]
    FROM TEST where Option in(1,2)
    GROUP BY ID2
    Dim Me As Coder

  6. #6
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Re: SQL COUNT(*) Function

    Read about CrossTab Queries in SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width