UPDATED:
I am using pivot table for displaying column names as header as per my knowledge everything is fine but when i tried to add parameter I am getting error while
calling
call sp_schreport(1,2,'2014-10-04'); $$
AND WHEN I TRIED TO ADD
"select venue,'null' as first,'null' as second,'null' as third,'null' as fourth from sch_venue where campusid =campid and venueid NOT IN (select venueid from sch_taskassigned where assignedstatus=status AND (sdate BETWEEN taskstartdate AND taskenddate)) UNION"
along with that code getting error for union function both query column should be equal so that only i have used 'null' first etc..
CREATE PROCEDURE sp_schreport(IN campid int,IN status int,IN sdate datetime)
BEGIN
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(CASE WHEN t.session = ''',
t.session,
''' THEN d.department END) AS `',
tagname, '`'
)
) INTO #sql
FROM sch_sessions t;
SET #sql
= CONCAT('select venue,'null' as first,'null' as second,'null' as third,'null' as fourth from sch_venue where campusid =campid and venueid NOT IN (select venueid from sch_taskassigned where assignedstatus=status AND (sdate BETWEEN taskstartdate AND taskenddate))
UNION SELECT v.venueid, ', #sql, '
from sch_taskassigned p
left join sch_sessions t
on p.sid = t.sid inner join sch_venue v on p.venueid=v.venueid inner join sch_departments d where p.campusid=campid and p.assignedstatus=status and (sdate BETWEEN taskstartdate AND taskenddate) group by p.venueid');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
Kindly find out the mistakes in my procedure
Related
I tried to create new stored procedure with this code, this error always appear to me:
CREATE PROCEDURE `retriveData`() NOT DETERMINISTIC CONTAINS SQL SQL
SECURITY DEFINER BEGIN
SET #sql = NULL
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near '' at line 4
I used this code to create stored procedure:
CREATE PROCEDURE `retriveData`() NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER
BEGIN
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(pa.item = ''',
item,
''', pa.comValue, 0)) AS ',
item
)
) INTO #sql
FROM salaryStructureView;
SET #sql = CONCAT('SELECT p.id
, p.fullname
, p.salary, ', #sql, '
FROM employee p
LEFT JOIN salaryStructureView AS pa
ON p.id = pa.id
GROUP BY p.id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
You forgot a delimiter:
DELIMITER $$
CREATE PROCEDURE `retriveData`() NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER
BEGIN
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(pa.item = ''',
item,
''', pa.comValue, 0)) AS ',
item
)
) INTO #sql
FROM salaryStructureView;
SET #sql = CONCAT('SELECT p.id
, p.fullname
, p.salary, ', #sql, '
FROM employee p
LEFT JOIN salaryStructureView AS pa
ON p.id = pa.id
GROUP BY p.id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
$$
I'm somewhat of a SQL rookie but I'm trying to write a pivot between three tables. I have the below query and it works perfectly aside from the MAX call. It causes my values that are less than the default value to be overridden. I have screenshots of my tables below. Any ideas on how to fix this?
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(cc.company_config_key_id = ', cck.id, ', cc.config_value, \'', cck.default, '\')) AS ', cck.company_config_key
)
) INTO #sql
FROM company_config cc
LEFT JOIN company_config_key cck
ON cck.id = cc.company_config_key_id;
SET #sql = CONCAT('SELECT co.id, co.name, ', #sql, ' FROM companies co LEFT JOIN company_config cc ON cc.company_id = co.id GROUP BY co.id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
companies:
company_config_key:
company_config:
Result (the secondary colors for companies 1 and 2 should be set):
It turns out I just had to set the internal if statement to NULL and then wrapped the max statement in an IFNULL.
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT('IFNULL(MAX(IF(cc.company_config_key_id = ', cck.id, ', cc.config_value, NULL)), \'', cck.default, '\') AS ',cck.company_config_key)
) INTO #sql
FROM company_config cc
LEFT JOIN company_config_key cck
ON cck.id = cc.company_config_key_id;
SET #sql = CONCAT('SELECT co.id, co.name, ', #sql, ' FROM companies co LEFT JOIN company_config cc ON cc.company_id = co.id GROUP BY co.id');
SELECT #sql;
PREPARE stmt FROM #sql;
EXECUTE stmt;
If I have this schema
lists (lead_id, list_id, campaign_id, ...)
campaign (campaign_id, ...)
custom_list_id (lead_id, ...)
there are many tables with name custom_list_id for various list_id s and I want to:
SELECT * from lists ls inner join campaign c ON c.id=ls.campaign_id
inner join custom_"#LISTID" cll ON ls.lead_id = cll.lead_id
How do I write this query?
thnx
Not sure what sql you really want, but try this:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'INNER JOIN custom_', list_id, 'cll', list_id, ' ON ls.lead_id = cll', list_id '.lead_id')
)
SEPARATOR ' '
) INTO #sql
FROM lists ;
SET #sql = CONCAT('SELECT * from lists ls inner join campaign c ON c.id=ls.campaign_id ', #sql);
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
I need to create a SQL query that lists the following tables. Lines from the language list as column names. Thanks
Picture table and query:
If languages are known upfront
SELECT r.id, r.parent,
MAX(CASE WHEN n.language_id = 1 THEN n.name END) cs,
MAX(CASE WHEN n.language_id = 2 THEN n.name END) en
FROM cat_route r LEFT JOIN cat_route_name n
ON r.id = n.cat_route_id
GROUP BY r.id
Here is SQLFiddle demo
If you want it to be dynamic depending on what languages you defined in language
SET #sql = NULL;
SELECT GROUP_CONCAT(CONCAT(
'MAX(CASE WHEN n.language_id = ', id, ' THEN n.name END) `', short, '`'))
INTO #sql
FROM language;
SET #sql = CONCAT(
'SELECT r.id, r.parent, ', #sql, '
FROM cat_route r LEFT JOIN cat_route_name n
ON r.id = n.cat_route_id
GROUP BY r.id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Here is SQLFiddle demo
I have the following dynamic sql query which works as intended. But now I want to join the result with the result of another query on account_id. I tried the usual way by making the query a derived table then joining but this doesn't work (I assume it has to do with all the semi-colons). Is there a special way to join a dynamic sql query with another query ?
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'sum(CASE WHEN phone_name = ''',
phone_name,
''' THEN 1 else 0 END) AS `',
phone_name, '`'
)
) INTO #sql
FROM yt;
SET #sql
= CONCAT('SELECT account_id, ', #sql, '
from yt
group by account_id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Can you simply put the rest of your query in your concatenation?
...
/* Your existing code */
SET #sql
= CONCAT('SELECT account_id, ', #sql, '
from yt
group by account_id');
/* Now put your derived table stuff in */
SET #sql
= CONCAT('select a.account_id, b.something_else
from some_table as b join (', #sql, ') as a
on a.account_id = b.account_id');
/* Continue your existing code */
PREPARE stmt FROM #sql;
...