{# Copyright 2020 Curtin University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Author: Richard Hosking, James Diprose #}

SELECT
  UPPER(TRIM(cited)) as doi,
  COUNT(cited) as citations_total, 
  COUNTIF(IF(STARTS_WITH(timespan, "-"), -1, IF(STRPOS(timespan, "Y")-1 > 0, CAST(SUBSTR(timespan, STRPOS(timespan, "P")+1, STRPOS(timespan, "Y")-2) AS INT64), 0)) < 0) as citations_before_publication,
  COUNTIF(IF(STARTS_WITH(timespan, "-"), -1, IF(STRPOS(timespan, "Y")-1 > 0, CAST(SUBSTR(timespan, STRPOS(timespan, "P")+1, STRPOS(timespan, "Y")-2) AS INT64), 0)) < 1) as citations_one_year,
  COUNTIF(IF(STARTS_WITH(timespan, "-"), -1, IF(STRPOS(timespan, "Y")-1 > 0, CAST(SUBSTR(timespan, STRPOS(timespan, "P")+1, STRPOS(timespan, "Y")-2) AS INT64), 0)) < 2) as citations_two_years,
  COUNTIF(IF(STARTS_WITH(timespan, "-"), -1, IF(STRPOS(timespan, "Y")-1 > 0, CAST(SUBSTR(timespan, STRPOS(timespan, "P")+1, STRPOS(timespan, "Y")-2) AS INT64), 0)) < 5) as citations_five_years,
  
  COUNTIF(journal_sc = True) as journal_sc_total,
  COUNTIF(IF(STARTS_WITH(timespan, "-"), -1, IF(STRPOS(timespan, "Y")-1 > 0, CAST(SUBSTR(timespan, STRPOS(timespan, "P")+1, STRPOS(timespan, "Y")-2) AS INT64), 0)) < 0 AND journal_sc = True) as journal_sc_before_publication,
  COUNTIF(IF(STARTS_WITH(timespan, "-"), -1, IF(STRPOS(timespan, "Y")-1 > 0, CAST(SUBSTR(timespan, STRPOS(timespan, "P")+1, STRPOS(timespan, "Y")-2) AS INT64), 0)) < 1 AND journal_sc = True) as journal_sc_one_year,
  COUNTIF(IF(STARTS_WITH(timespan, "-"), -1, IF(STRPOS(timespan, "Y")-1 > 0, CAST(SUBSTR(timespan, STRPOS(timespan, "P")+1, STRPOS(timespan, "Y")-2) AS INT64), 0)) < 2 AND journal_sc = True) as journal_sc_two_years,
  COUNTIF(IF(STARTS_WITH(timespan, "-"), -1, IF(STRPOS(timespan, "Y")-1 > 0, CAST(SUBSTR(timespan, STRPOS(timespan, "P")+1, STRPOS(timespan, "Y")-2) AS INT64), 0)) < 5 AND journal_sc = True) as journal_sc_five_years,
  
  COUNTIF(author_sc = True) as author_sc_total,
  COUNTIF(IF(STARTS_WITH(timespan, "-"), -1, IF(STRPOS(timespan, "Y")-1 > 0, CAST(SUBSTR(timespan, STRPOS(timespan, "P")+1, STRPOS(timespan, "Y")-2) AS INT64), 0)) < 0 AND author_sc = True) as author_sc_before_publication,
  COUNTIF(IF(STARTS_WITH(timespan, "-"), -1, IF(STRPOS(timespan, "Y")-1 > 0, CAST(SUBSTR(timespan, STRPOS(timespan, "P")+1, STRPOS(timespan, "Y")-2) AS INT64), 0)) < 1 AND author_sc = True) as author_sc_one_year,
  COUNTIF(IF(STARTS_WITH(timespan, "-"), -1, IF(STRPOS(timespan, "Y")-1 > 0, CAST(SUBSTR(timespan, STRPOS(timespan, "P")+1, STRPOS(timespan, "Y")-2) AS INT64), 0)) < 2 AND author_sc = True) as author_sc_two_years,
  COUNTIF(IF(STARTS_WITH(timespan, "-"), -1, IF(STRPOS(timespan, "Y")-1 > 0, CAST(SUBSTR(timespan, STRPOS(timespan, "P")+1, STRPOS(timespan, "Y")-2) AS INT64), 0)) < 5 AND author_sc = True) as author_sc_five_years
FROM
  `{{ project_id }}.{{ open_citations.dataset_id }}.{{ open_citations.table_id }}{{ open_citations.release_date.strftime('%Y%m%d') }}` as citations
GROUP BY
  UPPER(TRIM(cited))