query: # this query identifies the date-weekday cominations for when disasters happened
  # eg "Tuesday 12th"
  # original by Matěj Suchánek & Larske - https://www.wikidata.org/wiki/Wikidata:Request_a_query#Disasters_starting_on_Friday_13
  
  SELECT (?q AS ?Date) (SUM(?mon) AS ?Mon) (SUM(?tue) AS ?Tue) (SUM(?wed) AS ?Wed) (SUM(?thu) AS ?Thu) (SUM(?fri) AS ?Fri) (SUM(?sat) AS ?Sat) (SUM(?sun) AS ?Sun)  (COUNT(?start_time) AS ?Tot)  WHERE {
   { 
    ?disaster wdt:P31/wdt:P279* wd:Q3839081 ;
              p:P580 ?start_time_stm .          # find the date multi-day disasters started
    ?start_time_stm ps:P580 ?start_time .
    ?start_time_stm psv:P580 ?start_time_node .
    ?start_time_node wikibase:timePrecision ?tp . FILTER(?tp=11) 
   }
   UNION 
   {
    ?disaster wdt:P31/wdt:P279* wd:Q3839081 ;
              p:P585 ?start_time_stm .          # find all those which have "point in time" (ie one-day events)
    ?start_time_stm ps:P585 ?start_time .
    ?start_time_stm psv:P585 ?start_time_node .
    ?start_time_node wikibase:timePrecision ?tp . FILTER(?tp=11) 
   }
    # this beautiful bit of code finds all the relevant days of the week
    # using https://cs.wikipedia.org/wiki/Algoritmus_pro_výpočet_dne_v_týdnu#Zellerův_algoritmus
    BIND( DAY( ?start_time ) AS ?q ) .
    BIND( MONTH( ?start_time ) + IF( MONTH( ?start_time ) < 3, 12, 0 ) AS ?m ) .
    BIND( YEAR( ?start_time ) - IF( MONTH( ?start_time ) < 3, 1, 0 ) AS ?_year ) .
    BIND( ?_year - FLOOR( ?_year / 100 ) * 100 AS ?K ) . # modulo
    BIND( FLOOR( YEAR( ?start_time ) / 100 ) AS ?J ) .
    BIND( ?q + FLOOR( 13 * ( ?m + 1 ) / 5 ) + ?K + FLOOR( ?K / 4 ) + FLOOR( ?J / 4 ) - 2 * ?J AS ?_h ) .
    BIND( ?_h - FLOOR( ?_h / 7 ) * 7 AS ?weekday) . # modulo, 0 is Saturday
    BIND(IF(?weekday=2,1,0) AS ?mon)
    BIND(IF(?weekday=3,1,0) AS ?tue)
    BIND(IF(?weekday=4,1,0) AS ?wed)
    BIND(IF(?weekday=5,1,0) AS ?thu)
    BIND(IF(?weekday=6,1,0) AS ?fri)
    BIND(IF(?weekday=0,1,0) AS ?sat)
    BIND(IF(?weekday=1,1,0) AS ?sun)
  }
  GROUP BY ?q
  ORDER BY ?Date