Commit 28d10d6f authored by Oogie McGuire's avatar Oogie McGuire

Initial set of clean queries

First cut at uploading text files of my clean queries. 
parent fa9b3578
-- Get Gestation lengths for all breedings
SELECT
lambing_historyid
, dam_table.sheep_name as dam
, sire_table.sheep_name as sire
, breeding_record_table.date_ram_in
, lambing_date
, julianday(lambing_date) - julianday(breeding_record_table.date_ram_in) AS gestation_length
, lambing_history_table.id_breedingid
FROM lambing_history_table
INNER JOIN sheep_table AS sire_table ON lambing_history_table.sire_id = sire_table.sheep_id
INNER JOIN sheep_table AS dam_table ON lambing_history_table.dam_id = dam_table.sheep_id
INNER JOIN breeding_record_table ON lambing_history_table.id_breedingid = breeding_record_table.id_breedingid
ORDER BY
lambing_date
SELECT
sheep_breeding_table.ewe_id
, ewe_table.sheep_name AS ewe_name
, sheep_breeding_table.breeding_id
, breeding_record_table.ram_id
, ram_table.sheep_name AS ram_name
, breeding_record_table.date_ram_in
, breeding_record_table.time_ram_in
, breeding_record_table.date_ram_out
, breeding_record_table.time_ram_out
, breeding_record_table.service_type
, service_type_table.service_type
FROM breeding_record_table
LEFT OUTER JOIN sheep_breeding_table ON sheep_breeding_table.breeding_id = breeding_record_table.id_breedingid
INNER JOIN sheep_table as ram_table ON breeding_record_table.ram_id = ram_table.sheep_id
INNER JOIN sheep_table as ewe_table ON sheep_breeding_table.ewe_id = ewe_table.sheep_id
INNER JOIN service_type_table ON service_type_table.id_servicetypeid = breeding_record_table.service_type
WHERE
(breeding_record_table.service_type = 3
OR breeding_record_table.service_type = 5)
AND date_ram_in LIKE "2016%"
ORDER BY
ewe_name
, ram_name
-- Another version with sire and dam ages and shows breeding type
SELECT
sheep_breeding_table.ewe_id
, ewe_table.sheep_name AS ewe_name
, ewe_table.birth_date AS ewe_birth
, date_ram_in - ewe_table.birth_date + 1 AS ewe_age_at_lambing
, sheep_breeding_table.breeding_id
, breeding_record_table.ram_id
, ram_table.sheep_name AS ram_name
, ram_table.birth_date AS ram_birth
, date_ram_in - ram_table.birth_date AS ram_age
, breeding_record_table.date_ram_in
, breeding_record_table.time_ram_in
, breeding_record_table.date_ram_out
, breeding_record_table.time_ram_out
, breeding_record_table.service_type
, service_type_table.service_type
FROM breeding_record_table
LEFT OUTER JOIN sheep_breeding_table ON sheep_breeding_table.breeding_id = breeding_record_table.id_breedingid
INNER JOIN sheep_table as ram_table ON breeding_record_table.ram_id = ram_table.sheep_id
INNER JOIN sheep_table as ewe_table ON sheep_breeding_table.ewe_id = ewe_table.sheep_id
INNER JOIN service_type_table ON service_type_table.id_servicetypeid = breeding_record_table.service_type
WHERE
(breeding_record_table.service_type = 3
OR breeding_record_table.service_type = 5)
AND date_ram_in LIKE "2016%"
ORDER BY
ewe_name
, ram_name
-- Gets the current year lambs with the latest weight data
SELECT
sheep_table.sheep_id
, sheep_table.nsip_id as ID
, sheep_table.sex as SEX
, sheep_table.birth_date as DOB
, sire_table.nsip_id as SIRE
, dam_table.nsip_id as DAM
, custom_evaluation_traits_table.custom_evaluation_order as LAMBEASE
, sheep_table.birth_type as BT
, sheep_table.rear_type as RT
, sheep_table.birth_weight as BWT
, sheep_table.sheep_name as COMMENT
, sheep_evaluation_table.eval_date
, sheep_evaluation_table.age_in_days
, sheep_evaluation_table.trait_score11
FROM sheep_table
INNER JOIN sheep_table AS sire_table ON sheep_table.sire_id = sire_table.sheep_id
INNER JOIN sheep_table AS dam_table ON sheep_table.dam_id = dam_table.sheep_id
INNER JOIN custom_evaluation_traits_table ON sheep_table.lambease = custom_evaluation_traits_table.id_custom_traitid
INNER JOIN sheep_evaluation_table ON sheep_table.sheep_id = sheep_evaluation_table.sheep_id
WHERE
sheep_evaluation_table.eval_date > "2017-06%"
AND
(sheep_evaluation_table.trait_name11 = 16 OR
sheep_evaluation_table.trait_name12 = 16 OR
sheep_evaluation_table.trait_name13 = 16 OR
sheep_evaluation_table.trait_name14 = 16 OR
sheep_evaluation_table.trait_name15 = 16 )
AND
(sheep_table.remove_date IS NULL OR sheep_table.remove_date is '')
ORDER BY
sheep_table.nsip_id asc
-- Gets the NSIP data to add to the spreadsheet to send to LambPlan for new lambs
SELECT sheep_table.sheep_id
, sheep_table.nsip_id as ID
, sheep_table.sex
, sheep_table.birth_date as DOB
, sire_table.nsip_id as SIRE
, sire_table.sheep_name as sire_name
, dam_table.nsip_id as DAM
, dam_table.sheep_name as dam_name
, custom_evaluation_traits_table.custom_evaluation_order as lambease
, sheep_table.birth_type as BT
, sheep_table.rear_type as RT
,sheep_table.birth_weight as BWT
, sheep_table.sheep_name as COMMENT
FROM sheep_table
LEFT JOIN sheep_table as sire_table ON sheep_table.sire_id = sire_table.sheep_id
LEFT JOIN custom_evaluation_traits_table ON sheep_table.lambease = custom_evaluation_traits_table.id_custom_traitid
LEFT JOIN sheep_table as dam_table ON sheep_table.dam_id = dam_table.sheep_id
LEFT JOIN sheep_registration_table ON sheep_table.sheep_id = sheep_registration_table.sheep_id
WHERE
(sheep_registration_table.registration_contact = 25 OR sheep_registration_table.registration_contact = '')
-- Change the date to be the current year to add new lambs
AND sheep_table.birth_date > "2017%"
ORDER BY
sheep_table.nsip_id ASC
\ No newline at end of file
-- Get the count of current alive sheep that should be in each location
-- An inventory by pen or pasture
SELECT
location_table.location_name
, count (*)
FROM sheep_table
LEFT JOIN location_table ON sheep_table.id_locationid = location_table.id_locationid
WHERE
(sheep_table.remove_date IS NULL or sheep_table.remove_date is '')
GROUP BY location_table.location_name
\ No newline at end of file
-- Gets a table of all the rams and their breeding info for a particular season
SELECT
breeding_record_table.id_breedingid
, sheep_table.sheep_id
, sheep_table.sheep_name
, breeding_record_table.date_ram_in
, breeding_record_table.time_ram_in
, service_type_table.service_type
FROM sheep_table
LEFT OUTER JOIN breeding_record_table ON sheep_table.sheep_id = breeding_record_table.ram_id
INNER JOIN service_type_table ON breeding_record_table.service_type = service_type_table.id_servicetypeid
WHERE
breeding_record_table.date_ram_in > "2016-10%"
ORDER BY
breeding_record_table.date_ram_in
, sheep_table.sheep_name
-- Get all the carcass weights for all sheep
-- Includes sort by sex
SELECT
sheep_table.sheep_id
, sheep_table.sheep_name
, sheep_sex_table.sex_abbrev
, sheep_table.birth_date
, sheep_table.death_date
, sheep_evaluation_table.age_in_days, age_in_days/365 AS years
, sheep_evaluation_table.trait_score11 AS carcass_weight
, units_table.units_name
FROM sheep_evaluation_table
INNER JOIN sheep_table ON sheep_evaluation_table.sheep_id = sheep_table.sheep_id
INNER JOIN units_table ON sheep_evaluation_table.trait_units11 = units_table.id_unitsid
INNER JOIN sheep_sex_table ON sheep_table.sex = sheep_sex_table.sex_sheepid
WHERE
trait_name11 = 27
ORDER BY
years
, sheep_sex_table.sex_abbrev
, sheep_table.birth_date ASC
-- This version gets carcass weights for sheep removed on a specific date or year
-- Get on a specific year/month of slaughter
SELECT
sheep_table.sheep_id
, sheep_table.sheep_name
, sheep_sex_table.sex_abbrev
, sheep_table.birth_date
, sheep_table.death_date
, sheep_evaluation_table.age_in_days, age_in_days/365 AS years
, sheep_evaluation_table.trait_score11 as carcass_weight
, units_table.units_name
FROM sheep_evaluation_table
INNER JOIN sheep_table ON sheep_evaluation_table.sheep_id = sheep_table.sheep_id
INNER JOIN units_table ON sheep_evaluation_table.trait_units11 = units_table.id_unitsid
INNER JOIN sheep_sex_table ON sheep_table.sex = sheep_sex_table.sex_sheepid
WHERE
trait_name11 = 27
AND sheep_table.remove_date LIKE "2017-06%"
ORDER BY
years
, sheep_sex_table.sex_abbrev
, sheep_table.birth_date ASC
SELECT
sheep_table.sheep_name
, sheep_table.birth_date
, codon171_table.codon171_alleles
, sheep_sex_table.sex_name
FROM sheep_table
LEFT JOIN codon171_table ON sheep_table.codon171 = codon171_table.id_codon171id
INNER JOIN sheep_sex_table ON sheep_sex_table.sex_sheepid = sheep_table.sex
WHERE
sheep_table.birth_date LIKE "2016%"
ORDER BY
sheep_sex_table.sex_name
, sheep_table.birth_date ASC
-- Gets all drugs given on a specific date with the sheep name, drug ID number,
-- drug task and bpth date and time given.
SELECT
sheep_drug_table.id_sheepdrugid
, sheep_table.sheep_id
, sheep_table.sheep_name
, sheep_drug_table. drug_id
, drug_table.user_task_name
, sheep_drug_table.drug_date_on
, sheep_drug_table.drug_time_on
FROM sheep_table
INNER JOIN sheep_drug_table ON sheep_table.sheep_id = sheep_drug_table.sheep_id
INNER JOIN drug_table ON sheep_drug_table. drug_id = drug_table.id_drugid
WHERE
sheep_drug_table.drug_date_on LIKE "2018-07-20%"
-- Uncomment this to get a specific drug using the proper drug ID number
-- AND sheep_drug_table.drug_id = 56
ORDER BY
sheep_drug_table.drug_time_on ASC
SELECT
sheep_table.sheep_id
, sheep_table.sheep_name
, drug_table.user_task_name
, sheep_drug_table.drug_date_on
, sheep_drug_table.drug_time_on
, sheep_table.alert01
FROM sheep_table
INNER JOIN sheep_drug_table ON sheep_table.sheep_id = sheep_drug_table.sheep_id
INNER JOIN drug_table ON sheep_drug_table.drug_id = drug_table.id_drugid
WHERE
sheep_table.sheep_id = 214
ORDER BY
sheep_drug_table.drug_time_on ASC
SELECT
sheep_table.sheep_name
, sheep_table.nsip_id
, sheep_table.birth_date
, sheep_evaluation_table.age_in_days
, sheep_evaluation_table.id_evaluationid
, sheep_evaluation_table.eval_date
, (
sheep_table.birth_type
+ sheep_table.codon171
+ sheep_evaluation_table.trait_score01
+ sheep_evaluation_table.trait_score02
+ sheep_evaluation_table.trait_score03
+ sheep_evaluation_table.trait_score04
+ sheep_evaluation_table.trait_score05
+ sheep_evaluation_table.trait_score06
+ sheep_evaluation_table.trait_score07
+ sheep_evaluation_table.trait_score08
+ sheep_evaluation_table.trait_score09
+ sheep_evaluation_table.trait_score10
) AS overall_score
, sheep_evaluation_table.trait_score11
FROM sheep_evaluation_table
INNER JOIN sheep_table ON sheep_evaluation_table.sheep_id = sheep_table.sheep_id
WHERE
eval_date LIKE "2017-04-23"
ORDER BY
nsip_id
, overall_score DESC
, sheep_evaluation_table.id_evaluationid ASC
\ No newline at end of file
SELECT
-- Uncomment this to get the ewes name in the beginning
-- sheep_table.sheep_name
-- ,
*
FROM lambing_history_table
-- Uncomment this if you want to restrict by service types
-- JOIN breeding_record_table ON lambing_history_table.id_breedingid = breeding_record_table.id_breedingid
-- Uncomment this if you want to see the ewe name as well
-- INNER JOIN sheep_table on lambing_history_table.dam_id = sheep_table.sheep_id
WHERE
gestation_length >140
AND gestation_length < 162
-- Uncomment this to restrict to a specific year
-- AND lambing_date > "2016%"
-- Uncomment this to get all AI breedings. Natural service is type 1. or set to = 1 for natural service only
-- AND breeding_record_table.service_type > 1
ORDER BY
lambing_date
SELECT
sheep_note_table.id_noteid
, sheep_table.sheep_id
, sheep_table.sheep_name
, sheep_note_table.note_date
, sheep_note_table.note_time
, sheep_note_table.note_text
, predefined_notes_table.predefined_note_text
FROM sheep_table
JOIN sheep_note_table ON sheep_table.sheep_id = sheep_note_table.sheep_id
LEFT OUTER JOIN predefined_notes_table ON
sheep_note_table.id_predefinednotesid01 = predefined_notes_table.id_predefinednotesid
WHERE
sheep_note_table.sheep_id = 1
ORDER BY
sheep_note_table.note_date
, sheep_note_table.note_time
, sheep_table.sheep_name ASC
SELECT
sheep_table.sheep_id
, (SELECT tag_number FROM id_info_table WHERE official_id = "1" AND id_info_table.sheep_id = sheep_table.sheep_id AND (tag_date_off IS NULL or tag_date_off = '')) AS fedtag
, (SELECT tag_number FROM id_info_table WHERE tag_type = "4" AND id_info_table.sheep_id = sheep_table.sheep_id AND tag_color_male = 15 AND (tag_date_off IS NULL or tag_date_off = '')) AS farmtag
, (SELECT tag_number FROM id_info_table WHERE tag_type = "2" AND id_info_table.sheep_id = sheep_table.sheep_id AND tag_date_off IS NULL and id_info_table.official_id is NULL) AS eidtag
, (SELECT tag_number FROM id_info_table WHERE tag_type = "3" AND id_info_table.sheep_id = sheep_table.sheep_id AND (tag_date_off IS NULL or tag_date_off = '')) AS painttag
, sheep_table.sheep_name
FROM sheep_table
WHERE
painttag IS NOT null
ORDER BY
painttag ASC
SELECT
sheep_table.sheep_id
, sheep_table.sheep_name
, sheep_table.birth_date
, sheep_sex_table.sex_abbrev
, birth_type_table.birth_type
, custom_evaluation_traits_table.custom_evaluation_item
, sire_table.sheep_name AS sire_name
, dam_table.sheep_name AS dam_name
FROM sheep_table
LEFT JOIN birth_type_table ON sheep_table.birth_type = birth_type_table.id_birthtypeid
LEFT JOIN sheep_sex_table ON sheep_table.sex = sheep_sex_table.sex_sheepid
LEFT JOIN sheep_table AS sire_table ON sheep_table.sire_id = sire_table.sheep_id
LEFT JOIN sheep_table AS dam_table ON sheep_table.dam_id = dam_table.sheep_id
INNER JOIN custom_evaluation_traits_table ON sheep_table.lambease = custom_evaluation_traits_table.id_custom_traitid
WHERE
(sheep_table.remove_date IS NULL OR sheep_table.remove_date IS '' )
AND (sheep_table.sire_id IS NOT NULL OR sheep_table.dam_id IS NOT NULL)
AND (sheep_table.lambease = 10 OR sheep_table.lambease= 11 OR sheep_table.lambease= 12 OR sheep_table.lambease= 13)
AND sheep_table.birth_date LIKE "2014%"
ORDER BY
sheep_table.birth_date ASC
, dam_name
-- Find ewes who had problems with at least 1 lamb this year
SELECT DISTINCT
dam_table.sheep_id
, dam_table.sheep_name AS dam_name
FROM sheep_table
LEFT JOIN sheep_table AS dam_table ON sheep_table.dam_id = dam_table.sheep_id
INNER JOIN custom_evaluation_traits_table ON sheep_table.lambease = custom_evaluation_traits_table.id_custom_traitid
WHERE
(sheep_table.lambease= 10 OR sheep_table.lambease= 11 OR sheep_table.lambease= 12 OR sheep_table.lambease= 13)
AND sheep_table.birth_date LIKE "2016%"
ORDER BY
dam_name
SELECT
breeding_record_table.id_breedingid
, breeding_record_table.ram_id
, sheep_table.sheep_name
, breeding_record_table.date_ram_in
, breeding_record_table.time_ram_in
, breeding_record_table.date_ram_out
, breeding_record_table.time_ram_out
, breeding_record_table.service_type
, service_type_table.service_type
FROM breeding_record_table
INNER JOIN sheep_table ON breeding_record_table.ram_id = sheep_table.sheep_id
INNER JOIN service_type_table ON service_type_table.id_servicetypeid = breeding_record_table.service_type
WHERE
breeding_record_table.date_ram_in LIKE "2016-12%"
-- Get weaning weights for adding to the registry when the required weaning weight is at 50 days
SELECT
sheep_table.sheep_id
, sheep_table.nsip_id AS ID
, sheep_table.sheep_name AS name
, sheep_sex_table.sex_abbrev AS SEX
, sheep_table.birth_date AS DOB
, sire_table.sheep_name AS SIRE
, dam_table.sheep_name AS DAM
, sheep_table.birth_weight AS BWT
, sheep_evaluation_table.age_in_days
, sheep_evaluation_table.eval_date
-- Adjust what trait score you are using to be the one where the weaning weights were taken
-- Get the Average Daily Gain rounded to 2 decimal places
, ROUND (((sheep_evaluation_table.trait_score11-sheep_table.birth_weight)/sheep_evaluation_table.age_in_days) , 2) AS ADG
, ROUND (((((sheep_evaluation_table.trait_score11-sheep_table.birth_weight)/sheep_evaluation_table.age_in_days) * 50) + sheep_table.birth_weight ),2) AS WEIGHT50DAY
, sheep_evaluation_table.trait_score11
FROM sheep_table
INNER JOIN sheep_table AS sire_table ON sheep_table.sire_id = sire_table.sheep_id
INNER JOIN sheep_table AS dam_table ON sheep_table.dam_id = dam_table.sheep_id
INNER JOIN custom_evaluation_traits_table on sheep_table.lambease = custom_evaluation_traits_table.id_custom_traitid
INNER JOIN sheep_evaluation_table on sheep_table.sheep_id = sheep_evaluation_table.sheep_id
LEFT JOIN sheep_sex_table ON sheep_table.sex = sheep_sex_table.sex_sheepid
WHERE
-- Set this to be the range that includes the evaluation dates for the lambs for this year
sheep_evaluation_table.eval_date > "2017-07%"
AND sheep_evaluation_table.eval_date < "2017-09%"
-- Get only the records that record a weight in the trait_name11
-- This will need to match whatever location you used for the weights
AND sheep_evaluation_table.trait_name11 = 16
-- Add this if you want only current alive sheep
AND (sheep_table.remove_date IS NULL OR sheep_table.remove_date IS '')
-- Or use this to sort to a specific year of birth
AND (sheep_table.birth_date > "2017%" AND sheep_table.birth_date < "2018%" )
ORDER BY
-- Can add other sorting as desired. Watch out for having proper , in the query or it won't run
-- sheep_table.nsip_id
-- , sheep_table.birth_date
-- , adg desc
-- , sire
-- ,
sheep_table.sheep_name
-- Gets whole list of current sheep properly including sheep without a sire or dam or without EBVs
SELECT
sheep_table.sheep_id
, (SELECT
tag_number
FROM id_info_table
WHERE
official_id = "1"
AND id_info_table.sheep_id = sheep_table.sheep_id
AND (tag_date_off IS NULL OR tag_date_off = ''))
AS fedtag
, (SELECT
tag_number
FROM id_info_table
WHERE
tag_type = "4"
AND id_info_table.sheep_id = sheep_table.sheep_id
AND (tag_date_off IS NULL OR tag_date_off = ''))
AS farmtag
,(SELECT
tag_number
FROM id_info_table
WHERE
tag_type = "2"
AND id_info_table.sheep_id = sheep_table.sheep_id
AND (tag_date_off IS NULL OR tag_date_off = '')
AND ( id_info_table.official_id IS NULL OR id_info_table.official_id = 0 ))
AS eidtag
, sheep_table.sheep_name
, codon171_table.codon171_alleles
, sheep_ebv_table.usa_maternal_index
, sheep_ebv_table.self_replacing_carcass_index
, cluster_table.cluster_name
, sheep_table.birth_date
, sheep_sex_table.sex_abbrev
, birth_type_table.birth_type
, sire_table.sheep_name as sire_name
, dam_table.sheep_name as dam_name
, sheep_table.alert01
FROM sheep_table
INNER JOIN codon171_table ON sheep_table.codon171 = codon171_table.id_codon171id
LEFT JOIN birth_type_table ON sheep_table.birth_type = birth_type_table.id_birthtypeid
LEFT JOIN sheep_sex_table ON sheep_table.sex = sheep_sex_table.sex_sheepid
LEFT JOIN sheep_table AS sire_table ON sheep_table.sire_id = sire_table.sheep_id
LEFT JOIN sheep_table AS dam_table ON sheep_table.dam_id = dam_table.sheep_id
LEFT OUTER JOIN sheep_cluster_table ON sheep_table.sheep_id = sheep_cluster_table.sheep_id
LEFT JOIN cluster_table ON cluster_table.id_clusternameid = sheep_cluster_table.which_cluster
-- Edit the date to be the most recent run of EBV data by changing the date
LEFT OUTER JOIN sheep_ebv_table ON sheep_table.sheep_id = sheep_ebv_table.sheep_id
-- Modify this to be the latest EBV run date. Should be a way to get this automatically but I haven't figured it out.
AND sheep_ebv_table.ebv_date LIKE "2018-07%"
WHERE
(sheep_table.remove_date IS NULL
OR sheep_table.remove_date IS '')
-- To get only a single sex add this in the WHERE clause
-- Edit for sex by making Ram sex = 1 Ewe Sex = 2 Wether sex = 3
-- AND sheep_table.sex = 1
-- To get no butcher or sell add this in the WHERE clause
-- AND (sheep_table.alert01 NOT LIKE "%Sell%"
-- AND sheep_table.alert01 NOT LIKE "%Butcher%")
-- To get only a specific alert add this in the WHERE clause
-- AND sheep_table.alert01 LIKE "%Sold%"
-- To get only older sheep not current year lambs add this in the WHERE clause changing the year as required
-- To get only this years lambs change the < to a >
-- AND sheep_table.birth_date < "2018%"
ORDER BY
sheep_sex_table.sex_abbrev ASC
, cluster_table.cluster_name
, sheep_ebv_table.self_replacing_carcass_index DESC
, sheep_table.birth_date ASC
SELECT
COUNT (*) FROM
(SELECT
sheep_table.sheep_id
, (SELECT
id_scrapie_flock_table.scrapie_flockid
FROM id_info_table
INNER JOIN id_scrapie_flock_table ON id_scrapie_flock_table.id_flockid = id_info_table.id_flockid
WHERE
official_id = "1"
AND id_info_table.sheep_id = sheep_table.sheep_id
AND (tag_date_off IS NULL or tag_date_off = ''))
AS scrapieflock
, (SELECT
tag_number
FROM id_info_table
WHERE
official_id = "1"
AND id_info_table.sheep_id = sheep_table.sheep_id
AND (tag_date_off IS NULL or tag_date_off = ''))
AS fedtag
, (SELECT
tag_number
FROM id_info_table
WHERE
tag_type = "4"
AND id_info_table.sheep_id = sheep_table.sheep_id
AND (tag_date_off IS NULL or tag_date_off = ''))
AS farmtag
, (SELECT
tag_number
FROM id_info_table
WHERE
tag_type = "2"
AND id_info_table.sheep_id = sheep_table.sheep_id
AND (tag_date_off IS NULL or tag_date_off = '')
AND ( id_info_table.official_id is NULL or id_info_table.official_id = 0 ))
AS eidtag
, flock_prefix_table.flock_name
, sheep_table.sheep_name
, codon171_table.codon171_alleles
, sheep_table.birth_date
, sheep_sex_table.sex_abbrev
, birth_type_table.birth_type
, location_table.location_name
FROM sheep_table
INNER JOIN codon171_table ON sheep_table.codon171 = codon171_table.id_codon171id
INNER JOIN flock_prefix_table ON sheep_table.flock_prefix = flock_prefix_table.flock_prefixid
LEFT JOIN birth_type_table ON sheep_table.birth_type = birth_type_table.id_birthtypeid
LEFT JOIN sheep_sex_table ON sheep_table.sex = sheep_sex_table.sex_sheepid
LEFT JOIN location_table ON sheep_table.id_locationid = location_table.id_locationid
WHERE
(sheep_table.remove_date IS NULL or sheep_table.remove_date is '')
-- Edit this to be the appropriate year to get either older or younger sheep.
-- Change the year to be the year of the current lambs
-- Change the condition to be < for older sheep and > for the lambs
AND sheep_table.birth_date < "2017%"
-- Edit this to be the proper sex 1 for rams, 2 for ewes and 3 for wethers
AND sheep_table.sex = 2
ORDER BY
sheep_sex_table.sex_abbrev asc
, sheep_table.birth_date asc
)
-- Get the Report of Acquired sheep for filing with the Federal veterinarian for the yearly scrapie flock inspection
-- Includes the Federal Flock ID number as well as individual tag numbers
SELECT
sheep_table.sheep_id
, (SELECT
id_scrapie_flock_table.scrapie_flockid
FROM id_info_table
INNER JOIN id_scrapie_flock_table ON id_scrapie_flock_table.id_flockid = id_info_table.id_flockid
WHERE
official_id = "1"
AND id_info_table.sheep_id = sheep_table.sheep_id
AND (tag_date_off IS NULL or tag_date_off = ''))
AS scrapieflock
, (SELECT
tag_number
FROM id_info_table
WHERE
official_id = "1"
AND id_info_table.sheep_id = sheep_table.sheep_id
AND (tag_date_off IS NULL or tag_date_off = ''))
AS fedtag
, (SELECT
tag_number
FROM id_info_table
WHERE
tag_type = "4"
AND id_info_table.sheep_id = sheep_table.sheep_id
AND (tag_date_off IS NULL or tag_date_off = ''))
AS farmtag
, (SELECT
tag_number
FROM id_info_table
WHERE
tag_type = "2"
AND id_info_table.sheep_id = sheep_table.sheep_id
AND (tag_date_off IS NULL or tag_date_off = '')
AND ( id_info_table.official_id is NULL or id_info_table.official_id = 0 ))
AS eidtag
, flock_prefix_table.flock_name
, sheep_table.sheep_name
, acquire_date
, acquire_reason_table.acquire_reason
, contacts_table.contact_first_name AS breeder_first_name
, contacts_table.contact_last_name AS breeder_last_name
FROM sheep_table
INNER JOIN flock_prefix_table ON sheep_table.flock_prefix = flock_prefix_table.flock_prefixid
LEFT JOIN acquire_reason_table ON sheep_table.acquire_reason = acquire_reason_table.acquire_reasonid
LEFT JOIN contacts_table ON sheep_table.id_breederid = contacts_table.id_contactsid
WHERE
-- Edit this to be the year of the report for set to the previous year date
acquire_date > "2016%"
ORDER BY
acquire_reason_table.acquire_reason
, acquire_date
, sheep_name asc
-- Get the Report of Removed sheep for filing with the Federal veterinarian for the yearly scrapie flock inspection
-- Includes the Federal Flock ID number as well as individual tag numbers
SELECT
sheep_table.sheep_id
, (SELECT
id_scrapie_flock_table.scrapie_flockid
FROM id_info_table
INNER JOIN id_scrapie_flock_table ON id_scrapie_flock_table.id_flockid = id_info_table.id_flockid
WHERE
official_id = "1"
AND id_info_table.sheep_id = sheep_table.sheep_id
AND (tag_date_off IS NULL or tag_date_off = ''))
AS scrapieflock
, (SELECT
tag_number
FROM id_info_table
WHERE
official_id = "1"
AND id_info_table.sheep_id = sheep_table.sheep_id
AND (tag_date_off IS NULL or tag_date_off = ''))
AS fedtag
, (SELECT
tag_number
FROM id_info_table
WHERE
tag_type = "4"
AND id_info_table.sheep_id = sheep_table.sheep_id
AND (tag_date_off IS NULL or tag_date_off = ''))
AS farmtag
, (SELECT
tag_number
FROM id_info_table
WHERE
tag_type = "2"
AND id_info_table.sheep_id = sheep_table.sheep_id
AND (tag_date_off IS NULL or tag_date_off = '')
AND ( id_info_table.official_id is NULL or id_info_table.official_id = 0 ))
AS eidtag
, flock_prefix_table.flock_name
, sheep_table.sheep_name
, remove_date
, remove_reason_table.remove_reason
, contacts_table.contact_first_name AS owner_first_name
, contacts_table.contact_last_name AS owner_last_name
FROM sheep_table
INNER JOIN flock_prefix_table ON sheep_table.flock_prefix = flock_prefix_table.flock_prefixid
LEFT JOIN remove_reason_table on sheep_table.remove_reason = remove_reason_table.remove_reasonid
LEFT JOIN contacts_table ON sheep_table.id_ownerid = contacts_table.id_contactsid
WHERE
-- Edit this to be the year of the report for set to the previous year date
remove_date > "2017%"
-- Edit this to be the date of the scrapie flock inspection
-- AND remove_date < "2017-03-31"
ORDER BY
remove_reason_table.remove_reason
, remove_date
, sheep_name asc
-- Get the Report for filing with the Federal veterinarian for the yearly scrapie flock inspection
-- Includes the Federal Flock ID number as well as individual tag numbers
SELECT
sheep_table.sheep_id
, (SELECT
id_scrapie_flock_table.scrapie_flockid
FROM id_info_table
INNER JOIN id_scrapie_flock_table ON id_scrapie_flock_table.id_flockid = id_info_table.id_flockid
WHERE
official_id = "1"
AND id_info_table.sheep_id = sheep_table.sheep_id
AND (tag_date_off IS NULL or tag_date_off = ''))
AS scrapieflock
, (SELECT
tag_number
FROM id_info_table
WHERE
official_id = "1"
AND id_info_table.sheep_id = sheep_table.sheep_id
AND (tag_date_off IS NULL or tag_date_off = ''))
AS fedtag
, (SELECT
tag_number
FROM id_info_table
WHERE
tag_type = "4"
AND id_info_table.sheep_id = sheep_table.sheep_id
AND (tag_date_off IS NULL or tag_date_off = ''))
AS farmtag
, (SELECT
tag_number
FROM id_info_table
WHERE
tag_type = "2"
AND id_info_table.sheep_id = sheep_table.sheep_id
AND (tag_date_off IS NULL or tag_date_off = '')
AND ( id_info_table.official_id is NULL or id_info_table.official_id = 0 ))
AS eidtag