/*
 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: Keegan Smith
 */
/*
This script creates a temporary table full of the supplied ISBNs. 
It then joins these ISBNs on the Crossref Metadata table. 
This gives the same result as using a WHERE statement for each ISBN.
*/

WITH isbns AS (
  SELECT
    DISTINCT(ISBN13)
  FROM
    `{{ onix_table_id }}`
)
SELECT
  xref.DOI,
  xref.URL,
  xref.type,
  xref.title,
  xref.abstract,
  xref.issued,
  xref.ISSN,
  xref.ISBN,
  xref.issn_type,
  xref.publisher_location,
  xref.publisher,
  xref.member,
  xref.prefix,
  xref.container_title,
  xref.short_container_title,
  xref.group_title,
  xref.references_count,
  xref.is_referenced_by_count,
  xref.subject,
  xref.published_print,
  xref.license,
  xref.volume,
  xref.funder,
  xref.page,
  xref.author,
  xref.link,
  xref.clinical_trial_number,
  xref.alternative_id
FROM
  `{{ crossref_metadata_table_id }}` xref
JOIN
  isbns i
ON
  i.ISBN13 IN UNNEST(xref.ISBN)
