I need to copy "user1_id" & "user2_id" from "battle" table to new table called "user_battle",
tables structure:
"battle" table (structure):
id user1_id user2_id
1 1 2
2 1 3
3 2 3
4 2 4
5 1 4
"user_battle" table (structure): (should be filled like this)
battle_id user_id
1 1
1 2
2 1
2 3
3 2
3 3
4 2
4 4
5 1
5 4
thanks,
I think you can use union all to get the data you want:
select id, user1_id
from battle
union all
select id, user2_id
from battle;
You can put insert into user_battle(battle_id, user_id) before this statement for the insert.
insert into allows you to use a select to effectively "copy" data from one table to another.
By using union you can get separate rows for user1_id and user2_id.
insert into user_battle (battle_id, user_id)
(
select id, user1_id from battle
union
select id, user2_id from battle
)
insert into user_battle select id, user1_id from battle
And
insert into user_battle select id, user2_id from battle
Use a union.
INSERT INTO user_battle (battle_id, user_id)
SELECT id, user1_id
FROM battle
UNION ALL
SELECT id, user2_id
FROM battle;
Related
I have something like this in two tables on my database
"User" table
userId viewsIDS
1 1,2,3,4,5
2 2,4,6,12,13
3 1,4,5
... ...
"View" table
viewID webpageId
1 1
2 1
3 2
4 3
5 3
6 3
... ...
Now, which query should I use to find user IDs just having the webpage IDs?
I guess should somehow group "viewIDs" from "View" table and then check if one of these IDs is in the User "viewIDS" field so take the userID out, but I don't know which query would make this the better way possible.
You can try this:
CREATE TABLE #USER (userId INT, viewsId VARCHAR(50))
CREATE TABLE #VIEW (viewId INT, webPageId INT)
INSERT INTO #USER
SELECT 1, '1,2,3,4,5'
UNION SELECT 2, '2,4,6,12,13'
UNION SELECT 3, '1,4,5'
INSERT INTO #VIEW
SELECT 1,1
UNION SELECT 2,1
UNION SELECT 3,2
UNION SELECT 4,3
UNION SELECT 5,3
UNION SELECT 6,3
SELECT u.userId, v.webPageId FROM #USER u
CROSS APPLY fnSplitString(viewsId, ',') s
INNER JOIN #VIEW v ON s.splitdata = v.viewId
ORDER BY u.userId
I copy this function fnSplitString in:
http://www.sqlservercentral.com/blogs/querying-microsoft-sql-server/2013/09/19/how-to-split-a-string-by-delimited-char-in-sql-server/
But as was told before, you should normalize your table ;)
I would like to count(*) how much customers have created a post or made a comment. If the same customer has made several posts and comments, it should count only once.
Customer Table:
ID Name ...
1 Jonh
2 Mark
3 King
4 Doe
Post Table:
ID USER_ID...
1 1
2 1
3 3
4 1
Comment Table:
ID USER_ID...
1 1
2 3
3 3
4 4
It should return count(*) = 3
(user_id: 1, 3 and 4).
Try this one. It worked for me and returns what you're looking for:
SELECT COUNT( USER_ID ) AS TOTAL
FROM (
SELECT USER_ID
FROM POSTS
UNION
SELECT USER_ID
FROM COMMENTS
)X
I used POSTS and COMMENTS as table names bc I was unsure what your exact table names are, so make sure to change these in your query.
This should work:
SELECT COUNT(DISTINCT USER_ID) FROM (
SELECT USER_ID FROM POST_TABLE
UNION
SELECT USER_ID FROM COMMENT_TABLE
)
TO Plot Each Input table I have Separate query, need to apply functionality on that queries and want to create Single query for Output Table
Select Distinct Names, SUM(count) from
(Select Query table 1
union
Select Query table 2
union
Select Query table 3) table group by Names;
this query Not adding count properly Niether Sorting Names properly Whats wrong with this ?
Input Table 1 :-
Names count
bob 3
pol 4
Input Table 2 :-
Names count
bob 5
0 - name may be missing here neglect this entry
Input Table 3 :-
Names count
james 4
pol 7
bob 1
Expected output table :-
Names count
bob 9
pol 11
james 4
You can use UNION and them sum of those.
select sum(a), sum(b) from
(select 2 as a, 1 as b
union select 3 as a, 6 as b
union select 4 as a, 1 as b) as b
Try this query
select `Name`,sum(`Count`) total from ( select `Name`,`Count` from `table1` union all select `Name`,`Count` from `table2` union all select `Name`,`Count` from `table3` ) tot group by `Name`
May this help you.
I have a table like this:
**lead_id** **form_id** **field_number** **value**
1 2 1 Richard
1 2 2 Garriot
2 2 1 Hellen
2 2 2 Garriot
3 2 1 Richard
3 2 2 Douglas
4 2 1 Tomas
4 2 2 Anderson
Where field_number = 1 is the name and field_number = 2 is the surname.
I would like to find entries that are equal by name OR surname and group them by lead_id, so the output could be like this:
1
2
3
Any thoughts on how this can be done?
This should work and be reasonably efficient (depending upon indexes):
select distinct lead_id
from tablename as t1
where exists (
select 1
from tablename as t2
where t1.field_number = t2.field_number
and t1.value = t2.value
and t1.lead_id <> t2.lead_id
)
Select leadid from (
Select DISTINCT leadid,value from tablename
Where fieldnumber=1
Group by leadid,value
Having count(value) >1
Union all
Select DISTINCT leadid,value from tablename
Where fieldnumber=2
Group by leadid,value
Having count(value) >1
) as temp
Surely there is a faster option
I would like to each ID have 2 Custom_Column values.
How can I do that?
Example Table:
ID
1
2
3
Example Query:
SELECT id, (id*2) as Custom_Value, (id*4) as Custom_Value FROM numbers
Response:
ID Custom_Value
1 2
1 4
2 4
2 8
3 6
3 12
use UNION
SELECT ID, (ID*2) as CustomVal FROM tableNAme
UNION ALL
SELECT ID, (ID*4) as CustomVal FROM tableNAme
ORDER BY ID
SQLFiddle Demo