# Copia de lectura: los nombres de empresa van sustituidos por la marca (la web no nombra empresas). md5 del original: c8f81e695b2c00df7012475dbd72d794 import duckdb, json c = duckdb.connect(); c.execute("SET threads=2; SET memory_limit='3GB'") S = "read_parquet('/out/S2_filas.parquet')" T = "read_parquet('/h/ted/ted_es_can.parquet')" c.execute(f"CREATE TEMP TABLE t AS SELECT * FROM {T} WHERE cae_nif_clean IN ('Q2801660H','Q2802152E') OR cae_nationalid IN ('Q2801660H','Q2802152E') OR cae_name ILIKE '%ADIF%'") c.execute(f"CREATE TEMP TABLE n AS SELECT * FROM {S} WHERE NOT es_pedido AND procedimiento='Negociado_sin_publicidad'") print('NSP en S', c.execute("SELECT count(*), round(sum(imp)/1e6,2) FROM n").fetchone()) c.execute("""CREATE TEMP TABLE tj AS SELECT coalesce(nullif(expediente_sin_lote,''), internal_id_proc) exp, string_agg(DISTINCT nullif(direct_award_justification,''), ',') daj, string_agg(DISTINCT left(nullif(direct_award_justification_text,''),300), ' || ') dajt, string_agg(DISTINCT coalesce(top_type, procedure_type), ',') proc_ted, max(year) y FROM t GROUP BY 1""") print('NSP en S casados con TED', c.execute("SELECT count(*), count(tj.exp), count(tj.daj), round(sum(imp) FILTER (WHERE tj.exp IS NOT NULL)/1e6,2), round(sum(imp) FILTER (WHERE tj.daj IS NOT NULL)/1e6,2) FROM n LEFT JOIN tj ON tj.exp = n.expediente").fetchone()) print('motivos', c.execute("SELECT tj.daj, count(*), round(sum(imp)/1e6,2) FROM n JOIN tj ON tj.exp = n.expediente GROUP BY 1 ORDER BY 2 DESC").fetchall()) print('proc TED de los NSP casados', c.execute("SELECT tj.proc_ted, count(*), round(sum(imp)/1e6,2) FROM n JOIN tj ON tj.exp = n.expediente GROUP BY 1 ORDER BY 2 DESC").fetchall()) for r in c.execute("SELECT n.expediente, round(imp/1e6,2), tj.daj, tj.dajt, left(objeto,90) FROM n JOIN tj ON tj.exp = n.expediente WHERE tj.daj IS NOT NULL ORDER BY imp DESC NULLS LAST").fetchall(): print(r) # tecnología propietaria nombrada en el objeto q = r"tecnolog[ií]a\s+(de\s+)?(|||||||||||||||)" print('NSP con tecnología nombrada', c.execute(f"SELECT count(*), round(sum(imp)/1e6,2) FROM n WHERE regexp_matches(lower(objeto), '{q}')").fetchone()) print('Abierto con tecnología nombrada', c.execute(f"SELECT count(*), round(sum(imp)/1e6,2) FROM {S} WHERE NOT es_pedido AND procedimiento <> 'Negociado_sin_publicidad' AND regexp_matches(lower(objeto), '{q}')").fetchone()) print('NSP por marca', c.execute(f"SELECT regexp_extract(lower(objeto), '{q}', 2) m, count(*), round(sum(imp)/1e6,2) FROM n WHERE regexp_matches(lower(objeto), '{q}') GROUP BY 1 ORDER BY 2 DESC").fetchall()) print('NSP emergencia', c.execute("SELECT tramitacion, count(*), round(sum(imp)/1e6,2) FROM n GROUP BY 1").fetchall()) print('NSP top 15') for r in c.execute("SELECT expediente, fecha_adj, round(imp/1e6,2), num_ofertas, tramitacion, left(adjudicatario_nombre,40), left(objeto,110), url_origen FROM n ORDER BY imp DESC NULLS LAST LIMIT 15").fetchall(): print(r) # ofertas: cotejo PLACSP vs TED number_offers para S c.execute("""CREATE TEMP TABLE tof AS SELECT coalesce(nullif(expediente_sin_lote,''), internal_id_proc) exp, count(DISTINCT coalesce(ted_award_id, lot_result_id)) n_aw, min(number_offers) mn, max(number_offers) mx FROM t WHERE number_offers IS NOT NULL GROUP BY 1""") print('S casados con ofertas TED (expediente sin lotes en TED: 1 adjudicación)', c.execute(f"""SELECT count(*), count(*) FILTER (WHERE s.num_ofertas = tof.mn AND tof.mn = tof.mx), count(*) FILTER (WHERE tof.mn = tof.mx AND s.num_ofertas <> tof.mn) FROM {S} s JOIN tof ON tof.exp = s.expediente WHERE NOT s.es_pedido AND tof.n_aw = 1 AND s.lote_id IS NULL""").fetchone()) for r in c.execute(f"""SELECT s.expediente, s.num_ofertas, tof.mn, tof.mx, s.procedimiento FROM {S} s JOIN tof ON tof.exp = s.expediente WHERE NOT s.es_pedido AND tof.n_aw = 1 AND s.lote_id IS NULL AND tof.mn = tof.mx AND s.num_ofertas <> tof.mn LIMIT 20""").fetchall(): print(r) import json as _j res = {} res['nsp'] = c.execute("SELECT count(*), round(sum(imp)/1e6,2) FROM n").fetchone() res['nsp_ted_casados'] = c.execute("SELECT count(tj.exp), round(sum(imp) FILTER (WHERE tj.exp IS NOT NULL)/1e6,2) FROM n LEFT JOIN tj ON tj.exp = n.expediente").fetchone() res['nsp_motivo'] = c.execute("SELECT tj.daj, count(*), round(sum(imp)/1e6,2) FROM n JOIN tj ON tj.exp = n.expediente WHERE tj.daj IS NOT NULL GROUP BY 1 ORDER BY 2 DESC").fetchall() res['nsp_motivo_total'] = c.execute("SELECT count(*), round(sum(imp)/1e6,2) FROM n JOIN tj ON tj.exp = n.expediente WHERE tj.daj IS NOT NULL").fetchone() res['nsp_tecnologia_objeto'] = c.execute(f"SELECT count(*), round(sum(imp)/1e6,2) FROM n WHERE regexp_matches(lower(objeto), '{q}')").fetchone() res['nsp_motivo_o_tecnologia'] = c.execute(f"SELECT count(*), round(sum(imp)/1e6,2) FROM n LEFT JOIN tj ON tj.exp = n.expediente WHERE tj.daj IS NOT NULL OR regexp_matches(lower(objeto), '{q}')").fetchone() res['nsp_tramitacion'] = c.execute("SELECT tramitacion, count(*), round(sum(imp)/1e6,2) FROM n GROUP BY 1 ORDER BY 2 DESC").fetchall() res['ofertas_placsp_ted'] = c.execute(f"""SELECT count(*), count(*) FILTER (WHERE s.num_ofertas = tof.mn) FROM {S} s JOIN tof ON tof.exp = s.expediente WHERE NOT s.es_pedido AND tof.n_aw = 1 AND s.lote_id IS NULL AND tof.mn = tof.mx""").fetchone() _j.dump(res, open('/out/nsp_v2.json', 'w'), ensure_ascii=False, indent=1, default=str); print(res)