How to display Distinct values in cfoutput

2019-08-16 03:40发布

问题:

I have a query that returns results from the database. It returns duplicate information. I would like to prevent showing the duplicate data.

What I mean by duplicate data is that the Admit Date, Service Date, and Discharge Date columns repeats the same data (in this case dates). So if the date is 10/05/2019, for Admit Date, it will repeat the date 2 or more times. This is due to the Process Date column where the process of submitting the inquiry were done in different dates and causes the other dates to repeat twice.

I originally tried using SELECT DISTINCT Column1, Column2, ... FROM Data in my query but it errors outs. The following is the error I am getting:

Error Executing Database Query. [Macromedia][SQLServer JDBC Driver][SQLServer]The text data type cannot be selected as DISTINCT because it is not comparable.

What am I doing wrong? Any help would be appreciated

Here is my latest attempt:

<cfquery name="name" datasource="source">
        SELECT First_Name, 
               Last_Name, 
               DOB, Sex, 
               Service, 
               Service_Desc, 
               Distinct(service_dt) AS Service_Dt, 
               Distinct(admit_dt) AS Admit_Dt, 
               Phone, 
               Address1, 
               Address2, 
               City, 
               State, 
               Zip, 
               Account, 
               Hosp_Name,
               MR_Number, 
               Insurance, 
               Plan_Name, 
               Policy_No, 
               Group_No, 
               Reason_For_Visit, 
               Distinct(process_dt) AS Process_Dt,
               Distinct(discharge_dt) AS Discharge_Dt,
               [Pt Class] as PtClass
        FROM DATA
        WHERE 
            First_Name = <cfqueryparam value="#FName#" cfsqltype="cf_sql_varchar">
            AND
            Last_Name = <cfqueryparam value="#LName#" cfsqltype="cf_sql_varchar">
            AND
            DOB = <cfqueryparam value="#BirthDt#" cfsqltype="cf_sql_varchar">
            AND
            Hosp_Name like <cfqueryparam value="#Hosp#" cfsqltype="cf_sql_varchar">
    </cfquery>

回答1:

First things first, this is purely a SQL issue, not at all a ColdFusion issue.

Your data is consistent for all records, with the exception of the different date columns? Let's say there are four records for user_id = 1:

| user_id | admit_date | service_date | process_date | discharge_date |
| ------- | ---------- | ------------ | ------------ | -------------- |
| 1       | 2018-01-01 | 2018-01-01   | 2018-01-02   | 2018-01-05     |
| 1       | 2018-01-01 | 2018-01-02   | 2018-01-02   | 2018-01-05     |
| 1       | 2018-01-01 | 2018-01-03   | 2018-01-03   | 2018-01-05     |
| 1       | 2018-01-01 | 2018-01-04   | 2018-01-04   | 2018-01-05     |

Is this what your data looks like for a single person?

I'm imagining this data is just an aggregated data dump, so the admit_date is the same for all 4 records, as is the discharge_date. The other two dates are different for each record. Because this one user has the same first name, last name, dob and was at the same hospital, it matches your query criteria.

If this is good data, then selecting a distinct admit_date will give me 4 records, as will selecting a distinct discharge_date.

  • Are you trying to find the most recent record for this or all users?
  • Are you trying to find the first?
  • What are you actually trying to pull out of this data?

If you wanted to show an output of data, grouped by a single date, that's pretty easy:

<cfoutput query="qData" group="user_id">
    <!--- Output User Info here. --->
    <cfoutput group="admit_date">
        <!--- 
            This will loop over all records of the same 
            admit_date for the current user ID. 
        --->
        <cfoutput>
            More output.
        </cfoutput
    </cfoutput>
</cfoutput>