Autor Beitrag
GSE
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 740

Win 2k, Win XP Pro
D5 Prof, D6 Ent, D2k5 PE
BeitragVerfasst: Mi 04.08.04 07:21 
hi,
ist es möglich mit der SUM Funktion von SQL nur bestimmte Werte zu addieren
so ala SUM(Anzahl) WHERE ( Typ = '12' )

Danke schon mal.

mfg
GSE

_________________
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs
and the universe trying to produce bigger and better idiots. So far, the universe is winning. (Richard Cook)


Zuletzt bearbeitet von GSE am Mi 04.08.04 08:33, insgesamt 1-mal bearbeitet
MSCH
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1448
Erhaltene Danke: 3

W7 64
XE2, SQL, DevExpress, DevArt, Oracle, SQLServer
BeitragVerfasst: Mi 04.08.04 08:00 
ja.
grez
msch

_________________
ist das politisch, wenn ich linksdrehenden Joghurt haben möchte?
GSE Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 740

Win 2k, Win XP Pro
D5 Prof, D6 Ent, D2k5 PE
BeitragVerfasst: Mi 04.08.04 08:08 
:cry: und wie?

das Problem ist folgendes:
ich muss aus einer tabelle:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
NR  TYP Anz
01  RE  1
01  RE  3
01  BE  4
01  LI  2
02  BE  12
02  LI  6


nach der abfrage müsste folgendes rauskommen:

ausblenden Quelltext
1:
2:
3:
NR  BEAnz  REAnz  LIAnz
01   4       4      2
02   12      0      6


sry MSCH das ich erst zu allgemein gefragt habe

so hatte ich probiert

ausblenden Quelltext
1:
SELECT NR, SUM(Anz) WHERE ( Typ = 'RE') as REAnz					

das prob ist nur das ich die nr ja nicht übergeben kann da ja dann WHERE ( Nr = Nr ) dastehen würde und er nicht weiß welche NR :cry:

mfg
GSE

_________________
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs
and the universe trying to produce bigger and better idiots. So far, the universe is winning. (Richard Cook)


Zuletzt bearbeitet von GSE am Mi 04.08.04 08:19, insgesamt 2-mal bearbeitet
MSCH
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1448
Erhaltene Danke: 3

W7 64
XE2, SQL, DevExpress, DevArt, Oracle, SQLServer
BeitragVerfasst: Mi 04.08.04 08:15 
select sum(Integer, Money oder FloatField) as SummeVonFeld from [Datenbank.Tabelle] where Bedingung_StringField like 'huhu' and Bedingung_IntField=1grez
msch

_________________
ist das politisch, wenn ich linksdrehenden Joghurt haben möchte?
GSE Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 740

Win 2k, Win XP Pro
D5 Prof, D6 Ent, D2k5 PE
BeitragVerfasst: Mi 04.08.04 08:21 
hab meinen beitrag mal editiert, meine frage war wohl falsch gestellt, sry

_________________
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs
and the universe trying to produce bigger and better idiots. So far, the universe is winning. (Richard Cook)
smiegel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 992
Erhaltene Danke: 1

WIN 7
D7 Prof., C#, RAD XE Prof.
BeitragVerfasst: Mi 04.08.04 08:49 
Hallo,

so müsste es gehen:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
SELECT
  NR, SUM(IF(TYP="BE";Anz;0)) AS BEAnz,
      SUM(IF(TYP="RE;Anz;0)) AS REAnz,
      SUM(IF(TYP="LI";Anz;0) AS LIAnz
FROM
  Tabelle

_________________
Gruß Smiegel
Ich weiß, daß ich nichts weiß, aber ich weiß mehr als die, die nicht wissen, daß sie nichts wissen. (Sokrates)
GSE Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 740

Win 2k, Win XP Pro
D5 Prof, D6 Ent, D2k5 PE
BeitragVerfasst: Mi 04.08.04 09:35 
leider akzeptiert er das IF net (Invalid use of keyword "IF")
aber trotzdem danke, gibbet noch ne andre möglichkeit?
kann das daran liegen das es (leider) ne paradox table is?

mfg
GSE

_________________
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs
and the universe trying to produce bigger and better idiots. So far, the universe is winning. (Richard Cook)
smiegel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 992
Erhaltene Danke: 1

WIN 7
D7 Prof., C#, RAD XE Prof.
BeitragVerfasst: Mi 04.08.04 10:50 
Hallo,

stimmt, LocalSQL unterstütz kein IF obwohl es in der Liste der reservierten Wörter ist.

Eine weitere Möglichkeit Deine Daten zu erhalten könnte folgendermaßen aussehen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
const
  sQuelle="C:\MeinPfad\MeinTabelle.db";

  with Query1 do
  begin
    Close;
    SQL.Text:=
      'SELECT '+
        'NR, '+
        Format('(SELECT SUM(TYP) FROM "%s" WHERE (TYP="BE")) AS BEAnz, ', [sQuelle])+
        Format('(SELECT SUM(TYP) FROM "%s" WHERE (TYP="RE")) AS REAnz, ', [sQuelle])+
        Format('(SELECT SUM(TYP) FROM "%s" WHERE (TYP="LI")) AS LIAnz', [sQuelle])+#13#10+
      Format('FROM "%s"', [sQuelle]);
    Open;
    while not Eof do
    ...
    ...
  end;


Sollte dies nicht helfen, würde ich Dir ADO und Access als Alternative vorschlagen.

_________________
Gruß Smiegel
Ich weiß, daß ich nichts weiß, aber ich weiß mehr als die, die nicht wissen, daß sie nichts wissen. (Sokrates)
MSCH
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1448
Erhaltene Danke: 3

W7 64
XE2, SQL, DevExpress, DevArt, Oracle, SQLServer
BeitragVerfasst: Mi 04.08.04 11:31 
select nr, sum(..) wird so nicht funktionieren, da da sum ein Aggregatfunktion und nr ein simples feld ist.
(zumindest nicht beim MS-SQL)

es fehlt ein simples group by nr

//Edit

Damit verschwindet auch das ganze IF-Geraffe.
Select Nr, typ, sum(anz) from tabelle group by nr, typ

grez
msch

_________________
ist das politisch, wenn ich linksdrehenden Joghurt haben möchte?
GSE Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 740

Win 2k, Win XP Pro
D5 Prof, D6 Ent, D2k5 PE
BeitragVerfasst: Mi 04.08.04 12:44 
@MSCH: danke, geht jetzt schon mal soweit, dass er mir das alles einzeln anzeigt aber die typen leider getrennt und dann nicht auf einer zeile

mfg
GSE

_________________
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs
and the universe trying to produce bigger and better idiots. So far, the universe is winning. (Richard Cook)
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Di 31.08.04 12:11 
Hi,

schon mal mit Subselects gearbeitet? Hängt natürlich auch von der DB ab. Es soll welche geben, die das nicht können.

select * from
(select sum(xxx) from Tabelle where Typ ='12'),
(select sum(xxx) from Tabelle where Typ ='22')

oder Funktionen auf DB hinterlegen, falls möglich.

Gruß, Peter