Skip to content

Instantly share code, notes, and snippets.

@juanandresnyc
Last active December 6, 2019 14:29
Show Gist options
  • Save juanandresnyc/cd834aa4dd9a2eda2272e080674724e9 to your computer and use it in GitHub Desktop.
Save juanandresnyc/cd834aa4dd9a2eda2272e080674724e9 to your computer and use it in GitHub Desktop.
Patient Medication old query
SELECT
"capsule_core_patientmedication"."id", "capsule_core_patientmedication"."deleted_at", "capsule_core_patientmedication"."created_at", "capsule_core_patientmedication"."updated_at", "capsule_core_patientmedication"."is_hidden", "capsule_core_patientmedication"."active_rx_id", "capsule_core_patientmedication"."last_delivered_fill_id", "capsule_core_patientmedication"."upcoming_fill_id", "capsule_core_patientmedication"."medication_id", "capsule_core_patientmedication"."next_refill_date", "capsule_core_patientmedication"."patient_id", "capsule_core_patientmedication"."retrieval_preference", "capsule_core_patientmedication"."should_process_refill", "capsule_core_patientmedication"."should_precheck_next_fill", "capsule_core_patientmedication"."standard_fill_size", "capsule_core_patientmedication"."status", "capsule_core_prescription"."id", "capsule_core_prescription"."deleted_at", "capsule_core_prescription"."created_at", "capsule_core_prescription"."updated_at", "capsule_core_prescription"."patient_medication_id", "capsule_core_prescription"."provider_id", "capsule_core_prescription"."date_written", "capsule_core_prescription"."expiration_date", "capsule_core_prescription"."instructions", "capsule_core_prescription"."national_drug_code", "capsule_core_prescription"."pioneer_rx_fk", "capsule_core_prescription"."dispenser_rx_number", "capsule_core_prescription"."quantity", "capsule_core_prescription"."quantity_prescribed", "capsule_core_prescription"."quantity_remaining", "capsule_core_prescription"."should_process_refill", "capsule_core_prescription"."new_rx_worked", "capsule_core_prescription"."new_status", "capsule_core_prescription"."origin", "capsule_core_prescription"."refills_allowed", "capsule_core_prescription"."pioneer_rx_number", "capsule_core_prescription"."control_schedule", "capsule_core_prescriptionfulfillment"."id", "capsule_core_prescriptionfulfillment"."deleted_at", "capsule_core_prescriptionfulfillment"."created_at", "capsule_core_prescriptionfulfillment"."updated_at", "capsule_core_prescriptionfulfillment"."status", "capsule_core_prescriptionfulfillment"."status_claimed_at", "capsule_core_prescriptionfulfillment"."status_revoked_at", "capsule_core_prescriptionfulfillment"."status_ordering_at", "capsule_core_prescriptionfulfillment"."status_alternative_med_filled_at", "capsule_core_prescriptionfulfillment"."status_completed_at", "capsule_core_prescriptionfulfillment"."checkout_start_date", "capsule_core_prescriptionfulfillment"."checkout_end_date", "capsule_core_prescriptionfulfillment"."delivery_date", "capsule_core_prescriptionfulfillment"."dispense_as_written", "capsule_core_prescriptionfulfillment"."price", "capsule_core_prescriptionfulfillment"."price_at_checkout", "capsule_core_prescriptionfulfillment"."dispenser_fill_number", "capsule_core_prescriptionfulfillment"."order_item_id", "capsule_core_prescriptionfulfillment"."prescription_id", "capsule_core_prescriptionfulfillment"."product_id", "capsule_core_prescriptionfulfillment"."facility_id", "capsule_core_prescriptionfulfillment"."checkout_link_sent_at", "capsule_core_prescriptionfulfillment"."checkout_link_copy_fmt", "capsule_core_prescriptionfulfillment"."checkout_link_copy_msg", "capsule_core_prescriptionfulfillment"."status_need_contact_info_at", "capsule_core_prescriptionfulfillment"."status_need_insurance_at", "capsule_core_prescriptionfulfillment"."status_not_needed_at", "capsule_core_prescriptionfulfillment"."status_not_responsive_at", "capsule_core_prescriptionfulfillment"."status_on_hold_at", "capsule_core_prescriptionfulfillment"."status_prior_auth_at", "capsule_core_prescriptionfulfillment"."status_refill_too_soon_at", "capsule_core_prescriptionfulfillment"."pioneer_rx_transaction_fk", "capsule_core_prescriptionfulfillment"."national_drug_code_dispensed", "capsule_core_prescriptionfulfillment"."quantity_dispensed", "capsule_core_prescriptionfulfillment"."quantity_deducted_at", "capsule_core_prescriptionfulfillment"."should_waive_copay", "capsule_core_prescriptionfulfillment"."paid_claim_exists", "capsule_core_prescriptionfulfillment"."paid_secondary_claim_exists", "capsule_core_prescriptionfulfillment"."num_days_supply", "capsule_core_prescriptionfulfillment"."quantity_at_checkout", "capsule_core_prescriptionfulfillment"."num_days_supply_at_checkout", "capsule_core_prescriptionfulfillment"."gross_profit", "capsule_core_prescriptionfulfillment"."last_dispenser_sync", "capsule_core_prescriptionfulfillment"."pioneer_rx_id", T5."id", T5."deleted_at", T5."created_at", T5."updated_at", T5."status", T5."status_claimed_at", T5."status_revoked_at", T5."status_ordering_at", T5."status_alternative_med_filled_at", T5."status_completed_at", T5."checkout_start_date", T5."checkout_end_date", T5."delivery_date", T5."dispense_as_written", T5."price", T5."price_at_checkout", T5."dispenser_fill_number", T5."order_item_id", T5."prescription_id", T5."product_id", T5."facility_id", T5."checkout_link_sent_at", T5."checkout_link_copy_fmt", T5."checkout_link_copy_msg", T5."status_need_contact_info_at", T5."status_need_insurance_at", T5."status_not_needed_at", T5."status_not_responsive_at", T5."status_on_hold_at", T5."status_prior_auth_at", T5."status_refill_too_soon_at", T5."pioneer_rx_transaction_fk", T5."national_drug_code_dispensed", T5."quantity_dispensed", T5."quantity_deducted_at", T5."should_waive_copay", T5."paid_claim_exists", T5."paid_secondary_claim_exists", T5."num_days_supply", T5."quantity_at_checkout", T5."num_days_supply_at_checkout", T5."gross_profit", T5."last_dispenser_sync", T5."pioneer_rx_id", "capsule_core_medication"."id", "capsule_core_medication"."deleted_at", "capsule_core_medication"."created_at", "capsule_core_medication"."updated_at", "capsule_core_medication"."primary_name", "capsule_core_medication"."secondary_name", "capsule_core_medication"."dosage_form_label", "capsule_core_medication"."control_schedule", "capsule_core_medication"."gcn_seqno", "capsule_core_medication"."hicl_seqno", "capsule_core_medication"."hcfa_typ", "capsule_core_medication"."gcrt", "capsule_core_medication"."gcrt_desc", "capsule_core_medication"."gcdf", "capsule_core_medication"."strength", "capsule_core_medication"."gni", "capsule_core_medication"."refrigeration_type", "capsule_core_medication"."mixing_type", "capsule_core_medication"."unknown_status", "capsule_core_patient"."id", "capsule_core_patient"."deleted_at", "capsule_core_patient"."created_at", "capsule_core_patient"."updated_at", "capsule_core_patient"."account_id", "capsule_core_patient"."acquisition_source", "capsule_core_patient"."is_primary", "capsule_core_patient"."first_name", "capsule_core_patient"."last_name", "capsule_core_patient"."sex", "capsule_core_patient"."is_pregnant", "capsule_core_patient"."birthdate", "capsule_core_patient"."allergies", "capsule_core_patient"."medical_conditions", "capsule_core_patient"."other_medications", "capsule_core_patient"."preferences", "capsule_core_patient"."verification_needed", "capsule_core_patient"."should_auto_waive_copays", "capsule_core_patient"."last_notified_sms", "capsule_core_patient"."pioneer_person_id"
FROM
"capsule_core_patientmedication"
INNER JOIN "capsule_core_patient" ON ("capsule_core_patientmedication"."patient_id" = "capsule_core_patient"."id")
LEFT OUTER JOIN "capsule_core_prescription" ON ("capsule_core_patientmedication"."active_rx_id" = "capsule_core_prescription"."id")
LEFT OUTER JOIN "capsule_core_prescriptionfulfillment" ON ("capsule_core_patientmedication"."last_delivered_fill_id" = "capsule_core_prescriptionfulfillment"."id")
LEFT OUTER JOIN "capsule_core_prescriptionfulfillment" T5 ON ("capsule_core_patientmedication"."upcoming_fill_id" = T5."id")
INNER JOIN "capsule_core_medication" ON ("capsule_core_patientmedication"."medication_id" = "capsule_core_medication"."id")
WHERE (
"capsule_core_patientmedication"."deleted_at" IS NULL
AND "capsule_core_patientmedication"."is_hidden" = False
AND "capsule_core_patientmedication"."patient_id" = 59101
AND "capsule_core_patientmedication"."id" IN (
SELECT U0."patient_medication_id"
FROM "capsule_core_prescription" U0
WHERE U0."patient_medication_id" = ("capsule_core_patientmedication"."id")
)
)
ORDER BY "capsule_core_patientmedication"."updated_at" DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment