Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Pivot or Query

Posted on 2014-02-10
7
Medium Priority
?
327 Views
Last Modified: 2014-02-24
SQL coding question.
Consider the following table: Itin

 ID      Restraunt      Date        Position
 ---  --------------    ------ ----------
  1       Mikes            2/1/14  Waiter-1
  2       Mikes            2/1/14  Waiter-2
  3       Mikes            2/1/14  Waiter-3
  4       John            2/1/14  FrontDesk
  5       John            2/1/14  Bartender
  6       John            2/2/14  Waiter-1
  7       John            2/2/14  Waiter-3
 15       Mikes            2/2/14  Waiter-3
  8       Mikes            2/2/14  Tables
  9       Mikes            2/3/14  Waiter-3
 10       Mikes            2/3/14  FrontDesk
 11       Mikes            2/4/14  Bartender
 12       Mikes            2/4/14  Waiter-1
 13       Mikes            2/4/14  Waiter-3

What I want to do, is show the above in a grid of at least three or four columns.
Like this:
Restraunt       Date        Positions
 --------------  ------ ---------
  John             2/1/14 Bartender      Frontdesk      
  John              2/2/14      Waiter-1      Waiter-3
  Mike             2/1/14      Waiter-1      Waiter-2      Waiter-3
  Mike             2/2/14 Waiter-3      FronDesk

Is there anyway to remotely do this via a SQL query?
0
Comment
Question by:datafarmer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 2000 total points
ID: 39849511
Hi,

You may check this code:
SELECT [Restraunt], [Date], MAX(T.Positions)
FROM Itin I
CROSS APPLY
(
  SELECT [Position] + ',' AS [text()] 

  FROM Itin 

  WHERE [Restraunt] = I.[Restraunt]
         AND [Date] = I.[Date]

  FOR XML PATH('')
) T(Positions)
GROUP BY [Restraunt], [Date]

Open in new window


Have a look at this SQL Fiddle too:

Giannis
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39849748
Your topic areas include both MS Access and SQL Server.

Which are you using?  The SQL Syntax and/or code will be quite different depending on which platform you are using.
0
 

Author Comment

by:datafarmer
ID: 39849909
The reason I said Access and SQL is because I have to code for both, if possible.  I understand the syntax would be different.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 61

Expert Comment

by:mbizup
ID: 39849984
What is the exact setup?  Access Front-End/SQL Back-end?
0
 

Author Comment

by:datafarmer
ID: 39850197
The data is housed in two separate applications.  Desktop can use Access.  Web based is strictly MS SQL 2005.   We would like to be able to offer the same functionality (or view) to both sets of users.   The actual SQL query can be different.. as our software allows for that.
So I can have query that is used for Access an another that is used for SQL.
 
Access databases are 2003, and up.  Sql databases are 2005 and up.
0
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 39850201
Sorry, but i do not know about Access. About SQL, did you try my query posted earlier?
0
 

Author Closing Comment

by:datafarmer
ID: 39884585
wanted Access as well.. but no other suggestions.... Thanks for SQL
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question