Pregunta

I have not been able to find a similar problem (in the context of Access 2010 and TSQL) when conducting my research.

Challenge: Currently, my data looks like State #1 below. In State #1, there are duplicate rows, with the only difference being that they have differing field/column values for three particular fields (OffersMembership, HasSeating, QuietArea).

Desire: I would like to "collapse" the rows, by PlaceID and Year, and just generate a distinct count of the number of Y's present within each field for each row. I have generated sample data to represent my desired outcome.

My data currently looks as follows:

State #1

Row | Year | PlaceID | OffersMembership | HasSeating | QuietArea

001   2011    Park           Y                 Y           Y                
002   2011    Park           Y                 N           N  
003   2011   Library         Y                 Y           Y 
004   2011   Library         N                 Y           N 
005   2011   Museum          Y                 N            
006   2011   Museum          Y                 Y           Y 
006   2011  MovieTheater     Y                             N
007   2012    Park           Y                 Y           Y                
008   2012    Park                                         N 

What I want my data to look like:

State #2               

Row |  Year |  PlaceID | OffersMembership "Y"| HasSeating "Y"| QuietArea "Y" 

001    2011      Park             1                   1              1
002    2011     Library           1                   1              1
003    2011     Museum            1                   1              1
004    2011    Movietheater       1                  
004    2012      Park             1                   1              1

Notes:

1) I am using TSQL within Access 2010 because I am running Pass-Through queries that hit a SQL Server table.

2) Yes, there are "blank" cells present in the data in State #1.

¿Fue útil?

Solución

You can use a simple GROUP BY to collapse the data. I excluded the Row column because I assumed that was just a line number placeholder.

SELECT [Year]
      ,PlaceID
      ,COUNT(CASE OffersMembership WHEN 'Y' THEN 1 ELSE 0 END) AS OffersMembership
      ,COUNT(CASE HasSeating WHEN 'Y' THEN 1 ELSE 0 END) AS HasSeating
      ,COUNT(CASE QuietArea WHEN 'Y' THEN 1 ELSE 0 END) AS QuietArea
  FROM MyTable t
 GROUP BY t.[Year]
         ,PlaceID

Note: The above sample query has not been executed.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top