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
Solved

SQL Pivot or Query

Posted on 2014-02-10
7
313 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

829 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