SELECT
	pdb_el.sn.seriennummer AS serial_number
	,CAST(pdb_el.sn.produktionsauftrag AS INT) AS production_order
	,pdb_el.sn.n AS batch_index
	,(CASE WHEN pdb_el.auftraege.artikelcode LIKE 'T%' THEN CAST(SUBSTRING(pdb_el.auftraege.artikelcode, 2) AS INT) ELSE CAST(pdb_el.auftraege.artikelcode AS INT) END) AS article_code
	,(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
	,pdb_el.stammdaten.bezeichnung AS description
	,(CASE WHEN pdb_el.modulindex.modulindex = "" THEN NULL ELSE pdb_el.modulindex.modulindex END) AS module_index
FROM pdb_el.sn
JOIN pdb_el.auftraege ON pdb_el.sn.produktionsauftrag = pdb_el.auftraege.produktionsauftrag
JOIN pdb_el.stammdaten ON pdb_el.auftraege.artikelcode = pdb_el.stammdaten.Artikelcode
LEFT JOIN pdb_el.ems ON pdb_el.sn.seriennummer = pdb_el.ems.Seriennummer
LEFT JOIN pdb_el.modulindex ON pdb_el.sn.seriennummer = pdb_el.modulindex.sn_lp
WHERE 
	pdb_el.sn.seriennummer IN (202274021, 202393044, 202332421, 202396148)
	OR pdb_el.sn.seriennummer IN (203463293, 203535394, 203463311)
	OR pdb_el.sn.seriennummer IN (2173276);
