Solved

SQL Pivot or Query

Posted on 2014-02-10
7
310 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Viewers will learn how the fundamental information of how to create a table.

758 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

23 Experts available now in Live!

Get 1:1 Help Now