Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Datenbanken (https://www.delphipraxis.net/15-datenbanken/)
-   -   Delphi je ein Eintrag mit dem höchsten Datum (Group By ?) (https://www.delphipraxis.net/109352-je-ein-eintrag-mit-dem-hoechsten-datum-group.html)

hoika 28. Feb 2008 15:24

Datenbank: FB • Version: 1.5 • Zugriff über: egal

je ein Eintrag mit dem höchsten Datum (Group By ?)
 
Hallo #,

wieder diese vermaledeite Group By,
das kapier ich nie ;(

Folgende Tabellen habe ich:

WSProgress: WSId, TheDate, TheTime, TheValue (WS = Workstep = Arbeitsgang)
WorkStep: WSId, ProjectId
Project: ProjectId

in WSProgress stehen Fertigungsfortschritte drin, etwa so

WSId TheDate TheTime theValue
3 '2.1.2007' '12:30' 50
3 '3.1.2007' '15:30' 55
3 '4.1.2007' '17:22' 65
4 '2.1.2007' '11:30' 10
5 '2.1.2007' '11:30' 10
4 '2.2.2007' '11:22' 25


also pro Arbeitsgang mehrere.

Ich bräuchte jetzt eine Liste aller Arbeitsgänge eines Projektes,
und von denen

a) die Fortschritte bis zu einem bestimmten Datum
aber pro Arbeitsgang nur einen Fortschritt
b) die aktuellesten
aber pro Arbeitsgang nur einen Fortschritt


Ich habe das zur Zeit per Subselect für b
(a ist ja ableitbar)
SQL-Code:

select
  wsprogress1.wsid, wsprogress1.thedate, wsprogress1.thevalue
  workstep.wsid as ws_id, project.projectid as project_id
from wsprogress wsprogress1
join workstep on workstep.wsid=wsprogress1.wsid
join project project on project.projectid=workstep.projectid
where (project.id=110)
and (wsprogress1.thedate in
   (select max(wsprogress2.thedate)
    from wsprogress wsprogress2
    where wsprogress2.wsid=wsprogress1.wsid))
Das unnötige join auf die Projekte-Tabelle ist OK
(intern sind es noch ein paar Joins mehr).

Per GroupBy bekomme ich immer alle Arbeitsgänge und alle Fortschritte,
ich aber nur den mit dem höchsten Datum


Klappt es mit GroupBy überhaupt mit meinem Wunsch.


Danke


Heiko

mkinzler 28. Feb 2008 16:08

Re: je ein Eintrag mit dem höchsten Datum (Group By ?)
 
Bei einem Join bezieht sich die Gruppierung auf die gejointe Datenmenge.
Du müsstest das mit einer SP Lösen oder bei einem update auf FB2 mit einer DERIVED TABLE.

hoika 28. Feb 2008 16:15

Re: je ein Eintrag mit dem höchsten Datum (Group By ?)
 
Hallo,

aha, also bin ich nicht auf dem falschen Dampfer.

Danke


Heiko

omata 29. Feb 2008 00:44

Re: je ein Eintrag mit dem höchsten Datum (Group By ?)
 
Vielleicht so...

SQL-Code:
SELECT 'a' AS typ, wp.wsid, wp.thedate, wp.thevalue,
       s.wsid AS ws_id, p.projectid AS project_id
FROM wsprogress wp
INNER JOIN workstep s
  ON wp.wsid = s.wsid
INNER JOIN project p
  ON s.projectid = p.projectid
WHERE p.id = 110
  AND wp.thedate <= :bis_datum
  AND wp.thedate = (SELECT MAX(thedate)
                    FROM wsprogress
                    WHERE wsid = wp.wsid
                      AND thedate <= :bis_datum)

UNION

SELECT 'b' AS typ, wp.wsid, wp.thedate, wp.thevalue,
       s.wsid AS ws_id, p.projectid AS project_id
FROM wsprogress wp
INNER JOIN workstep s
  ON wp.wsid = s.wsid
INNER JOIN project p
  ON s.projectid = p.projectid
WHERE p.id = 110
  AND wp.thedate = (SELECT MAX(thedate)
                    FROM wsprogress
                    WHERE wsid = wp.wsid)
Gruss
Thorsten

hoika 29. Feb 2008 08:32

Re: je ein Eintrag mit dem höchsten Datum (Group By ?)
 
Hallo,

so etwa habe ich das dann auch geamcht,
wobei das erste

AND wp.thedate <= :bis_datum

sehr interessant ist, das schränkt ja die äussere Schleife
in der Query ein !! uiui

Ich hatte das bisher nur in der inneren (SubSelect) drin.

Meine prinzipielle Frage war aber,
ob es auch ohne SubSelect geht (und ohne Stored Procedure natürlich).

Oder anderes: Wie designe ich die Tabelle / App, dass ich kein Subselect brauche ?


Heiko

omata 29. Feb 2008 11:24

Re: je ein Eintrag mit dem höchsten Datum (Group By ?)
 
Ich würde das so lassen. Ohne SubSelect wird das nicht gehen.
Mit einer Prozedur würde ich bei sowas Einfachem sowieso nie arbeiten.
Ist die Abfrage mit SubSelect denn so langsam?
Deine Datenbankstruktur ist eigentlich soweit ok, wenn deine Performance besser sein muss dann müsstest du anfangen über z.B. Trigger eine zweite Struktur zufüllen. Diese zweite Struktur enthält dann nur noch deine Zusatzinformationen. Aber bedenke, dass dann jeder Schreibzugriff (INSERT, UPDATE, DELETE) langsamer wird. Das ist jetzt die Frage, was ist dir wichtiger ein schnelles Lesen oder Schreiben?
Wie schon gesagt, die Abfrage müsste so eigentlich schnell behandelbar sein und ich würde das so lassen.

Vielleicht bringt da ein Update von FB1.5 auf FB2.x auch Verbesserungen.

Gruss
Thorsten

hoika 29. Feb 2008 12:41

Re: je ein Eintrag mit dem höchsten Datum (Group By ?)
 
Hallo,

FB2 -> keine Vorteile

Ich wollte nur wissen, ob es ohne Subselect geht.

Die Abfrage ist schnell genug,
das Problem was ich habe,
es können pro Tag mehrere Einträge existieren
(es gibt noch eine Zeit-Feld TheTime).

in der where vor dem SubSelect kann man aber nicht schreiben TheDate+TheTime
"Expression not allowed"

;(


Heiko

omata 29. Feb 2008 19:03

Re: je ein Eintrag mit dem höchsten Datum (Group By ?)
 
Schau dir mal den Datentyp TIMESTAMP an.
Bei dem kann man dann auch Datum+Zeit in der WHERE-Klausel vergleichen ('29.02.2008 12:34:56').

Gruss
Thorsten

hoika 29. Feb 2008 20:09

Re: je ein Eintrag mit dem höchsten Datum (Group By ?)
 
Hallo,

schon klar,

ich kann die DB aber nicht ohne weiteres ändern.


Heiko

omata 29. Feb 2008 20:44

Re: je ein Eintrag mit dem höchsten Datum (Group By ?)
 
Ok, hier ein neuer Versuch...

SQL-Code:
SELECT 'a' AS typ, wp.wsid, wp.thedate, wp.thevalue,
       s.wsid AS ws_id, p.projectid AS project_id
FROM wsprogress wp
INNER JOIN workstep s
  ON wp.wsid = s.wsid
INNER JOIN project p
  ON s.projectid = p.projectid
WHERE p.id = 110
  AND wp.thedate <= :bis_datum
  AND              CAST(wp.thedate + wp.thetime AS TIMESTAMP)
      = (SELECT MAX(CAST(   thedate +    thetime AS TIMESTAMP))
         FROM wsprogress
         WHERE wsid = wp.wsid
           AND thedate <= :bis_datum)

UNION

SELECT 'b' AS typ, wp.wsid, wp.thedate, wp.thevalue,
       s.wsid AS ws_id, p.projectid AS project_id
FROM wsprogress wp
INNER JOIN workstep s
  ON wp.wsid = s.wsid
INNER JOIN project p
  ON s.projectid = p.projectid
WHERE p.id = 110
  AND              CAST(wp.thedate + wp.thetime AS TIMESTAMP)
      = (SELECT MAX(CAST(   thedate +    thetime AS TIMESTAMP))
         FROM wsprogress
         WHERE wsid = wp.wsid)
Gruss
Thorsten


Alle Zeitangaben in WEZ +1. Es ist jetzt 12:07 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz