我想在数据库中选择不同的记录,但根本无法正常工作。我尝试过DISTINCT和GROUP BY,但还是没有运气。
示例:
这是我的代码:
SELECT
t1.room_id as Room_ID,
t1.room_name as Room_Name,
t2.price_date as Price_for_date,
t3.amount as Price ,
COUNT(t3.unit_id) as c
FROM
table1 AS t1
LEFT JOIN table3 AS t3 ON t1.room_id = t3.room_id
LEFT JOIN table2 AS t2 ON t3.price_room_id = t2.price_room_id
WHERE
t2.price_date BETWEEN (20180130) AND (20180530)
GROUP BY
t2.price_date,
t1.room_id
ORDER BY
t1.room_id,
t2.price_date DESC
T1:
id | room_id | room_name
-----------------------
1 | room1 | rm1
2 | room2 | rm2
3 | room3 | rm3
4 | room4 | rm4
5 | room5 | rm5
T2:
id | price_room_id | price_date
-------------------------------
1 | 000001 | 2018-01-30
2 | 000002 | 2018-02-30
3 | 000003 | 2018-03-30
4 | 000004 | 2018-04-30
5 | 000005 | 2018-05-30
T3:
id | room_id | price_room_id | amount
-------------------------------------
1 | room1 | 00001 | 100000
2 | room1 | 00002 | 101000
3 | room2 | 00002 | 110000
4 | room3 | 00003 | 200000
5 | room3 | 00004 | 300000
6 | room4 | 00001 | 100000
7 | room5 | 00005 | 350000
我想要的是选择room_id,price_room_id和金额以及给定日期之间的最新记录。从上表中,我应该得到以下内容:
room_id | price_room_id | amount
-------------------------------------
room1 | 00002 | 101000
room2 | 00002 | 110000
room3 | 00004 | 200000
room4 | 00001 | 100000
room5 | 00005 | 350000
但是我得到的是这样的:
room_id | price_room_id | amount
-------------------------------------
room1 | 00002 | 101000
room1 | 00001 | 100000
room2 | 00002 | 110000
room3 | 00004 | 200000
room3 | 00003 | 110000
room4 | 00001 | 100000
room5 | 00005 | 350000
答案 0 :(得分:0)
不幸的是,我没有发表评论的声誉,但是@Caius Jard所说的是,每个def pow_mod(x, y, z):
"Calculate (x ** y) % z efficiently."
number = 1
while y:
if y & 1:
number = number * x % z
y >>= 1
x = x * x % z
return number
# prime p = 2^256 - 2^32 - 2^9 - 2^8 - 2^7 - 2^6 - 2^4 - 1
p = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f
# bitcoin's compressed public key of private key 55255657523dd1c65a77d3cb53fcd050bf7fc2c11bb0bb6edabdbd41ea51f641
compressed_key = '0314fc03b8df87cd7b872996810db8458d61da8448e531569c8517b469a119d267'
y_parity = int(compressed_key[:2]) - 2
x = int(compressed_key[2:], 16)
a = (pow_mod(x, 3, p) + 7) % p
y = pow_mod(a, (p+1)//4, p)
if y % 2 != y_parity:
y = -y % p
uncompressed_key = '04{:x}{:x}'.format(x, y)
print(uncompressed_key)
# should get 0414fc03b8df87cd7b872996810db8458d61da8448e531569c8517b469a119d267be5645686309c6e6736dbd93940707cc9143d3cf29f1b877ff340e2cb2d259cf
都有多个room_id
,这反映在您实际收到的输出中。但是,您想要的结果只与每个room_id关联一个价格,那么您想为每个price_room_id
返回哪个price_room_id
?从您的评论看来,您只想要每个房间的最新价格?但是,您提供给我们的初始查询也有一些输出未反映在您提供的示例/所需输出中。例如,room_id
您希望此输出是什么?
如果您只想显示最新的COUNT(t3.unit_id) as c
和price_date
,并且只显示那些最新的价格,则应该按照最新的COUNT(t3.unit_id) as c
进行过滤:
price_date
编辑: 好吧,我想我现在明白了。我不太确定如何从日期间隔中获取最大日期,我认为这可能是一个超级慢的查询,但是请尝试以下方法:
SELECT
t1.room_id as Room_ID,
t1.room_name as Room_Name,
t2.price_room_id as Price_room_id,
t2.price_date as Price_for_date,
t3.amount as Price ,
COUNT(t3.unit_id) as c
FROM
table1 AS t1
LEFT JOIN table3 AS t3 ON t1.room_id = t3.room_id
LEFT JOIN table2 AS t2 ON t3.price_room_id = t2.price_room_id
WHERE
t2.price_date = max(t2.price_date)
GROUP BY
t1.room_id
ORDER BY
t1.room_id;
答案 1 :(得分:0)
尝试删除Group by,然后将DISTINCT放入SELECT中。 SELECT DISTINCT ...,因为group by将对每个room_id进行分组,但是在您的情况下,您只想获取最新的price_date。
SELECT DISTINCT (t1.room_id as Room_ID),
t1.room_name as Room_Name,
t2.price_room_id as Price_room_id,
t2.price_date as Price_for_date,
t3.amount as Price ,
COUNT(t3.unit_id) as c
FROM
table1 AS t1
LEFT JOIN table3 AS t3 ON t1.room_id = t3.room_id
LEFT JOIN table2 AS t2 ON t3.price_room_id = t2.price_room_id
WHERE
t2.price_date = max(t2.price_date)
ORDER BY
t1.room_id;