Union Abfrage
Gülümser Köroglu <[email protected]>
| Newsgroups | gmane.comp.db.postgresql.german |
|---|---|
| Message-ID | <[email protected]> |
Hallo nochmals, ich habe folgendes Problem und bitte euch mir weiterzuhelfen. Ich habe 3 Tabellen aus denen ich mit 3 Abfragen (verknüpft mit Union) eine Abfrage erstellt habe. Wichtig ist hierbei, dass ich 50 rows ziehe, diese aber sortiert nach Datum sein müssen. Folgende Abfrage: (SELECT me.EventName as "EventName" , CASE WHEN meh.isStarted = true THEN 'Started' else 'Stopped' END as "Action" , meh.ActionTime, '' as "Related Device", '' as "Details" FROM ManagedEvent me, ManagedEventActionHistory meh WHERE me.Event_id = meh.Event_id AND me.EventName ~* '' AND meh.ActionTime >= '18.07.2012 21:00:00.000' AND meh.ActionTime < '27.07.2012 09:04:00.000' ORDER BY meh.ActionTime DESC LIMIT 50 OFFSET 0) UNION ( SELECT EventName , CASE WHEN Action = 0 THEN 'Started' When Action = 1 THEN 'Stopped' else 'Transferred' END as Action ,ActionTime , RelatedDevice as "Related Device" ,'' as "Details" FROM EventActionHistory WHERE EventName ~* '' AND ActionTime >= '18.07.2012 21:00:00.000'AND ActionTime < '27.07.2012 09:04:00.000' ORDER BY ActionTime DESC LIMIT 50 OFFSET 0 ) UNION ( SELECT m.eventname, CASE WHEN Action = 0 THEN 'Disabled' END as Action, h.ActionTime, '' as "Related Device" , h.User_id || ' for ' || h.Timespan ||' seconds' as "Details" FROM UserManagedEventActions h, ManagedEvent m where h.ActionTime >= '18.07.2012 00:00:00' AND h.ActionTime < '27.07.2012 00:00:00' AND (h.User_id IN (SELECT User_id FROM Users WHERE User_id ~* '' AND IsDeleted = false)) AND m.event_id = h.managedevent_id ORDER BY h.ActionTime DESC LIMIT 50 OFFSET 0 ) ORDER BY ActionTime DESC LIMIT 50 Ausgabe: 1."test";"Started";"2012-07-23 06:53:37.412";"test";"" 2."test";"Started";"2012-07-23 06:35:37.483";"test";"" 3."test";"Started";"2012-07-23 06:22:29.248";"test";"" 4."test2";"Started";"2012-07-19 09:05:10.195";"";"" 5."test2";"Disabled";"2012-07-19 09:05:04.993";"";"hanife for 5 seconds" 6."test2";"Stopped";"2012-07-19 09:05:03.889";"";"" Diese Abfrage liefert mir eine falsche Ausgabe. Der erste Select wird 'abgebrochen' und Daten von den anderen Tabellen angehaengt. (obwohl zeitlich gesehen die rows aus der ersten Tabelle erscheinen sollten) Zwischen der 3. und 4. sollten noch Rows von der (hier im Beispiel) ersten Tabelle erscheinen. Schlechter workaround waere, wenn ich die inneren limits höher setze. Aber das ist riskant und falsch, da immer noch falsche Ausgaben geliefert werden. Die internen Select-Abfragen muss ich ebenfalls limitieren (nach meinem SQL-Wissensstand :)), da von Mio von Rows die Daten gezogen werden und somit die Abfrage lange dauert. Gülümser Köroğlu Junior Software Engineer ________________________________ ________________________________ nanodems Ltd. Because Integration Matters ________________________________ ________________________________ Gazi Teknopark Golbasi 06830 Ankara / Turkey Tel:+90 312 485 06 78Fax: +90 312 485 06 78 ________________________________ This e-mail and the files attached to it (if any) have been sent by the senderunder his/her own individual discretion; they can not be copied, disclosed or sold for any purpose. If you are not the intended recipient (or have received this e-mail in error)please notify the sender immediately and destroy this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden. Nanodems ltd. sti. accepts no responsibility on the accuracy, integrity and currency of the information transmitted with this message.