SQL Challenge: Cross-Country Scoring (Solution)

I introduced the challenge of Scoring a Cross Country match in a 2005 article.

This article provides some SQL Solutions to parts of the Challenge.

1) Matching Teams:

In a four-way match each combination of teams, each team is scored against the others:

Teams: Ephrata, Manheim Township, Conestoga Valley, McCaskey

There are 6 pairs: 3+2+1

  1. Ephrata vs Manheim Township
  2. Ephrata vs Conestoga Valley
  3. Ephrata vs McCaskey
  4. Manheim Township vs Conestoga Valley
  5. Manheim Township vs McCaskey
  6. Conestoga Valley vs McCaskey

2) How do we generate these team pairings with SQL?

/* Get Pairs of Teams */
select 
	t1.team_name as t1, 			
	t2.team_name as t2
from 
	teams as t1
	cross join	/* Cross join two teams: generates all possible combinations */
	teams as t2
where
        /* 'greater than' fix to make sure that teams don't race against themselves or their inverse */
	t1.team_name > t2.team_name	
order by
	t1.team_name, t2.team_name;

3) How to we generate each runner’s points?

select 
	rank() over (order by place) as points,	  
	team_pairs.team1, 
	team_pairs.team2, 
	race_results.runner_name,
	teams.team_name, 
	race_results.place, 
	race_results.time 
from 
  ..

4) Combine Together Using “With-Statement”

/* Add the Race Results to the Team Parings */
with team_pairs as (		
	/* Team Pairs */
	select 
		t1.id as id1,
		t1.team_name as team1, 
		t2.id as id2,
		t2.team_name as team2
	from 
		teams as t1 
		cross join						
		teams as t2
	where
		t1.team_name > t2.team_name			/* could also use teams.id but I didn't want you to think the ids are meaningful */
	order by
		t1.team_name, t2.team_name

)
select 
	rank() over (order by place) as points,		
	team_pairs.team1, 
	team_pairs.team2, 
	race_results.runner_name,
	teams.team_name, 
	race_results.place, 
	race_results.time 
from 
	team_pairs					   /* team_pairs is the 'named' variable from the above with statement */
	left outer join race_results 			   /* this could change to an'inner join' but I want to see all teams, evem if no runners */
	  on 	team_pairs.id1 = race_results.team_id or
	  		team_pairs.id2 = race_results.team_id
	inner join teams
	  on teams.id = race_results.team_id

/* 	There's a lot of data returned.  Let's focus in on one match: Ephrata vs Manheim Township for which we at least have 4 runners. 
	We can remove the filter once we have a single match worked out */
where
	(	team_pairs.team1 = 'Ephrata' and
		team_pairs.team2 = 'Manheim Township'
	)
	or 
	(	team_pairs.team2 = 'Ephrata' and
		team_pairs.team1 = 'Manheim Township'
	)

order by 
	race_results.place;


	/* 	We can add another with statement and sum up the points for each team, grouping by team1, and team2
		We also have to figure out how we're going to solve the 6th and 7th runner rule.
		I think we can do that with a where clause that limits the top 7 runners, 
		which may require a second rank column and an order by statement, but I'm fuzzy on what I did before
	*/

 

View More:

 

 

How Bitcoin is Like Telsa

Tesla

Bitcoin Info  > How Bitcoin is Like Tesla

Tesla was built with early adopters who saw past certain drawbacks

 

Tesla1) Early Adopters: Telsa

Prior to Tesla, if you had pitched the idea of starting an electric car to an investor, you would likely have face skepticism:

  • How are you going to sell a car that has a maximum range of 100 miles?
  • How are you going to sell such a car that costs $100,000.

Elon must found a way to work around these limitations:

  • Make a sports car that has superior acceleration.
  • Sell this car to early adopters who are willing to pay $100,000 and don’t mind the range limitation.
  • Bring price and range down with more volume and research.

 

Number go up Technology2) Early Adopters: Bitcoin

The way Bitcoin attracted early adopters was not 0-60 acceleration, but financial returns.

True, critics will see the volatility.  But what the enthusiasts see is that those who have held Bitcoin over a 4-year cycle, have seen vastly superior returns.

 

Adoption Curve
Adoption Curve

3) Still Useful to Early Adopters

Just as it was true that Telsa did not have enough range for a 10 hour trip to your parents, Bitcoin is too volatile for daily purchases.

But that doesn’t mean that it doesn’t have value and can’t eventually fill that role.

 

Why The Yuppie Elite Dismiss Bitcoin  (audio version)

Bitcoin is more like the Federal Reserve than like VISA

Lightning Network vs VISA diagram

What is Bitcoin Good for?  > Bitcoin is more like the Federal Reserve than like VISA

 

  • Banks are build on top of the Federal Reserve clearing system. (Layer 1)
  • Payment systems like VISA (layer 3) are build on top of the banks (layer 2), (which are built on top of the Federal Reserve system.
  • Millions or Billions of VISA Transactions on layer 3 are aggregated in vastly fewer, but much larger Fed Wire transactions on layer 1.
VISA - Federal Reserve Diagram
VISA – Federal Reserve Diagram

 

  • If Bitcoin competes with the Federal Reserve as a Layer 1, VISA could operate as a layer 2.
  • With Bitcoin as layer 1, Lightning functions as a layer 2 competitor to VISA.
  • Millions or Billions of VISA and Bitcoin transactions are aggregated into fewer, but much larger transactions.

 

Lightning Network vs VISA diagram
Lightning Network vs VISA diagram

 

More likely, the Fed and Bitcoin will coexist and VISA will settle transactions with each.

 

Back: What is Bitcoin Good for?

Claim: Bitcoin is used by money launderers and other criminals.

What is Bitcoin Good for?  > Claim: Bitcoin is used by money launderers

  • Bitcoin developed a reputation for money-laundering based off of criminal activity in its early years (2012-13) because the technology was new and unknown to law enforcement.
  • Today, savvy criminals have learned to avoid Bitcoin because the log of Bitcoin transactions (the Bitcoin ledger) is public and the FBI has learned how to analyze the transaction logs to find the illicit activity .
  • The FBI learned how easy it is to catch crime when they caught two of their own agents stealing (Ted-talk video) from the Silk Road, an early black market which used Bitcoin.  Government Agents who had authority to order Internet Service Providers to erase their logs failed to cover their tracks because they couldn’t delete the Bitcoin logs of their theft.

Former Government Prosecutor of the Silk Road black market: Kathryn Haun

How Does Illicit Bitcoin Activity Levels Compare to the Existing Banking System?