SQL按日期选择最后一个并按分组

时间:2019-11-06 16:46:58

标签: mysql sql

使用MySQL,我要选择所有提交(行),最后一个提交为NULL,而上一个提交为NULL,按user_id分组。

如果考虑以下示例表,则答案将是行:2、5和7。

| ID | submission | date_submission | user_id |
|----|------------|-----------------|---------|
| 1  | lorem      | 2019-01-01      | 1       |
| 2  | ipsum      | 2019-01-02      | 1       |
| 3  | NULL       | 2019-01-03      | 1       |
| 4  | amet       | 2019-01-05      | 2       |
| 5  | NULL       | 2019-01-06      | 2       |
| 6  | sit        | 2019-01-04      | 1       |
| 7  | sed        | 2019-01-08      | 3       |
| 8  | elit       | 2019-01-07      | 4       |
| 9  | NULL       | 2019-01-09      | 3       |

MRE:

CREATE TABLE submissions (
  id int NOT NULL,
  submission varchar(45) NULL,
  date_submitted date NOT NULL,
  user_id int DEFAULT NULL
 );

insert into submissions (1, "lorem", 2019-01-01, 1);
insert into submissions (2, "ipsum", 2019-01-02, 1);
insert into submissions (3, NULL, 2019-01-03, 1);
insert into submissions (4, "amet", 2019-01-05, 2);
insert into submissions (5, NULL, 2019-01-06, 2);
insert into submissions (6, "sit", 2019-01-04, 1);
insert into submissions (7, "sed", 2019-01-08, 3);
insert into submissions (8, "elit", 2019-01-07, 4);
insert into submissions (9, NULL, 2019-01-09, 3);

2 个答案:

答案 0 :(得分:0)

首先为每个用户提交null,以获取最后一个日期,然后加入表格以获取先前日期的行。
如果不是ROW_NUMBER(),则使用null获取这些日期的最后日期:

select t.id, t.submission, t.date_submitted, t.user_id
from (
  select s.*,
    row_number() over (partition by s.user_id order by s.date_submitted desc) rn
  from submissions s inner join (
    select user_id,
      max(case when submission is null then date_submitted end) maxnulldate
    from submissions
    group by user_id
  ) g on g.user_id = s.user_id and g.maxnulldate > s.date_submitted  
) t
where t.rn = 1 and t.date_submitted is not null

请参见demo
结果:

| id  | submission | date_submitted | user_id |
| --- | ---------- | -------------- | ------- |
| 2   | ipsum      | 2019-01-02     | 1       |
| 4   | amet       | 2019-01-05     | 2       |
| 7   | sed        | 2019-01-08     | 3       |

我想您的意思是期望结果中的第4行而不是第5行,对吧?

答案 1 :(得分:0)

您可以使用import * as tf from '@tensorflow/tfjs'; import '@tensorflow/tfjs-react-native'; export class App extends React.Component { constructor(props) { super(props); this.state = { isTfReady: false, }; } init() { const model = tf.sequential({ layers: [ tf.layers.dense({ inputShape: [784], units: 32, activation: "relu" }), tf.layers.dense({ units: 10, activation: "softmax" }) ] }); model.weights.forEach(w => { console.log(w.name, w.shape); }); model.weights.forEach(w => { const newVals = tf.randomNormal(w.shape); // w.val is an instance of tf.Variable w.val.assign(newVals); }); model.compile({ optimizer: "sgd", loss: "categoricalCrossentropy", metrics: ["accuracy"] }); const data = tf.randomNormal([100, 784]); const labels = tf.randomUniform([100, 10]); function onBatchEnd(batch, logs) { console.log("Accuracy", logs.acc); } // Train for 5 epochs with batch size of 32. model .fit(data, labels, { epochs: 5, batchSize: 32, callbacks: { onBatchEnd } }) .then(info => { console.log("Final accuracy", info.history.acc); }); } async componentDidMount() { // Wait for tf to be ready. await tf.ready(); // Signal to the app that tensorflow.js can now be used. this.setState({ isTfReady: true, }); } render() { init() // } }

lag()

Here是db <>小提琴。

编辑:

在我看来,“最后提交”实际上是每个用户的最后提交。在这种情况下,可以对以上内容进行调整:

select s.*
from (select s.*,
             lag(submission) over (partition by user_id order by date_submitted) as prev_submission
      from submissions s
     ) s
where prev_submission is not null and submission is null;
相关问题