#!/bin/sh
#
# Test joins
# 

SHSQL="shsql -echo -h -delim space"

echo "-------------------------------------------------------------"
echo "1. left join of two tables with where condition 
         (leftdl join because of multiple occurances of HSTD key field)"
$SHSQL "select * 
	from nzplaces = n leftdl join pointdesc = p 
	on n.desc_code = p.code 
	where n.name like 'had*' 
	order by n.name, n.id"  				2>&1

# same as above but requesting specific fields

echo "-------------------------------------------------------------"
echo "2. same as above but requesting specific fields"
$SHSQL "select n.name, n.district, p.name 
	from nzplaces (as n) leftdl join pointdesc (as p) 
	on n.desc_code = p.code 
	where n.name like 'had*' 
	order by n.name, n.id"  				2>&1

# join of ordinary file (created above) with table  

echo "-------------------------------------------------------------"
echo "3. join of ordinary file with table
	(leftdl join since file has mutliple occurances of "CH" key field)"
$SHSQL "select * into /tmp/shsql_tmp1 from nzplaces where latitude inrange -44.34,-44.32 and longitude inrange 170.2,170.4"  2>&1
$SHSQL "select * from /tmp/shsql_tmp1 (as t) leftdl join landdist (as d) on t.district = d.code order by t.name, t.id"  2>&1


echo "-------------------------------------------------------------"
echo "4. Three table join, using ordinary file as intermediary"
$SHSQL "select n.name (as placename), n.district, p.name (as pointdesc) 
	into /tmp/shsql_tmp2 
	from nzplaces (as n) leftdl join pointdesc (as p) 
	on n.desc_code = p.code 
	where n.name like 'had*' "  				2>&1
  # now join the above file to district name..
  $SHSQL "select a.placename (as placename), b.name (as district), a.pointdesc (as featuretype)
	from /tmp/shsql_tmp2 (as a) leftdl join landdist (as b)
	on a.district = b.code
	order by a.placename"  				2>&1


