[英]Select multiple columns with GROUP BY SQL
I am trying to select the min date value from one column, while grouping by another non-unique id column which has multiple duplicate values and also want to show the same rows that satisfy previous two conditions.我试图从一列中选择最小日期值,同时按另一个具有多个重复值的非唯一 id 列进行分组,并且还想显示满足前两个条件的相同行。 The original database columns looks something like:
原始数据库列类似于:
['TransactionDate','TransactionStatusID','DateOfEntry','DateOfService','Amount','TransactionTypeID','PaymentTypeId','PostDate','BillID','ChargeDetailID'] ['TransactionDate','TransactionStatusID','DateOfEntry','DateOfService','Amount','TransactionTypeID','PaymentTypeId','PostDate','BillID','ChargeDetailID']
and 2nd table looks like:第二张桌子看起来像:
['Amount', 'PracticeID', 'LoadDate', 'DateOfService', 'PerformingProvderID', 'CPTCodeID', 'ChargeDetailID'] ['Amount', 'PracticeID', 'LoadDate', 'DateOfService', 'PerformingProvderID', 'CPTCodeID', 'ChargeDetailID']
I am joining these two tables on "ChargeDetailID" like:我在“ChargeDetailID”上加入这两个表,例如:
SELECT cd.PatientID, MIN(t.DateOfService) AS "Date Of Service"
FROM ChargeDetail as cd inner join transactions as t on cd.ChargeDetailID = t.ChargeDetailID
WHERE cd.PatientID IS NOT NULL
GROUP BY cd.PatientID
Results that I get from this query:我从这个查询中得到的结果:
| PatientID|Date Of Service|
+--------------------+---------------+
|D37224C7-9157-4C5...| 2020-03-30|
|5E693AF8-8332-477...| 2014-01-07|
|8C071F5C-2210-4A6...| 2013-02-14|
|38B11DAE-3FF1-4AD...| 2012-03-14|
|7DB00C43-36F6-40E...| 2009-01-16|
|940EEC5C-4781-4EB...| 2016-12-08|
Results I want:我想要的结果:
| PatientID|Date Of Service| cd.Amount| cd.BillID|
+--------------------+---------------+-----------+----------+
|D37224C7-9157-4C5...| 2020-03-30| 500 |10009
|5E693AF8-8332-477...| 2014-01-07| 250 |1492
|8C071F5C-2210-4A6...| 2013-02-14| 350 |15892
|38B11DAE-3FF1-4AD...| 2012-03-14| 222 |15596
|7DB00C43-36F6-40E...| 2009-01-16| 899 |20566
|940EEC5C-4781-4EB...| 2016-12-08| 650 |9566
You can use windows function as below-您可以使用 Windows 功能如下 -
Select
PatientID
, Amount
, BillId
, DateOfService As "Date Of Service"
from
(SELECT
cd.PatientID
,cd.Amount
,cd.BillId
,t.DateOfService
,row_number() over(partition by cd.PatientID order by t.DateOfService asc) as seqnum
FROM ChargeDetail as cd inner join transactions as t
on cd.ChargeDetailID = t.ChargeDetailID
WHERE cd.PatientID IS NOT NULL) t
Where t.seqnum = 1;
Note the MySQL & SqlServer are 2 different databases however the above approach should work for both.请注意 MySQL 和 SqlServer 是 2 个不同的数据库,但是上述方法应该适用于两者。
You can add cd.amount and cd.billid in the group by.您可以在组中添加 cd.amount 和 cd.billid。 But requirement isnt much clear.
但是要求不是很清楚。 You can use window functions too.
您也可以使用窗口函数。
SELECT cd.PatientID, cd.Amount, cd.BillID, MIN(t.DateOfService) AS "Date Of Service"
FROM ChargeDetail as cd inner join transactions as t on cd.ChargeDetailID = t.ChargeDetailID
WHERE cd.PatientID IS NOT NULL
GROUP BY cd.PatientID , cd.Amount, cd.BillID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.