SELECT TOP (100) PERCENT MsgCount.msgcount
, c.userid
, c.aspnet_guid
, c.fname
, c.lname
, c.profileimagepath
, c.profilealbumid
, p1.msgdate
, SUBSTRING(p1.msgcontent, 0, 50) AS msgcontent
, p1.msgtouser
FROM dbo.userprofile AS c
INNER JOIN dbo.usermessages AS p1
ON c.userid = p1.msgfromuser
LEFT OUTER JOIN dbo.usermessages AS p2
ON c.userid = p2.msgfromuser
AND (p1.msgdate < p2.msgdate
OR p1.msgdate = p2.msgdate
AND p1.msgid < p2.msgid)
LEFT OUTER JOIN
(
SELECT msgfromuser
, COUNT(msgfromuser) AS msgcount
FROM dbo.usermessages
GROUP BY msgfromuser
) AS MsgCount
ON MsgCount.msgfromuser = p1.msgfromuser
WHERE (p2.msgid IS NULL)
ORDER BY p2.msgdate DESC
質問する
46 次
2 に答える
0
多分次のように注文してみてください:
p1.msgdate
それ以外の:
p2.msgdate
これは単なる提案です:)
于 2012-05-10T23:22:13.450 に答える
0
あなたのテーブルでデータがどのように見えるかはわかりませんが、p2 への左外部結合があるため、値がなく、p2.msgdate が null であることが予想されると思います。p2.msgdate (null 列の可能性がある) だけで順序付けすると、常に値を持つ列で順序付けする場合とは異なり、一貫した結果が得られません。おそらく、p1.msgdate で並べ替えたり、coalesce(p2.msgdate,p1.msgdate) で並べ替えたりすると、より一貫した結果が得られる場合があります。
SELECT TOP (100) PERCENT MsgCount.msgcount
, c.userid
, c.aspnet_guid
, c.fname
, c.lname
, c.profileimagepath
, c.profilealbumid
, p1.msgdate
, SUBSTRING(p1.msgcontent, 0, 50) AS msgcontent
, p1.msgtouser
FROM dbo.userprofile AS c
INNER JOIN dbo.usermessages AS p1
ON c.userid = p1.msgfromuser
LEFT OUTER JOIN dbo.usermessages AS p2
ON c.userid = p2.msgfromuser
AND (p1.msgdate < p2.msgdate
OR p1.msgdate = p2.msgdate
AND p1.msgid < p2.msgid)
LEFT OUTER JOIN
(
SELECT msgfromuser
, COUNT(msgfromuser) AS msgcount
FROM dbo.usermessages
GROUP BY msgfromuser
) AS MsgCount
ON MsgCount.msgfromuser = p1.msgfromuser
WHERE (p2.msgid IS NULL)
ORDER BY p1.msgdate, p2.msgdate DESC
于 2012-05-11T02:49:31.670 に答える