sql >> Base de Datos >  >> RDS >> Sqlserver

Consulta Sql con unirse y agrupar por y

esta es una muestra, en su caso, debe obtener el valor de cada columna en una subconsulta (ya sea en la declaración de combinación o en la declaración de selección principal) ejemplo:

inner join (
        select distinct bartar_patientname
                ,(select top 1 bartar_pastePerMonth from [Bartar_MSCRM].[dbo].[Filteredbartar_callcenterreport] c2 where c2.bartar_patientname = cte.bartar_patientname and c2.bartar_pastePerMonth is not null order by c2.bartar_date desc) as bartar_date
                ,(select top 1 bartar_acc           from [Bartar_MSCRM].[dbo].[Filteredbartar_callcenterreport] c2 where c2.bartar_patientname = cte.bartar_patientname and c2.bartar_acc is not null order by c2.bartar_date desc) as bartar_acc
                ,(select top 1 bartar_insuranceinfo from [Bartar_MSCRM].[dbo].[Filteredbartar_callcenterreport] c2 where c2.bartar_patientname = cte.bartar_patientname and c2.bartar_insuranceinfo is not null order by c2.bartar_date desc) as bartar_insuranceinfo
                ,(select top 1 bartar_brand         from [Bartar_MSCRM].[dbo].[Filteredbartar_callcenterreport] c2 where c2.bartar_patientname = cte.bartar_patientname and c2.bartar_brand is not null order by c2.bartar_date desc) as bartar_brand
        from [Bartar_MSCRM].[dbo].[Filteredbartar_callcenterreport] cte
        ) r

Nuevamente, esta es una muestra de la solución.