我有一个没有正确求和的MYSQL查询,我在查询中看不到错误。
SELECT s.source, vanMoney.userid, vanMoney.Username, vanMoney.vandate, vanMoney.FirstBuy, vanMoney.LastBuy, vanMoney.SourceID, vanMoney.TodayDate,
Sum(inv_temp.cost* inv_temp.quantity) as TotalCost,
Sum(inv_temp.quantity) as TotalQTY
FROM source AS s
Left JOIN inv_temp ON inv_temp.source_id = s.source_id
Left JOIN vanMoney ON inv_temp.source_id = vanMoney.SourceID
WHERE s.buyer_type_id = 6 and quantity>0
group by vanMoney.vandate, vanMoney.userid
TotalCost和TotalQTY列是整个Source的汇总,不仅仅是source和vandate
the output i should get is
for userid 186 on 3/9/2018 is TotalCost=7 and TotalQTY=2
for userid 186 on 3/10/2018 is TotalCost=77 and TotalQTY=2
for userid 186 on 3/11/2018 is TotalCost=4 and TotalQTY=2
for userid 179 on 3/26/2018 is TotalCost=30 and TotalQTY=2
答案 0 :(得分:0)
您不会显示费用或数量列,但它看起来像这样:
SELECT s.source, vanMoney.userid, vanMoney.Username, vanMoney.vandate,
vanMoney.FirstBuy, vanMoney.LastBuy, anMoney.SourceID, vanMoney.TodayDate,
sum(TABLE.cost) as TotalCost
sum(TABLE.QTY) as TotalQTY
FROM source AS s
Left JOIN inv_temp ON inv_temp.source_id = s.source_id
Left JOIN vanMoney ON inv_temp.source_id = vanMoney.SourceID
WHERE s.buyer_type_id = 6
and vandate='2018-03-10'
group by s.source, vanMoney.userid, vanMoney.Username, vanMoney.vandate,
vanMoney.FirstBuy, vanMoney.LastBuy, vanMoney.SourceID, vanMoney.TodayDate
如果您只想按vandate和userid进行分组,则需要排除其他列。否则你会有重复的总数。
答案 1 :(得分:0)
当您使用GROUP BY
子句时,您需要声明所有字段而不进行聚合
功能
在子查询上使用SUM
GROUP BY
source_id
聚合totle
,source_id
可以JOIN
,然后SELECT s.source,
vanmoney.userid,
vanmoney.username,
vanmoney.vandate,
vanmoney.firstbuy,
vanmoney.lastbuy,
vanmoney.sourceid,
vanmoney.todaydate,
inv_temp.TotalCost,
inv_temp.TotalQTY
FROM source AS s
LEFT JOIN
(
SELECT source_id,
Sum(cost * quantity) AS TotalCost,
Sum(quantity) AS TotalQTY
FROM inv_temp
WHERE quantity > 0
GROUP BY source_id
) inv_temp
ON inv_temp.source_id = s.source_id
LEFT JOIN vanmoney
ON inv_temp.source_id = vanmoney.sourceid
WHERE s.buyer_type_id = 6
。
你可以试试这个。
inv_temp
修改强>
SELECT source_id,
date_created,
Sum(cost * quantity) AS TotalCost,
Sum(quantity) AS TotalQTY
FROM inv_temp
WHERE quantity > 0
GROUP BY source_id,
date_created
期望结果查询可能像
SELECT s.source,
vanmoney.userid,
vanmoney.username,
date_format(str_to_date(vanmoney.vandate, '%m/%d/%Y'),'%Y%m'),
vanmoney.firstbuy,
vanmoney.lastbuy,
vanmoney.sourceid,
vanmoney.todaydate,
Sum(inv_temp.cost * inv_temp.quantity) AS TotalCost,
Sum(inv_temp.quantity) AS TotalQTY
FROM source AS s
LEFT JOIN inv_temp
ON inv_temp.source_id = s.source_id
LEFT JOIN vanmoney
ON inv_temp.source_id = vanmoney.sourceid
WHERE s.buyer_type_id = 6
AND quantity > 0
GROUP BY s.source,
vanmoney.userid,
vanmoney.username,
date_format(str_to_date(vanmoney.vandate, '%m/%d/%Y'),'%Y%m'),
vanmoney.firstbuy,
vanmoney.lastbuy,
vanmoney.sourceid,
vanmoney.todaydate
从你的编辑答案我想你想要这样使用。
a = [['w1' , 'w3'] , ['w2' , 'w4']]
df=pd.DataFrame({'a': a })
print(df)
mlb = MultiLabelBinarizer()
print(np.array(mlb.fit_transform(df['a'].as_matrix())))
答案 2 :(得分:0)
因此,为了使查询不对整行进行求和,而只是对应于日期的行,我执行了where = statement
SELECT
vanMoney.userid,
vanMoney.Username,
vanMoney.vandate,
Max(vanMoney.FirstBuy) AS firstbuy,
Max(vanMoney.LastBuy) AS lastbuy,
vanMoney.SourceID,
inv_temp.date_created,
Sum(inv_temp.cost* inv_temp.quantity) AS TotalCost,
Sum(inv_temp.quantity) AS TotalQTY,
source.source
FROM
inv_temp
LEFT JOIN vanMoney ON inv_temp.purchased_by = vanMoney.userid
Left JOIN source ON inv_temp.source_id = source.source_id
where date_created=vandate
GROUP BY
vanMoney.vandate,
vanMoney.userid