我有一种情况,我从几个表MEMBERS,SS和SSUSED中撤回数据。如果“会员”是包含会员数据的表格,则“SS”是包含会员在其帐户上购买的产品的表格,而SSUSED是存储产品使用情况的表格。产品可以有1个,6个或12个用途。因此可以存在一对一,一到六或一到十二的匹配比率。当我运行我的查询时,我带回了所有的用法。我只需要带回最新版本(SSUSED.usedate)。
我也试图过滤掉某些产品和产品类别,但这似乎并不完全正常。我在这里和那里得到一个随机产品,我似乎无法隔离这个问题。我想这可能是因为我的逻辑不是以干净有效的方式编写的。
CODE:
SELECT DISTINCT
MEMBERS.scancode, MEMBERS.lname, MEMBERS.fname, MEMBERS.mtypeid, MEMBERS.status, MEMBERS.lastvisit, MEMBERS.datejoin,
PRODUCTS.description, EMPLOYEES.lname AS EmpLname, EMPLOYEES.fname AS EmpFName, SSUSED.transactiontype, SS.initialdate, SS.initialquantity,
SS.usedquantity, MEMBERTYPES.mtype, SS.ssid, SSUSED.usedate
FROM PRODUCTCATS INNER JOIN
PRODUCTS ON PRODUCTCATS.productcatid = PRODUCTS.productcatid INNER JOIN
SS ON PRODUCTS.productid = SS.productid INNER JOIN
MEMBERS ON SS.memid = MEMBERS.memid LEFT OUTER JOIN
SSUSED ON SS.ssid = SSUSED.ssid INNER JOIN
EMPLOYEES ON SSUSED.employeeid = EMPLOYEES.employeeid INNER JOIN
MEMBERTYPES ON MEMBERS.mtypeid = MEMBERTYPES.mtypeid
WHERE (SS.memid IN
(SELECT DISTINCT SS_1.memid
FROM SS AS SS_1 INNER JOIN
SSUSED AS SSUSED_1 ON SS_1.ssid = SSUSED_1.ssid INNER JOIN
MEMBERS AS MEMBERS_1 ON SS_1.memid = MEMBERS_1.memid
WHERE (MEMBERS_1.siteid = @rvSite) AND (SS_1.productid = '324' OR
SS_1.productid = '326') AND (SSUSED_1.usedate BETWEEN @rvStartDate AND @rvEndDate) AND (SSUSED_1.transactiontype = 'Use'))) AND
(PRODUCTS.productcatid = '27' OR
PRODUCTS.productcatid = '28' OR
PRODUCTS.productcatid = '29' OR
PRODUCTS.productcatid = '58' OR
PRODUCTS.productcatid = '77' OR
PRODUCTS.productcatid = '75' OR
PRODUCTS.productcatid = '30' OR
PRODUCTS.productcatid = '61' OR
PRODUCTS.productcatid = '31' OR
PRODUCTS.productcatid = '32' OR
PRODUCTS.productcatid = '47' OR
PRODUCTS.productcatid = '68') AND (SS.productid <> '32' OR
SS.productid <> '335' OR
SS.productid <> '350' OR
SS.productid <> '618' OR
SS.productid <> '1312' OR
SS.productid <> '1646' OR
SS.productid <> '54987' OR
SS.productid <> '55937' OR
SS.productid <> '58289' OR
SS.productid <> '58876' OR
SS.productid <> '601691') AND (SSUSED.transactiontype = 'Use' OR
SSUSED.transactiontype = 'Expired') AND (SSUSED.usedate >= @rvStartDate)
答案 0 :(得分:0)
这并没有真正回答这个问题,但展示了这个查询对于某些格式化,别名以及将这些巨大的列表转换为IN谓词的清晰程度。
SELECT DISTINCT m.scancode
, m.lname
, m.fname
, m.mtypeid
, m.STATUS
, m.lastvisit
, m.datejoin
, p.description
, e.lname AS EmpLname
, e.fname AS EmpFName
, su.transactiontype
, SS.initialdate
, SS.initialquantity
, SS.usedquantity
, mt.mtype
, SS.ssid
, su.usedate
FROM PRODUCTCATS pc
INNER JOIN PRODUCTS p ON pc.productcatid = p.productcatid
INNER JOIN SS ON p.productid = SS.productid
INNER JOIN MEMBERS m ON SS.memid = m.memid
LEFT JOIN SSUSED su ON SS.ssid = su.ssid
INNER JOIN EMPLOYEES e ON su.employeeid = e.employeeid
INNER JOIN MEMBERTYPES mt ON m.mtypeid = mt.mtypeid
WHERE SS.memid IN
(
SELECT DISTINCT SS_1.memid
FROM SS AS SS_1
INNER JOIN SSUSED AS SSUSED_1 ON SS_1.ssid = SSUSED_1.ssid
INNER JOIN MEMBERS AS MEMBERS_1 ON SS_1.memid = MEMBERS_1.memid
WHERE MEMBERS_1.siteid = @rvSite
AND SS_1.productid in ('324', '326')
AND SSUSED_1.usedate BETWEEN @rvStartDate AND @rvEndDate
AND SSUSED_1.transactiontype = 'Use'
)
AND p.productcatid in('27', '28', '29', '58', '77', '75', '30', '61', '31', '32', '47', '68')
AND SS.productid in ('32', '335', '350', '618', '1312', '1646', '54987', '55937', '58289', '58876', '601691')
AND su.transactiontype in ('Use', 'Expired'
AND su.usedate >= @rvStartDate
为了能够帮助解决这个问题,我们需要了解这应该做什么以及什么不起作用。