Schrift
[thread]3723[/thread]

esskar in der SQL welt

Leser: 2


<< |< 1 2 3 >| >> 28 Einträge, 3 Seiten
esskar
 2006-09-26 10:47
#34707 #34707
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
ich wühle mich jetzt durch das SQL; ich hab zwar schon in der Vergangenheit damit gearbeitet, aber entweder hab ich DBIx::Class meine Queries für mich schreiben lassen, oder die Queries waren trivial genug. momentan sieht meine query schon so aus (keine panik, ich bau die schon nicht von Hand! :) )

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
SELECT
  contacts.id AS 'contacts_id',
  contacts.sex AS 'contacts_sex',
  contacts.givenname AS 'contacts_givenname',
  contacts.surname AS 'contacts_surname',
  contacts.company AS 'contacts_company',
  contacts.email AS 'contacts_email',
  contacts.phone AS 'contacts_phone',
  contacts.fax AS 'contacts_fax',
  contacts.ticket AS 'contacts_ticket',
  contacts.position_id AS 'contacts_position_id',
  contacts.numofemployees_id AS 'contacts_numofemployees_id',
  contacts.contacttype_id AS 'contacts_contacttype_id',
  conversation.id AS 'conversation_id',
  conversation.contact_id AS 'conversation_contact_id',
  conversation.agent_id AS 'conversation_agent_id',
  conversation.inquiry AS 'conversation_inquiry',
  conversation.inquiry_time AS 'conversation_inquiry_time',
  conversation.inquiry_notification_sent AS 'conversation_inquiry_notification_sent',
  conversation.answer AS 'conversation_answer',
  conversation.answer_time AS 'conversation_answer_time',
  conversation.answer_notification_sent AS 'conversation_answer_notification_sent'
FROM
  jm_contact_contacts contacts
LEFT JOIN
  jm_contact_conversation conversation
ON
  (contacts.id = conversation.contact_id)
ORDER BY
  conversation.inquiry_time DESC


soweit, so gut.
die felder contacts.position_id, contacts.numofemployees_id und contacts.contacttype_id sind primary keys von drei anderen tabellen.
Diese sind im Grunde so aufgebaut:
id | value

Die Tabellen heißen jm_contact_positions, jm_contact_numofemployees und jm_contact_contacttypes

ich würde jetzt gerne das obige SELECT so erweitern, dass ich jeweils zu position_id den richtigen Wert aus der jm_contact_positions bekomme, für numofemployees_id den richtigen Wert aus jm_contact_numofemployees, etcpp.

wie stell ich das denn an?\n\n

<!--EDIT|esskar|1159257579-->
nepos
 2006-09-26 11:11
#34708 #34708
User since
2005-08-17
1420 Artikel
BenutzerIn
[Homepage] [default_avatar]
Einfach mit Joins einbauen:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
SELECT
 contacts.id AS 'contacts_id',
 contacts.sex AS 'contacts_sex',
 contacts.givenname AS 'contacts_givenname',
 contacts.surname AS 'contacts_surname',
 contacts.company AS 'contacts_company',
 contacts.email AS 'contacts_email',
 contacts.phone AS 'contacts_phone',
 contacts.fax AS 'contacts_fax',
 contacts.ticket AS 'contacts_ticket',
 contacts.position_id AS 'contacts_position_id',
 contacts.numofemployees_id AS 'contacts_numofemployees_id',
 contacts.contacttype_id AS 'contacts_contacttype_id',
 conversation.id AS 'conversation_id',
 conversation.contact_id AS 'conversation_contact_id',
 conversation.agent_id AS 'conversation_agent_id',
 conversation.inquiry AS 'conversation_inquiry',
 conversation.inquiry_time AS 'conversation_inquiry_time',
 conversation.inquiry_notification_sent AS 'conversation_inquiry_notification_sent',
 conversation.answer AS 'conversation_answer',
 conversation.answer_time AS 'conversation_answer_time',
 conversation.answer_notification_sent AS 'conversation_answer_notification_sent',
 jm_contact_positions.value,
 jm_contact_numofemployees.value,
 jm_contact_contacttypes.value
