简体   繁体   中英

SELECT DISTINCT AND GET ALL VALUES FOR EACH COLUMN SQL ACCESS

I have a table that have grouped all samples returned from the lab analysis of my work. The problem is that some labs send different elements via different files, and now I'm facing problems.

I need to make a query that grab all values that are stored in different columns and assign it in just one row for each sample. Example:

          Batch Source_File Value Value 2
SAMPLE A   1        A        150     null
SAMPLE A   1        B        null    100
SAMPLE B   2        C        null    300
SAMPLE B   2        D        100     null

OUTPUT

         Batch Source_File Value Value 2
SAMPLE A   1       A,B      150   100
SAMPLE B   2       C,D      100   300

You can use plain SQL:

SELECT 
    Samples.Sample, 
    Samples.Batch, 
    Min([Source_File]) & "," & Max([Source_File]) AS Source_Files,
    Sum(Samples.Value) AS Sum1, 
    Sum(Samples.Value2) AS Sum2
FROM 
    Samples
GROUP BY 
    Samples.Sample, 
    Samples.Batch;

Output:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM