我尝试修改显示共享ID的重复行的现有查询,同时具有不同的辅助ID。
SELECT IM.*, C.Client, U.Name, R.FrequencyUnit, R.FrequencyDuration, R.RIID
FROM itemsMisc IM
LEFT JOIN Clients C on C.ClientID = IM.ClientID
LEFT JOIN Users U on U.UserID = IM.UserID
LEFT JOIN Recurringitem R ON (R.ClientID = C.ClientID AND IM.Rate = R.Rate AND IM.Title = R.Title)
WHERE (IM.CID = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#CID#">)
AND (IM.Invoiced = 0)
ORDER BY IM.DateCompleted ASC;
发生的事情是,如果同一个用户制作了2个相似的重复项,则重复的行显示的是相同的IMID(来自itemsMisc),但是来自Recurringitems的RIID不同。
我在IMID上尝试了GROUP BY,发现我必须在GROUP BY中包含所有选择项,这让我无处可去。
SELECT IM.IMID, IM.UserID, IM.ClientID, IM.Rate, IM.Title, IM.CID, IM.QTY, IM.DateCompleted, C.Client, U.Name, R.FrequencyUnit, R.FrequencyDuration, R.RBID
FROM ItemMisc IM
LEFT JOIN Clients C on C.ClientID = IM.ClientID
LEFT JOIN Users U on U.UserID = IM.UserID
LEFT JOIN Recurringitem R ON (R.ClientID = C.ClientID AND IM.Rate = R.Rate AND IM.Title = R.Title)
WHERE (IM.CID = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#CID#">)
AND (IM.Invoiced = 0)
GROUP BY IM.IMID, IM.UserID, IM.ClientID, IM.Rate, IM.Title, IM.CID, IM.QTY, IM.DateCompleted, C.Client, U.Name, R.FrequencyUnit, R.FrequencyDuration, R.RBID
ORDER BY IM.DateCompleted ASC;
如果我能说清楚的话,请告诉我。
提前致谢
答案 0 :(得分:1)
你有一些选择
R.RBID
和select
条款中删除group by
如果它是你想要的最后一个,你可以使用row_number来获取一个且每个只有一个RBID(速率,标题,频率和频率)
WITH cte
AS (SELECT IM.imid, IM.userid, IM.clientid, IM.rate,
IM.title, IM.cid, IM.qty, IM.datecompleted,
C.client, U.name, R.frequencyunit, R.frequencyduration,
R.rbid,
Row_number()
OVER (
partition BY R.rate, R.title, R.frequencyunit, R.frequencyduration
ORDER BY R.rbid ) rn
FROM itemmisc IM
LEFT JOIN clients C
ON C.clientid = IM.clientid
LEFT JOIN users U
ON U.userid = IM.userid
LEFT JOIN recurringitem R
ON ( R.clientid = C.clientid
AND IM.rate = R.rate
AND IM.title = R.title )
WHERE (IM.CID = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#CID#">)
AND (IM.Invoiced = 0)
GROUP BY IM.imid, IM.userid, IM.clientid, IM.rate,
IM.title, IM.cid, IM.qty, IM.datecompleted,
C.client, U.name, R.frequencyunit, R.frequencyduration,
R.rbid)
SELECT *
FROM cte
WHERE rn = 1
ORDER BY im.datecompleted ASC
注意:您可以通过修改partition BY