简体   繁体   English

如何在选择多列的单列中显示 select 个唯一值?

[英]How do I select unique values in a single column with multiple column selected?

the query is actually fine and running but it doesn't select distinct the column A.CONTRACT_NUMBER.该查询实际上很好并且正在运行,但它与 A.CONTRACT_NUMBER 列不同 select。

This is the code:这是代码:

SELECT DISTINCT A.CONTRACT_NUMBER
, A.DTIME_SIGNATURE
, A.ID_CUID
, A.CLIENT_SEGMENT
, A.CLIENT_GENDER
, A.CLIENT_AGE
, A.CNT_SIGNED
, A.AMT_SIGNED
, A.INSTALMENTS
, A.PRODUCT
, B.AMT_INTEREST_INCOME
  FROM DM_SALES.V_SALES_DM_DATA A
  LEFT JOIN DM_FIN.FIN_LTV_DATAMART B ON A.CONTRACT_NUMBER = B.CONTRACT_NUMBER
  WHERE 1=1
  AND A.CONTRACT_STATE <> 'Cancelled' 
  AND a.cnt_signed=1 
  AND A.LOAN_TYPE = 'Consumer Loan'
  AND (TRUNC(A.DTIME_SIGNATURE) BETWEEN DATE'2022-08-01' AND DATE '2022-08-31')
  GROUP BY A.CONTRACT_NUMBER
  ORDER BY A.DTIME_SIGNATURE
;

It runs normally but after checking the data, there are still repeated values in the A.CONTRACT_NUMBER column.运行正常,但查了下数据,A.CONTRACT_NUMBER列还是有重复的值。 A.CONTRACT_NUMBER is like the primary key column and I'd like to select unique values to that column A.CONTRACT_NUMBER 就像主键列,我想为该列添加 select 个唯一值

Thanks: :D感谢:D

Per contract you want the average interest income from your datamart table, so aggregate your datamart data by contract and join this result to your sales data.table.对于每个合同,您需要数据集市表中的平均利息收入,因此按合同聚合数据集市数据并将此结果加入您的销售额 data.table。

SELECT
  sd.*,
  dm.avg_interest_income
FROM dm_sales.v_sales_dm_data sd
LEFT JOIN 
(
  SELECT contract_number, AVG(amt_interest_income) AS avg_interest_income
  FROM dm_fin.fin_ltv_datamart
  GROUP BY contract_number
) dm ON dm.contract_number = sd.contract_number
WHERE sd.contract_state <> 'Cancelled' 
  AND sd.cnt_signed = 1 
  AND sd.loan_type = 'Consumer Loan'
  AND sd.dtime_signature >= DATE '2022-08-01'
  AND sd.dtime_signature < DATE '2022-09-01'
ORDER BY sd.dtime_signature;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从具有默认范围的单个列中选择唯一值,并返回多个列 - How to select unique values from a single column, with a default scope, returning multiple columns 在 MS Access 中,如何在单个列中提取多个值? - In MS Access, How do I extract multiple values in a single column? 如何使用sqlite在单个列中插入多个值? - How do I insert multiple values into a single column using sqlite? 如何选择唯一的列值并显示其另一列的值的总和? - How do I select a unique column value and display its sum of values of another column? 如何在MySQL中从具有多个表的单列中选择多个值? - How to select multiple values from single column with multiple tables in MySQL? 在标准SQL中,如何选择行,以便对于一列中的每个唯一值,另一列中的所有值都是指定值? - In standard SQL, how do I select rows such that for each unique value in one column, all of the values in another column are a specified value? 如何从一行中选择多个值并将其作为单个列值联接 - How to SELECT multiple values from a row and join it as a single column value 在单个查询中选择多个列值-Oracle - Select multiple column values in single query - oracle 从多列中选择一个值 - Select values from multiple columns into single column 从单列中选择多个值 - Select Multiple Values From Single Column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM