SQL:如何对具有不同类型的记录进行分组

huangapple go评论89阅读模式
英文:

SQL: How to group records, when they have different types

问题

以下是您提供的内容的翻译:

"I do not know if I might be over-thinking this task. But I know I need some help. I have to modify a query in order to combine/sum those records that have multiple payment types for the same TransConfirmID."

我不知道是否可能过于思考这个任务。但我知道我需要一些帮助。我必须修改一个查询,以便合并/总结具有相同TransConfirmID的多种付款类型的记录。

"The data below shows the first three records with the same TransConfirmID, but 2 different payment types. These records should have their payment amount summed up, and the payment type should be 'MULTI'."

下面的数据显示了具有相同TransConfirmID但有2种不同付款类型的前三条记录。这些记录应该将它们的付款金额相加,并且付款类型应该是'MULTI'。

"The other records are various of the same TransConfirmID but with the same Payment Type for all 'ACH', these records do not need to be summed up/combined, they can stay separated, they do not need to be grouped."

其他记录是相同的TransConfirmID,但对于所有'ACH'都有相同的付款类型,这些记录不需要总结/合并,它们可以保持分开,不需要分组。

"Is this a change I can get help to complete?"

这个改变我可以得到帮助完成吗?

"I am including a bit of sample data, and the query as I have it so far."

我包括了一些示例数据,以及迄今为止我所拥有的查询。

"DESIRED OUTPUT (based on the sample data, this affects only the 3 first rows really)"

期望的输出(根据示例数据,实际上只影响前三行)

"Thank you in advance for your help, and/or for reading this question."

提前感谢您的帮助,和/或阅读这个问题。

英文:

I do not know if I might be over-thinking this task. But I know I need some help. I have to modify a query in order to combine/sum those records that have multiple payment types for the same TransConfirmID.

The data below shows the first three records with the same TransConfirmID, but 2 different payment types. These records should have their payment amount summed up, and the payment type should be 'MULTI'.

The other records are various of the same TransConfirmID but with the same Payment Type for all 'ACH', these records do not need to be summed up/combined, they can stay separated, they do not need to be grouped.

Is this a change I can get help to complete?

I am including a bit of sample data, and the query as I have it so far.

Data

AcctNumber	PaymentAmount	InitiationDate	SourceType	InvoiceNumber	PaymentType	TransConfirmID
80924401229	-41.09			12/17/2019		PRPONL		NULL			CASH		1072667
80924401229	1600			12/17/2019		PRPONL		NULL			CASH		1072667
80924401229	2400			12/17/2019		PRPONL		NULL			ACH			1072667
70919228936	32.39			12/12/2019		PRPKUB		NULL			ACH			34676067987
70919228093	2086.69			12/12/2019		PRPKUB		NULL			ACH			34676067987
70826415014	4003.87			12/13/2019		PRPKUB		NULL			ACH			34728080729
70824306182	8819.61			12/13/2019		PRPKUB		NULL			ACH			34728080729
70913350587	259.54			12/13/2019		PRPKUB		NULL			ACH			34733928441
70913349150	5952.72			12/13/2019		PRPKUB		NULL			ACH			34733928441
70922346155	259.54			12/13/2019		PRPKUB		NULL			ACH			34737135402
70922345404	9225.62			12/13/2019		PRPKUB		NULL			ACH			34737135402

SQL Query

SELECT 
	RIGHT('0000000000000000' + AccountNumber, 16) AS AcctNumber
,	PaymentAmount
,	RIGHT('0' + RTRIM(DATEPART(MM, InitiationDate)), 2) + '/' + RIGHT('0' + RTRIM(DATEPART(dd, InitiationDate)), 2) + '/' + RIGHT(DATEPART(yyyy, InitiationDate), 4) AS InitiationDate
,	PropertyTaxHeader.SourceType AS SourceType
,	InvoiceNumber
,	LTRIM(RTRIM(PaymentType)) AS PaymentType
,	TransConfirmID
FROM 
	PropertyTaxDetail
INNER JOIN PropertyTaxHeader
ON PropertyTaxDetail.HeaderID = PropertyTaxHeader.id
WHERE 	
	PropertyTaxHeader.STATUS = 'Active'
	PaymentStatus NOT IN ('RETN', 'RFND', 'RETURNED', 'REFUNDED') 	
ORDER BY TransConfirmID

DESIRED OUTPUT (based on the sample data, this affects only the 3 first rows really)

AcctNumber	PaymentAmount	InitiationDate	SourceType	InvoiceNumber	PaymentType	TransConfirmID
80924401229 3958.91			12/17/2019		PRPONL		NULL			MULTI		1072667
70919228936	32.39			12/12/2019		PRPKUB		NULL			ACH			34676067987
70919228093	2086.69			12/12/2019		PRPKUB		NULL			ACH			34676067987
70826415014	4003.87			12/13/2019		PRPKUB		NULL			ACH			34728080729
70824306182	8819.61			12/13/2019		PRPKUB		NULL			ACH			34728080729
70913350587	259.54			12/13/2019		PRPKUB		NULL			ACH			34733928441
70913349150	5952.72			12/13/2019		PRPKUB		NULL			ACH			34733928441
70922346155	259.54			12/13/2019		PRPKUB		NULL			ACH			34737135402
70922345404	9225.62			12/13/2019		PRPKUB		NULL			ACH			34737135402

