我正在查询基于用户的数据库,并尝试查找仅使用特定日期范围进行购买的人员。
e.g:
将所有仅在'2017-7-01'
和'2017-08-01'
之间购买但之后未购买的人带回来。
我试过了:
select payment_id
from table1
where created_at between '2017-7-01'
and '2017-08-01'
and not between '2017-7-01' and '2017-08-01';
以及各种变化。 我想这里有一个子查询,但不知道如何接近 希望有人可以提供帮助:)
由于
答案 0 :(得分:0)
使用条件COUNT()
查看用户是否在该日期范围内进行了任何购买,并且在上限范围之后没有购买。
此外,您需要与用户相关的内容,我猜user_id
所以您可以对所有内容进行分组。
SELECT user_id
FROM yourTable
GROUP BY user_id
HAVING COUNT(CASE WHEN created_at between '2017-07-01'
and '2017-08-01'
THEN 1
END) > 0
AND COUNT(CASE WHEN created_at > '2017-08-01'
THEN 1
END) = 0
答案 1 :(得分:0)
select payment_id
from (
select payment_id
from table1
where created_at between '2017-7-01' and '2017-08-01' )
where created_at > '2017-08-01'
答案 2 :(得分:0)
Select userid
From table1
Where userid not in (select userid from table1 where createdat between @startdate and @enddate)
Where createdat > @enddate
答案 3 :(得分:0)
如果您想过滤以确保MAX(日期)小于2017-08-01,那么您可以将{u'company_sub_post_installation_clean_up': [], u'dcms_': u'890835', u'email_inspection': None, u'company_sub_welding': [], u'company_sub_joining_plastic': [], u'job_type': u'service', u'job_material_type': u'plastic', u'if_other_type_crew_leader_name': None, u'company_sub_safety': [{u'equipment_safety_functional': None, u'fire_extinguisher_being_used': u'meets', u'appropriate_ppe_was_used': u'meets', u'server_modified_date': u'2017-10-24T17:41:15+00:00', u'id': 69, u'parent_element_id': 277320543, u'vehicletrailerequipment_cones_present': u'meets', u'modified_date': u'2017-10-24T17:39:22+00:00', u'vehicletrailer_wheel_chocks_present': u'meets', u'modified_by': u'company', u'fire_extinguisher_date_acceptable': u'meets', u'chemicals_properly_labeled_hazcom': None, u'created_by': u'company', u'safety_comments__photos': [], u'modified_device_id': u'990006001231835', u'complete_trench_entry_form': None, u'created_location': u'35.625220:-80.467894:0.000000:24.400999:24.400999:0.000000:0.000000:1508866785280.000000', u'appropriate_traffic_control_was_used': u'meets', u'select_items_not_met': None, u'javascript_state': u'{}', u'fire_extinguisher_fully_charged': u'meets', u'gas_co_oxygen_detector_used': None, u'parent_record_id': 87, u'shoring_equipment_trench_box_operation': None, u'shoring_used': None, u'modified_location': u'35.625220:-80.467894:0.000000:24.400999:24.400999:0.000000:0.000000:1508866785280.000000', u'parent_page_id': 3440925, u'ladder_operation_acceptable': None, u'ladder_used': None, u'created_device_id': u'990006001231835', u'equipment_safety_being_used': None, u'select_trafic_items_not_met': None, u'equipmenttools_proper_distance_from_trench': None, u'created_date': u'2017-10-24T17:39:22+00:00', u'prejob_brief_was_performed_and_documented': u'meets'}], u'server_modified_date': u'2017-10-24T17:41:14+00:00', u'id': 87, u'parent_element_id': 0, u'date1': u'2017-10-24', u'modified_date': u'2017-10-24T17:41:06+00:00', u'company_sub_trench': [{u'modified_date': u'2017-10-24T17:39:32+00:00', u'javascript_state': u'{}', u'parent_page_id': 3440925, u'no_weak_link_issues': None, u'oq_present_texca001_inspection_of_excavation_activities': u'meets', u'server_modified_date': u'2017-10-24T17:41:16+00:00', u'created_device_id': u'990006001231835', u'parent_record_id': 87, u'depth_consistent_along_trenchline': None, u'created_by': u'company', u'trench_photos__comments': [], u'trench_depth_adequate': None, u'modified_device_id': u'990006001231835', u'trench_width_adequate': None, u'created_date': u'2017-10-24T17:39:32+00:00', u'modified_by': u'company', u'created_location': u'35.625220:-80.467894:0.000000:24.812000:24.812000:0.000000:0.000000:1508866785280.000000', u'modified_location': u'35.625220:-80.467894:0.000000:24.812000:24.812000:0.000000:0.000000:1508866785280.000000', u'id': 48, u'parent_element_id': 277320810, u'proper_dewatering': None}], u'modified_by': u'company', u'job_materials_photos__comments': [], u'created_by': u'company', u'catalogue_number': None, u'company_sub_pressure_test': [], u'company_sub_working_around_utilities': [], u'modified_device_id': u'990006001231835', u'crew_leader': u'kenneth_black__n18031', u'company_sub__purging__abandon': [], u'title__address': u'464 Sonoma dr', u'damaged_material_repaired_or_removed': None, u'company_sub_equipment_and_tools': [], u'type_of_nonconformance': None, u'company_sub_tie_ins': [], u'pipe_within_allowable_date_range': u'meets', u'pipe_materials_properly_marked': u'meets', u'javascript_state': u'null', u'responsible_for_nonconformance': None, u'company_sub_job_documentation': [], u'company_sub_design_adherance': [], u'company_sub_backfilling': [], u'parent_record_id': 0, u'job_sub_type': u'residential', u'my_element4': u'', u'company_sub_pigging': [], u'company_sub_coating': [], u'company_sub_cathodic_protection': [], u'company_sub_bore': [{u'proper_depth': u'meets', u'bore_photos__comments': [], u'visual_inspection_of_pipe_during_pullback': None, u'environmental_standards__protections_observered': None, u'server_modified_date': u'2017-10-24T17:41:17+00:00', u'id': 18, u'parent_element_id': 277320816, u'modified_date': u'2017-10-24T17:39:48+00:00', u'modified_by': u'company', u'no_head_size_problems_size_of_reamer_appropriate': None, u'created_by': u'company', u'modified_device_id': u'990006001231835', u'created_location': u'35.624811:-80.468068:0.000000:27.521999:27.521999:0.000000:0.000000:1508866785280.000000', u'type_of_bore': None, u'no_weak_link_issues': None, u'javascript_state': u'{}', u'bore_profile_present': None, u'parent_record_id': 87, u'modified_location': u'35.624811:-80.468068:0.000000:27.521999:27.521999:0.000000:0.000000:1508866785280.000000', u'parent_page_id': 3440925, u'created_device_id': u'990006001231835', u'permitting_adequate': None, u'tracer_wire_present': None, u'created_date': u'2017-10-24T17:39:48+00:00'}], u'modified_location': u'35.625220:-80.467894:0.000000:25.257000:25.257000:0.000000:0.000000:1508866785280.000000', u'created_location': u'35.624811:-80.468068:0.000000:22.379999:22.379999:0.000000:0.000000:1508866654208.000000', u'company_inspected': u'mears_construction', u'project_ir_': u'08003', u'parent_page_id': 0, u'created_device_id': u'990006001231835', u'asset_information_multiple': [{u'date_a1_value': u'', u'picture_of_asset': None, u'data_value': u'', u'd_value': u'0', u'data_result': u'', u'lot_code_code': None, u'size_code': None, u'material_type': u'plastic_pipe', u'a4_lotcode': u'', u'lot_code': u'0', u'a1v': u'', u'a2_lotcode_code': None, u'c2_final_value': u'', u'c2': None, u'date_a3_code': u'', u'c1_final': u'', u'production_date_leap_year': u', 200', u'server_modified_date': u'2017-10-24T17:41:15+00:00', u'id': 51, u'parent_element_id': 277320738, u'a3v': u'', u'modified_date': u'2017-10-24T17:38:41+00:00', u'modified_by': u'company', u'a2_lotcode': u'', u'production_date': u', 200', u'dayly_final': u'', u'picture_of_nonconformance': None, u'a1_lotcode': u'', u'data_result_split': u' ', u'c1_final_value': u'', u'created_by': u'company', u'a3_lotcode': u'', u'date_code': None, u'modified_device_id': u'990006001231835', u'lotcodesplit': u'', u'date_a1_code': None, u'created_location': u'35.624811:-80.468068:0.000000:22.379999:22.379999:0.000000:0.000000:1508866654208.000000', u'leap_year': u'0', u'type_of_nonconformance': None, u'a3_lotcode_code': None, u'a4_lotcode_code': u'', u'component_type_code': None, u'javascript_state': u'{}', u'size_split': u'', u'responsible_for_nonconformance': None, u'a2v': u'', u'c2_final': u'', u'material': u'', u'parent_record_id': 87, u'year1': u'200', u'a1': None, u'barcode_on_fitting__pipe': None, u'a2': None, u'date_a3_value': u'', u'a1_lotcode_code': None, u'manufacturer_code': u' ', u'modified_location': u'35.624811:-80.468068:0.000000:22.379999:22.379999:0.000000:0.000000:1508866654208.000000', u'catalogue_number': None, u'date_d_value_split': u'||0', u'date_a2_value': u'', u'day_final': u'', u'parent_page_id': 3440925, u'd': u'0', u'component_type': u'', u'day_ly_raw': u'', u'created_device_id': u'990006001231835', u'a3': u'', u'date_code_split': u'', u'material_code': None, u'day_raw': u'', u'manufacturer': u'', u'created_date': u'2017-10-24T17:38:40+00:00', u'date_a2_code': None, u'nonconformance_': u'No', u'display': None}], u'matieral_condition': u'meets', u'company_sub_material_handling': [], u'company_sub_manuals': [], u'created_date': u'2017-10-24T17:38:25+00:00', u'inspector_id': u'company', u'inspection_overall_comments_and_photos': [], u'company_sub_environmental': []}
与GROUP BY
一起使用。
实施例
HAVING