Announcement

Collapse
No announcement yet.

selection with condition sum(x)

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • selection with condition sum(x)

    Hallo,

    ich habe eine Tabelle (order by cost):

    id cost
    1 2
    2 3
    3 4

    und condition: summe von cost <=5
    wenn ich select Befehl aufrufe,dann bekomme ich:

    id cost
    1 2
    2 3

    wie kann ich das machen mit select ?
    danke sehr!

  • #2
    Ich verstehe leider das Problem nicht ganz. Was hat das mit sum zu tun?

    [highlight=sql]
    SELECT *
    FROM tabellenName
    WHERE cost < 5
    [/highlight]

    Das ist wohl zu trivial. Aber es ist auch nichts da zum summieren, da ja nichts zum Gruppieren vorhanden ist.

    Comment


    • #3
      hi,
      also ich möchte sum() von der spalte "cost", aber die Summe wird nicht größer als 5.

      id cost
      1 2
      2 3
      3 4
      --------
      sum 9 hier: > 5

      ich brauche
      id cost
      1 2
      2 3
      --------------
      sum 5 hier Bedingung erfüllt !!


      dann bekomme ich mit select nur erste zwei records.

      Comment


      • #4
        Mit einem einfachen Select überhaupt nicht. Wäre ien Fall für eine SP.

        Comment


        • #5
          ok, dann mache ich was anders, vielen Dank!

          Comment


          • #6
            Hallo,
            Originally posted by Markus Kinzler View Post
            Mit einem einfachen Select überhaupt nicht. Wäre ien Fall für eine SP.
            Nicht unbedingt . In MySQL ginge z.B. sowas:
            [highlight=sql]
            set @zwsum = 0;
            select t.cost from (
            select @zwsum:=@zwsum+cost zwsum, cost
            from tabellenName) t
            where t.zwsum <= 5;
            [/highlight]

            Gruß Falk
            Wenn du denkst du hast alle Bugs gefunden, dann ist das ein Bug in deiner Denksoftware.

            Quellcode ohne ein Mindestmaß an Formatierung sehe ich mir nicht an! Ich leiste keinen Privatsupport per Mail oder PN!

            Comment


            • #7
              Das wäre aber dann auch eine Art SP oder &quot;execution block&quot;
              In FireBird würde der Code so
              [highlight=sql]
              execute block
              returns (id integer,cost float)
              as
              declare summe float = 0;
              begin
              for select id, cost from <Tabelle> into :id, :cost do
              begin
              summe = :summe + :cost;
              if ( summe <=5) then suspend;
              end
              end
              [/highlight]
              lauten
              Zuletzt editiert von Markus Kinzler; 21.05.2010, 11:15.

              Comment

              Working...
              X