#!/bin/sh
#
# Various SELECTs, using the 'nzplaces' table.
# 'nzplaces' is fairly large (55,000 records, 3MB), so everything is using indexes.
# Ordering is specified on most queries to give comparable results.

# first, remove any existing tmp files 
rm -f /tmp/shsql_tmp1 /tmp/shsql_tmp2 2> /dev/null


SHSQL="shsql -echo -n "

# print table definition of nzplaces
tabdef nzplaces


echo "*************************************************************"
echo "1."
$SHSQL "select id, name from nzplaces where id = 1000 "  	2>&1

echo "*************************************************************"
echo "2. field name aliases"
$SHSQL "select id (as snum), name (as placename) from nzplaces where id = 8252 " 2>&1

echo "*************************************************************"
echo "2a. field name aliases using ="
$SHSQL "select id = snum, name = placename from nzplaces where id = 8252 " 2>&1

echo "*************************************************************"
echo "3. "
$SHSQL "select id, name from nzplaces where id = 3 " 2>&1

echo "*************************************************************"
echo "4. first entry in id.1 index"
$SHSQL "select id, name from nzplaces where id = 10 " 2>&1

echo "*************************************************************"
echo "4a. before first entry in id.1 index - no rows returned"
$SHSQL "select id, name from nzplaces where id = 0 " 2>&1

echo "*************************************************************"
echo "5. last entry in id.1 index"
$SHSQL "select id, name from nzplaces where id = 9999 " 2>&1

echo "*************************************************************"
echo "5a. beyond last entry in id.1 index - no rows returned"
$SHSQL "select id, name from nzplaces where id = 99999 " 2>&1

echo "*************************************************************"
echo "6. like w/ wildcard"
$SHSQL "select name from nzplaces where name like 'had*' order by name" 2>&1

echo "*************************************************************"
echo "7. like w/ entire entry"
$SHSQL "select name from nzplaces where name like 'halfway rock' order by name" 2>&1

echo "*************************************************************"
echo "8. like w/ long entry"
$SHSQL "select name from nzplaces where name like 'Pyke Big Bay Route' order by name" 2>&1

echo "*************************************************************"
echo "10. first entry in name.1 index"
$SHSQL "select id, name from nzplaces where name like '\"opua*' order by name, id" 2>&1

echo "*************************************************************"
echo "11. last entry in name.1 index"
$SHSQL "select id, name from nzplaces where name like 'zurbrig*' order by name, id" 2>&1

echo "*************************************************************"
echo "12. = null"
$SHSQL "select * from nzplaces where desc_code = null order by name, id" 2>&1

echo "*************************************************************"
echo "13. like null"
$SHSQL "select * from nzplaces where desc_code like null order by name, id" 2>&1

echo "*************************************************************"
echo "14. is null"
$SHSQL "select * from nzplaces where name like 'shelter*' and desc_code is null order by name, id" 2>&1

echo "*************************************************************"
echo "15. isnot null"
$SHSQL "select * from nzplaces where name like 'shelter*' and desc_code isnot null order by name, id" 2>&1

echo "*************************************************************"
echo "16. compound"
$SHSQL "select * from nzplaces where name like 'shelter*' and desc_code != 'PNT' order by name, id" 2>&1

echo "*************************************************************"
echo "17. !like"
$SHSQL "select * from nzplaces where name like 'shelter*' and name !like '*point' order by name, id" 2>&1

echo "*************************************************************"
echo "18. !like"
$SHSQL "select * from nzplaces where name like 'shelter*' and name !like '*point' order by name, id" 2>&1

echo "*************************************************************"
echo "19. !like null"
$SHSQL "select * from nzplaces where name like 'shelter*' and desc_code !like null order by name, id" 2>&1


echo "*************************************************************"
echo "20. in"
$SHSQL "select * from nzplaces where desc_code in 'UNIV,WELL' order by name, id" 2>&1

echo "*************************************************************"
echo "20a. in, w/ constant list having embedded spaces"
$SHSQL "select * from nzplaces where name in 'Pigeon Creek,Lost Valley Creek' order by name, id" 2>&1

echo "*************************************************************"
echo "21. in null"
$SHSQL "select * from nzplaces where desc_code in null order by name, id" 2>&1

echo "*************************************************************"
echo "22. in UNIV,null"
$SHSQL "select * from nzplaces where desc_code in 'UNIV,null' order by name, id" 2>&1

echo "*************************************************************"
echo "23. !in"
$SHSQL "select * from nzplaces where name like 'shelter*' and desc_code !in 'PNT,BAY' order by name, id" 2>&1

echo "*************************************************************"
echo "24. inlike"
# inlike
$SHSQL "select * from nzplaces where name inlike 'gru*,bub*' order by name, id" 2>&1

echo "*************************************************************"
echo "25. inlike UN*,null"
$SHSQL "select * from nzplaces where desc_code inlike 'UN*,null' order by name, id" 2>&1

echo "*************************************************************"
echo "26. !inlike"
$SHSQL "select * from nzplaces where name like 'shelter*' and name !inlike '*point,*cove'  order by name, id" 2>&1

