me trying to make distinct data in temporary table, trying to simple it with create table #tabletemp still got wrong, it says unrecognize data type near distinct and comma or closing bracket was expected near ponumber
here's the code :
CREATE TEMPORARY TABLE t1(
SELECT DISTINCT
PONumber varchar(10),
POdate varchar(10),
customername varchar(35),
description varchar(22)
FROM tb_po
);
SELECT
p.PONumber,
p.podate,
p.customername,
p.description,
(
SELECT SUM(q.deliveryqty)
FROM tb_spb q
WHERE p.PONumber = q.PONumber AND p.description = q.description
) AS Total
FROM t1 p
If you really need it to be in a temporary table, another approach is using "SELECT INTO" wherein you wont need to declare the creation of a temporary table. (Although creating a table then inserting records is the more preferred method https://stackoverflow.com/a/6948850/6344844)
SELECT DISTINCT
p.PONumber,
p.POdate,
p.customername,
p.[description],
SUM(q.deliveryqty)
INTO #TEMPTABLE_NAME
FROM tb_po p
INNER JOIN tb_spb q
ON p.PONumber = q.PONumber
AND p.description = q.description
GROUP BY p.PONumber,p.POdate,p.customername,p.[description]
SELECT * FROM #TEMPTABLE_NAME
DROP TABLE #TEMPTABLE_NAME
You don't need to create a temporary table to get the result that you want. Here is my revised query based on your query:
SELECT DISTINCT
p.PONumber,
p.POdate,
p.customername,
p.[description],
SUM(q.deliveryqty)
FROM tb_po p
INNER JOIN tb_spb q
ON p.PONumber = q.PONumber
AND p.description = q.description
GROUP BY p.PONumber,p.POdate,p.customername,p.[description]
You have to first create the table then insert into the table. For SQL server You can do like this.
CREATE TABLE TEMPORARY(
PONumber varchar(10),
POdate varchar(10),
customername varchar(35),
description varchar(22)
);
insert into TEMPORARY SELECT DISTINCT
PONumber varchar(10),
POdate varchar(10),
customername varchar(35),
description varchar(22)
FROM
tb_po ;
If you want to create temp table in SQL server then you have use # before table name.
For reference http://www.c-sharpcorner.com/uploadfile/b19d5a/temporary-and-global-temporary-table-in-sql-server-2008/
Related
I'm trying to get this to work. When I run the SELECT on the whole dataset I know that the record with cust_number shows up in position 6 (When Using ORDER BY) but this code returns position 37327 which is it's non ordered by position.
SELECT
x.position,
x.cust_number,
x.company,
x.surname,
x.first_name,
x.title
FROM
(SELECT
#rownum:=#rownum + 1 AS position,
c.cust_number,
company,
surname,
first_name,
title
FROM
1_customer_records c
LEFT JOIN addresses a ON c.fk_addresses_id = a.id
JOIN (SELECT #rownum:=0) r
ORDER BY a.company , c.surname , c.first_name , c.title) x
WHERE
x.cust_number = 43246;
Here is another approach using a temp table
CREATE TEMPORARY TABLE row_calc (id INT AUTO_INCREMENT, fk INT NULL, PRIMARY KEY (id)) ENGINE=MEMORY;
INSERT INTO row_calc(fk)
SELECT
cust_number
FROM
1_customer_records c
LEFT JOIN
addresses a ON c.fk_addresses_id = a.id
ORDER BY company,surname,first_name,title;
SELECT
id
FROM
row_calc
WHERE
fk = 43246 LIMIT 1;
DROP TABLE row_calc;
I created a temp table and want to insert values in the table using select statements but when I run this query I am getting the above error. Is there any efficient way.
DROP TABLE IF EXISTS MANAGER_DATA;
CREATE TEMPORARY TABLE MANAGER_DATA (
WORKER_ID VARCHAR, ACCOUNT VARCHAR,
ACTIVE Boolean, REPORTING_NAME VARCHAR,
BUSINESS_TITLE VARCHAR, MANAGER_WORKER_ID VARCHAR,
MANAGER_REPORTING_NAME Text, MANAGER_OF_MANAGER_ID VARCHAR,
MANAGER_OF_MANAGER_NAME VARCHAR);
INSERT INTO MANAGER_DATA (
SELECT WORKER_ID,
ACCOUNT,
ACTIVE,
REPORTING_NAME,
BUSINESS_TITLE,
MANAGER_WORKER_ID,
(
SELECT
A.REPORTING_NAME
FROM ALL_ASSOCIATES_V AS A
INNER JOIN ALL_ASSOCIATES_V AS B
ON A.MANAGER_WORKER_ID = B.WORKER_ID),
(
SELECT B.MANAGER_WORKER_ID
FROM ALL_ASSOCIATES_V AS A
INNER JOIN ALL_ASSOCIATES_V AS B
ON A.MANAGER_WORKER_ID = B.WORKER_ID),
NULL
FROM ALL_ASSOCIATES_V
);
SELECT * FROM MANAGER_DATA;
I have three table person_firstname,person_middlename,person_lastname. All tables have common field entity_id. I have to select employee full name in single record . It is not sure person have only first name, all name or only last name. I tried union it returns three row.
Also need record of specific employee that is by entity_id. SO need to match record like entity_id=123. All names of employee whose entity_id is 123.
SELECT p1.first_name person_firstname,p2.middle_name person_middlename,p3.last_name person_lastname
FROM person_firstname p1 JOIN person_middlename p2 ON p1.entity_id=p2.entity_id
JOIN person_lastname p3 ON p3.entity_id=p2.entity_id;
/*
DROP TABLE PERSON_FIRSTNAME;
DROP TABLE PERSON_SECONDNAME;
DROP TABLE PERSON_LASTNAME;
CREATE TABLE PERSON_FIRSTNAME (ID INT, NAME VARCHAR(20));
CREATE TABLE PERSON_SECONDNAME (ID INT, NAME VARCHAR(20));
CREATE TABLE PERSON_LASTNAME (ID INT, NAME VARCHAR(20));
TRUNCATE TABLE PERSON_FIRSTNAME;
INSERT INTO PERSON_FIRSTNAME VALUES(1,'af'),(2,'bf') ;
TRUNCATE TABLE PERSON_SECONDNAME;
INSERT INTO PERSON_SECONDNAME VALUES(1,'as') ,(3,'cs') ;
TRUNCATE TABLE PERSON_LASTNAME;
INSERT INTO PERSON_LASTNAME VALUES(1,'al'),(2,'bl') ;
*/
SELECT S.*,
PF.NAME,
PS.NAME,
PL.NAME,
concat(IFnull(PF.NAME,''),IFnull(PS.NAME,''),IFNULL(PL.NAME,''))
FROM
(
SELECT ID FROM PERSON_FIRSTNAME
UNION
SELECT ID FROM PERSON_SECONDNAME
UNION
SELECT ID FROM PERSON_LASTNAME
) S
left outer JOIN PERSON_FIRSTNAME PF ON PF.ID = S.ID
left outer JOIN PERSON_SECONDNAME PS ON PS.ID = S.ID
left outer JOIN PERSON_LASTNAME PL ON PL.ID = S.ID
I have a base query which uses a view which uses another view, like this.
SELECT a,b,c,DEBIT_AMOUNT, CREDIT_AMOUNT FROM MAIN_VIEW WHERE a='foo' AND c='bar';
Here's the schema
create table BASE_TABLE (
id int not null auto_increment,
a varchar(20),
b varchar(20),
c varchar(20),
primary key (id));
create table OTHER_TABLE (
oid int not null auto_increment,
id int not null,
mtype varchar(10),
amount varchar(20),
primary key (oid));
create or replace view `MAIN_VIEW` AS
SELECT BT.a, BT.b, BT.c,SUB_VIEW.DEBIT_AMOUNT, SUB_VIEW.CREDIT_AMOUNT
FROM BASE_TABLE BT
LEFT JOIN SUB_VIEW ON SUB_VIEW.id = BT.id
create or replace view `SUB_VIEW` AS
SELECT BT.id,
( SELECT SUM(O.amount)
FROM OTHER_TABLE O
WHERE O.mtype = 'DR'
AND O.id = BT.id
) AS DEBIT_AMOUNT,
( SELECT SUM(O.amount)
FROM OTHER_TABLE O
WHERE O.mtype = 'CR'
AND O.id = BT.id
) AS CREDIT_AMOUNT
FROM BASE_TABLE BT
My query is permformance is very slow, to speed up query execution, i've modified the MAIN_VIEW like this
since the BASE_TABLE is already available on MAIN_VIEW, i thought fetching DEBIT_AMOUNT and CREDIT_AMOUNT from then and there rather than going into the SUB_VIEW
-- MAIN_VIEW ---
create or replace view `MAIN_VIEW` AS
SELECT BT.a, BT.b, BT.c,
( SELECT SUM(O.amount)
FROM OTHER_TABLE O
WHERE O.mtype = 'DR'
AND O.id = BT.id
) AS DEBIT_AMOUNT,
( SELECT SUM(O.amount)
FROM OTHER_TABLE O
WHERE O.mtype = 'CR'
AND O.id = BT.id
) AS CREDIT_AMOUNT
FROM BASE_TABLE BT
But after this modification, query performance is even worse.. can any one help? I thought subviews are be bad for performance...
You need INDEX(id, mtype) (in either order). This should make the subqueries faster, hence the entire query faster.
Does MySQL support common table expressions? For example in Oracle there's the WITH clause? :
WITH aliasname
AS
( SELECT COUNT(*) FROM table_name )
SELECT COUNT(*) FROM dept,aliasname
SELECT t.name,
t.num
FROM TABLE t
JOIN (SELECT c.id,COUNT(*) 'num1'
FROM TABLE1 c
WHERE c.column = 'a'
GROUP BY c.id) ta1 ON ta1.id = t.id
JOIN (SELECT d.id,COUNT(*) 'num2'
FROM TABLE2 d
WHERE d.column = 'a'
GROUP BY d.id) ta2 ON ta2.id = t.id
One way is to use a subquery:
SELECT COUNT(*)
FROM dept,
(
SELECT COUNT(*)
FROM table_name
) AS aliasname
Note that the , between the two tables will cross join the two tables the same as in your query you posted. IF there is any relation between them you can JOIN them instead.
No, MySQL does not support Common Table Expressions (CTE). So instead of using WITH tablealias as (....), you will have to do a subquery.
For example,
WITH totalcount AS
(select userid, count(*) as tot from logins group by userid)
SELECT a.firstname, a.lastname, b.tot
FROM users a
INNER JOIN
totalcount b
on a.userid = b.userid
can be re-written in MySQL as
SELECT a.firstname, a.lastname, b.totalcount
FROM users a
INNER JOIN
(select userid, count(*) as tot from logins group by userid) b
on a.userid = b.userid
So let's talk about WITH clause .
WITH clause and INNER JOIN otherwise JOIN are a kind of same , but WITH clause gives you much more latitude especially in WHERE clause ;
I am going to make a view that'll get values like count of users , user name and etc.
First (Creating our tables users and inserted_users) :
inserted_users table :
CREATE TABLE users (id BIGINT(10) AUTO INCEREMENT PRIMARY KEY , name VARCHAR(50))
users table :
CREATE TABLE users (id BIGINT(10) AUTO INCEREMENT PRIMARY KEY , name VARCHAR(50) , gender TINYINT(1))
Second (Inserting some values to work with) :
users table :
INSERT INTO users (name,gender) VALUES ('Abolfazl M' , 1)
I don't want to insert into inserted_users by query , but I want to add a TRUGGER which will insert data automatically to users_inserted table before data be inserted into users table.
Third (Creating trigger add_uinserted) :
DELIMITER $$
CREATE TRIGGER IF NOT EXISTS add_uinserted BEFORE INSERT ON users FOR EACH ROW
BEGIN
IF NEW.name <> '' THEN
INSERT INTO users_inserted (name) VALUES (NEW.name);
ELSE
INSERT INTO users (name,gender) VALUES ('Unknown',NEW.gender);
INSERT INTO users_inserted (name) VALUES ('Unknown');
END IF;
END$$
DELIMITER ;
Run the query and the trigger will be created and at last let's create a view to give us result from a query having WITH clause .
CREATE VIEW select_users AS
WITH GetCAll AS (
SELECT u1.id As Uid ,COUNT(u1.name) AS CnAll FROM users u1
)
SELECT u1.name AS NAME,CASE
WHEN s1.gender = 1 THEN "MALE"
WHEN s1.gender = 0 THEN "FEMALE"
ELSE "UNKNOWN"
END AS GENDER,CASE
WHEN u1.id = gca.Uid THEN "INSERTED TO users_inserted"
ELSE "NOT INSERTED TO users_inserted"
END AS INSERTED,gca.CnAll FROM GetCAll AS gca INNER JOIN users u1;
After you query got ran the view will be created and by calling the view select_users the data will be shown
Last step (calling the select_users view) :
SELECT * FROM select_users
Thanks for taking a look at my answer , and that's it !!