FROM
 jm_contact_contacts contacts
LEFT JOIN
 jm_contact_conversation conversation
ON
 (contacts.id = conversation.contact_id)
JOIN jm_contact_positions ON (contacts.position_id = jm_contact_positions.id)
JOIN jm_contact_numofemployees ON (contacts.numofemployees_id = jm_contact_numofemployees.id)
JOIN jm_contact_contacttypes ON (jm_contact_contacttypes.id = contacts.contacttype_id)
ORDER BY
 conversation.inquiry_time DESC


Oder hab ich dich nun falsch verstanden?\n\n

<!--EDIT|esskar|1159257608-->
esskar
 2006-09-26 11:52
#34709 #34709
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
okay; danke. das hat erstmal geholfen ... hätt ich auch drauf kommen können.
esskar
 2006-09-26 11:58
#34710 #34710
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
so, hier kommt schon meine nächste frage.

die ganze sache ist jetzt so auf gebaut.
ich hab einen contact, mit der id 1

dieser contact stellt mir eine anfrage, diese wird beantwortet, daraufhin wird wieder von dem contact eine anfrage gestellt.

ich habe also nun einen eintrag in der tabelle jm_contact_contacts und 2 einträge in der tabelle jm_contact_conversation unter der contact_id 1.

eine conversation (also eine anfrage + eine antwort) hat eine inquiry_time (die Zeit zu der die Anfrage gestellt wurde). ich hätte jetzt gerne in meiner SQL anfrage jeweils die älteste und die jüngste inquiry_time unter besagter conatct_id ermittelt.

geht das irgendwie? wenn ja, wie? danke.
renee
 2006-09-26 12:06
#34711 #34711
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Wie ist die inquiry_time dargestellt? Als Integer-Zeitstempel?

Dann kannst Du mit
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
SELECT
contacts.id AS 'contacts_id',
contacts.sex AS 'contacts_sex',
contacts.givenname AS 'contacts_givenname',
contacts.surname AS 'contacts_surname',
contacts.company AS 'contacts_company',
contacts.email AS 'contacts_email',
contacts.phone AS 'contacts_phone',
contacts.fax AS 'contacts_fax',
contacts.ticket AS 'contacts_ticket',
contacts.position_id AS 'contacts_position_id',
contacts.numofemployees_id AS 'contacts_numofemployees_id',
contacts.contacttype_id AS 'contacts_contacttype_id',
conversation.id AS 'conversation_id',
conversation.contact_id AS 'conversation_contact_id',
conversation.agent_id AS 'conversation_agent_id',
conversation.inquiry AS 'conversation_inquiry',
conversation.inquiry_time AS 'conversation_inquiry_time',
conversation.inquiry_notification_sent AS 'conversation_inquiry_notification_sent',
conversation.answer AS 'conversation_answer',
conversation.answer_time AS 'conversation_answer_time',
conversation.answer_notification_sent AS 'conversation_answer_notification_sent',
jm_contact_positions.value,
jm_contact_numofemployees.value,
jm_contact_contacttypes.value,
MIN(jm_contact_conversation.inquiry_time),
MAX(jm_contact_conversation.inquiry_time),
FROM
jm_contact_contacts contacts
LEFT JOIN
jm_contact_conversation conversation
ON
(contacts.id = conversation.contact_id)
JOIN jm_contact_positions ON (contacts.position_id = jm_contact_positions.id)
JOIN jm_contact_numofemployees ON (contacts.numofemployees_id = jm_contact_numofemployees.id)
JOIN jm_contact_contacttypes ON (jm_contact_contacttypes.id = contacts.contacttype_id)
GROUP BY
jm_contact_contacts.contact_id
ORDER BY
conversation.inquiry_time DESC


Jedenfalls so ungefähr... Mit MIN() und MAX() sollte es funktionieren...
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
esskar
 2006-09-26 12:15
