I need to insert values to my table 2 with id from table 1 where name equals to 'abc'. I mean smth like this:
INSERT INTO tab2 (id, f_id, name, date)
VALUES (uuid(), (here selected id where name = 'abc'), name, date)
How can I do it? I tried smth like this:
BEGIN
DECLARE f_id char(36);
END
SET #f_id = (SELECT id FROM users WHERE email = u_email)
INSERT INTO tab2 (id, f_id, name, date)
VALUES (uuid(),#f_id,'cba',getdate())
but it doesn't work. This is just my attempt at the principle maybe it will work.
use sample code :
INSERT INTO tab2 (id, f_id, name, date)
SELECT uuid(),id,'cba',getdate() FROM users WHERE email = u_email
https://dev.mysql.com/doc/refman/8.0/en/insert-select.html
I am currently using the SQL command
Select * from where name='john'
Is it possible to return 20 no matter the query, for example
Select * from where name='john' or return = 20
EDIT
If you have an oracle database you can do something like that:
SELECT *
FROM dual
WHERE 1=0
UNION
SELECT '20'
FROM dual;
check my answer
if exists (Select * from item where ItemName='ABC Daycare1')
begin
Select * from item where ItemName='ABC Daycare1'
end
else
select '20'
Try running this. This should return the top result (which is never 20 due to the custom sort) and then when the name doesn't match a value it returns 'Mark' and 20
SQL
IF OBJECT_ID('tempdb..#temp') IS NOT NULL DROP TABLE #temp
CREATE TABLE #temp (id int NOT NULL, name varchar(255) NOT NULL)
INSERT INTO #temp (id, name) VALUES (88,'John')
INSERT INTO #temp (id, name) VALUES (20,'Mark')
SELECT TOP 1
*
FROM #temp
WHERE (name = 'Mark' OR name = 'John')
ORDER BY (
CASE
WHEN id = 20 THEN 0 ELSE 1
END) DESC
MySQL - MySQL fiddle
IF OBJECT_ID('tempdb..#temp') IS NOT NULL DROP TABLE #temp
CREATE TABLE #temp (id int NOT NULL, name varchar(255) NOT NULL)
INSERT INTO #temp (id, name) VALUES (88,'John')
INSERT INTO #temp (id, name) VALUES (20,'Mark')
SELECT
*
FROM temp
WHERE (name = 'Mark' OR name = 'John')
ORDER BY (
CASE
WHEN id = 20 THEN 0 ELSE 1
END) DESC
LIMIT 1
I need to make trigger to increase the 2nd digit when the new value found to be a duplicate.
For Instance, I have a unique filed with 10 digits value. I want when someone insert same number it increase the second left digit like 0100012345. How I can do that? Thank you.
FirstName LastName Code
Houssam Salim 0100012345 to be 0200012345
Try this
I have found a solution with instead off trigger
'/*
CREATE TABLE [Employee1]
(
[id] VARCHAR(20) PRIMARY KEY,
[name] VARCHAR(50)
)
CREATE TRIGGER AutoIncrement_Trigger
ON [Employee1]
instead OF INSERT
AS
BEGIN
DECLARE #ch CHAR
DECLARE #num INT
IF EXISTS (SELECT 1
FROM Employee1 e
JOIN inserted i
ON i.id = e.id)
BEGIN
SET #num=(SELECT max(CONVERT(INT, substring(e.id, 1, 2))) + 1
FROM employee1 e
JOIN inserted i
ON substring(e.id, 3, len(e.id)) = substring(i.id, 3, len(i.id)))
INSERT INTO [Employee1]
(id,
name)
SELECT '0' + CONVERT(VARCHAR(10), #num)
+ substring(i.id, 3, len(i.id)),
e.name
FROM Employee1 e
JOIN inserted i
ON i.id = e.id
END
ELSE
BEGIN
INSERT INTO [Employee1]
(id,
name)
SELECT inserted.id,
inserted.name
FROM inserted
END
END
*/
INSERT INTO [Employee1]
VALUES ('0100012345',
'John')
SELECT *
FROM [Employee1]
INSERT INTO [Employee1]
VALUES ('0100012345',
'John')
SELECT *
FROM [Employee1]
'
I'm trying to make a query but I don't know how to do it. I'm a newbie in SQL querying, so please be patient.
Here is what I have:
select
count(*), sum(time), 'peter'
from
(select *
from ACTUATION
where OPERATOR != 'peter'
and team in (select eq1.name
from TEAMWORKS eq1
where operator1 = 'peter'
or operator2 = 'peter'
or operator3 = 'peter'
or operator4 = 'peter'
or operator5 = 'peter'
or operator6 = 'peter'
or operator7 = 'peter' )) as a
This works OK, but I need to do it for every operator in the table operators and don't know how to do it.
I've tried several subqueries and groups by, but I can not make it work properly.
Kind regards
EDIT:
Let's say I have this DataBase:
create table TEAMWORKS
(
operator1 varchar(50),
operator2 varchar(50),
operator3 varchar(50),
operator4 varchar(50),
operator5 varchar(50),
operator6 varchar(50),
operator7 varchar(50),
name varchar(50)
)
insert into TEAMWORKS (operator1,operator2,name) values ('Peter', 'Paul', 'Pe-Pa')
insert into TEAMWORKS (operator2,operator3,name) values ('Peter', 'John', 'Pe-Jo')
insert into TEAMWORKS (operator1,operator4,name) values ('John', 'Paul', 'Jo-Pa')
insert into TEAMWORKS (operator5,operator6,name) values ('John', 'Peter', 'Jo-Pe')
create table OPERATORS
(
name varchar(50),
surname varchar(50)
)
insert into OPERATORS (name,surname) values ('Peter', 'Font')
insert into OPERATORS (name,surname) values ('Paul', 'Bridges')
insert into OPERATORS (name,surname) values ('John', 'Oldfield')
create table ACTUATION
(
ID int,
time int,
operator varchar(50),
team varchar(50),
description varchar(999)
)
insert into ACTUATION (ID,time,operator,team,description) values (1,30,'Peter', '','Pick flowers')
insert into ACTUATION (ID,time,operator,team,description) values (2,15,'Paul', '','Throw flowers')
insert into ACTUATION (ID,time,operator,team,description) values (3,30,'Peter', 'Jo-Pe','Pick stones')
insert into ACTUATION (ID,time,operator,team,description) values (4,5,'John', 'Jo-Pe','Throw stones')
insert into ACTUATION (ID,time,operator,team,description) values (5,15,'Paul', 'Jo-Pa','Throw tables')
insert into ACTUATION (ID,time,operator,team,description) values (6,30,'Peter', 'Pe-Pa','Pick tables')
And I need to get the time used by Every operator in table OPERATORS where he is not the main operator in Table ACTUATIONS, but part of a TEAM in the ACTUATIONS.
In the given example, I would like to get as result:
Operator #Actuations (count) Time(sum) 'Origin (only for clarifying, list not needed)
Peter 1 5 'from actuation #4
Paul 1 30 'from actuation #6
John 2 45 'form actuations #3 and #5
Hope now it's more clear
EDIT 2:
You have a working example here: http://sqlfiddle.com/#!3/91373/6/0
That's the desired result, but obviously I would not be able to use unions as I would not know the Operators.
Edit:
After OP having altered the question, here is a working query for your example:
select top 1000
o.name
, count(1)
, sum(a.time)
from OPERATORS o
left join (
select
unpvt.Operator
, unpvt.ColName
, unpvt.name
from
(select name, operator1, operator2, operator3, operator4, operator5, operator6, operator7
from TEAMWORKS) ot
UNPIVOT (
Operator FOR ColName IN (operator1, operator2, operator3, operator4, operator5, operator6, operator7)
) as unpvt
) tw on o.name = tw.Operator
left join ACTUATION a on a.team = tw.name
where a.operator != o.name
group by o.name
This returns your decired result :)
Old Post:
I don't know the structure of the database, but I've had a go at guessing.
Firstly, i create to temporary tables in T-SQL - this is done for testing.
I've previously been down-voted for using temporary tables without descriptions in answers on SO for being to complicated, so please ask if you don't understand
declare #ACTUATION table(
time datetime default getdate()
, Operator varchar(8)
, Workteam varchar(8)
)
insert into #ACTUATION (Operator, Workteam)
select 'Gunnar', 'Peter'
union all select 'Peter', 'Gunnar'
We also need the table Teamworks:
declare #TEAMWORKS table(
time datetime default getdate()
, sometext varchar(32)
, operator1 varchar(8)
, operator2 varchar(8)
, operator3 varchar(8)
, operator4 varchar(8)
, operator5 varchar(8)
, operator6 varchar(8)
, operator7 varchar(8)
)
insert into #TEAMWORKS(sometext, operator1, operator2, operator3, operator4, operator5, operator6, operator7)
select 'Blah Blah', 'Gunnar', 'Jack', 'Sam', 'Joe', 'Lee', 'Jane', 'Jim'
union all select 'More Blah', 'Bob', 'Sal', 'Phil', 'Clark', 'Jones', 'Sue', 'Peter'
union all select 'Even more Blaah', 'Im', 'Running', 'out of', 'dummy', 'names', 'Peter', 'Gunnar'
What I've done here is to create two "virtual" tables, which only consist as variables. Eg. they are recreated each time the query is run. However, I think that they are a great tool when trying to visualize.
Next step is to UNPIVOT the operators to a more normalized structure.
This is done using the following query:
select
unpvt.time
, unpvt.sometext
, unpvt.Operator
, unpvt.ColName
from
(select time, sometext, operator1, operator2, operator3, operator4, operator5, operator6, operator7
from #TEAMWORKS) ot
UNPIVOT (
Operator FOR ColName IN (operator1, operator2, operator3, operator4, operator5, operator6, operator7)
) as unpvt;
This query should make a great starting point for further joins.
For instance, I assume that this is (partly) the result you want:
select
a.Operator
, count(1)
, max(t.time)
from #ACTUATION a
left join (
select
unpvt.Operator
, unpvt.time
from (
select time, sometext, operator1, operator2, operator3, operator4, operator5, operator6, operator7
from #TEAMWORKS
) ot
UNPIVOT (
Operator FOR ColName IN (operator1, operator2, operator3, operator4, operator5, operator6, operator7)
) as unpvt
) t on a.Workteam = t.Operator
where a.Operator != t.Operator
group by a.Operator
I need to list all the order done with every customer , So if a certain customer had passed 6 order then we need to list order1 | Order2| order3| ...
I have a table called Order where we have the Client Id , I am using SqlServer 2008 R2 , I have tried different approach but with no success , they run for ever and never gave a result :s , Here my lates try :
SELECT convert(varchar(1),isnull(user.Order.OrderId,'')) +' | '+ convert(varchar(1),isnull(Order_1.OrderId,'')) +' | '+convert(varchar(1),isnull(Order_2.OrderId,'')) +' | '+convert(varchar(1),isnull(Order_3.OrderId,'')) +' | '+convert(varchar(1),isnull(Order_4.OrderId,'')) +' | '+convert(varchar(1),isnull(Order_5.OrderId,'')) as OrderIdList,
user.client.kdnr
FROM user.Order
left outer JOIN user.Order AS Order_1 ON user.Order.clientnr = Order_1.clientnr AND Order_1.OrderId <> user.Order.OrderId and Order.orderDate < Order_1.orderDate
left outer JOIN user.Orderold AS Order_2 ON user.Order.clientnr = Order_2.clientnr AND Order_2.OrderId not in ( user.Order.OrderId , Order_1.OrderId ) and Order_1.orderDate < Order_2.orderDate
left outer JOIN user.Orderold AS Order_3 ON user.Order.clientnr = Order_3.clientnr AND Order_3.OrderId not in ( user.Order.OrderId , Order_1.OrderId, Order_2.OrderId ) and Order_1.orderDate < Order_2.orderDate
left outer JOIN user.Orderold AS Order_4 ON user.Order.clientnr = Order_4.clientnr AND Order_4.OrderId not in ( user.Order.OrderId , Order_1.OrderId, Order_2.OrderId, Order_3.OrderId ) and Order_2.orderDate < Order_3.orderDate
left outer JOIN user.Orderold AS Order_5 ON user.Order.clientnr = Order_5.clientnr AND Order_5.OrderId not in ( user.Order.OrderId , Order_1.OrderId, Order_2.OrderId, Order_3.OrderId, Order_4.OrderId ) and Order_3.orderDate < Order_4.orderDate
INNER JOIN user.client ON Order_1.clientnr = user.client.kdnr
group by client.kdnr ,user.client.name1, user.client.firstname, user.Order.OrderId, Order_1.OrderId,Order_1.Ordernr,Order_2.OrderId ,Order_3.OrderId,Order_4.OrderId,Order_5.OrderId `
Does any body have an dea how would I do that Correctly?
By using XML capabilities of SQL Server 2005 and above you can easily and efficiently generate comma separated values.
Try this (adjust according to your table and column names):
-- Sample tables
declare #Customer table
(
CustomerId int,
Name varchar(50)
)
declare #Order table
(
OrderId int,
CustomerId int
)
-- Sample data
insert into #Customer (CustomerId, Name) values (1, 'Alice')
insert into #Customer (CustomerId, Name) values (2, 'Mary')
insert into #Customer (CustomerId, Name) values (3, 'David')
insert into #Order (OrderId, CustomerId) values (1, 1)
insert into #Order (OrderId, CustomerId) values (2, 1)
insert into #Order (OrderId, CustomerId) values (3, 1)
insert into #Order (OrderId, CustomerId) values (4, 2)
insert into #Order (OrderId, CustomerId) values (5, 2)
insert into #Order (OrderId, CustomerId) values (6, 3)
----select * from #Customer
----select * from #Order
/* Actual Query that would add the OrderId from #Order table as a comma separated list against relevant Customer */
SELECT Customer.CustomerId as CustomerId, Customer.Name,
(
SELECT Convert(varchar(1), O.OrderId) + ','
FROM #Order AS O
WHERE O.CustomerId = Customer.CustomerId
ORDER BY O.OrderId
FOR XML PATH('')
) AS OrderList
FROM #Customer AS Customer
Well, i can't give you a complete answer, but i can tell you how to solve it. Try to use Common Table Expressions (http://msdn.microsoft.com/en-us/library/ms177410(v=sql.105).aspx).
With this you can join to your own resultset, where you make a recursive loop to solve this issue.
Hope it helps you