2

私は同じ問題を抱えている多くの人々を探していましたが、解決策ははるかに異なっているようです?!

私は3つのテーブルを持っています:

+-------------------------------------------+
| ITEM: itemid(pk), status(fk), owner(fk)   |
|                                           |
| STATUS: statusid(pk), statusname          |
|                                           |
| OWNER: ownerid(pk), ownername             |
+-------------------------------------------+

私には2つのステータスがあります:1:利用可能2:壊れています。

この場合、壊れたアイテムが1つある所有者は1人だけです。私はこのコードを使用しています。最初は違いましたが、LEFTJOINとGROUP/ORDERBYでうまくいくようです。残念ながら私にとっては。

SELECT ownername, SUM(price) AS  'totalbrokenpriceeach', COUNT( itemid ) AS  'totalbrokenitemseach'
                                FROM item
                                LEFT JOIN owner ON item.owner = owner.ownerid
                                LEFT JOIN status on item.status = status.statusid
                                WHERE statusid='2'
                                GROUP BY ownerid

これは次を返します:

ownername   totalbrokenpriceeach    totalbrokenitemseach
Owner #1            60                          1
Owner #5            180                         4

私は戻ってきたいです:

ownername   totalbrokenpriceeach    totalbrokenitemseach
Owner #1            60                          1
Owner #2            0                           0
Owner #3            0                           0
Owner #4            0                           0
Owner #5            180                         4

何をすべきか?誰か解決策はありますか?



編集:

         OWNER                          
+-----------------------+
| ownerid ownername     |
|     1:    Henk        |
|     2:    Jan         |
|     3:    Piet        |
|     4:    Klaas       |
+-----------------------+

         STATUS
+-----------------------+
| statusid statusbeschr |
|     1:    Available   |
|     2:    Broken      |
+-----------------------+

         ITEM
+--------------------------------------+
| itemid    price     owner     status |
|     1:    90          1          1   |
|     2:    40          2          1   |
|     3:    20          2          1   |
|     4:    120         3          2   |
+--------------------------------------+

I need returned:

+-------------------------------------------------------+
| ownername    SumOfBrokenItems   AmountOfBrokenItems   |
|     Henk:       0                       0             |
|     Jan:        0                       0             |
|     Piet:       120                     1             |
|     Klaas:      0                       0             |
+-------------------------------------------------------+
4

1 に答える 1

3

SELECT  a.ownername, 
        SUM(price) AS  totalbrokenpriceeach, 
        COUNT(b.itemid) AS  totalbrokenitemseach
FROM    owner a
        LEFT JOIN   item b
            ON a.ownerID = b.owner
        LEFT JOIN   status c
            ON b.status = c.statusID AND 
               c.StatusID = 2
GROUP BY    ownername

更新1

SELECT  a.ownername, 
        SUM(CASE WHEN c.statusbeschr = 'Broken' THEN b.price ELSE 0 END) AS  totalbrokenpriceeach, 
        SUM(CASE WHEN c.statusbeschr = 'Broken' THEN 1 ELSE 0 END) AS  totalbrokenitemseach
FROM    owner a
        LEFT JOIN   item b
            ON a.ownerID = b.owner
        LEFT JOIN   status c
            ON b.status = c.statusID        
GROUP BY a.ownerid
ORDER BY a.ownerid
于 2012-11-07T15:00:05.263 に答える