在写sql 的时候,我们有时候,只需要拿出某些特定的数据,去跟一些表进行关联,但是如果直接通过表关联的话,可能拿到
的数据有冗余,这个时候,我们就可以巧用临时表,讲自己需要的特定数据筛选出来,然后组成临时表,在用系统表去关联它,这样
可能就能得到我们想要的数据,这样操作,有意想不到的效果。
SELECT DISTINCT A.[TYPE],
SUM(A.GROSS_WEIGHT) AS GROSS_WEIGHT ,
SUM(A.VOLUME) AS VOLUME ,
IC.IN_CUSTOMS_DECLARTION_NO
FROM WMS_ASN AS A
INNER JOIN (
SELECT DISTINCT ASN_ID,IN_CUSTOMS_DECLARTION_NO
FROM WMS_CUSTOMS_IN
GROUP BY ASN_ID,IN_CUSTOMS_DECLARTION_NO
) AS IC
ON A.KID=IC.ASN_ID
WHERE A.MDF_TIME BETWEEN dateadd(year,-2,getdate()) and dateadd(year,-1,getdate())
GROUP BY A.[TYPE],IN_CUSTOMS_DECLARTION_NO
如上述sql,通过SELECT DISTINCT ASN_ID,IN_CUSTOMS_DECLARTION_NO
FROM WMS_CUSTOMS_IN
GROUP BY ASN_ID,IN_CUSTOMS_DECLARTION_NO
查出需要的数据,去掉重复数据后,再跟数据表进行关联,这样既能得到自己想要的数据,又能去掉重复列。