OT: SQL PIVOT question

OT: SQL PIVOT question

Postby Marc Vanzegbroeck » Wed Jun 22, 2016 10:37 am

Hi,

This is not a FWH-question, but I use this query in my FWH-program :D

I have a PIVOT query on a table with fields:
NAME,PARAMETER,INHOUD,POINT_TYPE

I use this query to show the result:
Code: Select all  Expand view
SELECT
    name,
    ( max( if( PARAMETER = 'NTWKNUM' , inhoud , NULL ) ) ) AS 'NTWKNUM',
    ( max( if( PARAMETER = 'NODENUM' , inhoud , NULL ) ) ) AS 'NODENUM',
    ( max( if( PARAMETER = 'MODNUM'  , inhoud , NULL ) ) ) AS 'MODNUM' ,
    ( max( if( PARAMETER = 'SLOTNUM' , inhoud , NULL ) ) ) AS 'SLOTNUM',
     TEMPLATE AS POINT_TYPE
FROM  
   mytable
 WHERE
    template = 'ANINNIM'   
GROUP BY name


This is working fine.
Now I want to show only the rows with NTWKNUM = '01'
The problem is that I can't put
Code: Select all  Expand view
WHERE template = 'ANINNIM'  AND NTWKNUM = '01'

because the field NTWKNUM does not exist in the table, but is a field from the result of the query.

How can I do this?
Thanks
Regards,
Marc

FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Marc Vanzegbroeck
 
Posts: 1159
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium

Re: OT: SQL PIVOT question

Postby Marcelo Via Giglio » Wed Jun 22, 2016 1:16 pm

Hola,

try adding HAVING

Code: Select all  Expand view

SELECT
    name,
    ( max( if( PARAMETER = 'NTWKNUM' , inhoud , NULL ) ) ) AS 'NTWKNUM',
    ( max( if( PARAMETER = 'NODENUM' , inhoud , NULL ) ) ) AS 'NODENUM',
    ( max( if( PARAMETER = 'MODNUM'  , inhoud , NULL ) ) ) AS 'MODNUM' ,
    ( max( if( PARAMETER = 'SLOTNUM' , inhoud , NULL ) ) ) AS 'SLOTNUM',
     TEMPLATE AS POINT_TYPE
FROM  
   mytable
 WHERE
    template = 'ANINNIM'
HAVING
    NTWKNUM = '01'  
GROUP BY name

 
Marcelo Via Giglio
 
Posts: 1064
Joined: Fri Oct 07, 2005 3:33 pm
Location: Cochabamba - Bolivia

Re: OT: SQL PIVOT question

Postby Marc Vanzegbroeck » Wed Jun 22, 2016 2:05 pm

Thank you Marcelo,

Now it's working!
I have to move HAVING to the end, like this.
Code: Select all  Expand view
SELECT
    name,
    ( max( if( PARAMETER = 'NTWKNUM' , inhoud , NULL ) ) ) AS 'NTWKNUM',
    ( max( if( PARAMETER = 'NODENUM' , inhoud , NULL ) ) ) AS 'NODENUM',
    ( max( if( PARAMETER = 'MODNUM'  , inhoud , NULL ) ) ) AS 'MODNUM' ,
    ( max( if( PARAMETER = 'SLOTNUM' , inhoud , NULL ) ) ) AS 'SLOTNUM',
     TEMPLATE AS POINT_TYPE
FROM  
   mytable
WHERE
    template = 'ANINNIM'
GROUP BY name
HAVING
    NTWKNUM = '01'  


Marcelo Via Giglio wrote:Hola,

try adding HAVING

Code: Select all  Expand view

SELECT
    name,
    ( max( if( PARAMETER = 'NTWKNUM' , inhoud , NULL ) ) ) AS 'NTWKNUM',
    ( max( if( PARAMETER = 'NODENUM' , inhoud , NULL ) ) ) AS 'NODENUM',
    ( max( if( PARAMETER = 'MODNUM'  , inhoud , NULL ) ) ) AS 'MODNUM' ,
    ( max( if( PARAMETER = 'SLOTNUM' , inhoud , NULL ) ) ) AS 'SLOTNUM',
     TEMPLATE AS POINT_TYPE
FROM  
   mytable
 WHERE
    template = 'ANINNIM'
HAVING
    NTWKNUM = '01'  
GROUP BY name

 
Regards,
Marc

FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Marc Vanzegbroeck
 
Posts: 1159
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 38 guests