echo "*************************************************************"
echo "27. !inlike UN*,null"
$SHSQL "select * from nzplaces where name like 'shelter*' and desc_code !inlike 'UN*,null'  order by name, id" 2>&1

echo "*************************************************************"
echo "30. inrange"
$SHSQL "select * from nzplaces where latitude inrange -44.34,-44.32 and longitude inrange 170.2,170.4 order by name, id" 2>&1

echo "*************************************************************"
echo "31. outrange"
$SHSQL "select * from nzplaces where latitude outrange -47.26,-34.15  order by name, id" 2>&1

echo "*************************************************************"
echo "32. < "
$SHSQL "select * from nzplaces where latitude < -47.26  order by name, id" 2>&1

echo "*************************************************************"
echo "33. > "
$SHSQL "select * from nzplaces where latitude > -34.15  order by name, id" 2>&1


echo "*************************************************************"
echo "34. null, numeric field"
$SHSQL "select * from nzplaces where latitude = null order by name, id" 2>&1

echo "*************************************************************"
echo "35. null, numeric field"
$SHSQL "select * from nzplaces where name like 'shelter*' and latitude is null order by name, id" 2>&1

echo "*************************************************************"
echo "36. isnot null, numeric field"
$SHSQL "select * from nzplaces where name like 'shelter*' and latitude isnot null order by name, id" 2>&1


echo "*************************************************************"
echo "40. no rows found"
$SHSQL "select * from nzplaces where name like 'hhh*'  order by name, id" 2>&1

echo "*************************************************************"
echo "41. no rows found"
$SHSQL "select * from nzplaces where name like 'hhh*' order by desc_code, name, id" 2>&1

echo "*************************************************************"
echo "42. no rows found"
$SHSQL "select * from nzplaces where name like 'hhh*' order by name, id limit 30, 50 " 2>&1

echo "*************************************************************"
echo "43. no rows found"
$SHSQL "select desc_code, count(*) from nzplaces group by desc_code where name like 'hhh*'" 2>&1


echo "*************************************************************"
echo "50. select distinct"
$SHSQL "select distinct name from nzplaces where name like 'had*' order by name, id" 2>&1

echo "*************************************************************"
echo "51. select distinct"
$SHSQL "select distinct name from nzplaces where name like 'halfway rock' order by name, id" 2>&1


# INLIKE, ORDER BY and LIMIT..

echo "*************************************************************"
echo "52. limit" 
$SHSQL "select * from nzplaces where name inlike 'jone*,joan*,john*' order by name, id limit 10" 2>&1


echo "*************************************************************"
echo "53. order by alpha, limit 10"
$SHSQL "select district, name from nzplaces where name inlike 'jone*,joan*,john*' order by district, name limit 10" 2>&1
  

echo "*************************************************************"
echo "54. order by alpha, descending"
$SHSQL "select district, name from nzplaces where name inlike 'jone*,joan*,john*' order by district descending, name limit 10" 2>&1
  
echo "*************************************************************"
echo "55. order by numeric"
$SHSQL "select name, longitude from nzplaces where name inlike 'jone*,joan*,john*' order by longitude num, name limit 10" 2>&1
  
echo "*************************************************************"
echo "56. order by numeric, descending"
  $SHSQL "select name, longitude from nzplaces where name inlike 'jone*,joan*,john*' order by longitude num descending, name limit 10" 2>&1
  
echo "*************************************************************"
echo "57. limit range"
$SHSQL "select name, longitude from nzplaces where name inlike 'jone*,joan*,john*' order by longitude num descending, name limit 20, 35" 2>&1


echo "*************************************************************"
echo "60. select count(*)"
$SHSQL "select count(*) from nzplaces where name like 'pige*'" 2>&1


echo "*************************************************************"
echo "62. group by"
$SHSQL "select desc_code, count(*) from nzplaces group by desc_code where name like 'pige*' " 2>&1


echo "*************************************************************"
echo "63. aggregate functions"
$SHSQL "select desc_code, 
	'count=', count(*), 
	'sum=', sum( latitude ), 
	'min=', min( latitude ), 
	'max=', max( latitude ), 
	'avg=', avg( latitude ) 
	from nzplaces group by desc_code where name like 'pige*' " 2>&1



echo "*************************************************************"
echo "70. select into tmp table"
$SHSQL -ttprefix selects1 "select * into \$tmp1 from nzplaces where latitude inrange -44.34,-44.32 
	and longitude inrange 170.2,170.4 order by name, id" 2>&1

echo "*************************************************************"
echo "71. retrieve from the tmp table"
$SHSQL -ttprefix selects1 "select * from \$tmp1 where name like 'tek*'"

echo "*************************************************************"
echo "72. select into ordinary file"
$SHSQL "select * into /tmp/shsql_tmp1 from nzplaces where latitude inrange -44.34,-44.32 
	and longitude inrange 170.2,170.4 order by name, id" 2>&1

echo "*************************************************************"
echo "73. select from ordinary file"
$SHSQL "select * from /tmp/shsql_tmp1 order by latitude num descending , name, id" 2>&1