#34712 #34712
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
[quote=renee,26.09.2006, 10:06]Wie ist die inquiry_time dargestellt? Als Integer-Zeitstempel?[/quote]
eine DATETIME feld.
mal sehen, wie ob und wie das klappt!
esskar
 2006-09-28 12:41
#34713 #34713
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
okay, noch eine Frage - keine Ahnung, ob ich das schon von SQL server lösen lassen kann.

ich hab folgende tabelle
|---------------------------------------------------|
| Start               | End                 | Count |
|---------------------------------------------------|
| 2006-09-19 00:00:00 | 2006-09-19 01:00:00 |     1 |
| 2006-09-19 00:00:00 | 2006-09-19 02:00:00 |     8 |
| 2006-09-19 00:00:00 | 2006-09-19 03:00:00 |    19 |
| 2006-09-19 00:00:00 | 2006-09-19 04:00:00 |    49 |
| 2006-09-19 00:00:00 | 2006-09-19 05:00:00 |   110 |
| 2006-09-19 00:00:00 | 2006-09-19 06:00:00 |   212 |
|---------------------------------------------------|


so, count bezieht sich also immer auf den ganzen zeitraum.
ich hätt jetzt gern die differenz zum vorgänger.

also sowas:

|-----------------------------------------------------------|
| Start               | End                 | Count | Delta |
|-----------------------------------------------------------|
| 2006-09-19 00:00:00 | 2006-09-19 01:00:00 |     1 |     0 |
| 2006-09-19 00:00:00 | 2006-09-19 02:00:00 |     8 |     7 |
| 2006-09-19 00:00:00 | 2006-09-19 03:00:00 |    19 |    11 |
| 2006-09-19 00:00:00 | 2006-09-19 04:00:00 |    49 |    30 |
| 2006-09-19 00:00:00 | 2006-09-19 05:00:00 |   110 |    61 |
| 2006-09-19 00:00:00 | 2006-09-19 06:00:00 |   212 |   112 |
|-----------------------------------------------------------|


natürlich will ich das nicht in er tabelle speichern, sondern nur mit meiner query berechnen lassen!\n\n

<!--EDIT|esskar|1159433322-->
renee
 2006-09-28 13:34
#34714 #34714
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Code: (dl )
SELECT tb2.count - tb1.count FROM tabelle as tb1, tabelle as tb2 WHERE tb1.end = tb2.start;


count ist ein reserviertes Wort (da Funktionsname)
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
esskar
 2006-10-20 15:44
#34715 #34715
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
was ist denn der genaue unterschied zwischen folgenden SELCTS unter mySQL.

[sql]SELECT * from candidate, photos WHERE candidate.id = photos.candidate_id AND candidate.id = 7;
[/sql]

[sql]SELECT * from candidate INNER JOIN photos ON candidate.id = photos.candidate_id WHERE candidate.id = 7;
[/sql]

[sql]SELECT * from candidate LEFT JOIN photos ON candidate.id = photos.candidate_id WHERE candidate.id = 7;
[/sql]

[sql]SELECT * from candidate RIGHT JOIN photos ON candidate.id = photos.candidate_id WHERE candidate.id = 7;
[/sql]

irgendwie raff ich es noch nicht.
momentan bekomm ich nämlich immer das selbe ergebnis!
renee
 2006-10-20 16:20
#34716 #34716
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Es kommt auf die Daten an, ob Du das selbe Ergebnis bekommst...

Dein erstes Statement ist ein Kreuzprodukt beider Tabellen, wobei Du Dir nur die Einträge anzeigen lässt, die in beiden Tabellen die gleiche ID haben und die ID 7.

Bei den anderen drei Statements ist es so, dass nicht erst der Aufwand "Kreuzprodukts bilden und dann selektieren" gemacht wird, sondern es wird "intelligent" gebildet...

Ob INNER, RIGHT oder LEFT hängt mit evtl. NULL-Werten ab. Aber da bin ich nicht mehr so ganz firm drin...
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
<< |< 1 2 3 >| >> 28 Einträge, 3 Seiten



View all threads created 2006-09-26 10:47.