我有一份患者信息表,其中有大约5000万条记录。我需要检查每年的一些样品,这些样品可能是任何顺序。以下是数据库“20090722”,“20080817”,......“19980301”中可用的样本日期。我还有一个名为“PID”的主键列。我的要求是每年通过查询获得2或3个样本。
我尝试使用子查询每年获得2个样本,但我没有成功。
本论坛中的任何人都对这种要求有所了解,如果有,请帮助我。
伙计们我在sql server中尝试了以下查询,它找不到工作。但我需要在MYSQL中查询。请帮帮我。
select pid,studydate
FROM (SELECT ROW_NUMBER() OVER ( PARTITION BY studydate ORDER BY pid DESC ) AS
'RowNumber', pid,studydate
FROM patient
) pt
WHERE RowNumber <= 2
答案 0 :(得分:0)
如果我理解你,你可以这样做:
select year(datecolumn) as Year,
(select id from PatiendRecords pr2 where pr2.id>=min(pr.id)+rand()*max(pr.id) LIMIT 1),
(select id from PatiendRecords pr2 where pr2.id>=min(pr.id)+rand()*max(pr.id) LIMIT 1),
(select id from PatiendRecords pr2 where pr2.id>=min(pr.id)+rand()*max(pr.id) LIMIT 1)
from PatiendRecords pr
group by year(datecolumn);
修改强>
delimiter //
CREATE PROCEDURE RandomRecordsPerYear(n INT)
BEGIN
CREATE TEMPORARY TABLE lookup
(id INT) ENGINE = MEMORY;
SET @x = 0;
REPEAT SET @x = @x + 1;
INSERT INTO lookup (id)
SELECT (SELECT id FROM PatientRecords pr WHERE pr2.id>=min(pr.id)+rand()*max(pr.id) LIMIT 1) AS Id FROM PatientRecords pr GROUP BY year(created_at);
UNTIL @x >= n END REPEAT;
SELECT * FROM PatientRecords s JOIN lookup l ON l.id=pr.id;
DROP TABLE lookup;
END
//
call RandomRecordsPerYear(3)//
PS。我发现在MySQL数据库中有5000万条患者记录非常酷。 DS。
答案 1 :(得分:0)
SELECT md.*
FROM (
SELECT @r := @r + 1 AS y
FROM (
@r := 0
) vars
CROSS JOIN
mytable
LIMIT 200
) years
JOIN mytable md
ON md.datecol >= CAST('1900-01-01' AS DATETIME) + INTERVAL y YEARS
AND md.datecol < CAST('1900-01-01' AS DATETIME) + INTERVAL y + 1 YEARS
AND md.id <=
COALESCE(
(
SELECT id
FROM mytable mi
WHERE mi.datecol >= CAST('1900-01-01' AS DATETIME) + INTERVAL y YEARS
AND mi.datecol < CAST('1900-01-01' AS DATETIME) + INTERVAL y + 1 YEARS
ORDER BY
id
LIMIT 2
), 0xFFFFFFFF)