请帮助我创建一个查询,以确定下表中的最小date_time
:
ID | Name | Date_Time | Location
---------------------------------------
001 | John | 01/01/2015 | 901
001 | john | 02/01/2015 | 903
001 | john | 05/01/2015 | 905
001 | john | 06/01/2015 | 904
002 | Jack | 01/01/2015 | 903
002 | Jack | 03/01/2015 | 904
002 | Jack | 04/01/2015 | 905
003 | Sam | 01/01/2015 | 904
003 | Sam | 03/01/2015 | 903
003 | Sam | 04/01/2015 | 901
003 | Sam | 06/01/2015 | 903
我尝试了这个查询:
SELECT ID, NAME, MIN(DATE_TIME), LOCATION
FROM TABLE
GROUP BY (ID)
但我收到此错误消息:
ORA-00979: not a GROUP BY expression
答案 0 :(得分:1)
如果使用聚合功能,则指定应该应用聚合的字段。所以你正在使用group by子句。在这种情况下,您可能需要找到每个id,名称组合的最小date_time。
select id, name, min(date_time)
from my_table group by id, name
答案 1 :(得分:1)
当您对某些内容进行分组时,所有其他行将保持群集到该分组键。对于密钥,您只能在SELECT
中获取行(实体)之一。
快捷方式是,GROUP BY
中的SELECT
可以自由地进行AGGREGATE
。否则,它们必须包含在001
函数中。
按ID分组时,
SELECT
键有4行聚集在它上面。想想,当你在MIN(date)
中指定非分组列时会发生什么。在使用SELECT ID,MIN(NAME),MIN(LOCATION),MIN(DATE)
FROM TABLE
GROUP BY ID
时,在4个日期中,至少需要一个。
因此,您的查询必须是
SELECT ID,LOCATION,NAME,MIN(DATE)
FROM TABLE
GROUP BY ID,LOCATION,NAME
OR
SELECT ID,LOCATION,DATE,MIN(DATE) OVER(PARTITION BY ID ORDER BY NULL) AS MIN_DATE
FROM TABLE.
OR
分析方法。
SELECT T.ID,T.NAME,T.LOCATION,MIN_DATE
FROM
(
SELECT ID,MIN(DATE) AS MIN_DATE
FROM TABLE T1
GROUP BY ID
) AGG, TABLE T
WHERE T.ID = AGG.ID
AND T.DATE = AGG.MIN_DATE
然而,关于如何重写查询,这取决于要求。
编辑:要获取与最短日期相对应的行,我们可以创建一个类似下面的SELF JOIN。
SELECT ID,NAME,LOCATION,MIN_DATE
FROM
(
SELECT ID,
NAME,
LOCATION,
MIN(DATE) OVER(PARTITION BY ID ORDER BY NULL) MIN_DATE,
ROW_NUMBER() OVER(PARTITION BY ID ORDER BY NULL) RNK
FROM TABLE
)
WHERE RNK = 1;
OR
char *pri_key[] = "some key"; // ---> some key, that i've got from server
RSA *rsa;
BIO *keybio;
keybio = BIO_new_mem_buf(pri_key, strlen(pri_key));
rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa, NULL, NULL);
// Decrypt it
// Encoded message is in buff
char *decrypt = new char[BUFF_SIZE];
int decrypt_len = RSA_private_decrypt(BUFF_SIZE, (unsigned char*)buff, (unsigned char*)decrypt,
rsa, RSA_PKCS1_OAEP_PADDING); // ------> it fails here
答案 2 :(得分:0)
尝试对所有其他列进行分组...如果表名为'table',请尝试将表名更改为架构中的其他名称。
SELECT ID , NAME , MIN(DATE_TIME) , LOCATION FROM TABLE GROUP BY ID, Name, Location
答案 3 :(得分:0)
选择t1.name,t1.id,t1.location,t1.date from(select id,MIN(Date)as min_date from table group by id)t2 inner join TABLE t1 on t1.date = t2.min_date and t1.id = t2.id;