SQLITE: Keep all unmatched rows during join - database

I'm trying to join multiple tables.
Table 1:
ID
TEMP
DESC
NUMB
32
89
Y
6
47
NULL
Y
5
56
43
N
4
34
54
N
3
22
78
NULL
NULL
Table 2
ID
IND
FLAV
32
Y
G
47
N
G
56
Y
R
34
Y
B
22
Y
Y
Table 3:
ID
COLOR
SHAPE
32
RED
SQUARE
47
BLUE
CIRCLE
52
NULL
TRI
22
ORANGE
NULL
I want the resulting table:
ID
TEMP
DESC
NUMB
IND
FLAV
COLOR
SHAPE
32
89
Y
6
Y
G
RED
SQUARE
47
NULL
Y
5
N
G
BLUE
CIRCLE
56
43
N
4
Y
R
NULL
NULL
34
54
N
3
Y
B
NULL
NULL
22
78
NULL
NULL
Y
Y
ORANGE
NULL
52
NULL
NULL
NULL
NULL
NULL
NULL
TRI
The row order of the resulting ID's doesn't matter to me.
I've tried:
SELECT *
FROM Table1
INNER JOIN Table2 USING(ID)
LEFT JOIN Table3 USING(ID)
But it leaves out ID 52. I want to be sure no unmatched ID's from either table are left out.
Is this possible in SQLITE?

For this requirement the correct type of join is FULL OUTER JOIN wich is not supported by SQLite.
A workaround is to use a subquery that returns the distinct ids of all 3 tables and then do LEFT joins to the tables with the USING clause:
SELECT *
FROM (SELECT ID FROM Table1 UNION SELECT ID FROM Table2 UNION SELECT ID FROM Table3) t
LEFT JOIN Table1 USING (id)
LEFT JOIN Table2 USING (id)
LEFT JOIN Table3 USING (id);
See the demo.

Related

TSQL Selection Criteria For Join - Left Join

Select *
From cacheAttendanceMeasures cam
Left Join dmcUserSelectedAttendanceMeasures usam on usam.attMeasureID = cam.attMeasureID
And usam.personID = #personID
And usam.pageID = #pageID
I understand that the above query will return all rows from cacheAttendanceMeasures as well as any rows from dmcUserSelectedAttendanceMeasures and dmcUserSelectedStudentMonths where the conditions match. What I would really like it to do is this:
If the left join(s) don't match, then return all rows from cacheAttendanceMeasures -- so what it is currently doing
However, if the left join(s) DO match, then return ONLY the rows that match from cacheAttendanceMeasures
Is this possible?
EDIT:
I simplified the query above to only include one Left Join table, I don't want to over-complicate the issue.
Below are dataset examples I'd like to see returned based on if there are no matches between the tables versus there is a match:
cam Table
cID attMeasureID Value
1 1 530
2 2 95.7
3 3 380
4 4 742.57
5 5 200
usam Table
uID personID pageID attMeasureID
1 877450 31 1
2 923450 28 2
3 877450 31 3
4 369842 28 4
5 212193 25 1
Dataset to Return if #personID = 577597 & #pageID = 20:
CID attMeasureID Value uID pageID attMeasureID
1 1 530 null nul null
2 2 95.7 null null null
3 3 380 null null null
4 4 742.57 null null null
5 5 200 null null null
Dataset to Return if #personID = 877450 & #pageID = 31:
CID attMeasureID Value uID personID pageID attMeasureID
1 1 530 1 877450 31 1
3 3 380 3 877450 31 3
I'm not really sure what results you want...
At first I thought maybe something like this..
SELECT *
FROM cacheAttendanceMeasures cam
LEFT JOIN dmcUserSelectedAttendanceMeasures usam
on usam.attMeasureID = cam.attMeasureID
And usam.personID = #personID
And usam.pageID = #pageID
LEFT JOIN dmcUserSelectedStudentMonths ussm
on ussm.monthSeq = cam.pupilMonth
And ussm.personID = USAM.PersonID
And ussm.pageID = USAM.PageId
--This where clause would seem silly as it negates the left joins; making them inner joins and violates #1 as to what you're after. so I don't think that's what you're after...
WHERE ussm.personID is not null
and usam.personID is not null
So with rule 2 are you saying... if a single record isn't null, then only return records which exist in both Left joined tables and the 1st table?
So given:
T1 T2 T3
X X X
Y NULL Y
Z Y NULL
T NULL NULL
I think you would want just record X
But Given
So given:
T1 T2 T3
X X NULL
Y NULL Y
Z Y NULL
T NULL NULL
I think you would want X, Y Z, and T... need better clairification.

