I have 3 tables Trial1, TrialMid and Trial2. The tables are as follows :
Trial1:
ID Location Numbers
--------------------------
TR1 US 890
TR2 AME 876
TR3 HUY 78
TrialMid
ID Trial2Id Area Trial1id
----------------------------------------
TRM01 tr11 45 TR1
TRM02 tr11 345 TR1
TRM03 tr22 456 TR1
Trial2
ID Name Area Reference
-----------------------------------------
tr11 Guza 897 324
tr22 asd 876 12
These are the three tables and I want to get the ID from Trial2 using the Id from Trial1.
I want this output (using the Trial1id):
ID
-----
tr11
tr22
I also want to get the count of the Trial2 Ids but I can't use "group by".
The challenge I am facing is that the query that I want is to get the result as :
Total Trial1
----------------
2 TR1
Id Trial1
-----------------
tr11 TR1
tr22 TR1
I want to get the Ids from the table Trial2 and not from TrialMid. If I use count() by using group by it is giving me result as
Total Trial1
-----------------
2 TR1
1 TR1
Your question is very hard to follow. But I think you might be asking for the number of distinct Trial2Ids in the "mid" table for each Trialid. If so:
select trial1id, count(distinct Trial2Id )
from TrialMid
group by trial1id;
You can try the below -
select trial1id,count(*) as total
from
(
select t.id as trial2id,t1.id as trial1id,row_number() over(partition by tm.Trial2Id order by tm.Area desc) as rn
from Trial2 t inner join TrialMid tm on t.id=tm.Trial2Id
inner join Trial1 t1 on tm.Trial1id=t1.id
)A where rn=1
As far as I can understand you're looking for a window function including PARTITION BY clause as mentioning that you can't use "group by" :
SELECT DISTINCT T2.ID AS "Total",
COUNT(T2.ID) OVER (PARTITION BY T1.ID, T2.ID) AS "Trial1"
FROM TrialMid TM
JOIN Trial2 T2
ON T2.ID = TM.Trial2Id
JOIN Trial1 T1
ON T1.ID = TM.Trial1Id
Total Trial1
tr11 2
tr22 1
Demo
Related
This is my data:
id
key
nbr of subs
1
ABC
10
1
XXX
3
2
MNO
120
3
ABC
5
3
FGH
110
I need the key for the record (ID) that has the max nbr of subscriptions:
id
key
nbr of subs
1
ABC
10
2
MNO
120
3
FGH
110
I don't mind deleting the extra records, or electing the ones I need and insert them into other table. Any ideas?
SELECT P.Key, MAX(P.[Nbr of Subcriptions])
FROM P
GROUP BY P.Key;
Thank you very much
You need correlated subquery. Try below SQL-
SELECT t1.ID, t1.Key, t1.NBR FROM Table1 as t1
INNER JOIN (SELECT Table1.ID, Max(Table1.NBR) AS MaxOfNBR
FROM Table1 GROUP BY Table1.ID) as t2 ON (t1.NBR = t2.MaxOfNBR) AND (t1.ID = t2.ID);
SELECT
t3.id,
t3.prod_ID,
MIN(diff) AS min_time
FROM
(SELECT
t1.id,
(UNIX_TIMESTAMP(t2.time_stamp_2) - UNIX_TIMESTAMP(t1.time_stamp)) AS diff
FROM
production t1
LEFT JOIN
process t2 ON t1.id = t2.id
HAVING
diff >= 0) tx
LEFT JOIN
production t3 ON t3.id = tx.id
GROUP BY
t3.id
After run, the returned result is:
id prod_ID min_time
-----------------------
1 2 1200
What it should return instead is
id prod_ID min_time
1 9 1200
I initially thought there was an error in joining, after multiple join test, same error result.
SQLFiddle
SQLFiddle_2
SQLFiddle_3
SQLFiddle_2 to clarify that I use Group By because I have multiple ID
SQLFiddle_3 to expand more.
After run on SQLFiddle_3, the returned result is:
id prod_ID min_time
-----------------------
1 2 1200
2 2 960
3 2 360
What it should be is
id prod_ID min_time
-----------------------
1 9 1200
2 2 960
3 3 360
In your query you GROUP BY t3.id only and you don't aggregate on t3.prod_ID, so the value returned is undefined.
I believe that you don't need to GROUP BY, or even rejoin to production.
Try this:
SELECT t1.id, t1.prod_ID,
(UNIX_TIMESTAMP(t2.time_stamp_2) - UNIX_TIMESTAMP(t1.time_stamp)) AS diff
FROM production t1 INNER JOIN process t2
ON t1.id = t2.id
AND (t1.id, t1.prod_ID) = (
SELECT p.id, p.prod_ID
FROM production p
WHERE p.id = t2.id AND (UNIX_TIMESTAMP(t2.time_stamp_2) - UNIX_TIMESTAMP(p.time_stamp)) >= 0
ORDER BY (UNIX_TIMESTAMP(t2.time_stamp_2) - UNIX_TIMESTAMP(p.time_stamp)) LIMIT 1
)
See the demo.
Results:
> id | prod_ID | diff
> -: | ------: | ---:
> 1 | 9 | 1200
> 2 | 2 | 960
Your query is very wrong indeed, your inner query is essentially making a list of the time differences, but then you throw all this good work away and don't join or group on the prod_id, so it essentially just picks the first value you inserted into your database. If you switch the order you insert the data so prod_id is inserted first then you get the result you want, but for the wrong reason.
Your inner query is sort of doing this:
SELECT
t1.id,
t1.prod_id,
(UNIX_TIMESTAMP(t2.time_stamp_2)-UNIX_TIMESTAMP(t1.time_stamp)) as diff
FROM production t1
left JOIN process t2
ON t1.id = t2.id
WHERE (UNIX_TIMESTAMP(t2.time_stamp_2)-UNIX_TIMESTAMP(t1.time_stamp)) > 0
If you run that you see the values you want:
id prod_id diff
1 2 1800
1 3 1380
1 9 1200
...but then your outer query does nothing with this.
There's many ways to do this, here's one simple method:
SELECT
t1.id,
t1.prod_id,
(UNIX_TIMESTAMP(t2.time_stamp_2)-UNIX_TIMESTAMP(t1.time_stamp)) as diff
FROM production t1
left JOIN process t2
ON t1.id = t2.id
WHERE (UNIX_TIMESTAMP(t2.time_stamp_2)-UNIX_TIMESTAMP(t1.time_stamp)) > 0
ORDER BY 3
LIMIT 1
Hi i have table1 and table2.
table1 is the logtime table of employees and table2 is the groupcode of the employee.
On table1 some employees has duplicate time in because they time in multiple time to just to secure their time in.
Table1
ID EMPID Time_IN
1 001 7:01 AM
2 004 7:04 AM
3 034 7:10 AM
4 034 7:11 AM
5 019 7:11 AM
6 019 7:12 AM
Table2
ID empID GroupName
1 001 AA
2 004 AB
3 034 AA
4 019 AA
result
GroupName CNT
AA 5
AB 1
Expected result
GroupName CNT
AA 3
AB 1
current query
Select b.GroupName, count(*) as cnt
from table1 a
inner join table2 b
on a.EMPID = b.empID
Group by b.GroupName
How can i achive as expected result above?
Thankyou in advance.
you can use distinct count as follows:
select t2.groupname, count(distinct empid) as cnt
from table1 t1 join table2 t2
on t1.empid = t2.empid
group by t2.groupname
The join is superfluous for the question you have asked:
select t2.GroupName, count(*) as cnt
from table2 t2
group by t2.GroupName;
This is much more efficient than joining and using count(distinct). You probably really have a different question, which should be asked as a new question.
I have table like below one -
Name Null Type
--------- -------- ------------
ID NOT NULL NUMBER
Name VARCHAR2(20)
PARENT_ID NUMBER
Table contents
ID Name PARENT_ID
--------- -------- ------------
1 Ramesh null*
2 Ajay 1
I want to find out best SQL join query where I can populate the results like below.
For each row I want to know the ParentName not ID. How can I do that ?
ID Name ParentName
--------- -------- ------------
1 Ramesh null*
2 Ajay Ramesh
*null or blank
This is not example of the my requirement.
I have tried below SQL with left join but I am not sure if its the proper way.
SELECT S1.ID,S1.CRID AS PARENT_CRID,S2.CRID AS CRID FROM DAJ_JOINS S1
left JOIN DAJ_JOINS S2
ON S1.ID=S2.PARENT_ID
order by id asc;
Your query is close but the joining clause needs to be reversed as
select
t1.ID,
t1.Name,
t2.Name as PARENT_Name
from DAJ_JOINS t1
left join DAJ_JOINS t2 on t1.PARENT_ID = t2.ID
order by t1.ID
With SQL Server 2005+, you can try:
SELECT *
FROM DAJ_JOINS D
OUTER APPLY (SELECT Name As Parent_Name FROM DAJ_JOINS WHERE ID = D.Parent_ID) A
I have one complicated question. I'll try to explain it with example:
have one table that have primary key, and I want to join other table there the first's table primary key is foreign key, and I want If in the second table there is duplicate foreign key to select the number of repeatability. For example:
1-st table:
id name
--- -----
1 Greg
2 Alan
3 George
4 John
5 Peter
2-nd table
id aid data
--- ----- -------
1 2 CCCV
2 2 VVVV
3 3 DDDDD
4 3 SSSS
5 4 PPPPP
I want the result of the join to be:
id(1st table) aid name Data Number
----------- ---- ----- ----- -----
1 null Greg null 1
2 1 Alan CCCV 1
2 2 Alan VVVV 2
3 3 George DDDDD 1
3 4 George SSSS 2
4 5 John PPPPP 1
5 null Peter null 1
I searched a lot, I couldn't find anything. Maybe I do not know how search, or there is no such thing as what I want to do.
SELECT Table1.id, Table2.id as aid, Table1.name, Table2.data,
GREATEST(1, (SELECT COUNT(*)
FROM Table2 t2
WHERE t2.aid = Table1.id
AND t2.id <= Table2.id))
AS number
FROM Table1
LEFT JOIN Table2
ON Table2.aid = Table1.id
ORDER BY id, aid;
works in both MySQL and PostgreSQL.
As per my comment, you've tagged this both MySQL and PostgreSQL.
This answer is for PostgreSQL.
SELECT
table1.id,
table2.aid,
table1.name,
table2.data,
ROW_NUMBER() OVER (PARTITION BY table1.id ORDER BY table2.aid) AS number
FROM
table1
LEFT JOIN
table2
ON table1.id = table2.aid
Queries for PostgreSQL 8.3 which has no window functions.
With bigger tables it is regularly much faster to use a JOIN instead of a correlated sub-query.
The first query aggregates values for Table2 before joining to Table1, which should befaster, too:
SELECT t1.id, t2.aid, t1.name, t2.data, COALESCE(t2.ct, 1) AS number
FROM Table1 t1
LEFT JOIN (
SELECT x.aid, x.data, count(y.aid) + 1 AS ct
FROM Table2 x
LEFT JOIN Table2 y ON x.aid = y.aid AND x.id > y.id
GROUP BY x.aid, x.data
) t2 ON t2.aid = t1.id
ORDER BY t1.id, t2.ct;
And ORDER BY should be fixed.
Alternative without sub-query. Might be faster, yet:
SELECT t1.id, t2.aid, t1.name, t2.data, count(*) + count(t3.id) AS number
FROM Table1 t1
LEFT JOIN Table2 t2 ON t2.aid = t1.id
LEFT JOIN Table2 t3 ON t3.aid = t2.aid AND t3.id < t2.id
GROUP BY t1.id, t2.aid, t1.name, t2.data
ORDER BY t1.id, count(t3.id);
Not sure, didn't test with a bigger set. Test performance with EXPLAIN ANALYZE. Could you report back your results?