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. Sum the top 2 records

Sum the top 2 records

Scheduled Pinned Locked Moved Database
databasecomhelpquestion
4 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.
  • B Offline
    B Offline
    Broken Bokken
    wrote on last edited by
    #1

    I have this table UserAttributes UserAttributeId int identity (1,1) not null AttributeId int not null UserId int not null AttributeValue default (1) not null Attribute Id is a FK to a table of attributes and user Id is a FK to the table of users. I am trying to create a trigger that whenever AttributeValue is changed that the sum of the two highest attributes is loaded into a field in the Users table. I would like it to be one statement (so I don't have to iterate a cursor), but I'm stuck on the syntax. Any ideas?

    Broken Bokken You can't carry out a ninja-style assasination dressed as an astronaut. It's the luminous fabric; too visible. - Tripod http://www.brokenbokken.com

    H W 2 Replies Last reply
    0
    • B Broken Bokken

      I have this table UserAttributes UserAttributeId int identity (1,1) not null AttributeId int not null UserId int not null AttributeValue default (1) not null Attribute Id is a FK to a table of attributes and user Id is a FK to the table of users. I am trying to create a trigger that whenever AttributeValue is changed that the sum of the two highest attributes is loaded into a field in the Users table. I would like it to be one statement (so I don't have to iterate a cursor), but I'm stuck on the syntax. Any ideas?

      Broken Bokken You can't carry out a ninja-style assasination dressed as an astronaut. It's the luminous fabric; too visible. - Tripod http://www.brokenbokken.com

      H Offline
      H Offline
      Harini N K
      wrote on last edited by
      #2

      Hi I am not clear with your question, do you want to get two maximum values from a column without using cursor? Please go thro' the following SQL Script, I am not sure whether you are looking for this solution: USE northwind DECLARE @MaxStock SMALLINT DECLARE @NextMaxStock SMALLINT SELECT @MaxStock = MAX(UnitsInStock) FROM PRODUCTS SELECT @MaxStock SELECT @NextMaxStock = MAX(UnitsInStock) FROM PRODUCTS WHERE UnitsInStock < @MaxStock SELECT @NextMaxStock SELECT @MaxStock + @NextMaxStock Hope this is clear.

      Harini

      1 Reply Last reply
      0
      • B Broken Bokken

        I have this table UserAttributes UserAttributeId int identity (1,1) not null AttributeId int not null UserId int not null AttributeValue default (1) not null Attribute Id is a FK to a table of attributes and user Id is a FK to the table of users. I am trying to create a trigger that whenever AttributeValue is changed that the sum of the two highest attributes is loaded into a field in the Users table. I would like it to be one statement (so I don't have to iterate a cursor), but I'm stuck on the syntax. Any ideas?

        Broken Bokken You can't carry out a ninja-style assasination dressed as an astronaut. It's the luminous fabric; too visible. - Tripod http://www.brokenbokken.com

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

        You could try something like: Select Sum(AttributeValue) From ( Select top 2 AttributeValue From UserAttributes Order by AttributeValue desc )

        Wout Louwers

        B 1 Reply Last reply
        0
        • W WoutL

          You could try something like: Select Sum(AttributeValue) From ( Select top 2 AttributeValue From UserAttributes Order by AttributeValue desc )

          Wout Louwers

          B Offline
          B Offline
          Broken Bokken
          wrote on last edited by
          #4

          This was exactly what I was trying to do. Thanks!

          Broken Bokken You can't carry out a ninja-style assasination dressed as an astronaut. It's the luminous fabric; too visible. - Tripod http://www.brokenbokken.com

          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