Joining two SQL tables based on the equality of few columns

I am trying to Create a SQL View by joining two SQL tables and return only the lowest value from second table and all the rows from first table similar to left join.
My problem can be clearly explained with the below example.
Table1
Id Product Grade Term Bid Offer
100 ABC A Q1 10 20
101 ABC A Q1 5 25
102 XYZ A Q2 25 30
103 XYZ B Q2 20 30
Table2
Id Product Grade Term TradeValue
1 ABC A Q1 100
2 ABC A Q1 95
3 XYZ B Q2 100
In the above data I want to join Table1 and Table2 when ever the columns Product,Grade and Term from both the tables are equal and return all the rows from Table1 while joining the lowest Value of the column TradeValue from Table2 to the first record of the match and making TradeValue as NULL for other rows of the resultant View and the resultant View should have the Id of Table2 as LTID
So the resultant SQL View should be
RESULT
Id Product Grade Term Bid Offer TradeValue LTID
100 ABC A Q1 10 20 95 2
101 ABC A Q1 5 25 NULL 2
102 XYZ A Q2 25 30 NULL NULL
103 XYZ B Q2 20 30 100 3
I tried using the following query
CREATE VIEW [dbo].[ViewCC]
AS
SELECT
a.Id,a.Product,a.Grade,a.Term,a.Bid,a.Offer,
b.TradeValue
FROM Table1 AS a
left JOIN (SELECT Product,Grade,Term,MIN(TradeValue) TradeValue from Table2 Group by Product,Grade,Term,) AS b
ON b.Product=a.Product
and b.Grade=a.Grade
and b.Term=a.Term
GO
The above Query returned the following data which is apt to the query I wrote but that is not what I was trying to get
Id Product Grade Term Bid Offer TradeValue
100 ABC A Q1 10 20 95
101 ABC A Q1 5 25 95 --This should be null
102 XYZ A Q2 25 30 NULL
103 XYZ B Q2 20 30 100
As we can see minimum value of TradeValue being assigned to all matching rows in Table1 and also I was not able to return Id As LTID from Table2 as I have issues with group by clause as I cannot group it by b.Id as it returns too many rows.
May I know a better way to deal with this?
You need a row number attached to each record from Table1, so that the requirement of only joining the first record from each group of Table1 can be fulfilled:
CREATE VIEW [dbo].[ViewCC]
AS
SELECT a.Id, a.Product, a.Grade, a.Term, a.Bid, a.Offer,
b.TradeValue, b.Id AS LTID
FROM (
SELECT *, ROW_NUMBER() OVER(PARTITION BY Product, Grade, Term ORDER BY Id) AS rn
FROM Table1
) a
OUTER APPLY (
SELECT TOP 1 CASE WHEN rn = 1 THEN TradeValue
ELSE NULL
END AS TradeValue, Id
FROM Table2
WHERE Product=a.Product AND Grade=a.Grade AND Term=a.Term
ORDER BY TradeValue) b
GO
OUTER APPLY returns a table expression containing either the matching record from Table2 with the lowest TradeValue, or NULL if no matching record exists.

To count rows that is NOT NULL in SSRS

I want to number some base rows in table without mixing the ordering. I have table like this:
Status ProductId
A 12
NULL 25
B 35
C 56
NULL 89
NULL 99
D 120
E 140
I want to add No column, to count Statuses which is not null with same ProductId ordering, but, don't want to count NULL rows. I want the result like this:
No Status ProductId
1 A 12
NULL 25
2 B 35
3 C 56
NULL 89
NULL 99
4 D 120
5 E 140
I work on SQL Sever 2008, SSRS. Someone can give solution in SQL side or in RDL file.
You can do this:
WITH CTE
AS
(
SELECT
ROW_NUMBER() OVER(ORDER BY Status) AS No,
Status, ProductId
FROM table1
WHERE Status IS NOT NULL
)
SELECT
c.No,
t.Status,
t.ProductId
FROM table1 AS t
LEFT JOIN CTE AS c ON c.ProductId = t.ProductId
ORDER BY ProductId;
SQL Fiddle Demo
You can use this query directly in your report.

