Solved

SQL Pivot or Query

Posted on 2014-02-10
7
311 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 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 is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

867 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now