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

x
?
Solved

SQL Transform ... In ()

Posted on 2012-03-26
3
Medium Priority
?
336 Views
Last Modified: 2012-03-26
Hello Experts,

would it possible to fix the following syntax to get the IN Values from a table (my last line)?

TRANSFORM Nz(Sum([fldSales]),0) AS ProdSales
SELECT BBSales.SalesLocation, BBSales.SalesDate
FROM tblProducts INNER JOIN (BBSales INNER JOIN tblProdSales ON BBSales.SalesID = tblProdSales.fldSalesID) ON tblProducts.fldProdID = tblProdSales.fldProdID
GROUP BY BBSales.SalesLocation, BBSales.SalesDate
PIVOT tblProducts.fldName

IN (SELECT fldName FROM tblProducts WHERE fldArchived = False);

with this syntax I'm getting "SELECT fldName FROM tblProducts WHERE fldArchived = False" as the column heading, which isnt obviosly what I'm after.
0
Comment
Question by:APD_Toronto
[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 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 total points
ID: 37767082
0
 

Author Comment

by:APD_Toronto
ID: 37767117
so, you're saying build my query string in vba?  interesting.
0
 
LVL 18

Expert Comment

by:lludden
ID: 37767397
You can do it all in SQL using dynamic SQL

Here is template code for an example.
CREATE TABLE #T1 (ID int, Event char(1), Value int)
INSERT INTO #T1 VALUES (1,'A',7)
INSERT INTO #T1 VALUES (1,'M',5)
INSERT INTO #T1 VALUES (1,'T',6)
INSERT INTO #T1 VALUES (2,'A',2)
INSERT INTO #T1 VALUES (2,'M',1)
INSERT INTO #T1 VALUES (3,'T',4)
INSERT INTO #T1 VALUES (3,'R',1)
DECLARE @PivotColumnHeaders varchar(MAX)
SELECT @PivotColumnHeaders =
  COALESCE(
    @PivotColumnHeaders + ',[' + UC.Event + ']',
    '[' + Event + ']'
  )
FROM (SELECT Event FROM #T1 GROUP BY Event) UC

DECLARE @PQuery varchar(MAX) = '
SELECT * FROM (SELECT ID, Event, Value FROM #T1 T0) T1
PIVOT (SUM([value]) FOR Event IN (' + @PivotColumnHeaders + ') ) AS P'
EXECUTE (@PQuery)

DROP TABLE #T1 

Open in new window

0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

721 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