在查询中获取不同的行

Get Distinct rows in query

我有 SQL 服务器查询以从 SQL 服务器获取数据到 Python 数据框,如下所示。我只需要保留基于两列的唯一行 日期 & 计数器

SELECT 
    CAST([A2_TIRE_A_CO_D300_TIMESTAMP] AS DATE) AS date,
    [A2_TIRE_A_CO_D362_VALUE] AS counter,
    [A2_TIRE_A_CO_D400_VALUE] AS D400,
    [A2_TIRE_A_CO_D402_VALUE] AS D402,
    [A2_TIRE_A_CO_D412_VALUE] AS D412,
    [A2_TIRE_A_CO_D414_VALUE] AS D414,
    [A2_TIRE_A_CO_D416_VALUE] AS D416,
    [A2_TIRE_A_CO_D420_VALUE] AS D420,
    [A2_TIRE_A_CO_D422_VALUE] AS D422,
    [A2_TIRE_A_CO_D432_VALUE] AS D432
FROM 
    [aaaa2_tttt_a].[dbo].[tttt_a] 
WHERE
    [A2_TIRE_A_CO_D300_TIMESTAMP] >= DATEADD(Hour, -1, GETDATE()) 
    AND [A2_TIRE_A_CO_L102_VALUE] = 1 
    AND [A2_TIRE_A_CO_L100_VALUE] = 1
| date       | counter | D400        | D402                         | D412        |
|------------|---------|-------------|------------------------------|-------------|
| 2022-04-23 | 2434    | 12848 14393 | 20808 8257 12339 13360 12601 | 16705 16948 |
| 2022-04-23 | 2434    | 12848 14393 | 20808 8257 12339 13360 12601 | 16705 16948 |
| 2022-04-23 | 2434    | 12848 14393 | 20808 8257 12339 13360 12601 | 16705 16948 |
| 2022-04-23 | 2435    | 12848 14393 | 20808 8257 12339 13360 12601 | 16705 16948 |
| 2022-04-23 | 2435    | 12848 14393 | 20808 8257 12339 13360 12601 | 16705 16948 |
| 2022-04-23 | 2435    | 12848 14393 | 20808 8257 12339 13360 12601 | 16705 16948 |
| 2022-04-23 | 2435    | 12848 14393 | 20808 8257 12339 13360 12601 | 16705 16948 |

要求

| date       | counter | D400        | D402                         | D412        |
|------------|---------|-------------|------------------------------|-------------|
| 2022-04-23 | 2434    | 12848 14393 | 20808 8257 12339 13360 12601 | 16705 16948 |
| 2022-04-23 | 2435    | 12848 14393 | 20808 8257 12339 13360 12601 | 16705 16948 |

我尝试了不同的方法和其他来源。我该怎么做?

我尝试通过连接日期和计数器并应用不同来创建新列:

DISTINCT(CONCAT(CAST([A2_TIRE_A_CO_D300_TIMESTAMP] AS DATE),[A2_TIRE_A_CO_D362_VALUE]) AS unique_column)

但我认为这是不正确的方法

你可以尝试使用ORDER BYwindowROW_NUMBER的功能。

partition by 可能需要放置您要表示的列 unique 行。

SELECT TOP 1 WITH 
    CAST([A2_TIRE_A_CO_D300_TIMESTAMP] AS DATE) as date,
    [A2_TIRE_A_CO_D362_VALUE] as counter,
    [A2_TIRE_A_CO_D400_VALUE] as D400,
    [A2_TIRE_A_CO_D402_VALUE] as D402,
    [A2_TIRE_A_CO_D412_VALUE] as D412,
    [A2_TIRE_A_CO_D414_VALUE] as D414,
    [A2_TIRE_A_CO_D416_VALUE] as D416,
    [A2_TIRE_A_CO_D420_VALUE] as D420,
    [A2_TIRE_A_CO_D422_VALUE] as D422,
    [A2_TIRE_A_CO_D432_VALUE] as D432
FROM [aaaa2_tttt_a].[dbo].[tttt_a] 
where 
    [A2_TIRE_A_CO_D300_TIMESTAMP] >= DATEADD(Hour,  -1,  GETDATE()) 
and 
    [A2_TIRE_A_CO_L102_VALUE] =1 
and 
    [A2_TIRE_A_CO_L100_VALUE] =1
order by row_number() over (partition by CAST([A2_TIRE_A_CO_D300_TIMESTAMP] AS DATE),[A2_TIRE_A_CO_D362_VALUE] order by date)

但我认为您可以直接从样本数据中使用 DISTINCT 并期待结果。

SELECT DISTINCT CAST([A2_TIRE_A_CO_D300_TIMESTAMP] AS DATE) as date,
    [A2_TIRE_A_CO_D362_VALUE] as counter,
    [A2_TIRE_A_CO_D400_VALUE] as D400,
    [A2_TIRE_A_CO_D402_VALUE] as D402,
    [A2_TIRE_A_CO_D412_VALUE] as D412,
    [A2_TIRE_A_CO_D414_VALUE] as D414,
    [A2_TIRE_A_CO_D416_VALUE] as D416,
    [A2_TIRE_A_CO_D420_VALUE] as D420,
    [A2_TIRE_A_CO_D422_VALUE] as D422,
    [A2_TIRE_A_CO_D432_VALUE] as D432
FROM [aaaa2_tttt_a].[dbo].[tttt_a] 
where 
    [A2_TIRE_A_CO_D300_TIMESTAMP] >= DATEADD(Hour,  -1,  GETDATE()) 
and 
    [A2_TIRE_A_CO_L102_VALUE] =1 
and 
    [A2_TIRE_A_CO_L100_VALUE] =1