Counting twice on one field

  • I am trying to do a count or a sum on

    Categories where 001, 003, 004 are Tests and 002 are sessions. All these codes exists under one field called Lesson_CatId. I need to split the counts of tests and sessions. My results should look like:

    e.g.

    Total_Test_For_Oct Total_Sessions_For_Oct

    3 2

    The SQL below is not allowing me to do this. How can I edit this to get my results?

    SELECT SUM(IIF(Lession_CatId <> "002", 1, 0)) AS Total_Test_For_Oct, SUM(IIF(Lesson_CatId = "002", 1, 0)) AS Total_Session_For_Oct

    FROM Booking

    WHERE ((Lesson_DateSchedule.Lesson_Date) Between #10/1/2008# And #10/31/2008#);

  • cindy_sinath (11/9/2008)


    I am trying to do a count or a sum on

    Categories where 001, 003, 004 are Tests and 002 are sessions. All these codes exists under one field called Lesson_CatId. I need to split the counts of tests and sessions. My results should look like:

    e.g.

    Total_Test_For_Oct Total_Sessions_For_Oct

    3 2

    The SQL below is not allowing me to do this. How can I edit this to get my results?

    SELECT SUM(IIF(Lession_CatId <> "002", 1, 0)) AS Total_Test_For_Oct, SUM(IIF(Lesson_CatId = "002", 1, 0)) AS Total_Session_For_Oct

    FROM Booking

    WHERE ((Lesson_DateSchedule.Lesson_Date) Between #10/1/2008# And #10/31/2008#);

    Since you didn't post any test data, I can only answer why this particular query isn't running...

    The from clause only handles the Booking table, though in your where clause the Lesson_DateSchedule suddenly appears. When I run the query on my own test data without the where clause, it runs fine.

    Is this enough answer?

    Greetings,

    Ronald

    Ronald HensbergenHelp us, help yourself... Post data so we can read and use it: http://www.sqlservercentral.com/articles/Best+Practices/61537/-------------------------------------------------------------------------2+2=5 for significant large values of 2

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply