SteemSQL Tutorial: How to Get Historic Posts of Today on SteemIt?


mssql SteemSQL Tutorial: How to Get Historic Posts of Today on SteemIt? mssql sql SteemIt

MSSQL Database

@dapeng has some posts on “History of Today” which reveals some old posts that were published at the same time (but not this year apparently) on steemit. It can be achieved using the following SQL.

1
2
3
4
5
6
7
8
9
select top 10 *
from 
    Comments
where
    FORMAT(created,'MM-dd','en-us') = FORMAT(GetDate(),'MM-dd','en-us') and 
    FORMAT(created,'yyyy','en-us') <> FORMAT(GetDate(),'yyyy','en-us') and
    title <> ''
order by 
    total_payout_value desc
select top 10 *
from 
    Comments
where
    FORMAT(created,'MM-dd','en-us') = FORMAT(GetDate(),'MM-dd','en-us') and 
    FORMAT(created,'yyyy','en-us') <> FORMAT(GetDate(),'yyyy','en-us') and
    title <> ''
order by 
    total_payout_value desc

Let’s explain this line by line.

  • select top 10 * Choosing the first 10 posts
  • from Comments Table Comments holds posts and comments
  • FORMAT(created,’MM-dd’,’en-us’) = FORMAT(GetDate(),’MM-dd’,’en-us’) The Month and Day should be the same as today
  • FORMAT(created,’yyyy’,’en-us’) <> FORMAT(GetDate(),’yyyy’,’en-us’) But it can’t be this year.
  • title <> ” Filter out comments, which have empty titles.
  • order by total_payout_value desc Show the most earned posts first.

We could also add other conditions, such as categories = ‘cn’ if we are only interested in the CN posts.

@arcange suggested the following improved version:

1
2
3
4
5
6
7
8
9
10
SELECT TOP 10 
    *
FROM
    Comments (NOLOCK)
WHERE
    FORMAT(created,'MM-dd','en-us') = FORMAT(GetDate(),'MM-dd','en-us')
    AND YEAR(created) <> YEAR(GETUTCDATE()) and
    AND depth = 0
ORDER BY
    total_payout_value DESC
SELECT TOP 10 
    *
FROM
    Comments (NOLOCK)
WHERE
    FORMAT(created,'MM-dd','en-us') = FORMAT(GetDate(),'MM-dd','en-us')
    AND YEAR(created) <> YEAR(GETUTCDATE()) and
    AND depth = 0
ORDER BY
    total_payout_value DESC

Some things to care about:

  • Never forget to add (NOLOCK) after each table name
  • total_payout_value is not an indexed field (can slow down performances)
  • use GETUTCDATE() rather than GETDATE() because all time stamps in the database are UTC
  • avoid returning ALL FIELDS from the `Comments` table because this can be a bunch of data
  • The (nolock) is important to avoid dead-locking SteemSQL’s Database Injector process and to improve performances (to avoid killing poorly written queries)

You may also like: STEEM SQL 系列之 历史上的今天怎么实现的?

STEEM SQL Tutorial

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
724 words
Last Post: How to get notification on SteemIt contests using IFTTT + Feed?
Next Post: Logic Tests Series (3) - SUBT

The Permanent URL is: SteemSQL Tutorial: How to Get Historic Posts of Today on SteemIt?

Leave a Reply