Thank you in advance for your help, and/or for reading this question.

Best regards.

答案1

得分: 3

以下是翻译好的部分:

> 下面的数据显示了具有相同的TransConfirmID的前三条记录,但具有2种不同的付款类型。这些记录的付款金额应该被总结,并且付款类型应该为'MULTI'。

如果你使用 GROUP BY TransConfirmID,那么在 SELECT 区域可以这样写:

CASE WHEN MIN(PaymentType) <> MAX(PaymentType) THEN 'MULTI' ELSE MIN(PaymentType) END as PaymentType

如果PaymentType可能为null,请考虑如何处理这些情况。此代码在确定是否显示MULTI时会忽略NULL值。如果值只有"CASH/NULL",并且这应该是一个多重付款,请使用COALESCE为null赋予一个永远不会出现在列中的值:

CASE WHEN MIN(COALESCE(PaymentType, 'xxx')) <> MAX(PaymentType) THEN 'MULTI' ELSE MIN(PaymentType) END as PaymentType

> 其他记录都是相同的TransConfirmID,但所有的付款类型都是'ACH',这些记录不需要被汇总/合并,它们可以保持分开,不需要分组。

你使用了像"can"和"not need"这样的措辞,而不是硬性规定"must/shouldn't",所以我认为仅按TransConfirmID分组是可以接受的,因为如果这样做,这些事情将会被合并;你暗示这是可以的。如果你确实希望它们分开,看起来你应该考虑按AcctNumber分组。

或者,要回答标题中提出的问题 - "如何分组记录,当它们具有不同的类型",你可以使用CASE WHEN将不同的类型转换为相同的东西。以下是一个示例:

SELECT
  Country,
  CASE PaymentType 
    WHEN 'CASH' THEN 'Untraceable'
    WHEN 'Bitcoin' THEN 'Untraceable'
    WHEN 'Visa' THEN 'Traceable'
    WHEN 'Mastercard' THEN 'Traceable'
  END as Traceability,
  SUM(Amount) as SumAmount
FROM
  data
GROUP BY
  Country,
  CASE PaymentType 
    WHEN 'CASH' THEN 'Untraceable'
    WHEN 'Bitcoin' THEN 'Untraceable'
    WHEN 'Visa' THEN 'Traceable'
    WHEN 'Mastercard' THEN 'Traceable'
  END

与获取现金/比特币/Visa/Mastercard的总和(4行)不同,此查询生成2行,将不同的付款类型值合并为单个值。

如果要合并许多值,请考虑添加一个将paymenttype的数百个值映射到各种(相同的)traceability值的表,并将其加入,然后根据连接表的表列进行分组。

英文:

>The data below shows the first three records with the same TransConfirmID, but 2 different payment types. These records should have their payment amount summed up, and the payment type should be 'MULTI'.

If you GROUP BY TransConfirmID then you could say in the SELECT area:

CASE WHEN MIN(PaymentType) <> MAX(PaymentType) THEN 'MULTI' ELSE MIN(PaymentType) END as PaymentType

If PaymentType will ever be null, consider how you want to handle those. This code ignores NULLs, when working out if a MULTI should be shown. If the values are only "CASH/NULL" and this should be a multi, use COALESCE to give the null a value that will never appear in the column:

CASE WHEN MIN(COALESCE(PaymentType, 'xxx')) <> MAX(PaymentType) THEN 'MULTI' ELSE MIN(PaymentType) END as PaymentType

>The other records are various of the same TransConfirmID but with the same Payment Type for all 'ACH', these records do not need to be summed up/combined, they can stay separated, they do not need to be grouped.

You've used language like "can" and "not need" rather than a hard rule of "must/shouldn't" so I assume that grouping by only TransConfirmID is acceptable, because if you do so then these things WILL become combined; you imply that this is OK. If you want them separated for sure, it looks like you should consider grouping by AcctNumberinstead

Alternatively, to answer the question asked in the title - "How to group records, when they have different types", you can use a CASE WHEN do convert the different types into the same thing. Here is an example:

  SELECT
    Country,
    CASE PaymentType 
      WHEN 'CASH' THEN 'Untraceable'
      WHEN 'Bitcoin' THEN 'Untraceable'
      WHEN 'Visa' THEN 'Traceable'
      WHEN 'Mastercard' THEN 'Traceable'
    END as Traceability,
    SUM(Amount) as SumAmount
  FROM
    data
  GROUP BY
    Country,
    CASE PaymentType 
      WHEN 'CASH' THEN 'Untraceable'
      WHEN 'Bitcoin' THEN 'Untraceable'
      WHEN 'Visa' THEN 'Traceable'
      WHEN 'Mastercard' THEN 'Traceable'
    END

Rather than getting sum totals for cash/bitcoin/visa/mastercard (4 rows) this query produces 2 rows, with different values for payment types rolled up into a single value

If you have a lot of values to roll up consider adding a table that maps the hundreds of values for paymenttype to the various (same) traceability values and join it in, then group on the table columns of the joined table

huangapple
  • 本文由 发表于 2020年1月7日 01:09:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定