Share via

Hilfe bei einer Query T-Sql

Moser Matthias 21 Reputation points
2022-01-11T14:39:59.717+00:00

Hallo Liebe Leute

Ich bekomme eine Anforderung in meinem Kopf nicht aufgelöst.

Database MS Sql 2019

In der Tabelle habe ich eine Spalte mit "Auftrag"

Die Werte sehen Beispielsweise so aus:
123456_1
123456
123456_2
1233214
4567890_1
4567890_2
4567890

Ich mus mit einem Select dieses Ergebnis erziehlen:

SplitCount | Auftrag

3 | 123456
1 | 123321
3 | 4567890

4
Ich hoffe irgend wer von euren schlauen Köpfen kann mir Helfen

Mit alller besten Grüßen
Euer Loki

Azure Data Factory
Azure Data Factory

An Azure service for ingesting, preparing, and transforming data at scale.

0 comments No comments

Answer accepted by question author

Olaf Helper 47,621 Reputation points
2022-01-11T14:46:27.857+00:00

Hello Loki,

This is an Englisch spoken forum.

Like this?

;with demoData as
    (select '123456_1' as Auftrag
     union all select '123456'
     union all select '123456_2'
     union all select '1233214'
     union all select '4567890_1'
     union all select '4567890_2'
     union all select '4567890')

     ,sub as
    (select case when CHARINDEX('_', Auftrag, 1) = 0
                 then Auftrag
                 else LEFT(Auftrag, CHARINDEX('_', Auftrag, 1) - 1)
                 End as Auftrag
     from demoData)
select COUNT(*) as SplitCount, Auftrag
from sub
group by Auftrag
order by Auftrag

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.