在以下示例中,我使用以下查询获得每辆车的销售总额:
AND YEAR(sal.sale_date) = YEAR(CURRENT_DATE())
如果我在查询中使用以下细分,我只会获得2017年销售的汽车注册,但这不是我想要的。
SELECT car.id, car.cars,
SUM(case when YEAR(sal.sale_date) = YEAR(CURRENT_DATE())
then sal.sales
else 0
end) AS value_sales
SUM(sal.sales) AS value_sales_total_years
FROM cars car
LEFT JOIN sales_cars sal ON car.id = sal.id_cars
GROUP BY car.id
我想要达到的目的是获得所有汽车的销售记录(如上一次查询),但只是当前年份值的总和。
例如: 1 |福特| 30 而非 50 (2016年和2017年)
执行此查询的正确方法是什么?
EDITED
获取当前年份和总年份(在juergen回答之后):
2017-03-29 20:34:02.749498: I tensorflow/core/distributed_runtime/rpc/grpc_server_lib.cc:239] Started server with target: grpc://localhost:8338
Extracting /home/mvo010/.tmp/tensorflow/mnist/input_data/train-images-idx3-ubyte.gz
Extracting /home/mvo010/.tmp/tensorflow/mnist/input_data/train-labels-idx1-ubyte.gz
Extracting /home/mvo010/.tmp/tensorflow/mnist/input_data/t10k-images-idx3-ubyte.gz
Extracting /home/mvo010/.tmp/tensorflow/mnist/input_data/t10k-labels-idx1-ubyte.gz
Traceback (most recent call last):
File "/home/mvo010/inf3203-1/mnist_softmax.py", line 173, in <module>
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
File "/share/apps/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 44, in run
_sys.exit(main(_sys.argv[:1] + flags_passthrough))
File "/home/mvo010/inf3203-1/mnist_softmax.py", line 24, in main
mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)
File "/share/apps/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py", line 256, in read_data_sets
train = DataSet(train_images, train_labels, dtype=dtype, reshape=reshape)
File "/share/apps/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py", line 138, in __init__
images = numpy.multiply(images, 1.0 / 255.0)
MemoryError
答案 0 :(得分:2)
使用条件SUM()
(-> Number Number Number)
答案 1 :(得分:2)
LEFT JOIN
中第一个表的条件进入WHERE
子句。后续表中的条件位于ON
子句中。因此,编写此查询的最佳方法是:
SELECT car.id, car.cars, SUM(sal.sales) AS value_sales
FROM cars car LEFT JOIN
sales_cars sal
ON car.id = sal.id_cars AND YEAR(sal.sale_date) = YEAR(CURRENT_DATE())
GROUP BY car.id;
对于2017年没有销售的汽车,LEFT JOIN
将为NULL
生成sal.sale_date
个值。您的WHERE
子句会过滤掉这些记录。由于ON
。
LEFT JOIN
不会