Ich habe ein Problem mit dem folgenden Code in einem Python-Programm, das ich gebaut habe. Das Programm ruft Daten aus einer Datenbank ab und speichert sie in einer Excel-Tabelle basierend auf einer Liste von Referenznummern 'GrantRefNumber'
, die sie eingeben.Python/SQL - WHERE-Klausel nicht für alle Zeilen
Es funktioniert, aber aus irgendeinem Grund nur die erste Referenznummer hat die 'a.reporting_period_id like 'none-'
angewendet, den Rest nicht.
Ich verwende eine Variablensubstitution im SQL-Code, um die Liste der Referenznummern in die Zeichenfolge einzufügen.
Jede Hilfe wäre sehr willkommen!
SQL:
"SELECT a.fa_reference as [GrantRefNumber],
a.fa_name as [Award Title],
a.location as [RO],
ISNULL(cat.grant_department_name, '') as [Department],
a.funding_start_date as [Start Date],
a.funding_end_date as [End Date],
a.[pi] as [PI ID],
a.pi_initials as [PI Name],
a.pi_surname as [PI Surname],
r1_2 as [Type],
DATEADD(s, cast(last_submitted_date as int), '1970-01-01 00:00:00') as [Submitted Date]
from keywordagreements a inner join entries_publications p on a.id =
p.agreement_id left outer join mrc_categories cat on a.origid = cat.id and cat.centre not in ('2')
where a.[pi] NOT LIKE 'S%' and response_code not like 'Test' and Closed is null
and a.reporting_period_id like 'none-'
and a.funding_organisation not like '%UKSA%' and {}'
Order by [RO], [PI ID], [GrantRefNumber]".format(finalList)
finalList (variable Substitution):
Die finalList ist eine Liste der Referenznummern, die ich von dem Benutzer in Python erhalten genannte 'Artikel'
items = dfCall['GrantRefNumber'].values.tolist()
refList = " OR ".join(["a.fa_reference LIKE '%s'" % num for num in items])
finalList = refList[:-1]
Ich verwende PYODBC, um die Daten zu extrahieren.
Der SQL in meinem Code sieht wie folgt aus (ich die Anführungszeichen entfernt (und einige der Spalten) in dem vorherigen Code leichter lesbar zu machen):
stringQ = "SELECT a.fa_reference as [GrantRefNumber], a.fa_name as [Award Title]," \
" a.location as [RO], ISNULL(cat.grant_department_name, '') as [Department]," \
" a.funding_start_date as [Start Date], a.funding_end_date as [End Date]," \
" a.[pi] as [PI ID], a.pi_initials as [PI Name]," \
" a.pi_surname as [PI Surname], " \
" r1_2 as [Type], ISNULL(r1_2_1, '') as [PubMed ID]," \
" r1_2_2 as [Author], r1_2_3 as [Publication], ISNULL(r1_2_4, '') as [Journal]," \
" ISNULL(r1_2_8, '') as [Month], ISNULL(r1_2_9, '') as [Year], ISNULL(r1_2_4_1, '') as [Conference]," \
" ISNULL(r1_2_36, '') as [PubMed Central ID], ISNULL (r1_2_19, '') as [DOI]," \
" case when nullif(r1_2_1,'') is not null then 'http://europepmc.org/abstract/MED/' + r1_2_1 else case when" \
" nullif(r1_4,'') is not null then r1_4 else case when nullif(r1_2_19,'') is not null then" \
" 'http://dx.doi.org/' + r1_2_19 else isnull(r1_2_1,'') end end end as [URL], ISNULL(r1_2_21, '') " \
"as [ISBN]," \
" ISNULL(r1_2_30, '') as [ISBN (Electronic)], " \
" ISNULL(r1_2_25, '') as [Chapter Number], " \
"ISNULL(r1_2_26, '')" \
" as [Chapter Title], ISNULL(r1_2_27, '') as [Chapter Author]," \
" ISNULL(r1_2_29, '') as [ISSN (Print)], ISNULL(r1_2_32, '') as [ISSN (Digital)], " \
"ISNULL(r1_2_31, '') as [Web of Science ID], ISNULL(r1_2_34, '') as [Scopus ID], " \
"ISNULL(r1_2_35, '') as [arXiv DepositID]," \
" ISNULL(r1_2_38, '') as [Bibcode], ISNULL(r1_2_39, '') as [Ethos], ISNULL(r1_2_43, '') as [NASA-ADS ID]," \
" ISNULL(r1_2_46, '') as [Inspire], ISNULL(r1_2_40, '') as [PMC Manuscript ID], ISNULL(r1_2_45, '')" \
" as [ORCID Work Putcode]," \
" ISNULL(r1_2_61, '') as [OpenAire Access License], ISNULL(r1_2_52, '') " \
"as [In EPMC?], ISNULL(r1_2_53, '') as [In PMC?]," \
" ISNULL(r1_2_51, '') as [EPMC Open Access], " \
" DATEADD(s, cast(last_submitted_date as int), '1970-01-01 00:00:00') as [Submitted Date] " \
"from keywordagreements a inner join entries_publications p on a.id = " \
"p.agreement_id left outer join mrc_categories cat " \
"on a.origid = cat.id and cat.centre not in ('2') where a.[pi] NOT LIKE 'S%' and " \
"response_code not like 'Test' and Closed is null " \
"and a.reporting_period_id like 'none-' " \
"and a.funding_organisation not like '%UKSA%' and {}' " \
"Order by [RO], [PI ID], [GrantRefNumber]".format(finalList)
Hey, tut mir leid, ich habe eine Zeile Code (das wichtigste Bit) verpasst. Ich habe es meiner ursprünglichen Frage hinzugefügt. Es tut uns leid! – ScoutEU
Ich werde jetzt die Triple-Quote-Formatierung prüfen! :) – ScoutEU
Das dreifache Zitat funktioniert leider nicht. Wirft einen Syntaxfehler (ich habe bereits in jeder Zeile in Anführungszeichen gesetzt, um mehrere Zeilen zu führen. – ScoutEU