2014-02-21 69 views
0

我想顯示預覽頁面,每個圖片組的1張圖片的最低位置和Online = Y add。我想在網上得到總圖片組(如果可能)用WHERE和GROUP BY獲取最低值的全行

我有一個圖片庫如下表:

PicID(autoinc) | PicGrp(INT) | Online(Y|N) | Pos(0-10,unique within Grp) | ... | Description 

1 | 1 | Y | 0 
2 | 1 | Y | 1 
3 | 2 | N | 1 
4 | 2 | N | 3 
5 | 2 | Y | 7 
6 | 2 | Y | 2 
7 | 2 | Y | 10 

所以我想有:

1 | 1 | Y | 0 | ... | Description | Total(2) 
6 | 2 | Y | 2 | ... | Description | Total(3) 

一個Direction..not工作,當然:-)

SELECT PictureID, COUNT(*) AS Total 
FROM Pictures 
WHERE MIN(Position) AND Online = 'Y' 
GROUP BY PictureGroup 

回答

0

很肯定這是你想要什麼:

select y.picid, y.picgrp, y.online, y.pos, count(z.picid) as total 
    from pictures y 
    join pictures z 
    on y.picgrp = z.picgrp 
    and y.online = z.online 
where y.pos = (select min(x.pos) 
        from pictures x 
       where x.picgrp = y.picgrp 
        and x.online = 'Y') 
    and y.online = 'Y' 
group by y.picid, y.picgrp, y.online, y.pos 
+0

編輯包括計數。 –