I have two following tables.
Table A:
Email Phone
A#gmail.com 1234
B#gmail.com 2345
C#gmail.com 4567
D#gmail.com 5432
E#gmail.com 4568
Table B:
Email LinkName
A#gmail.com X
B#gmail.com Y
C#gmail.com
D#gmail.com X
B#gmail.com X
Desired Output:
Email LinkName
C#gmail.com
E#gmail.com
Following is the query which I tried. Please let me know is it correct:
Select Email from Table A
Left join Table B
on A.Email = B.Email
where C.LinkName is null
No, C.LinkName you don't have C table
SELECT
A.Email
FROM TABLE A
LEFT JOIN TABLE B
ON A.Email = B.Email
WHERE B.LinkName IS NULL
This one is the right one, LinkName is B column so you have to modify it in WHERE statement
If you want LinkName column (as in your desired output) you should add B.LinkName in your SELECT, but it will be filled with NULL values only
Related
So i have a database with a number of AccountId's and GameId's. I want to get all entries where a where a specified AccountId is used, furthermore i only want them if the table contains a value pair where a specified accountID (different one) has the same GameId as for one of the entries gotten.
An example of this could be
AccountId
GameID
asd
123
zxc
123
zxc
789
In this example the AccountId zxc would be the first specified AccountId while asd would be the seccond.
In this case i only want the the following returned
AccountId
GameID
zxc
123
I hope this makes sense
SELECT t1.AccountId, t1.GameId
from TableName t1
inner join TableName t2 on
t1.GameId = t2.GameId
where t1.AccountId = 'Accountid1' AND t2.AccountId = 'AccountId2'
I want to select and fetch all mismatch value of two table having same Column Name But Id, Name and City are different. I am Using Sql Server Management Studio
Table A:
id Name City
1 John karachi
2 smith Capetown
3 liza Washington
Table B:
id Name City
7 Grey Dubai
8 Clarke Texas
9 liza Washington
OUTPUT:
7 Grey Dubai
8 Clarke Texas
This has been answered hundreds and hundreds of times. Not quite sure what you mean by "having same Column Name But Id, Name and City are different" but here are a couple of example of how you can do this.
You can use a left join where tablea is null,
select b.Name
, b.City
from tableb b
left join tablea a on a.name = b.name
and a.city = b.city
where a.name is null
You can use except.
select Name
, City
from tableb
except
select Name
, City
from tablea
You can try the below query. Assuming that the id column is never null
select tb.*
from tableB tb
left join tableA ta
on tb.Name=ta.Name and tb.City=ta.City
where ta.id is NULL
Link to demo sql fiddle: http://sqlfiddle.com/#!3/250e5/1
I have two tables in my database like this:
Table-1 Table-2
id Name id Name
1 A 1 D-a
2 B 2 D-b
3 C 3 D-c
4 D
I want a query which gives me output such as
A B C D-a D-b D-c
i.e. check if there is entry of D in Table-1 if there is then select all entries of Table-2 and remove D from output
I think this is not the best answer to your question, but it can help you:
Select Distinct Table1.name
From Table1, Table2
WHERE Not Table2.name Like Table1.name+'%'
UNION All
Select Distinct Table2.name
From Table1, Table2
WHERE Table2.name Like Table1.name+'%'
Now you have your results in a column.
If you want to have them in a row follow this question:
Concatenate many rows into a single text string
I have 3 tables:
Table Position:
KodePosition | NamePosition | UserLogin
========================================
0037 Master A winz\alfa
0038 Master B winz\beta
0043 Master C winz\carlie
Table UserBackup (PJS):
KodePosition | UserOrigin | UserChange | StartDate | EndDate
================================================================
0037 winz\alfa winz\carlie 10-10-2014 17-10-2014
Table History:
IdHistory | KodePosition | StartDate | EndDate | User | Comment
===============================================================================
19F5FCFC 0038 14-10-2014 14-10-2014 winz\beta i not agree...
19F5FCFC 0043 15-10-2014 15-10-2014 winz\carlie i agree...
I want to display data like this :
Name | Date | Position | Comment
===================================================
winz\beta 14-10-2014 Master B i not agree...
winz\carlie 15-10-2014 Master A i agree...
Description :
please note the data in Table UserBackup(PJS).
if StartDate in Table History between StartDate and EndDate in Table UserBackup(PJS) and also the same UserChange with user, and then get NamePosition from Table Position by KodePosition of Table UserBackup(PJS).
For now, I have a stored procedure like this, but doesn't display the data I need.
select
A.IdHistory, A.StartDate, B.NamePosition, B.UserLogin, A.Comment
from
History as A
left join
Position as B on A.KodePosition = B.KodePosition
Where
A.IdHistory = '19F5FCFC'
order by
A.StartDate asc
Please help me guys... Thanks...
If you want to ask three tables, you can do it like this (sorry for not using your data, but I dont understand the connections of them(or your explanations)).
SELECT a.Column1, a.Column2, b.Column1
FROM table1 AS a
LEFT JOIN table2 AS b
ON a.Column1=b.Column1
WHERE (
SELECT c.Column1
FROM table3 AS c
INNER JOIN table2 as b
ON b.Column1=c.Column1
WHERE c.Column2 LIKE 'MyTarget%');
You got to think in two SELECTs, dont try to put everything in one. Btw. most DBs dont support multi Joins.
Without more information about what you are trying to do, this is a little bit of a stab in the dark...but based on your description, I believe you are trying to first filter the rows in the UserBackup(PJS) table based on whether the corresponding History.KodePosition record falls within the StartDate and EndDate of the corresponding UserBackup(PJS) record. Then, based on the returned KodePositions, you want to retrieve the related records from the Position table. I'm not sure this is the complete picture of what you are looking for, but hopefully this gets you further along:
;WITH cteData
AS (
SELECT u.KodePosition, h.IDHistory, h.StartDate, h.Comment
FROM UserBackup(PJS) u
INNER JOIN History h ON h.User = u.UserChange AND (h.StartDate >= u.Startdate and h.StartDate <= u.EndDate)
)
SELECT c.IDHistory, c.StartDate, p.NamePostition, p.UserLogin, c.Comment
FROM Position p
INNER JOIN cteData c ON c.KodePosition = p.KodePosition
I have a problem in joining the two columns from two different tables.
The Scenario is: I have a table A with 11 columns and another table B with 6 columns.
There is a column names SAMPLE1 which exists in both the tables.But, SAMPLE2 from 1st table A and ABC from 2nd table B are having the same value but with different column names. Same with SAMPLE3 and DEF. Now I would like to join that columns to a single column (which consists of data from both the tables) and rest of the columns also should appear on that final table.
Example:
Table A
SAMPLE1 SAMPLE2 SAMPLE3 .........SAMPLE 11 (Total 11 columns in this table)
US 75.2 US1_US NULL
INDIA 71 I3_INDIA NULL
UK 1851.23 UK1_UK NULL
Table B
SAMPLE1 ABC DEF............. XYZ (Total 6 columns in this table)
CHINA 123.2 C1_CHINA 2
JAPAN 1.1 J1_JAPAN 2
GERMANY 10.2314 G1_GERMANY 2
SINGAPORE 100.22 S1_SINGAPORE 2
Now I would like to see the output like this:
SomeTable
SAMPLE1 SOMENAME1 SOMENAME2..SAPMLE 11 ABC DEF ..... SOMENAME3
US 75.2 US1_US NULL NULL NULL NULL
INDIA 71 I3_INDIA NULL NULL NULL NULL
UK 1851.23 UK1_UK NULL NULL NULL NULL
CHINA 123.2 C1_CHINA NULL NULL NULL 2
JAPAN 1.1 J1_JAPAN NULL NULL NULL 2
GERMANY 10.2314 G1_GERMANY NULL NULL NULL 2
SINGAPORE 100.22 S1_SINGAPORE NULL NULL NULL 2
In short:
SELECT
(SAMPLE1(FROM TABLE A) + SAMPLE1(FROM TABLE B)) AS SAMPLE1,
(SAMPLE2 + ABC) AS SOMENAME1,
(SAMPLE3 + DEF) AS SOMENAME2,
A.SAMPLE4, A.SAMPLE5,...,
B.GHI, B.JKL,...
(A.SAMPLE11 +B.XYZ) AS SOMENAME3
I have used union but it didn't work.
select SAMPLE1,SAMPLE2,SAMPLE3,...,SAMPLE 11 from TABLE A
UNION
SELECT SAMPLE1, ABC, DEF, ...., XYZ FROM TABLE B
Now I am getting an error:
Msg 205, Level 16, State 1, Line 1
All queries combined using a UNION,
INTERSECT or EXCEPT operator must have
an equal number of expressions in
their target lists.
I have used union, coalesce, full outer join (all the suggestions or answers below)
The final code will be used in a select statement. How?
You're probably looking for a join, like:
select a.sample1
, a.sample2
, b.abc
, b.def
... etc ...
from TableA as a
full outer join
TableB as b
where a.sample1 = b.sample1
Join the two tables using FULL OUTER (preserve data from either table where it does not exist on the other), then use COALESCE to get the common SAMPLE1 column from whichever table has it.
SELECT COALESCE(A.SAMPLE1, B.SAMPLE1) SAMPLE1,
A.SAMPLE2,
A.SAMPLE3,
...
A.SAMPLE11,
B.ABC,
B.DEF,
...
B.XYZ
FROM table1 A FULL OUTER JOIN table2 B on A.SAMPLE1 = B.SAMPLE1
References: MSDN - Using Outer Joins / COALESCE
TRY THIS, IT MAY BE USEFUL
table 1 contains 5 columns
select col1,col2...col5 from table1
table 2 contains 3 columns
select col1,col2,col3 from table1
Query:
select col1,col2,col3,col4,col5 from table1
union
select col1,col2,col3,'','' from table1