I am trying to count the number of distinct email addresses in a view, but I can't figure out the syntax... Here's what I have:
BEGIN
SELECT COUNT(*) AS UserCount FROM (
SELECT DISTINCT EmailAddress
FROM viewCohortsAuthorizedByContractor
WHERE (ListAccess = 1) AND (ContractorId = #id)
)
END
The inner select works fine, but if I try adding the COUNT, I get the following error: Msg 156, Level 15, State 1, Procedure rptContractorUsersWithListUserCount, Line 23 [Batch Start Line 15]
Incorrect syntax near the keyword 'END'.
I'm sure there's a simple solution, I just can't figure it out...
You don't need the subquery. Try this:
SELECT count(DISTINCT EmailAddress) as UserCount
FROM viewCohortsAuthorizedByContractor
WHERE (ListAccess = 1) AND (ContractorId = #id)
BEGIN
SELECT COUNT(*) AS UserCount FROM (
SELECT DISTINCT EmailAddress
FROM viewCohortsAuthorizedByContractor
WHERE (ListAccess = 1) AND (ContractorId = #id)
)A -----give a name for result set
END
Related
i need help for the below code. My problem is that i want only that code executes once per statement (after i search i checked that expression don't exists anymore only once per row).
So i tried to add:
IF NOT EXISTS
(Select count(*) FROM replay_replays_access WHERE id_game = new.id_game GROUP BY id_game HAVING count(*) <5)
THEN
But didn't work either what can i do, its duplicating sometime triplicating the information?
TRIGGER replay
AFTER UPDATE
ON table_replays FOR EACH ROW
begin
IF EXISTS
(SELECT
replay_games.room_name
FROM replay_games
WHERE replay_games.room_name = 'Tournament Room' and replay_games.id = new.id_game)
THEN
IF NOT EXISTS
(Select
count(*)
FROM replay_replays_access
WHERE id_game = new.id_game
GROUP BY id_game
HAVING count(*) <5)
THEN
INSERT INTO replay_replays_access(id_game, id_player, replay_name, do_not_hide)
SELECT
new.id_game,
replay_users.id ,
CONCAT(
(SELECT game_types
FROM replay_games
WHERE id=new.id_game),
': ',
(SELECT
descr
FROM replay_games
WHERE id=new.id_game)) ,
0
FROM replay_users
WHERE
(replay_users.admin > 0 OR
replay_users.privlevel = 'TOURNAMENT MEMBER')
AND NOT replay_users.name = (
SELECT
replay_games.creator_name
FROM replay_games
WHERE replay_games.id = new.id_game);
END IF;
END IF;
END
I can't figure out what's wrong with my stored procedure.
CREATE PROCEDURE duplicatecheck2
#QmemberID INT,
#InputDate DATE
AS
INSERT INTO tbl_availableMembers
SELECT memberId
FROM tbl_attendancesheet
WHERE MemberId = #QmemberId
AND [date] = #InputDate
AND [clockin] IS NOT NULL
AND [clockout] IS NULL
UNION
INSERT INTO tbl_availableMembers
SELECT memberId
FROM tbl_attendancemembers
WHERE memberId NOT IN (SELECT memberId
FROM tbl_attendanceSheet)
WHERE date = #InputDate)
I get these errors:
Msg 156, Level 15, State 1, Procedure duplicatecheck2, Line 14
Incorrect syntax near the keyword 'Insert'.
Msg 156, Level 15, State 1, Procedure duplicatecheck2, Line 18
Incorrect syntax near the keyword 'WHERE'.
My other stored procedure that union has worked in it:
CREATE PROCEDURE duplicateCheck
#inputdate DATE
AS
BEGIN
DELETE FROM tbl_availableMembers
INSERT INTO tbl_availableMembers
SELECT memberId
FROM tbl_attendancemembers
WHERE memberId NOT IN (SELECT memberId FROM tbl_attendanceSheet)
UNION
SELECT memberId
FROM tbl_attendancemembers
WHERE memberId IN (SELECT memberId
FROM tbl_attendanceSheet
GROUP BY memberId, date
HAVING COUNT(*) <= 1 AND date = #inputdate)
END
Get rid of the "UNION". The stored procedure can just run two insert statements.
Noel
You cannot apply the UNION to two INSERT statements (as in your first code snippet) - but you can apply it to two SELECT statements (as in your second code sample)
By #marc_s
SELECT IF((SELECT id FROM users where store='XXX' limit 1) IS NULL, null, (SELECT id FROM users where store='XXX' limit 1)) INTO #user_id;
select #user_id;
INSERT INTO `orders`(`user_id`)VALUES(#user_id);
I need is that when #user_id is NULL I don't want to insert into my table orders. If I put an if else statement above the insertion statement,I'll get an error saying that "syntax error unexpected IF".
Use IFNULL function, this way:
SELECT IFNULL(condition, value_if_condition_is_null)
In your query, something like:
SELECT IFNULL((SELECT id FROM users where store='XXX' limit 1), (SELECT id FROM users where store='XXX' limit 1))) INTO #user_id;
I try to count all items from another table with this select:
SELECT id, name, (SELECT count(*)
FROM prekes_main
WHERE prekes_main.pristKaina = 1
and prekes_main.pg_kodas LIKE 'grupes_main.pg_kodas%') as pristKaina
FROM grupes_main
WHERE grupes_main.level = 1
and grupes_main.name <> ''
In LIKE clause I want automatically get selected grupes_main column pg_kodas, but in this query it always returns 0, where is mistake in LIKE function? thx
SELECT id, name,
(
SELECT COUNT(*)
FROM prekes_main
WHERE prekes_main.pristKaina = 1
AND prekes_main.pg_kodas LIKE CONCAT(grupes_main.pg_kodas, '%')
) pristKaina
FROM grupes_main
WHERE grupes_main.level = 1
AND grupes_main.name <> ''
I'm trying to use a while loop in a one-off query on a MySQL (5.1.41-3ubuntu12.10-log) database:
WHILE ((SELECT COUNT(*) FROM
(SELECT id, COUNT(*) AS cnt
FROM foo
GROUP BY id
ORDER BY COUNT(*) DESC) cnts
WHERE cnt > 1) != 0) DO
BEGIN
SET #curr_id = (SELECT id FROM
(SELECT id, COUNT(*) AS cnt
FROM foo
GROUP BY id
ORDER BY COUNT(*) DESC) cnts
WHERE cnt > 1
LIMIT 1);
SET #new_id = (SELECT MAX(id) + 1
FROM foo);
UPDATE foo
SET id = #new_id
WHERE id = #curr_id
LIMIT 1;
END WHILE;
What this does is while there are multiple records with the same id, update one of them with the next id.
The syntax in the body is correct and the predicate used in the while statement also executes without any trouble on it's own. MySQL returns a syntax error on the beginning of the query:
Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHILE ((SELECT count(*) FROM
(SELECT id, COUNT(*) AS cnt
FROM stock_produ' at line 1
I realize this might not be The Right Way of doing things, but this is a very badly (or rather not-at-all) thought out database, so it would be awesome if I could get it to work this way.
Thanks,
Robin
It looks as though you are trying to run this procedural code as an anonymous block. While this works in some databases (like Oracle) it can't be done in MySQL.
If you want to run this then put it in a stored procedure and then call the procedure. Hence:
Create procedure
DELIMITER $$
CREATE PROCEDURE `foo_update_routine`()
BEGIN
WHILE ((SELECT COUNT(*) FROM
(SELECT id, COUNT(*) AS cnt
FROM foo
GROUP BY id
ORDER BY COUNT(*) DESC
) cnts
WHERE cnt > 1) != 0)
DO
SET #curr_id = (SELECT id FROM
(SELECT id, COUNT(*) AS cnt
FROM foo
GROUP BY id
ORDER BY COUNT(*) DESC
) cnts
WHERE cnt > 1
LIMIT 1);
SET #new_id = (SELECT MAX(id) + 1 FROM foo);
UPDATE foo SET id = #new_id
WHERE id = #curr_id
LIMIT 1;
END WHILE;
END $$
Call Procedure
CALL `foo_update_routine`;
PS You might want to investigate the HAVING clause for your select statements...