SELECT * FROM(SELECT DISTINCT DRUG_NAME, SUM(WEIGHT) FROM FIR
INNER JOIN USER_LOGIN ON FIR.USER_ID = USER_LOGIN.USER_ID
INNER JOIN POLICE_STATION ON FIR.PS_ID = POLICE_STATION.PS_ID
INNER JOIN DRUG_INFO ON FIR.FIR_NO = DRUG_INFO.FIR_NO
WHERE WEIGHT != 0 AND DRUG_FORM = 'Litres (l)'
GROUP BY DRUG_NAME
)
我希望在drug_name
上有一个小组,但希望选择drug_form,weight,picture,username
中的这些列。上面的sql语句工作正常但我无法选择drug_form,weight,picture,username
列。
我想将像鸦片这样的药物名称合并在一起,并在1列上显示总数。
答案 0 :(得分:0)
任何不在group by子句中的列都必须使用聚合函数(MAX,AVG,SUM等)。因为你是"分组"将多个值放入单个列中,您需要某种方法将数据压缩为单个值。
将其他列放入您的组中,或使用聚合函数。
答案 1 :(得分:0)
如果您尝试这样做(未经测试)
SELECT d.drug_form,
d.weight,
ull.picture,
ull.username,
x.DRUG_NAME,
x.TotalWeight
FROM DRUG_INFO d inner join FIR f
on d.FIR_NO = f.FIR_NO
inner join USER_LOGIN ull
on f.USER_ID = ull.USER_ID
inner join
(SELECT DRUG_NAME,
SUM(WEIGHT) as TotalWeight
FROM DRUG_INFO
WHERE WEIGHT != 0
AND DRUG_FORM = 'Litres (l)'
GROUP BY DRUG_NAME
) x
on d.DRUG_NAME = x.DRUG_NAME