2012-04-03 3 views
0

Ich habe eine BLOB-Spalte, die ich gleichzeitig mit einer anderen Spalte auswählen möchte, und eine COUNT der Anzahl der zugeordneten Zeilen in einer anderen Tabelle. Hier ist, was ich habe:eindeutige Zeilen mit COUNT und einer BLOB-Spalte - DB2

SELECT locations.id, locations.name, photo, 
    COUNT(items.id) OVER (PARTITION BY locations.id) AS num_items 
FROM locations 
LEFT OUTER JOIN items ON locations.id = items.location_id 
ORDER BY locations.name ASC 

photo ist die BLOB-Spalte. Diese Abfrage wird alle Daten, die ich will, aber ich sehe x Zeilen für jeden location, wo x ist die Anzahl der item Reihen im Zusammenhang mit diesen location.

Also ich bin immer:

id   name   photo   num_items 
1   location1     3 
1   location1     3 
1   location1     3 
2   location2     1 
3   location3     2 
3   location3     2 
4   location4     0 

Was ich will:

id   name   photo   num_items 
1   location1     3 
2   location2     1 
3   location3     2 
4   location4     0 

Das Problem ist, dass Sie nicht DISTINCT tun können, GROUP BY oder MIN auf einer BLOB Spalte.

Antwort

1

Ich finde immer die Antwort direkt nach dem Posten auf hier. : P

SELECT location_data.*, photo 
FROM (
    SELECT locations.id, locations.name, COUNT(DISTINCT items.id) AS num_items 
    FROM locations 
    LEFT OUTER JOIN items ON locations.id = items.location_id 
    GROUP BY locations.id, locations.name 
) location_data 
INNER JOIN (
    SELECT locations.id, photo 
    FROM locations 
) photo_data ON location_data.id = photo_data.id 
ORDER BY name ASC 
Verwandte Themen