美好的一天!您能帮我查询吗?
我有一个表格“付款”:
import loompy as lp
import tensorflow as tf
from sklearn.model_selection import train_test_split
model_input_name = ""
input_size = 10000
batch_size = 32
epochs = 10
# Input functions for train, test and eval sets.
def train_input_fn():
return _input_fn('TRAIN')
def test_input_fn():
return _input_fn('TEST')
def eval_input_fn():
return _input_fn('EVAL')
# General purpose input function
def _input_fn(mode = 'TRAIN'):
"""
Arguments
mode : 'TRAIN', 'TEST', 'EVAL'
"""
# A generator to yield data and labels from the given FILE,
# based on the indices assigned to the "indices" variable.
# If you change the labels, remember to update the from_generator()
# parameters below, to reflect their datatype.
def gen():
with lp.connect(FILE, 'r') as ds:
if ae:
for i in indices:
yield {model_input_name: ds[:, i]}, ds[:, i]
else:
for i in indices:
yield {model_input_name: ds[:, i]}, ds.ca.x_CellType[i]
# Get the indices for train, test and eval sets
train_idx, test_idx, eval_idx = train_test_set_idx_split(TRAIN_RT, TEST_RT, EVAL_RT)
# Check condition and assign the respective set to the "indices" variable
if mode == 'TRAIN':
indices = train_idx
elif mode == 'TEST':
indices = test_idx
elif mode == 'EVAL':
indices = eval_idx
else:
print("Wrong mode choice: ", mode)
exit(1)
dataset = tf.data.Dataset.from_generator(gen, ({model_input_name: tf.int64}, tf.int64),
output_shapes=({model_input_name: [input_size,]}, []))
# Shuffle, batch, map, prefetch and repeat your dataset.
# If you need to do some preprocessing on the data, create your function on
# the cell above, and call it within a map() function.
dataset = dataset.shuffle(buffer_size=batch_size*50)
dataset = dataset.batch(batch_size)
dataset = dataset.map(_reshape_labels)
dataset = dataset.map(_int2float)
# Map on whatever other functions you need
dataset = dataset.map( ... )
dataset = dataset.prefetch(2)
dataset = dataset.repeat(epochs)
iterator = dataset.make_one_shot_iterator()
return iterator.get_next()
# Get train, test, eval indices for the given dataset
def train_test_set_idx_split(train_rt, test_rt, eval_rt):
""" This function returns indices for the train, test and evaluation sets,
given an input Dataset.
Arguments:
train_rt: ratio of the train dataset
test_rt: ratio of the test dataset
eval_rt: ratio of the evaluation dataset
Returns:
train_idx: indices (of the given dataset) for the train dataset
test_idx: indices (of the given dataset) for the test dataset
evel_idx: indices (of the given dataset) for the evaluation dataset
Note:
This function will work correctly as long as (test_rt == evel_rt) is True.
If you need (test_rt != evel_rt), you need something more sophisticated.
"""
with lp.connect(FILE, 'r') as ds:
idx = np.array(range(0, ds.shape[1]))
train_idx, test_idx = train_test_split(idx, train_size=train_rt, test_size=test_rt+eval_rt)
test_idx, eval_idx = train_test_split(test_idx, train_size=0.5, test_size=0.5)
return train_idx, test_idx, eval_idx
# Reshape labels as needed
def _reshape_labels(data, labels):
return data, tf.reshape(labels, (-1,1))
任务是选择2012年1月1日至2012年3月31日的总付款金额,并根据用户曾经支付的金额将该金额按组划分。
组为“ 0-10”-如果总和为0 -10 $ “ 10或更多”-如果总和> 10 $。
我的代码:
payments
user_id amount payment_time sale_type
1 20 31.01.2011 card
1 10 02.01.2012 cash
3 10 03.01.2012 card
4 15 05.02.2012 cash
...and so on
这是怎么了?
预期产量
SELECT * from (select IFnull(t.diapason,'total') as diapason, total_amount
FROM
(SELECT p.user_id, p.amount as total_amount, CASE
when amount<=10 then '0-10'
when amount>10 then '10 and more' END AS diapason
FROM (SELECT distinct payments.user_id, SUM(amount) AS amount
FROM payments inner JOIN (SELECT DISTINCT user_id
FROM payments where payment_time between '2012-01-01'
and '2012-01-30') a ON payments.user_id = a.user_id
GROUP BY payments.user_id) p) t GROUP BY diapason WITH ROLLUP) as
t1 ORDER BY total_amount desc;
答案 0 :(得分:1)
尝试此查询-
select case when p2.amount <=10 then '0-10'
else '10 and more' end diapason
,p1.amount "total amount"
,p1.payment_by_card
,p1.cash
from (select user_id, sum(amount) amount, payment_by_card, cash
from payments
where payment_time between '2012-01-01' and '2012-01-30'
group by user_id, payment_by_card, cash) p1
join (select user_id, sum(amount) amount
from payments
group by user_id) p2
on p1.user_id = p2.user_id