Autor Beitrag
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 14.10.08 21:22 
Moin!

Ich brauch mal (My)SQL-Nachhilfe, bitte. ;)

Ich habe eine Tabelle, die (vereinfacht) zwei Spalten hat:
ausblenden SQL-Anweisung
1:
2:
zeitpunkt: datetime
id: char(20)

Im Laufe der Zeit sammeln sich IDs und zugehörige Zeitpunkte an (könnte man als Login ansehen, zum besseren Verständnis), wobei die Usermenge zwar relativ groß, aber begrenzt ist.

Ich brauch nun die Anzahl eindeutiger (distinct) IDs über die ganze Tabelle. Das ist nicht schwer:
ausblenden SQL-Anweisung
1:
SELECT COUNTDISTINCT id ) FROM tabelle					


Jetzt wird´s aber leider schwer :? ich brauche nun auch noch die Anzahl der eindeutigen IDs, die auf einen bestimmten Zeitraum bezogen "neu" sind. Beispiel:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
User A meldet sich am 01.09.2008 an
User B meldet sich am 02.09.2008 an
User A meldet sich am 03.10.2008 an
User C meldet sich am 04.10.2008 an
User A meldet sich am 05.10.2008 an

Anzahl(gesamt) = 3 (es gibt User A, B und C)

Anzahl(09.2008) = 2 (A+B)
Anazhl(10.2008) = 1 (nur C, A ist "alt")

Geht sowas mit einem SQL-Statement überhaupt? Wenn ja, wie? :?!?:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Di 14.10.08 21:28 
ausblenden SQL-Anweisung
1:
SELECT COUNTDISTINCT id ) FROM tabelle where DATUM > "09.10.2008"					

? Mit den " bin ich mir nicht ganz sicher, könnten auch ' sein

_________________
PROGRAMMER: A device for converting coffee into software.
Narses Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 14.10.08 21:32 
Moin!

Das wird leider nicht funktionieren, denn diese Anfrage wird 0 ergeben. :?

Vielleicht nochmal anders gesagt: Interessant ist, wieviele "neue" Benutzer in einem Monat vorhanden sind. :idea:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Di 14.10.08 21:36 
Ach, hab nicht gelesen, dass es hier um ganze Monate geht.

Neuer Versuch:
ausblenden SQL-Anweisung
1:
SELECT COUNTDISTINCT id ) FROM tabelle WHERE DATUM > "01.10.2008" and DATUM <= "31.10.2008"					


?
Hab grad keine Datenbank hier zum ausprobieren :(

_________________
PROGRAMMER: A device for converting coffee into software.
Narses Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 14.10.08 21:41 
Moin!

user profile iconXentar hat folgendes geschrieben Zum zitierten Posting springen:
Neuer Versuch:
ausblenden SQL-Anweisung
1:
SELECT COUNTDISTINCT id ) FROM tabelle WHERE DATUM > "01.10.2008" and DATUM <= "31.10.2008"					
Das habe ich natürlich auch in den Abfragen drin, sieht so aus: :)
ausblenden SQL-Anweisung
1:
SELECT COUNTDISTINCT id ) FROM tabelle WHERE year(datum)=2008 and month(datum)=10					
Allerdings, das liefert logischerweise eine 2 (nämlich A+C), da A aber schon im Monat davor da war, darf er nicht mitgezählt werden! :idea:

Jetzt klar? :zwinker:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
littleDave
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 111
Erhaltene Danke: 2

Win 7
Delphi 7 Prof, Turbo Delphi, VS 2008 Team System, VS 2010 Premium
BeitragVerfasst: Di 14.10.08 21:45 
Ich bin jetzt auch nicht so der SQL-Crack, jedoch würd ich ein paar Klammern im SQL setzen:
ausblenden SQL-Anweisung
1:
SELECT COUNTDISTINCT id ) FROM tabelle WHERE (year(datum)=2008) and (month(datum)=10)					
Narses Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 14.10.08 21:46 
Moin!

user profile iconlittleDave hat folgendes geschrieben Zum zitierten Posting springen:
Ich bin jetzt auch nicht so der SQL-Crack, jedoch würd ich ein paar Klammern im SQL setzen:
Das würde ich auch tun (und hab´s auch getan), ändert aber nix am Ergebnis, da das Konzept dieser Abfrage "falsch" ist. :nixweiss:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Di 14.10.08 21:47 
user profile iconNarses hat folgendes geschrieben Zum zitierten Posting springen:
Allerdings, das liefert logischerweise eine 2 (nämlich A+C), da A aber schon im Monat davor da war, darf er nicht mitgezählt werden! :idea:

Jetzt klar? :zwinker:

Ahh, ok, jetzt hab ich's, sorry :D

ausblenden SQL-Anweisung
1:
SELECT COUNTDISTINCT id ) FROM tabelle WHERE year(datum)=2008 and month(datum)=10 and (ID not in (SELECT DISTINCT id FROM tabelle WHERE Datum < "1.10.2008"))					

Sollte alle ausschließen, die im Zeitraum vorher schonmal da waren.

Edit: Not in? Oder war es <>any ? Eins von beiden sollte passen.

_________________
PROGRAMMER: A device for converting coffee into software.
Yogu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: Di 14.10.08 21:52 
user profile iconlittleDave hat folgendes geschrieben Zum zitierten Posting springen:
Ich bin jetzt auch nicht so der SQL-Crack, jedoch würd ich ein paar Klammern im SQL setzen:

Das kann gar nicht der Fehler sein, da MySQL noch gar nicht weiß, was user profile iconNarses will. Da muss noch irgendwie rein, dass der User nicht vorher vorgekommen ist. :idea:

(Edit: Oh, 2 Posts zu spät :oops:)

Ich hab noch nie mit DISTINCT gearbeitet, aber kannst du das nicht irgendwie mit der Datumsabfrage verknüpfen?

ausblenden SQL-Anweisung
1:
SELECT FROM tabelle WHERE year(datum)=2008 and month(datum)=10 and COUNTDISTINCT id )					

Das wird so sicherlich nicht funktionieren, aber ich will damit versuchen klar zu machen, was ich meine.

PS: Juhu, ich bin hatte Recht! user profile iconXentar hat die Query nur noch mal "überarbeitet". :mrgreen:


Zuletzt bearbeitet von Yogu am Di 14.10.08 21:59, insgesamt 1-mal bearbeitet
littleDave
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 111
Erhaltene Danke: 2

Win 7
Delphi 7 Prof, Turbo Delphi, VS 2008 Team System, VS 2010 Premium
BeitragVerfasst: Di 14.10.08 21:56 
Jetzt versteh ich das Problem erst :oops:. Hab oben nicht gesehen, dass es ja nur 3 Benutzer gibt. Bin davon ausgegangen, dass es 5 Benutzer gibt und das Datum die Registrierung sein soll. Doch jetzt ist mir alles klar.
Narses Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 14.10.08 22:18 
Moin!

user profile iconXentar hat folgendes geschrieben Zum zitierten Posting springen:
Sollte alle ausschließen, die im Zeitraum vorher schonmal da waren.
Jup, das tut´s. :) Danke. :zustimm:

(Wat´n blödes Brett vor´m Kopp, auf so ein einfaches Subquery nicht zu kommen :roll: woran merkt man, dass ich keine Ahnung von DBs habe... :lol:)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.