display data from two different table which don't have any relation? - sql-server

table1: in that three columns billno, billamt, wno
table2: in that three column chequeentry, chequeamt, wno
i want display the result which is not repeated rows where wno is equal to same
if from table1 i got four rows and from table2 i got two rows. I want display the rows but not repeated. if i use join its done the multiply and show the result and my table2's rows are repeated. But dont want this. it display the null value it i accepted.
i pass the one query i.e join the table
select b.billno, b.billamt, c.chequeentry, c.chequeamt
from t2 as b
inner join t3 as c on b.wno=c.wno where b.wno=1
i got output but some value are repeated.

Use the Full outer Join, it return both matching, and not matching rows from both tables.
Syntax:
SELECT column_name(s) FROM table1 FULL OUTER JOIN table2 ON table1.column_name=table2.column_name;

Related

How to do left join with tables with composite key and condition?

Table1
Table2
I would like to join these table so that I need fruits with number 1 and fruits with number 12.
It should look like this:
Query
The problem is even if I use left join I still need a criteria that it should be 12 or nothing, but if it doesnt exist in the second table it wont show it.

Compare rows two tables

I want to compare rows value from one table with rows value from another table. Structure of the first table is :
The other table is more complex than first table and have the same structure but number of rows is million rows, by the way first have 60.000 rows.
I need to compare these two tables by biling_profiles so I have an insight into the price but that billing profile turn in the column. It's easily done using the left outer join because approximately 6 or 7 billing_profiles . Next thing I need is that if the other table has no value (for example, the first table has row with (destination 201425, cost 2,624) and the other does not , then I have trimmed one character from right to left and then search (destination - 20142). If no again result i repeat again trim from right to left (destination - 2014), when i found same destination then show in table)
So, how to solve that ?
try
SELECT DISTINCT A.Id
FROM
table1 A
INNER JOIN table 2B
ON A.Id = B.Id AND A.biling_profiles = B.biling_profiles
or
select t1.biling_profiles,t2.biling_profiles
from table1 t1
inner join table2 t2 on
t1.id= t2.id
where t1.biling_profiles= #biling_profiles

Join Query Combining Two Tables

I need to display 3 columns from table A and one column from table B. I used the following join query to combine them but not getting the value in the expected columns.
example1 -this is working.(If I display one column from table A and one column from table B it works)
SELECT
tableA.col1(fkid),
tableB.col1
FROM tableA
INNER JOIN tableB
ON tableA.fkid = tableB.pkid;
I need to display 3 columns from table A and one column from table B the below query is not working.
SELECT
tableA.col1,
tableA.col2,
tableA.col3,
tableB.col1
FROM tableA
FULL JOIN tableB
ON tableA.fkid = tableB.pkid;
Original query:
select device.name,device.description, device.fkphonetemplate,phonetemplate.name from device inner join phonetemplate ON device.fkphonetemplate=phonetemplate.pkid;
Result:
description fkphonetemplate name
Nikhil (nkalantr) 10ce46f6-615d-4605-9f42-454225df5647 ARRAY(0xc7153b0)
Expected result should be:
description fkphonetemplate name
Nikhil (nkalantr) 10ce46f6-615d-4605-9f42-454225df5647 Standard 7960 SCCP
I don't get name from device table in the result and the name from phonetemplate table shows something as Array0X... but I need to get the name of phonetemplate like Standard 7960 as shown in expected result.
Can you refine my query or suggest what's wrong with the 2nd query?
Any reason for you to using full join instead of inner join ?
If no,try using inner join.
SELECT
a.col1,
a.col2,
a.col3,
b.col1
FROM tableA a
INNER JOIN tableB b
ON a.fkid = b.pkid;
Can you provide your database sample data , so we can clearly know what your need.

Inner Join and Group By in SQL with out an aggregate function.

I am trying to retrieve data from 2 table with some conditions. When I just do the inner join with the conditions , I get a huge value (200000 data). But when I group by I get a very less value like (8000 data).
SELECT Tcg.SK_tID, Tcg.SK_ServiceProviderID
INTO #CHDetails
FROM #ClientGroup Tcg
INNER JOIN dbo.Component AS chd ON chd.SK_PID = Tcg.SK_PID
AND chd.SK_ServiceProviderID = Tcg.SK_ServiceProviderID
AND chd.SK_CompID = #CHD
AND chd.ReportDate < #ReportDate
GROUP BY Tcg.SK_PID ,Tcg.SK_ServiceProviderID
Can you please let me know the cause for this. Inner join always takes the common data.
The data in the #ClientGroup table is around 70000 , while data in the dbo.Component is very huge. When I query for common PID and Service provider logically it shoul give me the records equal to or less than #ClientGroup. How is it giving more ?
When I do group by i get 8000. But why should I do group by in a inner join for 2 tables.
The group by is essentially performing a distinct on the result. The reason you have to do this is likely because you have duplicates in both tables.
See this sqlfiddle: http://sqlfiddle.com/#!3/cbdca/2
In it, table1 has 3 rows and table2 has 3 rows. When joined together, they return 9 rows.
if a join is giving you more records than expected that means that your join criterea is not complete. The extreme case for this would be a cartesian join.
check your joining criteria.
Group by combines rows together. Usually it is done to produce aggregate data or produce a list of unique values. In your example, the #ClientGroup table is grouped together and the total number of rows you get returned shrinks. You should be getting less rows than are in #ClientGroup.
It will not reduce the number of rows because The INNER JOIN does not perform DISTINCT instead INNER JOIN finds all matching data from both tables.
Let’s say you have table A and table B. both has column ID.
Table A has total 3 rows in it and all values are unique. values are 1,2,3.
TABLE B has total 300 rows, but only 3 unique values. Values are 1,2,3.
(table B has 100 rows for each unique value)
Now in this example if I do INNER JOIN between Table A and Table B
then every row from A will get join Every matching row in table B.
So
value 1 from Table A will match 100 rows in table B
value 2 from Table A will match 100 rows in table B
value 3 from Table A will match 100 rows in table B
so after doing INNER join I will get total 300 rows.
On the contrary if Table B has completely different values (other than 1,2,3) then INNER JOIN would has produced no result.

Merging Two Tables in SQL Server

I have two tables, each with some columns that are the same. However, each table also contains data that is unique. (Similar data includes a row name).
What I need to do is tack on the data from table two to it's matching row in table one (matching the name column).
Is there any way to do this?
I need stuff from table two to go into table 1 where the names match:
The following query should return all matching rows with columns from both tables. Note that any unique rows (that only exist in table one or two) will be excluded.
SELECT
one.matchingColum,
one.oddColum,
two.evenColumn
FROM one
JOIN two on one.matchingColumn = two.matchingColumn
If the data types are the same, then you can do a union
SELECT *
FROM table1
UNION
SELECT *
FROM table2
If the datatypes are not the same and you have a field that you can JOIN on, then you can do a JOIN
SELECT *
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id

Resources