SET @SerialNumber = 105280826;
SET @SerialNumberEms = 105776982;
SET @SerialNumberAnonymous = 105713103;

SELECT
	pdb_el.geraete_daten.seriennummer AS serial_number
	,CAST(pdb_el.geraete_daten.produktionsauftrag AS INT) AS production_order
	,(CASE WHEN pdb_el.geraete_daten.sn_lp1 = 0 THEN NULL ELSE pdb_el.geraete_daten.sn_lp1 END) AS sn_pcb1
	,(CASE WHEN pdb_el.geraete_daten.sn_lp2 = 0 THEN NULL ELSE pdb_el.geraete_daten.sn_lp2 END) AS sn_pcb2
	,(CASE WHEN pdb_el.geraete_daten.sn_lp3 = 0 THEN NULL ELSE pdb_el.geraete_daten.sn_lp3 END) AS sn_pcb3
	,(CASE WHEN pdb_el.geraete_daten.sn_lp4 = 0 THEN NULL ELSE pdb_el.geraete_daten.sn_lp4 END) AS sn_pcb4
	,(CASE WHEN pdb_el.geraete_daten.sn_lp5 = 0 THEN NULL ELSE pdb_el.geraete_daten.sn_lp5 END) AS sn_pcb5
	,(CASE WHEN pdb_el.geraete_daten.sn_lp6 = 0 THEN NULL ELSE pdb_el.geraete_daten.sn_lp6 END) AS sn_pcb6
	,(CASE WHEN pdb_el.geraete_daten.sn_lp7 = 0 THEN NULL ELSE pdb_el.geraete_daten.sn_lp7 END) AS sn_pcb7
	,(CASE WHEN pdb_el.geraete_daten.sn_lp8 = 0 THEN NULL ELSE pdb_el.geraete_daten.sn_lp8 END) AS sn_pcb8
	,(CASE WHEN pdb_el.geraete_daten.sn_lp9 = 0 THEN NULL ELSE pdb_el.geraete_daten.sn_lp9 END) AS sn_pcb9
	,(CASE WHEN pdb_el.geraete_daten.sn_lp10 = 0 THEN NULL ELSE pdb_el.geraete_daten.sn_lp10 END) AS sn_pcb10
	,(CASE WHEN pdb_el.geraete_daten.ERev_geplant = "" THEN NULL ELSE pdb_el.geraete_daten.ERev_geplant END) AS er_planned
	,(CASE WHEN pdb_el.geraete_daten.ERev_gefertigt = "" THEN NULL ELSE pdb_el.geraete_daten.ERev_gefertigt END) AS er_produced
	,(CASE WHEN ISNULL(pdb_el.geraete_daten.ausliefer_datum) THEN FALSE ELSE TRUE END) AS is_rework
	,pdb_el.auftraege.artikelcode AS article_code
	,(CASE WHEN ISNULL(pdb_el.anonyme_cg_geraete.C_Nummer) THEN FALSE ELSE TRUE END) AS is_anonymous
	,(CASE WHEN ISNULL(pdb_el.anonyme_cg_geraete.Artikelcode) THEN (CASE WHEN pdb_el.auftraege.C_Nummer = "" THEN NULL ELSE pdb_el.auftraege.C_Nummer END) ELSE pdb_el.anonyme_cg_geraete.C_Nummer END) AS cg_number
	,(CASE WHEN ISNULL(pdb_el.ems.Seriennummer) THEN pdb_el.auftraege.IX ELSE pdb_el.ems.Equi_index END) AS bom_index
	,(CASE WHEN ISNULL(pdb_el.ems.Seriennummer) THEN FALSE ELSE TRUE END) AS has_ems_update
	,FALSE AS is_test_converter
	,pdb_el.geraete_daten.montagedatum AS initial_test_date
	,(CASE WHEN pdb_el.geraete_daten.ergebnis_erstinbetriebnahme = "PASS" THEN TRUE ELSE FALSE END) AS intial_test_result
	,pdb_el.geraete_daten.hv_test_datum AS hv_test_date
	,(CASE WHEN pdb_el.geraete_daten.hv_test_ergebnis = "PASS" THEN TRUE ELSE FALSE END) AS hv_test_result
	,pdb_el.geraete_daten.temp_test_datum AS temperature_test_date
	,(CASE WHEN pdb_el.geraete_daten.temp_test_ergebnis = "PASS" THEN TRUE ELSE FALSE END) AS temperature_test_result
	,pdb_el.geraete_daten.kalibrierung_datum AS calibration_date
	,(CASE WHEN pdb_el.geraete_daten.kalibrierung_ergebnis = "PASS" THEN TRUE ELSE FALSE END) AS calibration_result
	,pdb_el.geraete_daten.ausliefer_datum AS delivery_date
FROM pdb_el.geraete_daten
JOIN pdb_el.auftraege ON pdb_el.geraete_daten.produktionsauftrag = pdb_el.auftraege.produktionsauftrag
LEFT JOIN pdb_el.anonyme_cg_geraete ON pdb_el.auftraege.artikelcode = pdb_el.anonyme_cg_geraete.Artikelcode
LEFT JOIN pdb_el.ems ON pdb_el.geraete_daten.seriennummer = pdb_el.ems.Seriennummer
WHERE
pdb_el.geraete_daten.seriennummer = @SerialNumber
OR pdb_el.geraete_daten.seriennummer = @SerialNumberEms
OR pdb_el.geraete_daten.seriennummer = @SerialNumberAnonymous