Left Join and Sum between tables giving me a weird result - PostgreSQL

Below are the two tables.
Table1:
Label value
A 10
A 18
A 15
B 11
B 20
B 10
C 17
C 17
C 18
Table2:
Label count
A 20
A 17
A 11
B 20
B 17
B 17
C 14
C 20
C 19
I'm running this query.
SELECT
Table1."label",
sum("value"),
sum("count")
FROM Table1
LEFT JOIN Table2 ON
Table1."label" = Table2."label"
GROUP BY Table1."label"
Result what i need to get is this.
label value count
A 43 48
B 41 54
C 52 53
But what i get is.
label value count
A 12491 12346
B 213295 1243456
C 1578105 123434
I don't know why I get that result.
Help me out if I'm doing something wrong in the query.
I've just stepped into PostgreSQL and databases.
Your result has three columns.
label, sum(value), sum(count)
so
label sum
A 12491
B 213295
C 1578105
you see is :
a 124 91
b 2132 95
c 15781 105
what's the space ?
SEE MY EXP :
digoal=# create table t1(id int, v int);
CREATE TABLE
digoal=# create table t2(id int, c int);
CREATE TABLE
digoal=# insert into t1 values (1,10);
INSERT 0 1
digoal=# insert into t1 values (1,20);
INSERT 0 1
digoal=# insert into t1 values (1,30);
INSERT 0 1
digoal=# insert into t1 values (2,10);
INSERT 0 1
digoal=# insert into t1 values (2,20);
INSERT 0 1
digoal=# insert into t1 values (2,30);
INSERT 0 1
digoal=# insert into t1 values (3,10);
INSERT 0 1
digoal=# insert into t1 values (3,20);
INSERT 0 1
digoal=# insert into t1 values (3,30);
INSERT 0 1
digoal=# insert into t2 select * from t1;
INSERT 0 9
digoal=# select t1.id,sum(v),sum(c) from t1 left join t2 on t1.id=t2.id group by t1.id;
id | sum | sum
----+-----+-----
1 | 180 | 180
2 | 180 | 180
3 | 180 | 180
(3 rows)
you can set fieldsep to see.
pg93#db-172-16-3-150-> psql -A
psql (9.3.3)
Type "help" for help.
digoal=# \pset fieldsep ,
Field separator is ",".
digoal=# select t1.id,sum(v),sum(c) from t1 left join t2 on t1.id=t2.id group by t1.id;
id,sum,sum
1,180,180
2,180,180
3,180,180
(3 rows)

Updating each row in a table

I have two tables both of which have a column named column_value which holds a number value. Now what i want is to sum the values of column_value in both the tables individually for each row and then update the same column (column_value) in the first table with the sum that i get for each row.
For example I have table A and table B, both of them have a column name AMOUNT.
Table A:
id AMOUNT
1 20
2 30
Table B:
id AMOUNT
1 10
2 25
First of all i want to get the following result
id AMOUNT AMOUNT TOTALAMOUNT
1 20 10 30
2 30 25 55
Now i would like to update each row of the A table with the TOTALAMOUNT against each id
so that after the Update the table A should look like
id AMOUNT
1 30
2 55
Selection :
SELECT A.ID,
NVL(A.AMOUNT,0) A_AMOUNT ,
NVL(B.AMOUNT,0) B_AMOUNT ,
NVL(A.AMOUNT,0) + NVL(B.AMOUNT,0) AS TOTAL_AMOUNT
FROM TABLEA A, TABLEB B
WHERE A.ID = B.ID
Update:
UPDATE TABLEA A
SET A.AMOUNT = (SELECT NVL(A.AMOUNT,0) + NVL(B.AMOUNT,0)
FROM TABLEB B
WHERE A.ID = B.ID)
You should have to use left join. For example:
Table_a
id val
1 10
2 20
Table_b
id val
1 20
2 30
update Table_a a left join Table_b b on a.id = b.id set a.val = (a.val+b.val);
After this operation:
Table_a
id val
1 30
2 50

Resources