Need to be steered in the right direction on how to do this query

  • Hopefully some of you whom are way better a SQL can give me an idea how to get started at this. I need to get three distinct SUM values based off of a field value in a table within a date range.

    In pseudo-script I need:

    Select SumA When Field1 = A, SumB When Field1 = B, SumC When Field1 = C From MyTable Where Field2 >= Date1 and Field2 < = Date2

    How do I go about actually scripting something like that? Thanks for any help.

  • Please post table definitions, sample data and desired output. Read this to see the best way to post this to get quick responses.

    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • something like this perhaps?

    select sum(case when field1 = 'A' then 1 else 0 end) as SUMA,

    sum(case when field1 = 'B' then 1 else 0 end) as SUMB,

    sum(case when field1 = 'C' then 1 else 0 end) as SUMC

    from MyTable Where Field2 >= Date1 and Field2 < = Date2

    The probability of survival is inversely proportional to the angle of arrival.

  • sturner (7/2/2010)


    something like this perhaps?

    select sum(case when field1 = 'A' then 1 else 0 end) as SUMA,

    sum(case when field1 = 'B' then 1 else 0 end) as SUMB,

    sum(case when field1 = 'C' then 1 else 0 end) as SUMC

    from MyTable Where Field2 >= Date1 and Field2 < = Date2

    Let me try, thanks for the input.

    Edit: Yeah that will do it, changing 1 to my field with actual values. Thanks so much.

Viewing 4 posts - 1 through 3 (of 3 total)

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