view: cm_track_artists {
  sql_table_name: INTELLIGENCE.DBT_PROD.CM_TRACK_ARTISTS ;;
#  derived_table: {
 #   sql:with track_artists as (
#with track_artist as (select t.name as track_name, t.isrc, a.name, a.id as cm_artist, a.code2 as cm_artist_country
#from CHARTMETRIC.RAW_DATA.CM_TRACK t
#left join CHARTMETRIC.RAW_DATA.L_CM_TRACK_CM_ARTIST cm on t.id = cm.cm_track
#left join CHARTMETRIC.RAW_DATA.CM_ARTIST a on a.id = cm.cm_artist)
#
#select track_name, isrc as ISRC, cm_artist, name as artist_name, cm_artist_country as artist_country,
##    LISTAGG(DISTINCT(artist_name), ', ')
#    WITHIN GROUP (order by artist_name)
#    OVER (PARTITION BY ISRC) as artists
#from track_artist)
#
#select *
#from track_artists ;;
 # }

#  dimension: id {
#    type: number
#    primary_key: yes
#  }

  dimension: track_name {
    label: "Track Name"
    type: string
    sql: ${TABLE}.track_name ;;
    view_label: "Track"
    }

  dimension: isrc {
    label: "ISRC"
    type: string
    sql: ${TABLE}.isrc ;;
    view_label: "Track"
    link: {
      label: "Charts View"
      url:
      "

      {% if _explore._name == 'cm_unioned_chart_advanced' %}
      https://theorchard.looker.com/dashboards/1523?Chart%20Date=2%20days%20ago&ISRC={{ cm_track_artists.isrc._value }}


      {% endif %}
      "
    }
  }

  dimension: artist_country {
    label: "Artist Country"
    type: string
    sql: ${TABLE}.artist_country ;;
    view_label: "Artist"
  }

  dimension: local_artist {
    hidden: yes
    label: "Local Artist?"
    view_label: "CM Playlist - DO NOT USE"
    description: "Local artist to playlist country - only use in playlist model"
    type: yesno
    sql: {% if _explore._name == 'cm_unioned_playlist_current'%}

    ${TABLE}.artist_country = ${cm_unioned_playlist_current.country}

    {% else %}

    NULL

    {% endif %}
    ;;
  }

  dimension: artist_name {
    label: "Artist Name"
    type: string
    sql: ${TABLE}.artist_name ;;
    view_label: "Artist"
    link: {
      label: "Artist View"
      url:
      "

      {% if _explore._name == 'cm_unioned_chart_advanced' or 'cm_unioned_playlist_current' or 'cm_unioned_chart_2' %}
      https://theorchard.looker.com/dashboards/1524?Artist%20Name={{ cm_track_artists.artist_name._value }}


      {% endif %}
      "
    }

  }



  dimension: cm_artist {
    hidden: yes
    type: number
    sql: ${TABLE}.cm_artist;;
  }

  dimension: artists {
    label: "Artists"
    type: string
    sql: ${TABLE}.artists ;;
    view_label: "Artist"
    link: {
      label: "Artist View"
      url:
      "

      {% if _explore._name == 'cm_unioned_chart_advanced' %}
      https://theorchard.looker.com/dashboards/1524?Artist%20Name={{ cm_track_artists.artists._value }}


      {% endif %}
      "
    }
  }

}
