Autor Beitrag
mexx
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1183



BeitragVerfasst: Di 16.10.07 09:53 
Ich habe eine Hauptgruppe, eine Untergruppe und eine ID. Beispiel.

H U ID
1 1 1000
1 2 1000
2 1 1001
1 1 1001

Mit welcher SQL würde ich nun nun herrausfinden, welche Hauptgruppe und Untergruppe für ID 1000 und 1001 identisch ist?

_________________
Das Unsympathische an den Computern ist, dass sie nur ja oder nein sagen können, aber nicht vielleicht.


Zuletzt bearbeitet von mexx am Di 16.10.07 11:12, insgesamt 1-mal bearbeitet
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 16.10.07 10:01 
ausblenden SQL-Anweisung
1:
2:
3:
Select id
  from tabelle
 where h = u

Das hat aber irgendwie nichts mit Sortierung zu tun. Passe bitte den Titel des Topics an.
mexx Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1183



BeitragVerfasst: Di 16.10.07 10:08 
Ich glaube, Du hast mich nicht verstanden. Ich habe nur die beiden IDs und muss wissen, in welchen Gruppen sie sich befinden. Beide IDs befinden sich aber auch in unterschiedlichen Gruppen. Ich möchte nur die Gruppen haben, in denen sich ID 1000 und 1001 gemeinsam befinden.

_________________
Das Unsympathische an den Computern ist, dass sie nur ja oder nein sagen können, aber nicht vielleicht.
alex517
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 60


D7Ent, FB, FIBPlus
BeitragVerfasst: Di 16.10.07 10:26 
Hi mexx,

ausblenden SQL-Anweisung
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
select
  M.H,
  M.U
from
  M
where
  M.ID in (1000, 1001)
group by
  M.H,
  M.U
HAVING
  count(*) > 1


alex
mexx Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1183



BeitragVerfasst: Di 16.10.07 10:40 
Klasse Alex, ich danke Dir. Mit Group by hatte ich es auch schon versucht, aber HAVING kannte ich nicht und so ging das immer schief. Thx!

_________________
Das Unsympathische an den Computern ist, dass sie nur ja oder nein sagen können, aber nicht vielleicht.
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 16.10.07 10:44 
Hat aber immer noch nix mit Sortierung zu tun.
Bitte passe den Titel an.

Sorry, dass ich Deine Frage falsch interpretiert habe.
mexx Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1183



BeitragVerfasst: Di 16.10.07 12:02 
Sorry, Jungs, aber es passt dann doch noch nicht ganz.

H U ID
1 1 1000
1 2 1000
2 1 1001
1 1 1001
3 3 1002

Wenn ich nun die 3 IDs 1000,1001,1002 verwende und identische U zu finden, erhalte ich Ergebniss alle Untergruppen, wo ein count > 1 erfüllt wird. Aber ich möchte NUR die Gruppen haben, wo 1000, 1001 und 1002 gemeinsam drin stehen. Im aktuellen Beispiel, dürfte da jetzt keine Gruppe raus kommen, weil die 3 IDs nicht in allen Gruppen drin sind.

_________________
Das Unsympathische an den Computern ist, dass sie nur ja oder nein sagen können, aber nicht vielleicht.
alex517
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 60


D7Ent, FB, FIBPlus
BeitragVerfasst: Di 16.10.07 12:57 
Zitat:
Wenn ich nun die 3 IDs 1000,1001,1002 verwende und identische U zu finden, ..


Dann muß count(*) = 3 sein stimmts?!?! :D

ausblenden SQL-Anweisung
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
select 
  M.HG,
  M.UG,
  count(*)
from
  MEXX M
where
  M.IDENT in (1000, 1001, 1002)
group by
  M.HG,
  M.UG
HAVING
  count(*) = 3


Diese Variante geht aber nur wenn die Kombination H,U,ID unique sind.
Wenn z.B sowas möglich ist
H U ID
1 1 1000
1 1 1000
1 1 1001
..

dann mußt du dir die auszuwertenden Menge als distinct(H,U,ID) erst zurechtlegen,
damit gleich Sätzen nur einmal gezählt werden.
In FB2.x geht das mit derived table.

ausblenden SQL-Anweisung
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
select
  X.HG,
  X.UG,
  count(*)
from
  (select distinct
    IDENT,
    HG,
    UG
  from
    MEXX
  where
    IDENT in (1000, 1001, 1002)) as X
group by
  X.HG,
  X.UG
HAVING count(*) = 3


An Stelle eine derived table könnte man auch eine View verwenden.


alex