Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Combining Rows
Message
De
18/03/2015 18:16:24
 
 
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Titre:
Versions des environnements
SQL Server:
SQL Server 2014
Application:
Web
Divers
Thread ID:
01616946
Message ID:
01616953
Vues:
28
>>>>>>I have a table that looks like this
>>>>>>
>>>>>>Location ServiceDay
>>>>>>
>>>>>>Farmingdale Monday
>>>>>>Farmingdale Tuesday
>>>>>>Bethpage Monday
>>>>>>Bethpage Saturday
>>>>>>
>>>>>>I'd like to have a query that gives me one row for each location like this
>>>>>>
>>>>>>Location Sunday Monday Tuesday Wednesday Thursday Friday Saturday
>>>>>>Farmingdale Yes Yes
>>>>>>Bethpage Yes Yes
>>>>>>
>>>>>>
>>>>>>I want yes or space to show in column relating to the service day
>>>>>>Any ideas?
>>>>>
>>>>>Look up PIVOT operator in SQL Server. It is very easy.
>>>>
>>>>I did and can't make sense of it.
>>>>They seem to want to aggregate.
>>>
>>>Select * from (select Location, ServiceDay, 'Yes' as [Service] from ServicesPerformed) X 
>>>PIVOT (max(Service) for ServiceDay IN ([Monday], [Tuesday], etc.)) pvt
>>>
>>>If you don't like NULL you would need
>>>
>>>select Location, COALESCE([Monday], '') as Monday, etc.
>>>from ...
>>
>>Thank you, Naomi
>>I'll try it.
>
>I was updating my message with the alternative solution
>
>Or just use case based pivot.
>
>select Location, max(case when ServiceDay = 'Monday' then 'Yes' else '' end) as Monday,
>etc.
>
>from ServicesPerformed
>GROUP BY Location
>ORDER BY Location
>
>You can also read these articles:
>
>http://blogs.lessthandot.com/index.php/datamgmt/datadesign/understanding-sql-server-2000-pivot/
>
>and
>
>http://blogs.lessthandot.com/index.php/datamgmt/datadesign/dynamic-pivot-on-multiple-columns/
>
>And in TechNet Wiki:
>
>http://social.technet.microsoft.com/wiki/contents/articles/17351.sql-server-pivot.aspx

Thanks, Naomi
I tried the case based pivot and that worked, but I'll also try to get it using a pivot

Got the pivot working.
Thanks again, Naomi.
Anyone who does not go overboard- deserves to.
Malcolm Forbes, Sr.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform