홈>
올바른 날짜 범위를 쿼리하는 데 문제가 있습니다. 내 쿼리는 post_date>(날짜 제공)의 Where 절을 고려하지 않는 것 같습니다.
SELECT `code`,
`description`,
SUM( IF( month = 3 && year = 2018, monthly_quantity_total, 0 ) ) AS monthlyqt,
SUM( IF( month = 3 && year = 2018, monthly_price_total, 0 ) ) AS monthlypt,
SUM( monthly_quantity_total ) AS yearlyqt,
SUM( monthly_price_total ) AS yearlypt
FROM (
SELECT `invoices_items`.`code`,
`invoices_items`.`description`,
SUM( invoices_items.discounted_price * invoices_items.quantity_supplied ) AS monthly_price_total,
SUM( invoices_items.quantity_supplied ) AS monthly_quantity_total,
YEAR( invoices_items.datetime_created ) AS year,
MONTH( invoices_items.datetime_created ) AS month
FROM `invoices_items`
JOIN `invoices` ON `invoices`.`id` = `invoices_items`.`invoice_id`
WHERE `invoices`.`is_finalised` = 1
AND `invoices`.`post_date` > 2018-02-28
AND `invoices_items`.`type` = 1
GROUP BY `year`, `month`, `invoices_items`.`code`
UNION ALL
SELECT `credit_notes_items`.`code`,
`credit_notes_items`.`description`,
SUM( credit_notes_items.discounted_price * credit_notes_items.quantity_supplied * -1 ) AS monthly_price_total,
SUM( credit_notes_items.quantity_supplied ) AS monthly_quantity_total,
YEAR( credit_notes_items.datetime_created ) AS year,
MONTH( credit_notes_items.datetime_created ) AS month
FROM `credit_notes_items`
JOIN `credit_notes` ON `credit_notes`.`id` = `credit_notes_items`.`credit_note_id`
WHERE `credit_notes`.`is_finalised` = 1
AND `credit_notes`.`post_date` > 2018-02-28
AND `credit_notes_items`.`type` = 1
GROUP BY `year`, `month`, `credit_notes_items`.`code`
) AS sub
GROUP BY code;
기본적으로 4 개의 테이블이 쿼리됩니다.
invoices
invoices_items
credit_notes
그리고
credit_notes_items
.
표 1-송장
id post_date is_finalised
1 2018-01-01 1
2 2018-02-01 1
3 2018-03-01 1
표 2-송장 _ 항목
id invoice_id code description discounted_price quantity_total type
1 1 TEST-01 Test product 9.99 1 1
2 1 TEST-01 Test product 9.99 2 1
3 2 TEST-01 Test product 9.99 5 1
4 3 TEST-01 Test product 9.99 5 1
위에 몇 가지 예제 행을 제공했습니다. 위의 표 1과 2에서 원하는 결과는 다음과 같아야합니다.
원하는 결과
code description monthyqt monthlypt yearlyqt yearlypt
TEST-01 Test product 5 49.95 5 49.95
받은 결과는 다음과 같습니다.
수신 된 출력
code description monthyqt monthlypt yearlyqt yearlypt
TEST-01 Test product 5 49.95 13 129.87
Where 절을 사용하여 달성하려는 날짜 범위를 제외하고 쿼리가 의도 한대로 작동합니다.
invoices
와 일치하지 않는 행을 필터링하려고합니다.
.
post_date
>2018-02-28 (및
credit_notes
)
.
post_date
>2018-02-28).
여기서 내가 뭘 잘못했는지 잘 모르겠지만 도움을 주시면 감사하겠습니다.
미리 감사합니다.
- 답변 # 1
관련 자료
- 쿼리가 결과를 찾지 못함 (php mysql)
- reactjs - ApolloClient v3 fetchMore (중첩 된 쿼리 결과 포함)
- MySQL에서 이벤트를 예약 할 수 없습니다
- javascript - firestore - 쿼리에서 문서를 가져올 수 없습니다docdata는 함수가 아닙니다
- java - Hibernate Search를 사용하여 JPA 쿼리 결과에 대한 전체 텍스트 검색 수행
- 값 중첩 매개 변수를 기반으로 결과를 가져 오기 위해 mongodb를 쿼리하는 방법은 무엇입니까?
- date - mysql - 어제와 오늘 사이에 특정 시간이 포함 된 쿼리 레코드
- python - Django에서 모델 객체 쿼리 결과 확인
- sql - 여러 조건 합계를 사용하고 싶은이 mysql 쿼리가 있습니다 같은 줄에 2 곳을 사용하면 오류가 표시됩니다
- sql - mysql 쿼리에 introspection 입력
- sccm - 사용자 계정에 따라 WMI 쿼리의 결과가 다릅니 까?
- sql - 쿼리의 중복 된 결과
- python - sqlite 쿼리 - 0 개의 결과를 반환하는 select from
- sql - MySQL 내 접합 테이블에서 정보와 해당 접합에없는 데이터를 가져 오는 쿼리를 어떻게 만들 수 있습니까?
- sql - 정확히 동일한 쿼리를 두 번 사용할 때 Athena 쿼리 결과가 다릅니다
- elasticsearch - 탄력적 검색이 타임 스탬프 범위 쿼리를 수행하지 못함
- sql - MySQL 쿼리로 논리 작업 수행
- 자바 스크립트의 mysql 쿼리가 ER_PARSE_ERROR를 얻지 만 phpmyadmin에서 작동했습니다
- hiveql - Hive 테이블에서 CTE를 사용하여 쿼리를 작성할 수 없습니다
- sql - 동일한 quote_num이지만 최대 상태 및 최대 버전 인 MySQL 쿼리에서 멈춤
트렌드
- OpenCv의 폴더에서 여러 이미지 읽기 (python)
- 파이썬 셀레늄 모든 "href"속성 가져 오기
- html - 자바 스크립트 - 클릭 후 변경 버튼 텍스트 변경
- git commit - 자식 - 로컬 커밋 된 파일에 대한 변경을 취소하는 방법
- JSP에 대한 클래스를 컴파일 할 수 없습니다
- javascript - 현재 URL에서 특정 div 만 새로 고침/새로 고침
- jquery - JavaScript로 현재 세션 값을 얻으시겠습니까?
- javascript - swiperjs에서 정지, 재생 버튼 추가
- JavaScript 변수를 HTML div에 '출력'하는 방법
- python - 문자열에서 특정 문자 제거
나는 그것을 해결했다. 기본적으로 날짜 값을 구문 분석하는 변수는 작은 따옴표로 묶어야했습니다.
예 :
항상이 단순한 것만으로도 당신을 버립니다.