Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. sql with count [modified]

sql with count [modified]

Scheduled Pinned Locked Moved Database
databasehelptutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    foryou
    wrote on last edited by
    #1

    Hi!! I have the following database : id_student-------id_module-----id_validat-------years 1000--------------11------------2----------------2006 1000---------------11-----------2-----------------2007 2000---------------11-----------3----------------2006 2000---------------12-----------1-----------------2007 3000---------------11------------2----------------2009 3000---------------11------------3-----------------2008 I try to show id_student who id_module=11 and id_validat = 2For two different years in this example the student who id_student=1000 verefier the condition. but my sql is false

    SELECT COUNT(*) AS Expr1, id_student,years
    FROM T1
    WHERE (id_MODULE=11 )AND (id_validat= 2)
    GROUP BY id_student,years
    HAVING (COUNT(*) = 2)

    Can you help me? Thanks

    modified on Wednesday, February 4, 2009 8:50 AM

    B W 2 Replies Last reply
    0
    • F foryou

      Hi!! I have the following database : id_student-------id_module-----id_validat-------years 1000--------------11------------2----------------2006 1000---------------11-----------2-----------------2007 2000---------------11-----------3----------------2006 2000---------------12-----------1-----------------2007 3000---------------11------------2----------------2009 3000---------------11------------3-----------------2008 I try to show id_student who id_module=11 and id_validat = 2For two different years in this example the student who id_student=1000 verefier the condition. but my sql is false

      SELECT COUNT(*) AS Expr1, id_student,years
      FROM T1
      WHERE (id_MODULE=11 )AND (id_validat= 2)
      GROUP BY id_student,years
      HAVING (COUNT(*) = 2)

      Can you help me? Thanks

      modified on Wednesday, February 4, 2009 8:50 AM

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      SELECT COUNT(idstudent) AS IDStudentCount, idstudent, (SELECT TOP (1) t1.years FROM myTable AS t1 WHERE (t1.idstudent = myTable.idstudent)) AS Years FROM myTable WHERE (idvalidat = 2) AND (idmodule = 11) GROUP BY idstudent HAVING (COUNT(idstudent) = 2)


      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

      1 Reply Last reply
      0
      • F foryou

        Hi!! I have the following database : id_student-------id_module-----id_validat-------years 1000--------------11------------2----------------2006 1000---------------11-----------2-----------------2007 2000---------------11-----------3----------------2006 2000---------------12-----------1-----------------2007 3000---------------11------------2----------------2009 3000---------------11------------3-----------------2008 I try to show id_student who id_module=11 and id_validat = 2For two different years in this example the student who id_student=1000 verefier the condition. but my sql is false

        SELECT COUNT(*) AS Expr1, id_student,years
        FROM T1
        WHERE (id_MODULE=11 )AND (id_validat= 2)
        GROUP BY id_student,years
        HAVING (COUNT(*) = 2)

        Can you help me? Thanks

        modified on Wednesday, February 4, 2009 8:50 AM

        W Offline
        W Offline
        Wendelius
        wrote on last edited by
        #3

        Your query was very close. Just don't group by on years column:

        SELECT id_student,
        COUNT(*) as years
        FROM T1
        WHERE (id_MODULE=11 )
        AND (id_validat= 2)
        GROUP BY id_student
        HAVING (COUNT(*) = 2)

        The need to optimize rises from a bad design.My articles[^]

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups