473,698 Members | 2,434 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert MS Access Pivot Query to SQL Server 2005

29 New Member
I have Pivot Table query in MS Access that I would like to run in SQL Server 2005. Can anyone help me to convert the syntax over to SQL Server. The following is the query in MS Access:

Expand|Select|Wrap|Line Numbers
  1.  TRANSFORM SUM(ps.psScheduled)
  2.     SELECT pi.piMaxTurns,
  3.            pi.piAMP,
  4.            pi.piTons,
  5.            '',
  6.            '',
  7.            ps.psPipe
  8.       FROM PipeScheduling ps
  9.                INNER JOIN
  10.            PipeInfo pi
  11.         ON ps.psPipe = pi.piPipe
  12.        AND ps.psMachine = pi.piMachine
  13.        AND ps.psPlant = pi.piPlant
  14.      WHERE ps.psPlant = 'W'
  15.        AND ps.psMachine = '1'
  16.        AND ps.psDate BETWEEN
  17.                #12/17/2007# 
  18.                   AND
  19.                #12/23/2007#
  20.   GROUP BY ps.psPipe,
  21.            pi.piSequence,
  22.            pi.piMaxTurns,
  23.            pi.piAMP,
  24.            pi.piTons
  25.   ORDER BY pi.piSequence
  26.      PIVOT ps.psDate
Dec 20 '07 #1
18 4510
smckibbe01
29 New Member
This is what I've come up with so far. It is still alitte slow. Any advice?

Expand|Select|Wrap|Line Numbers
  1.     SELECT pi.piMaxTurns,
  2.            pi.piAMP,
  3.            pi.piTons,
  4.            '',
  5.            '',
  6.            ps.psPipe,
  7.            SUM(CASE WHEN psDate = '1/7/2008' THEN ps.psScheduled END) AS Col1,
  8.            SUM(CASE WHEN psDate = '1/8/2008' THEN ps.psScheduled END) AS Col2,
  9.            SUM(CASE WHEN psDate = '1/9/2008' THEN ps.psScheduled END) AS Col3,
  10.            SUM(CASE WHEN psDate = '1/10/2008' THEN ps.psScheduled END) AS Col4,
  11.            SUM(CASE WHEN psDate = '1/11/2008' THEN ps.psScheduled END) AS Col5,
  12.            SUM(CASE WHEN psDate = '1/12/2008' THEN ps.psScheduled END) AS Col6,
  13.            SUM(CASE WHEN psDate = '1/13/2008' THEN ps.psScheduled END) AS Col7,
  14.            pi.piSequence
  15.       FROM fpc44.PipeScheduling ps
  16.                INNER JOIN
  17.            fpc44.PipeInfo pi
  18.                ON ps.psPlant = pi.piPlant
  19.               AND ps.psMachine = pi.piMachine
  20.               AND ps.psPipe = pi.piPipe
  21.      WHERE ps.psDate BETWEEN
  22.                '1/7/2008'
  23.                    AND
  24.                '1/13/2008'
  25.        AND ps.psPlant = 'W'
  26.        AND ps.psMachine = '1'
  27.   GROUP BY ps.psPipe,
  28.            pi.piMaxTurns,
  29.            pi.piAMP,
  30.            pi.piTons,
  31.            pi.piSequence
  32.   ORDER BY pi.piSequence
  33.  
Jan 3 '08 #2
smckibbe01
29 New Member
Does anyone know a better way of doing this?
Apr 2 '08 #3
ck9663
2,878 Recognized Expert Specialist
It would be faster for me to build your query if I'm looking at your table that you want to pivot and the the desired result.If you can post some sample data, it would be better.

-- CK
Apr 2 '08 #4
smckibbe01
29 New Member
The following is PipeInfo for plant "A" and PipeScheduling for one week for plant "A".

PipeInfo:
piPlant,piMachi ne,piPipe,piSeq uence,piMaxTurn s,piAMP,piTons
A,1,153CPB,1,,, 0.508
A,1,154CPB,2,,, 0.508
A,1,155CPB,3,,, 0.508
A,1,183CPB,4,50 ,,0.671
A,1,184CPB,5,,, 0.671
A,1,185CPB,6,,, 0.671
A,1,243CPB,7,50 ,,1.0605
A,1,244CPB,8,,, 1.0605
A,1,245CPB,9,,, 1.0605
A,1,303CPB,10,, ,1.535
A,1,304CPB,11,, ,1.535
A,1,305CPB,12,, ,1.535
A,1,363CPB,13,5 0,,2.0945
A,1,364CPB,14,, ,2.0945
A,1,365CPB,15,, ,2.0945
A,1,423CPB,16,, ,2.739
A,1,424CPB,17,, ,2.739
A,1,425CPB,18,, ,2.739
A,1,483CPB,19,, ,3.469
A,1,484CPB,20,, ,3.469
A,1,485CPB,21,, ,3.469

PipeScheduling:
psPlant,psDate, psMachine,psPip e,psScheduled,p sPoured
A,1/28/2008,1,153CPB,0 ,0
A,1/28/2008,1,154CPB,0 ,0
A,1/28/2008,1,155CPB,0 ,0
A,1/28/2008,1,183CPB,0 ,0
A,1/28/2008,1,184CPB,0 ,0
A,1/28/2008,1,185CPB,0 ,0
A,1/28/2008,1,243CPB,0 ,0
A,1/28/2008,1,244CPB,0 ,0
A,1/28/2008,1,245CPB,0 ,0
A,1/28/2008,1,303CPB,0 ,0
A,1/28/2008,1,304CPB,0 ,0
A,1/28/2008,1,305CPB,0 ,0
A,1/28/2008,1,363CPB,0 ,0
A,1/28/2008,1,364CPB,0 ,0
A,1/28/2008,1,365CPB,0 ,0
A,1/28/2008,1,423CPB,0 ,0
A,1/28/2008,1,424CPB,0 ,0
A,1/28/2008,1,425CPB,0 ,0
A,1/28/2008,1,483CPB,0 ,0
A,1/28/2008,1,484CPB,0 ,0
A,1/28/2008,1,485CPB,0 ,0
A,1/29/2008,1,153CPB,1 20,0
A,1/29/2008,1,154CPB,0 ,0
A,1/29/2008,1,155CPB,0 ,0
A,1/29/2008,1,183CPB,0 ,0
A,1/29/2008,1,184CPB,0 ,0
A,1/29/2008,1,185CPB,0 ,0
A,1/29/2008,1,243CPB,6 0,56
A,1/29/2008,1,244CPB,0 ,0
A,1/29/2008,1,245CPB,0 ,0
A,1/29/2008,1,303CPB,3 0,28
A,1/29/2008,1,304CPB,0 ,0
A,1/29/2008,1,305CPB,0 ,0
A,1/29/2008,1,363CPB,0 ,0
A,1/29/2008,1,364CPB,0 ,0
A,1/29/2008,1,365CPB,0 ,0
A,1/29/2008,1,423CPB,0 ,0
A,1/29/2008,1,424CPB,0 ,0
A,1/29/2008,1,425CPB,0 ,0
A,1/29/2008,1,483CPB,0 ,0
A,1/29/2008,1,484CPB,0 ,0
A,1/29/2008,1,485CPB,0 ,0
A,1/30/2008,1,153CPB,1 20,4
A,1/30/2008,1,154CPB,0 ,0
A,1/30/2008,1,155CPB,0 ,0
A,1/30/2008,1,183CPB,0 ,80
A,1/30/2008,1,184CPB,0 ,0
A,1/30/2008,1,185CPB,0 ,0
A,1/30/2008,1,243CPB,6 0,56
A,1/30/2008,1,244CPB,0 ,0
A,1/30/2008,1,245CPB,0 ,0
A,1/30/2008,1,303CPB,3 0,27
A,1/30/2008,1,304CPB,0 ,0
A,1/30/2008,1,305CPB,0 ,0
A,1/30/2008,1,363CPB,0 ,0
A,1/30/2008,1,364CPB,0 ,0
A,1/30/2008,1,365CPB,0 ,0
A,1/30/2008,1,423CPB,0 ,0
A,1/30/2008,1,424CPB,0 ,0
A,1/30/2008,1,425CPB,0 ,0
A,1/30/2008,1,483CPB,0 ,0
A,1/30/2008,1,484CPB,0 ,0
A,1/30/2008,1,485CPB,0 ,0
A,1/31/2008,1,153CPB,1 20,72
A,1/31/2008,1,154CPB,0 ,0
A,1/31/2008,1,155CPB,0 ,0
A,1/31/2008,1,183CPB,0 ,0
A,1/31/2008,1,184CPB,0 ,0
A,1/31/2008,1,185CPB,0 ,0
A,1/31/2008,1,243CPB,6 0,38
A,1/31/2008,1,244CPB,0 ,0
A,1/31/2008,1,245CPB,0 ,0
A,1/31/2008,1,303CPB,3 0,19
A,1/31/2008,1,304CPB,0 ,0
A,1/31/2008,1,305CPB,0 ,0
A,1/31/2008,1,363CPB,0 ,0
A,1/31/2008,1,364CPB,0 ,0
A,1/31/2008,1,365CPB,0 ,0
A,1/31/2008,1,423CPB,0 ,0
A,1/31/2008,1,424CPB,0 ,0
A,1/31/2008,1,425CPB,0 ,0
A,1/31/2008,1,483CPB,0 ,0
A,1/31/2008,1,484CPB,0 ,0
A,1/31/2008,1,485CPB,0 ,0
A,2/1/2008,1,153CPB,1 20,104
A,2/1/2008,1,154CPB,0 ,0
A,2/1/2008,1,155CPB,0 ,0
A,2/1/2008,1,183CPB,0 ,0
A,2/1/2008,1,184CPB,0 ,0
A,2/1/2008,1,185CPB,0 ,0
A,2/1/2008,1,243CPB,6 0,52
A,2/1/2008,1,244CPB,0 ,0
A,2/1/2008,1,245CPB,0 ,0
A,2/1/2008,1,303CPB,3 0,26
A,2/1/2008,1,304CPB,0 ,0
A,2/1/2008,1,305CPB,0 ,0
A,2/1/2008,1,363CPB,0 ,0
A,2/1/2008,1,364CPB,0 ,0
A,2/1/2008,1,365CPB,0 ,0
A,2/1/2008,1,423CPB,0 ,0
A,2/1/2008,1,424CPB,0 ,0
A,2/1/2008,1,425CPB,0 ,0
A,2/1/2008,1,483CPB,0 ,0
A,2/1/2008,1,484CPB,0 ,0
A,2/1/2008,1,485CPB,0 ,0
A,2/2/2008,1,153CPB,0 ,0
A,2/2/2008,1,154CPB,0 ,0
A,2/2/2008,1,155CPB,0 ,0
A,2/2/2008,1,183CPB,0 ,0
A,2/2/2008,1,184CPB,0 ,0
A,2/2/2008,1,185CPB,0 ,0
A,2/2/2008,1,243CPB,0 ,0
A,2/2/2008,1,244CPB,0 ,0
A,2/2/2008,1,245CPB,0 ,0
A,2/2/2008,1,303CPB,0 ,0
A,2/2/2008,1,304CPB,0 ,0
A,2/2/2008,1,305CPB,0 ,0
A,2/2/2008,1,363CPB,0 ,0
A,2/2/2008,1,364CPB,0 ,0
A,2/2/2008,1,365CPB,0 ,0
A,2/2/2008,1,423CPB,0 ,0
A,2/2/2008,1,424CPB,0 ,0
A,2/2/2008,1,425CPB,0 ,0
A,2/2/2008,1,483CPB,0 ,0
A,2/2/2008,1,484CPB,0 ,0
A,2/2/2008,1,485CPB,0 ,0
A,2/3/2008,1,153CPB,0 ,0
A,2/3/2008,1,154CPB,0 ,0
A,2/3/2008,1,155CPB,0 ,0
A,2/3/2008,1,183CPB,0 ,0
A,2/3/2008,1,184CPB,0 ,0
A,2/3/2008,1,185CPB,0 ,0
A,2/3/2008,1,243CPB,0 ,0
A,2/3/2008,1,244CPB,0 ,0
A,2/3/2008,1,245CPB,0 ,0
A,2/3/2008,1,303CPB,0 ,0
A,2/3/2008,1,304CPB,0 ,0
A,2/3/2008,1,305CPB,0 ,0
A,2/3/2008,1,363CPB,0 ,0
A,2/3/2008,1,364CPB,0 ,0
A,2/3/2008,1,365CPB,0 ,0
A,2/3/2008,1,423CPB,0 ,0
A,2/3/2008,1,424CPB,0 ,0
A,2/3/2008,1,425CPB,0 ,0
A,2/3/2008,1,483CPB,0 ,0
A,2/3/2008,1,484CPB,0 ,0
A,2/3/2008,1,485CPB,0 ,0
Apr 2 '08 #5
ck9663
2,878 Recognized Expert Specialist
And how do you want your result?

-- CK
Apr 2 '08 #6
smckibbe01
29 New Member
pi.piAMP,
pi.piTons,
'',
'',
ps.psPipe,
psDate(0),
psDate(1),
psDate(2),
psDate(3),
psDate(4),
psDate(5),
psDate(6)
Apr 7 '08 #7
ck9663
2,878 Recognized Expert Specialist
Based on the sample you posted. How would your pivoted table would look like?

-- CK
Apr 9 '08 #8
smckibbe01
29 New Member
Scheduled:
,,0.508,,,153CP B,,,,
,,0.508,,,154CP B,,,,
,,0.508,,,155CP B,,,,
,,0.671,200,,18 3CPB,160,160,16 0,160
,,0.671,,,184CP B,,,,
,,0.671,,,185CP B,,,,
,,1.0605,100,,2 43CPB,,,,
,,1.0605,,,244C PB,,,,
,,1.0605,,,245C PB,,,,
,,1.535,,,303CP B,,,,
,,1.535,,,304CP B,,,,
,,1.535,,,305CP B,,,,
,,2.0945,50,,36 3CPB,80,80,80,8 0
,,2.0945,,,364C PB,,,,
,,2.0945,,,365C PB,,,,
,,2.739,,,423CP B,,,,
,,2.739,,,424CP B,,,,
,,2.739,,,425CP B,,,,


Poured:
,,0.508,,,153CP B,,,,
,,0.508,,,154CP B,,,,
,,0.508,,,155CP B,,,,
,,0.671,200,,18 3CPB,136,128,10 4,87
,,0.671,,,184CP B,,,,
,,0.671,,,185CP B,,,,
,,1.0605,100,,2 43CPB,,,,
,,1.0605,,,244C PB,,,,
,,1.0605,,,245C PB,,,,
,,1.535,,,303CP B,,,,
,,1.535,,,304CP B,,,,
,,1.535,,,305CP B,,,,
,,2.0945,50,,36 3CPB,62,62,22,1 2
,,2.0945,,,364C PB,,,22,23
,,2.0945,,,365C PB,,,,
,,2.739,,,423CP B,,,,
,,2.739,,,424CP B,,,,
,,2.739,,,425CP B,,,,
Apr 22 '08 #9
ck9663
2,878 Recognized Expert Specialist
You lost me here.....

You have


Expand|Select|Wrap|Line Numbers
  1. PipeInfo:
  2. piPlant,piMachine,piPipe,piSequence,piMaxTurns,piA MP,piTons
Expand|Select|Wrap|Line Numbers
  1.  
  2. PipeScheduling:
  3. psPlant,psDate,psMachine,psPipe,psScheduled,psPour ed
