Jump to content
php.lv forumi

SELECT join SUM


reiniger

Recommended Posts

Sākums šeit

 

Jauna problēma ar datu atkārtošanos

 

Piemērs

tabula1  // noliktavas tabula
t1ID | val | title
 1  |  1  | nol1
 2  |  1  | nol2
 3  |  1  | nol3
 4  |  2  | nol4

tabula2  // burkānu kastes konkreta noliktava
t2ID | t1ID | burk
 1  |  1   |  6
 2  |  1   |  5
 3  |  2   |  2
 4  |  2   |  2
 5  |  3   |  3
 6  |  4   |  9

tabula3  // izvestais burkanu daudzums no kastes
t3ID | t2ID | burk
 1  |  1   |  4
 2  |  2   |  2
 3  |  3   |  2
 4  |  4   |  1

tabula4  // pārkrāmētās kastes
t4ID | t2ID | burk
 1  |  1   |  1
 2  |  2   |  1

MYSQL

SELECT t2.t2ID, t1.title, t2.burk-COALESCE(SUM(t3.burk),0)-COALESCE(SUM(t4.burk),0) AS atlburk
FROM tabula1 AS t1 
LEFT JOIN tabula2 AS t2 ON (t2.t1ID=t1.t1ID) 
LEFT JOIN tabula3 AS t3 ON (t3.t2ID=t2.t2ID)  
LEFT JOIN tabula4 AS t4 ON (t4.t2ID=t2.t2ID)  
WHERE t1.val = 1
GROUP by t2.t2ID 
HAVING atlburk != 0

Rezultāts

t2id |  title| atlburk
 1  |  nol1 | 1
 2  |  nol1 | 2

 

Bet ja manā tabula4 ir divreiz ieraksts ar atiecīgo t2ID at no šī tabulas tiek noņemts 2x vairāk, tas ir, ja vajadzetu noņemt 2 un 2 summa 4, bet tiek noņemts 2,2,2 un 2 summa 8.

 

Paskatijos lai query izdruka visus datus ar * redzeju ka vienkārši tas sasumme ierakstus tadeļ citas tabulas laikam liek atkārtot ierakstus, kur piemeram 2 vieta tiek izvaditi 4 ieraksti.

 

Ceru ka sapratat manu problēmu! Ka varētu izdarīt lai tikai attēloti tiktu tikai tik cik ir tabula4 ieraksti? Ar kaut kādu atsevišķu SELECT?

Link to comment
Share on other sites

Ir 2 iespējas - vai nu uzlikt unikālo ierobežojumu tabulā4, lai katram unikālajam t2ID pārkrāmētās kastes tiktu fiksētas tikai vienā ierakstā, t.i. šai ierakstā ir pārkrāmēto kastu kopsumma. Otra iespēja ir vaicājuma laikā šādu lietu iegūt ar apakšvaicājumu grupējot tabulas4 ierakstus pēc t2ID. Atceramies, ka savienojumus veidojot ieejā var būt ne tikai tabula, bet jebkas, kas ģenerē ierakstus, tai skaitā apakšvaicājums:

mysql> insert into tabula4 values (3, 1, 1);
Query OK, 1 row affected (0.06 sec)
mysql> SELECT t2.t2ID, t1.title, t2.burk-COALESCE(SUM(t3.burk),0)-COALESCE(SUM(t
4.burk),0) AS atlburk, t2.burk, sum(t3.burk), sum(t4.burk)
   -> FROM tabula1 AS t1
   -> LEFT JOIN tabula2 AS t2 ON (t2.t1ID=t1.t1ID)
   -> LEFT JOIN tabula3 AS t3 ON (t3.t2ID=t2.t2ID)
   -> LEFT JOIN (
   ->   SELECT t2ID, SUM(burk) AS burk
   ->   FROM tabula4
   ->   GROUP BY t2ID)
   -> AS t4 ON (t4.t2ID = t2.t2ID)
   -> GROUP by t2.t2ID, t1.title
   -> ;
+------+-------+---------+------+--------------+--------------+
| t2ID | title | atlburk | burk | sum(t3.burk) | sum(t4.burk) |
+------+-------+---------+------+--------------+--------------+
|    1 | nol1  |       0 |    6 |            4 |            2 |
|    2 | nol1  |       2 |    5 |            2 |            1 |
|    3 | nol2  |       0 |    2 |            2 |         NULL |
|    4 | nol2  |       1 |    2 |            1 |         NULL |
|    5 | nol3  |       3 |    3 |         NULL |         NULL |
|    6 | nol4  |       9 |    9 |         NULL |         NULL |
+------+-------+---------+------+--------------+--------------+
6 rows in set (0.00 sec)

 

Gints Plivna

http://datubazes.wordpress.com

Link to comment
Share on other sites

Paldies, palīdzēja.

MYSQL

SELECT t2.t2ID, t2.burk-COALESCE(SUM(t3.burk),0)-COALESCE(t4.burk,0) AS atlburk
FROM tabula1 AS t1
LEFT JOIN tabula2 AS t2 ON (t2.t1ID=t1.t1ID)
LEFT JOIN tabula3 AS t3 ON (t3.t2ID=t2.t2ID)
LEFT JOIN (
  SELECT t2ID, SUM(burk) AS burk
  FROM tabula4
  GROUP BY t2ID)
AS t4 ON (t4.t2ID = t2.t2ID)
GROUP by t2.t2ID

Noņemot SUM no COALESCE(t4.burk,0) viss aizgāja.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...