开发者

SQL Distinct Query

开发者 https://www.devze.com 2023-01-23 06:55 出处:网络
开发者_运维问答My SQL seems to be letting me down this morning. I have a table with the columns

开发者_运维问答My SQL seems to be letting me down this morning. I have a table with the columns

Id, Guid,AttributeId,AttributeValue,CreationDate,Status

This stores data from a qustonnaire which has around 15 pages to it. Each time you move on to the next question (next page) in the questionnaire the entire questionnaire is persisted to the table i.e after completing the 1st question, that questions data is stored in the table, after completing the 2nd question, the 1st and 2nd question is persisted to the table meaning that know, we have two lots of the 1st qestion and one lot of the second question saved in the table.

I need to write a query that will return the latest lot of saved data for a given questionnaire (and all questionnaires). i.e. if the user got to question 13 i would want only that set of data returned.


Something like...

SELECT Q.*
FROM Questionnaire Q
INNER JOIN (
  SELECT TOP 1 Guid, CreationDate
  FROM Questionnaire
  ORDER BY CreationDate DESC
) Q2
ON Q2.Guid = Q.Guid AND Q2.CreationDate = Q.CreationDate

...ought to do it. The join to Guid is possibly redundant - and you'll presumably need a WHERE somewhere to ensure you get the questionnaire for the particular user / session.


Maybe this does the trick...

SELECT TOP 1 * FROM Questionaire ORDER BY CreationDate DESC
0

精彩评论

暂无评论...
验证码 换一张
取 消