OK so I have a query I am trying to build.. I have 2 tables, table1 has a bunch of regular records as normal with a unique ID (auto increment) and table2 has records that include some of those ids from table1. I am trying to order by the highest records with that same ID in table1.. Heres what I've got:
SELECT * FROM table1
WHERE table1.status = 1
AND (SELECT COUNT(*) FROM table2 WHERE table2.tbl1_id = table1.id)
ORDER BY table1.id DESC
Thanks :)
SELECT table1.id
FROM table1
LEFT JOIN table2 ON table2.tbl1_id = table1.id
WHERE table1.status = 1
GROUP BY table1.id
ORDER BY COUNT(table2.tbl1_id) DESC
Try this:
SELECT a.*, b.cnt
FROM table1 a LEFT JOIN
(
SELECT tbl1_id, COUNT(*) cnt
FROM table2
GROUP BY tbl1_id
) b
ON a.id = b.tbl1_id
WHERE table1.status = 1
ORDER BY cnt DESC
Related
TableA
id | b_id
TableB
id | name
select b_id, count(*) from TableA group by b_id order by count(*) asc
I am getting the result as
50 1
100 2
150 3
I want corresponding names to 50,100, 150 as my result
item50 1
item100 2
item150 3
You may add the name column to the GROUP BY and SELECT clauses:
SELECT
b.id,
b.name,
COUNT(a.b_id) AS cnt
FROM TableB b
LEFT JOIN TableA a
ON b.id = a.b_id
GROUP BY
b.id,
b.name
ORDER BY
COUNT(*);
select tableB.name||tableA.b_id:: character varying as b_id,count(*)
from tableA
inner join tableB on tableA.b_id = tableB.id
group by tableB.name,tableA.b_id
I am faced with an issue returning data records. I first wanted to find records where a certain value existed '0000' in a column doing a join for 2 tables. Below is my T-SQL;
SELECT ColumnA, ColumnB, ColumnC
FROM Table1, Table2
WHERE Table1.ColumnB. = Table2.ColumnB
and ColumnC='0000'
This returns the desired data records where '0000' exists at least once in all returned records.
The question I have is, how do I do the same, only returning Distinct records where '0000' is the only value that exists (one or many times) and no other value exists for the returned data records
Many thanks!
SELECT distinct ColumnA, ColumnB
FROM Table1, Table2
WHERE Table1.ColumnB = Table2.ColumnB
and ColumnC = '0000'
except
SELECT distinct ColumnA, ColumnB
FROM Table1, Table2
WHERE Table1.ColumnB = Table2.ColumnB
and ColumnC <> '0000'
if you want to use a join
guessing ColumnC is in Table2
SELECT distinct Table1.ColumnA, Table1.ColumnB, Table2.ColumnC
FROM Table1
JOIN Table2
on Table1.ColumnB = Table2.ColumnB
and Table2.ColumnC = '0000'
left join Table2 exclude
on Table1.ColumnB = exclude.ColumnB
and exclude.ColumnC <> '0000'
where exclude.ColumnB is null
this may be the best performer
SELECT distinct Table1.ColumnA, Table1.ColumnB, Table2.ColumnC
FROM Table1
JOIN Table2
on Table1.ColumnB = Table2.ColumnB
and Table2.ColumnC = '0000'
and not exists (select * from table2 exclude
where exclude.ColumnB = Table1.ColumnB
and exclude.ColumnC <> '0000')
Here's a solution using the ALL keyword
SELECT DISTINCT
columna,
columnb,
columnc
FROM table1 t1
INNER JOIN table2 t2
ON table1.columnb = table2.columnb
WHERE t2.columnc = '0000'
AND t2.columnc = ALL (SELECT columnc
FROM table2 t2Check
WHERE t2.columnb = t2Check.columb)
Here's a Example where I'm using only one table since the joins in your problem aren't actually important.
I have 2 tables.
Table 1:
Id Name
1 John
2 Mike
3 Sam
Table 2:
Name Data
John Data1
John Data1
John Data1
Mike Data2
Mike Data2
Sam Data3
If I write
select Table2.Name, Table2.Data
from Table1
inner join Table2 on Table1.Name= Table2.Name
I get all the duplicate data.
I would like to be able to retrieve something like:
John Data1
Mike Data2
Sam Data3
SELECT DISTINCT NAME
, DATA
FROM Table2
WHERE NAME IN (SELECT NAME
FROM Table1)
There are a few different options here...
There are two ways to do that.
You can use distinct clause:
select distinct t2.Name, t2.Data
from Table1 t1
inner join Table2 t2 on t1.Name= t2.Name
Here is link to MSDN.
You can use group by :
select t2.Name, t2.Data
from Table1 t1
inner join Table2 t2 on t1.Name= t2.Name
group by t2.Name, t2.Data
Here is link to MSDN.
I prefer second solution, because I always can add grouping functions.
Note:
In both queries I used aliases (t1,t2). It's more readable.
You can use CTE for this and apply a row_number()
;with cte as
(
select t1.name, t2.data,
row_number() over(partition by t1.id order by t1.id) rn
from table1 t1
inner join table2 t2
on t1.name = t2.name
)
select *
from cte
where rn = 1
Or a non-CTE version:
select *
from
(
select t1.name, t2.data,
row_number() over(partition by t1.id order by t1.id) rn
from table1 t1
inner join table2 t2
on t1.name = t2.name
) x
where rn = 1
see SQL Fiddle with Demo
Are you looking for something like:
select Table2.Name, Table2.Data, count(*) from Table1
inner join Table2 on Table1.Name= Table2.Name
group by Table2.Name, Table2.Data;
select id from table = 260 595 records
select id from table
left join table2 on table2.id = table.parent = 260 595 records
select id from table
inner join table2 on table2.id = table.parent = 260 192 records
What is the easiest way to find out what records in table have wrong (nonexistent) join so I can correct them?
Thanks.
select id from table left join table2 on table2.id = table.parent
EXCEPT
select id from table inner join table2 on table2.id = table.parent
Use this(it will give you id's in table that doesn't have corresponding records in table2):
select id
from table left join table2
on table2.id = table.parent
where table.parent is null
select id from table inner join table2 on table2.id <> table.parent
I have a SQL like this:
Select tbl.id, tbl.name
From
(select table1.id, table1.name
from table1
inner join table2 on table1.id = table2.id
order by table2.priority
) tbl
group by table1.id
order by table1.name
What I'm trying to achieve is to first sort (order by table2.priority), and then get the record with table1.id, name with highest priority.
Note, MAX(table2.priority) doesn't work here, because table1 to table2 is one to many, and for one table1 record, table2 can have N records with the highest priority = 1, where another table1 record with highest priority = 3.
If you only need one record from the result, and they are in order such that the record you need is at the end (or beginning) of the sort, simply limit the results to one. i.e:
SELECT tbl.id, tbl.name
FROM (
SELECT table1.id, table1.name
FROM table1
INNER JOIN table2 ON table1.id = table2.id
ORDER BY table2.priority
) tbl
GROUP BY table1.id
ORDER BY table1.name
LIMIT 1;
Note that depending on the order, you could specify ASC or DESC to ensure the correct record is the one you retrieve.