SQL double pivot table

I have the following working SQL pivot query:

SELECT ProjectID, TEDescription, [Sun],[Mon], [Tue], [Wed], [Thu], [Fri], [Sat]
FROM (
      SELECT EmployeeID, ProjectID, TEDescription, TEHours, LEFT(DATENAME(WEEKDAY, TEDate), 3) WeekDayName FROM TimeEntry
      WHERE (EmployeeID='BJD' AND TEDate>='9/6/2015' And TEDate<='9/12/2015')
) data
PIVOT (
  SUM(TEHours)
  FOR WeekDayName IN ([Sun], [Mon], [Tue], [Wed], [Thu], [Fri], [Sat])
) p
ORDER BY ProjectID

I would like to add the field TEID which matches the TEHours record in the pivot fields.  I am thinking the additional output fields could be SunTEID, MonTEID and so on.  Results would as follows:

ProjectID   TEDescription  Sun  SunTEID  Mon MonTEID ...
15-001       Testing              1.0   abc            2.0   def
15-001       More Testing   2.0   ghi             1.0   jkl
15-002       Coding              3.0   mno          1.0   pqr
15-003       Coding              2.0   stu             2.0   vwx
.
.
.

Any help here?
MrBeedgeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulEE Topic AdvisorCommented:
So the previous accepted answer, which was based on excluding the description, was not correct after all. oh well. The rationale presented at that time was that there could be more than one description hence it would make too may rows.

However as we don't know your data in any depth we still don't know if this problem still exists in your new layout.

Is there any chance (ANY chance at all) of more than one description per day? if yes, what are we to do?
PortletPaulEE Topic AdvisorCommented:
Aside from the questions above, I think you are better off now ignoring the "pivot operator" and instead use case expressions to perform the transposition from rows to columns, as follows:
select
        EmployeeID
      , ProjectID
      , SUM(case when datediff(day,0,TEDate) % 7 = 6 then Hours end)         Sun
      , MAX(case when datediff(day,0,TEDate) % 7 = 6 then TEDescription end) SunDesc
      , SUM(case when datediff(day,0,TEDate) % 7 = 0 then Hours end)         Mon
      , MAX(case when datediff(day,0,TEDate) % 7 = 0 then TEDescription end) MonDesc
      , SUM(case when datediff(day,0,TEDate) % 7 = 1 then Hours end)         Tue
      , MAX(case when datediff(day,0,TEDate) % 7 = 1 then TEDescription end) TueDesc
      , SUM(case when datediff(day,0,TEDate) % 7 = 2 then Hours end)         Wed
      , MAX(case when datediff(day,0,TEDate) % 7 = 2 then TEDescription end) WedDesc
      , SUM(case when datediff(day,0,TEDate) % 7 = 3 then Hours end)         Thu
      , MAX(case when datediff(day,0,TEDate) % 7 = 3 then TEDescription end) ThuDesc
      , SUM(case when datediff(day,0,TEDate) % 7 = 4 then Hours end)         Fri
      , MAX(case when datediff(day,0,TEDate) % 7 = 4 then TEDescription end) FriDesc
      , SUM(case when datediff(day,0,TEDate) % 7 = 5 then Hours end)         Sat
      , MAX(case when datediff(day,0,TEDate) % 7 = 5 then TEDescription end) SatDesc
from Timecards
group by
        EmployeeID
      , ProjectID
order by
        EmployeeID
      , ProjectID

Open in new window

Using the sample data of your former question that query produces this output
| EmployeeID |  ProjectID |    Sun | SunDesc |    Mon | MonDesc |    Tue | TueDesc |    Wed | WedDesc |    Thu | ThuDesc |    Fri | FriDesc |    Sat |    SatDesc |
|------------|------------|--------|---------|--------|---------|--------|---------|--------|---------|--------|---------|--------|---------|--------|------------|
|        BJD | 99-999:001 | (null) |  (null) | (null) |  (null) | (null) |  (null) | (null) |  (null) |      2 |   Stuff |      3 |   Stuff |      1 | More Stuff |
|        BJD | 99-999:002 |      1 |   Stuff | (null) |  (null) | (null) |  (null) | (null) |  (null) | (null) |  (null) | (null) |  (null) | (null) |     (null) |

Open in new window

CREATE TABLE Timecards
    ([TEDate] datetime, [EmployeeID] varchar(3), [ProjectID] varchar(10), [TEDescription] varchar(10), [Hours] int)
;
    
INSERT INTO Timecards
    ([TEDate], [EmployeeID], [ProjectID], [TEDescription], [Hours])
VALUES
    ('2015-01-01 00:00:00', 'BJD', '99-999:001', 'Stuff', 2),
    ('2015-01-02 00:00:00', 'BJD', '99-999:001', 'Stuff', 3),
    ('2015-01-03 00:00:00', 'BJD', '99-999:001', 'More Stuff', 1),
    ('2015-01-04 00:00:00', 'BJD', '99-999:002', 'Stuff', 1)
;

Open in new window

also see: http://sqlfiddle.com/#!6/c90ab/7

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MrBeedgeAuthor Commented:
Paul -

This got it done for me with some minor edits.  Couldn't have done it without your help - SQL is not my thing.  Thanks again!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.