2017-02-13 3 views
2

Ich brauche einen Wert von verschiedenen Abfrage in MS SQL Server. Sie können sehen, ich habe Abfrage erstellt, aber Ergebnis hat zwei Wert in "ManifestNo" ist 5. Wenn "ManifestNo" Spalte enthalten "A", schreiben Sie "A" sonst schreiben Sie "B".Sql Server Distinct One Value

Vielen Dank für Ihre Unterstützung.

SELECT distinct a.ManifestNo ,CASE WHEN a.[Active]='A' THEN 'A' ELSE 'P' END AS ActiveState 
FROM [YOTK_WH_Trace].[dbo].[Orders] as a INNER JOIN RefTable as b ON a.WEBARD1_PARTNO = b.UrunNo 
where [ManifestYear]=2017 and [ManifestMonth]=02 and [ManifestDay]=13 and (b.State = 1) 
order by a.ManifestNo 

Images

ManifestNo ActiveState 
1   P 
2   A 
3   A 
4   A 
5   A 
5   P 
6   A 

I Tabelle unten brauchen:

ManifestNo ActiveState 
1   P 
2   A 
3   A 
4   A 
5   A 
6   A 
+2

fügen Sie Ihre Anfrage –

+1

Bitte, formatierte Text für Datentabelle - nicht Bilder. – jarlh

+0

Es gibt 2 unterschiedliche Werte von '(MinifestNo, ActiveState)' – McNets

Antwort

1

Distinct Stichwort entfernen und verwenden GROUP BY Klausel und MIN Aggregatfunktion

SELECT a.ManifestNo ,MIN(CASE WHEN a.[Active]='A' THEN 'A' ELSE 'P' END) AS ActiveState 
FROM [YOTK_WH_Trace].[dbo].[Orders] as a 
INNER JOIN RefTable as b ON a.WEBARD1_PARTNO = b.UrunNo 
where [ManifestYear]=2017 and [ManifestMonth]=02 and [ManifestDay]=13 and (b.State = 1) 
GROUP BY a.ManifestNo 
order by a.ManifestNo 
+0

Wooouuw! Vielen Dank !! Du sparst meinen Tag :) Ich liebe "StackOverFlow" !! –

0

andere Lösung

select * from (
    SELECT a.ManifestNo ,CASE WHEN a.[Active]='A' THEN 'A' ELSE 'P' END AS ActiveState, 
    row_number() over(partition by a.ManifestNo order by CASE WHEN a.[Active]='A' THEN 'A' ELSE 'P' END) as rang 
    FROM [YOTK_WH_Trace].[dbo].[Orders] as a INNER JOIN RefTable as b ON a.WEBARD1_PARTNO = b.UrunNo 
    where [ManifestYear]=2017 and [ManifestMonth]=02 and [ManifestDay]=13 and (b.State = 1) 
) tmp 
where tmp.rang=1 
order by tmp.ManifestNo