Expand|Select|Wrap|Line Numbers
  1. Pivot:
  2. ???
How would your pivot table look like?

-- CK
Apr 23 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
9931
by: Charles Law | last post by:
I have posted this on a couple of Access and SQL Server newsgroups, but with no answer yet. There seems to be very little activity on the ones I have looked at, so I thought I would post here as well, since I have no doubt that many people here also use Access/SQL Server. Anyway, enough of my rambling and on with the problem/question. I have two queries that I use in Access: <qrySelect> SELECT table2.column1, table2.column2,...
4
3870
by: Brian Andrus | last post by:
Ok. I upgraded to MS Access 2003 recently and now I am having great heartache. In Access 2002, when I opened a table to view the data, there were wonderful little "plus" signs that I could click on for each record that would open up the corresponding record in any linked table. I could drill down and edit/add records in a most efficient and simple manner. In Access 2003, this is GONE! ARGHH!
6
3922
by: Jean | last post by:
Hi, I am using an Access 2000 front-end to an Oracle 9 backend. I want to write a query that returns all records that are not older than one year for Column "Status_30" (which is a Date). When I look at the ODBC Datasource in Table DWHADMIN_V_PROBLEM , the Date formatting looks normal to me, like #05/07/2005#. However, when I try using the following in my Access Query :
8
4862
by: Jerome Ranch | last post by:
Okay So I've got pivot tables setup in Access 2003. Only about 30K records in the current 2005 databases...the pivots summarize the info in a number of nice ways. I need to get the pivot tables into a document so I can distribute the tables, as is, as an electronic report (without the detailed data) So I export to rtf and xls, and I get an error that there is a too much information. I don't want all the data, just the summary table!
9
101438
by: dotnetguru | last post by:
Hi SMART GUYS, Please help me write a query. Actually I want to convert my rows into columns. Can anyone kindly give me the query to do it? My rows are about employees. There can be any number of employees (in the following table I have 3 employees ABC, EFG, WRI) with any number of records for each employee (there can be 20, 30 records each), but all employees have the same number of records (in the following table each employee has 3...
3
6030
by: Nassa | last post by:
Hi everyone, I have 3 tables: Telbook(Id:int,Name:char,address:char,comment:ntext,owneruserid:int), PK:id TelNumbers(telbookid:int,telno:char,telNotype:int,syscode:int),PK:syscode,F­K:telNumbers.telbookid=telbook.id Teltypes(teltypes:char,fk:int),FK:telnumbers.telnotype=teltypes.fk
2
2504
by: wujtehacjusz | last post by:
Dear All I am very new to MS SQL Server and I am wondering is there some tool which would allow me to build pivot tables in SQL more easily. At the moment writing a query can be quite challenging and difficult. Is there any software which allows you to do it more intuitively and gives you some visual feedback about query you are building? I would be very grateful for any help with this.
0
2405
by: retroviz | last post by:
Hello and thankyou in advance for your help :) I am developing a sales forcasting application that allows users to enter expected prices for different products by month. I have about six months to plan this so am just doing some preliminary investigation now. My database (which will be SQL Server) should have as a minimum, 2 tables; "Products" to store information relating to products and "Prices" to store the prices for each product. ...
1
1571
by: mnarewec | last post by:
Team here is my SQL Pivot view. I want to convert to MS Access View. Please assist USE GO /****** Object: View . Script Date: 10/17/2008 20:41:23 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE VIEW . AS
0
8675
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8862
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6521
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.