Solved

SQL Transform ... In ()

Posted on 2012-03-26
3
330 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
3 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
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…

773 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