我的查询:
SELECT DISTINCT V.COMPANY, V.VENDOR_NUM, V.VENDOR_PAYEE_NUM,
C.CONTACT_NAME, C.CONTACT_AUDIT_DATE
FROM
VENDOR_TABLE AS V INNER JOIN CONTACT_TABLE AS C ON (V.COMPANY = C.COMPANY AND DIGITS(V.VENDOR_NUM) = C.VENDOR_NUM)
WHERE DATE(INSERT(INSERT(DIGITS(V.VENDOR_AUDIT_DATE), 5, 0, '-'), 8, 0, '- ')) >= DATE(VARCHAR_FORMAT(TIMESTAMP_ISO(CURRENT DATE), 'YYYY-MM-DD')) - 21 DAYS
AND V.VENDOR_AUDIT_DATE <> 0 AND (V.STATUS = ' ' OR V.STATUS IS NULL)
它返回以下结果:
COMPANY VENDOR_NUM VENDOR_PAYEE_NUM V CONTACT_NAME CONTACT_AUDIT_DATE ------- ---------- ---------------- ------------------------------ ------------------ 908 13514 13514 Coleen 20120427 908 34242 34242 Frank Cheese 20100120 908 60148 60148 Sarah Lee/Jonh Doe 20141121 908 60148 60148 Sarah Lee/Jonh Doe 20141121 908 60151 60151 Sarah Lee/Jonh Doe 20140919 908 60151 60151 Sarah Lee/Jonh Doe 20140919 908 60152 60152 Sarah Lee/Jonh Doe 20140919 908 60152 60152 Sarah Lee/Jonh Doe 20140919
联系人表格可能包含同一供应商的多个联系人。但是,我只想从供应商表中检索每个供应商编号的一个联系人。如何加入这些表,但只从供应商表中的每个供应商的Contact表中选择1个或不同的供应商联系人?
我稍微修改了我的查询。我发现联系表上有一个我可以操作的序列号。我需要该记录集中的最大序列。但是,我一直收到错误,&#34;列CZCO或SELECT列表中的表达式无效&#34;。我不确定我可能做错了什么......非常感谢任何帮助。
SELECT COMPANY, VENDOR_NUM, VENDOR_PAYEE_NUM,
VENDOR_NAME, COUNTRY_CODE, ADDRESS_1,
ADDRESS_2, CITY_STATE, ZIP_CODE,
PAY_ADDR_1, PAY_ADDR_2, PAY_CITY_STATE,
PAYEE_ZIP_CODE, VENDOR_AUDIT_DATE
FROM VENDOR_TABLE V
INNER JOIN
(
SELECT CONTACT_KEY, COMPANY, CONTACT_PHONE,
CONTACT_FAX, CONTACT_EMAIL, CONTACT_NAME,
CONTACT_AUDIT_DATE, MAX(SEQ_NUM) AS SEQ_NUM
FROM CONTACT_TABLE GROUP BY CONTACT_KEY
) C ON (V.COMPANY = C.COMPANY AND DIGITS(V.VENDOR_NUM) = C.CONTACT_KEY)
WHERE DATE(INSERT(INSERT(DIGITS(V.VENDOR_AUDIT_DATE), 5, 0, '-'), 8, 0, '-')) >= DATE(VARCHAR_FORMAT(TIMESTAMP_ISO(CURRENT DATE), 'YYYY-MM-DD')) - 20 DAYS
AND V.VENDOR_AUDIT_DATE <> 0 AND (V.STATUS = ' ' OR V.STATUS IS NULL)
答案 0 :(得分:0)
一种方法是使用keep dense rank函数按某种方式分组并将其余部分保持在一起。
WITH sample_data AS (SELECT 908 AS company,
13514 AS vendor_num,
13514 AS vendor_payee_num,
'Coleen' AS contact_name,
20120427 AS contact_audit_date
FROM DUAL
UNION ALL
SELECT 908,
34242,
34242,
'Frank Cheese',
20100120
FROM DUAL
UNION ALL
SELECT 908,
60148,
60148,
'Sarah Lee/Jonh Doe',
20141121
FROM DUAL
UNION ALL
SELECT 908,
60148,
60148,
'Sarah Lee/Jonh Doe',
20141121
FROM DUAL
UNION ALL
SELECT 908,
60151,
60151,
'Sarah Lee/Jonh Doe',
20140919
FROM DUAL
UNION ALL
SELECT 908,
60151,
60151,
'Sarah Lee/Jonh Doe',
20140919
FROM DUAL
UNION ALL
SELECT 908,
60152,
60152,
'Sarah Lee/Jonh Doe',
20140919
FROM DUAL
UNION ALL
SELECT 908,
60152,
60152,
'Sarah Lee/Jonh Doe',
20140919
FROM DUAL)
SELECT company,
vendor_num,
vendor_payee_num,
MIN (contact_name)
KEEP (DENSE_RANK FIRST ORDER BY contact_name, contact_audit_date)
contact_name,
MIN (contact_audit_date)
KEEP (DENSE_RANK FIRST ORDER BY contact_name, contact_audit_date)
contact_audit_date
FROM sample_data
GROUP BY company, vendor_num, vendor_payee_num
COMPANY VENDOR_NUM VENDOR_PAYEE_NUM CONTACT_NAME CONTACT_AUDIT_DATE
908 13514 13514 Coleen 20120427
908 34242 34242 Frank Cheese 20100120
908 60148 60148 Sarah Lee/Jonh Doe 20141121
908 60151 60151 Sarah Lee/Jonh Doe 20140919
908 60152 60152 Sarah Lee/Jonh Doe 20140919