I have 2 tables.
tbl1 has colums:
id1, someothercolumns
tbl2 has colums:
id2, id1, bool
I can join them by id1 without problem. I can do this:
SELECT tbl1.id1, Count(tbl2.id2) AS CountOfid2
FROM tbl1 LEFT JOIN tbl2 ON tbl1.id1= tbl2.id1
GROUP BY tbl1.id1;
But I don't want to count all items from tbl2, on those where
bool=false
So, I want to see all records from tbl1 and not count all records from tbl2. I tried with sub-select, but Access doesn't like my ideas. :(
Can you help me?
I figured out the solution:
SELECT tbl1.id1, Count(tmptbl.id2) AS CountOfid2
FROM tbl1 LEFT JOIN (SELECT tbl2.id1, tbl2.id2 FROM tbl2 WHERE bool=false) as tmptbl ON tbl1.id1= tmptbl.id1
GROUP BY tbl1.id1;
Give me cookie!
please test this script:
select distinct t1.id1,t4.countofID2 from tbl1 t1
left outer join
(select t3.con as countofID2 ,* from tbl2
,(SELECT COUNT(t2.id2)as con
FROM tbl2 t2) as t3)as t4
on(t1.id1=t4.id1)
Be happy .....
Related
I have about 10 tables that I make one big nested tables by rounds with the following query:
R1 AS(
SELECT ANY_VALUE(Table1).*, ARRAY_AGG(( SELECT AS STRUCT Table2.* EXCEPT(ID))) AS Table2
FROM Table1 LEFT JOIN Table2 USING(ID)
GROUP BY Table1.ID),
R2 AS(
SELECT ANY_VALUE(R1).*, ARRAY_AGG(( SELECT AS STRUCT Table3.* EXCEPT(ID))) AS Table3
FROM R1 LEFT JOIN Table3 USING(ID)
GROUP BY R1.ID),
...
SELECT ANY_VALUE(R9).*, ARRAY_AGG(( SELECT AS STRUCT Table10.* EXCEPT(ID))) AS Table10
FROM R9 LEFT JOIN Table10 USING(ID)
The thing is that for example in my first table I can have two records with the same ID but some other fields will be different and I want to consider them as two distinct records and thus group by all the fields of the table while I join.
Then I want to do the same with all the "sub-table" (the R tables in the query), so I will able to group by all the fields of the nested tables.
How can I do it easily ?
I tried GROUP BY Table1.* but it doesn't work...
Thank you in advance
Try to_json_string:
...
FROM Table1 t1
...
GROUP BY to_json_string(t1)
You seem to want something like this:
select *
from table1 t1 left join
(select t2.*
from table2 t2
where true
qualify row_number() over (partition by t2.id order by t2.id) = 0
) t2
using (id)
This uses qualify instead of group by to fetch one row.
If you don't want all rows from from table1, you can whittle them down as well:
select *
from (select t1.*
from table1 t1
where true
qualify row_number() over (partition by id, col1, col2 order by id) = 1
) t1 left join
(select t2.*
from table2 t2
where true
qualify row_number() over (partition by t2.id order by t2.id) = 0
) t2
using (id)
How to Group By all fields ...?
I tried GROUP BY Table1.* but it doesn't work...
Consider below example
SELECT ANY_VALUE(t1).*,
ARRAY_AGG(( SELECT AS STRUCT t2.* EXCEPT(ID))) AS Table2
FROM Table1 t1 LEFT JOIN Table2 t2 USING(ID)
GROUP BY FORMAT('%t', t1)
I have two tables, as below:
I'm hoping to create a view, where the result is as per the result above. That is, the Act column is the total of all matching records between tbl1 and tbl2.
Additional explaination, graphically:
I'd left join tbl1 with an aggregate query on tbl2:
SELECT t1.id, t1.req, COALESCE(t2.act, 0) AS act
FROM tbl1 AS t1
LEFT JOIN (SELECT id, SUM(act)
FROM tbl2
GROUP BY id) t2 ON t1.id = t2.id
I have a table t1. It has columns [id] and [id2].
Select count(*) from t1 where id=1;
returns 31,189 records
Select count(*) from t1 where id=2;
returns 31,173 records
I want to know the records where id2 is in id=1 but not in id=2.
So, I use the following:
Select * from t1 a left join t1 b on a.id2=b.id2
Where a.id=2 And b.id=1
And b.id2 Is Null;
It returns zero records.
Using an inner join to see how many records have id2 in common, I do...
Select * from t1 a inner join t1 b on a.id2=b.id2
Where a.id=2 And b.id=1;
And that returns 31,060. So where are the extra records in my first query that don't match?
I am sure I must be missing something obvious.
Sample Data
id id2
1 101
1 102
1 103
2 101
2 102
My expected results is to find the record with '103' in it. 'id2' not shared.
Thanks for any help.
Jeff
You are attempting to do what is generally called an exclude join. This involves doing a LEFT JOIN between two tables, then using a WHERE clause to only select rows where the right table is null, i.e. there was no record to join. In this way, you select everything from the left table except what exists in the right table.
With this data, it would look something like this:
SELECT
t1.id,
t1.id2
FROM test_table t1
LEFT JOIN
(SELECT
id,
id2
FROM test_table
WHERE id = 2) t2
ON t2.id2 = t1.id2
WHERE t1.id = 1
AND t2.id IS NULL --This is what makes the exclude join happen
And here is a SQLFiddle demonstrating this in MySQL 5.7 with the sample data you provided.
I think maybe Access changes the left join to an inner join when you add a where clause to filter rows (I know SQL Server does this), but if you do the filtering in derived tables it should work:
select
a.*
from
(select * from t1 where id = 1) a
left join
(select * from t1 where id = 2) b
on a.id2 = b.id2
where b.id2 is null
I have 3 tables.
Table1: Group_Code, Group_Name,companyID;(PK: Group_Code)
Table2: PartyID,GroupID,companyID;(FK: GroupID, PK:PartyID)
Table3: VendorID, companyID;(FK:VendorID)
I want to fetch Group_Name from Table1 for all VendorID of Table3. How can I do this?
here i write a code. But it shows "Syntex error in FROM clause." My database is in ms access.
select Group_Name from Table1 join Table2 on Table1.Group_Code= Table2.GroupID
join Table3 on Table2.PartyID=Table3.VendorID
try this !!!
SELECT table1.group_name FROM (table1
INNER JOIN ON table1.group_code=table2.groupid)
INNER JOIN table3 ON table2.partyid=table3.vendorid
GROUP BY table1.group_name
select Group_Name from Table1
join Table2 on Table1.Group_Code = Table2.GroupID
join Table3 on Table2.PartyID = Table3.VendorID
SELECT table1.group_name FROM table1 join table2
ON table1.group_code=table2.groupid
join table3 ON table2.partyid=table3.vendorid
error becoz you didnt take the group name DB instance ?
You Can do this :
select Table1.Group_Name, Table3.VendorID from Table1 join Table2 on Table1.Group_Code= Table2.GroupID join Table3 on Table2.PartyID =Table3.VendorID
If you are data has been stored in proper relationship. the query should get you going. :)
use this code for question,
Select Table1.Group_Name from ((Table1
left join Table2 on Table1.Group_Code=Table2.GroupID)
left join Table3 on Table2.PartyID=Table3.VendorID)
I have two tables
Table1: FieldA, FieldB
Table2: FieldA
Table1 is grouped by FieldB, and FieldA is the link between the two tables.
For each grouping in Table1, if all rows in that group do not have an entry in Table2, then return no rows corresponding to this group. If at least one row in the group has an entry in Table2, then return all rows in the group.
Is this kind of query possible?
Thanks
Simon
it sounds like you need to do a simple inner join:
SELECT t1.* FROM Table1 t1 INNER JOIN Table2 t2 ON t1.FieldA=t2.FieldA;
I'm unclear from your question if the values in Table2 are unique. If they are not unique then this subquery might work better:
SELECT * FROM Table1 WHERE FieldA in (SELECT distinct(FieldA) FROM Table2);
If I understand your problem correctly, this JOIN would to do it;
SELECT DISTINCT t1a.*
FROM Table1 t1a
JOIN Table1 t1b
ON t1a.FieldB = t1b.FieldB
JOIN Table2 t2
ON t2.FieldA=t1b.FieldA;
Demo here.
SELECT *
FROM Table1
WHERE FieldB IN (
SELECT t1.FieldB
FROM Table1 t1
JOIN Table2 t2
ON t1.FieldA = t2.FieldA
GROUP BY t1.FieldB
)