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. Totals of each transaction type

Totals of each transaction type

Scheduled Pinned Locked Moved Database
questionhelptutorial
5 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.
  • R Offline
    R Offline
    Richard Berry100
    wrote on last edited by
    #1

    Hi How can I get the total number of two types of transaction between two dates per user Example data: User TransactionType MovementDate Avril RECP 2012/01/04 Pat RECP 2012/01/09 Pat SRET 2012/01/09 What I would like is the following: User Recipts Returns Avril 1 0 Pat 1 1 This is where I was heading, but as soon as I put in a where clause to limit the date range I get an Error " Incorrect column expression: 'SUM(CASE WHEN stkhistm.transaction_type = 'RECP Then 1 else 0 end)'

    SELECT SUM(CASE WHEN stkhstm.transaction_type = 'RECP' then 1 else 0 end) , SUM(CASE WHEN stkhstm.transaction_type = 'SRET' then 1 else 0 end)
    FROM vektron.scheme.stkhstm stkhstm
    WHERE stkhstm.movement_date>=[?]

    M 1 Reply Last reply
    0
    • R Richard Berry100

      Hi How can I get the total number of two types of transaction between two dates per user Example data: User TransactionType MovementDate Avril RECP 2012/01/04 Pat RECP 2012/01/09 Pat SRET 2012/01/09 What I would like is the following: User Recipts Returns Avril 1 0 Pat 1 1 This is where I was heading, but as soon as I put in a where clause to limit the date range I get an Error " Incorrect column expression: 'SUM(CASE WHEN stkhistm.transaction_type = 'RECP Then 1 else 0 end)'

      SELECT SUM(CASE WHEN stkhstm.transaction_type = 'RECP' then 1 else 0 end) , SUM(CASE WHEN stkhstm.transaction_type = 'SRET' then 1 else 0 end)
      FROM vektron.scheme.stkhstm stkhstm
      WHERE stkhstm.movement_date>=[?]

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      What database are you using, [?] as a parameter looks like Access, the code looks like TSQL and you are mixing the parameter types. A TSQL parameter would be @Date

      Never underestimate the power of human stupidity RAH

      R 1 Reply Last reply
      0
      • M Mycroft Holmes

        What database are you using, [?] as a parameter looks like Access, the code looks like TSQL and you are mixing the parameter types. A TSQL parameter would be @Date

        Never underestimate the power of human stupidity RAH

        R Offline
        R Offline
        Richard Berry100
        wrote on last edited by
        #3

        Oops sorry - SQL Server 2008 R2 - MS Query - Excel Seem to have got a liitle further: This works:

        SELECT user_id, SUM(CASE WHEN stkhstm.transaction_type = 'RECP' then 1 else 0 end) ,
        SUM(CASE WHEN stkhstm.transaction_type = 'SRET' then 1 else 0 end)
        FROM vektron.scheme.stkhstm stkhstm
        GROUP BY user_id

        Then I tried to add a restriction on the dates:

        SELECT user_id, SUM(CASE WHEN stkhstm.transaction_type = 'RECP' then 1 else 0 end) *
        SUM(CASE WHEN stkhstm.movement_date > #01/01/2012# then 1 else 0 end) * sum(CASE WHEN stkhstm.movement_date < #01/01/2013# then 1 else 0 end),
        SUM(CASE WHEN stkhstm.transaction_type = 'SRET' then 1 else 0 end)
        FROM vektron.scheme.stkhstm stkhstm
        GROUP BY user_id

        But get the same error as before

        P 1 Reply Last reply
        0
        • R Richard Berry100

          Oops sorry - SQL Server 2008 R2 - MS Query - Excel Seem to have got a liitle further: This works:

          SELECT user_id, SUM(CASE WHEN stkhstm.transaction_type = 'RECP' then 1 else 0 end) ,
          SUM(CASE WHEN stkhstm.transaction_type = 'SRET' then 1 else 0 end)
          FROM vektron.scheme.stkhstm stkhstm
          GROUP BY user_id

          Then I tried to add a restriction on the dates:

          SELECT user_id, SUM(CASE WHEN stkhstm.transaction_type = 'RECP' then 1 else 0 end) *
          SUM(CASE WHEN stkhstm.movement_date > #01/01/2012# then 1 else 0 end) * sum(CASE WHEN stkhstm.movement_date < #01/01/2013# then 1 else 0 end),
          SUM(CASE WHEN stkhstm.transaction_type = 'SRET' then 1 else 0 end)
          FROM vektron.scheme.stkhstm stkhstm
          GROUP BY user_id

          But get the same error as before

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          As to the dates, you probably want to use BETWEEN -- I do hope you are storing dates as DATE or DATETIME, not strings. If you execute your command in SSMS, what line does it tell you the error is on?

          R 1 Reply Last reply
          0
          • P PIEBALDconsult

            As to the dates, you probably want to use BETWEEN -- I do hope you are storing dates as DATE or DATETIME, not strings. If you execute your command in SSMS, what line does it tell you the error is on?

            R Offline
            R Offline
            Richard Berry100
            wrote on last edited by
            #5

            Thanks PIEBALDconsult The problem was amongst other things the dates !! This works now:

            SELECT user_id,
            SUM(CASE WHEN (stkhstm.transaction_type = 'RECP' AND stkhstm.movement_date >= {ts '2012-01-01 00:00:00'} AND stkhstm.movement_date < {ts '2013-01-01 00:00:00'}) then 1 else 0 end) ,
            SUM(CASE WHEN (stkhstm.transaction_type = 'SRET' AND stkhstm.movement_date >= {ts '2012-01-01 00:00:00'} AND stkhstm.movement_date < {ts '2013-01-01 00:00:00'}) then 1 else 0 end)
            FROM vektron.scheme.stkhstm stkhstm
            GROUP BY user_id

            'BETWEEN' would be a bit more elegant I guess :)

            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