SQL - Using SELECT in the WHERE clause of another SELECT - sql

i have next query
select No,
Description,
Item,
Date
from myTable1
and get
and just one select
select * from myTable2
and get
Now I need to select from MyTable1 all data and join with MyTable2
and get sum of quantity
group by NO and ITEM
where MyTable2.Date <= MyTable1.Date
Any idea?
I want to get this table

Try this to get you started, without more info it is difficult to produce an accurate answer:
SELECT T1.No
, T1.Item
, SUM(T2.Quantity)
, MIN(T1.Date) as Date FROM mytable1 T1
INNER JOIN mytable2 T2
ON T1.No = T2.No AND T1.Item = T2.Item AND T2.Date <= T1.Date
GROUP BY T1.No, T1.Item

Try this:
select t1.*,sum(t2.quantity)
from myTable1 t1
INNER JOIN mytable2 t2 on t2.no = t1.no and t2.item=t1.item
WHERE t2.date<=t1.date
GROUP BY t1.No,t1.item

Try This.
SELECT
T1.No,
t1.Description ,
T1.Item,
MIN(T1.Date) as Date ,
SUM(T2.Quantity) as Quantity
FROM Table1 T1 INNER JOIN Table2 T2
ON T1.No = T2.No AND T1.Item = T2.Item AND T2.Date <= T1.Date
GROUP BY T1.No, T1.Item , T1.Description
let us know if you have any quetions.

SELECT No,Description,Item,Date,sum(t2.quantity)
FROM myTable1
JOIN myTable2 ON myTable1.No = myTable2.No and Description = item
WHERE MyTable2.Date <= MyTable1.Date
GROUP BY No,Description,Item,Date

Try this:
SELECT T1.No, T1.Description, T1.Item, T1.Date, sum(T2.Quantity)
FROM myTable1 T1
LEFT JOIN myTable2 T2
ON T1.No = T2.No AND T1.Item = T2.Item AND T1.Date >= T2.Date
GROUP BY T1.No, T1.Item;

Related

Displaying records only when there are more than same three values

Is there a way to display only records where one email corresponds to more than 3 names?
I have tried the code below, but it does not return anything.
SELECT
t1.Name, t2.Email
FROM
Table1 t1
JOIN Table2 t2 on t1.ID=t2.PersonID
GROUP BY
t1.Name, t2.Email
HAVING COUNT(t2.Email) > 3
If your version supports window function then you can do :
select t.*
from (select t1.name, t2.email,
count(t2.email) over (partition by t1.name) as cnt
from t1 inner join
t2
on t2.personid = t1.id
) t
where cnt > 3;
I think you want:
SELECT t1.Name, GROUP_CONCAT(t2.Email)
FROM Table1 t1 JOIN
Table2 t2
ON t1.ID = t2.PersonID
GROUP BY t1.Name
HAVING COUNT(t2.Email) > 3;
The big change is to the GROUP BY -- this does not have EMAIL. You seem to want the emails returns on each row, so this concatenates them together.
EDIT:
In SQL Server, you would use string_agg():
SELECT t1.Name, STRING_AGG(t2.Email, ',')
FROM Table1 t1 JOIN
Table2 t2
ON t1.ID = t2.PersonID
GROUP BY t1.Name
HAVING COUNT(t2.Email) > 3;
Or, if you want individual rows:
SELECT n.*
FROM (SELECT t1.Name, t2.Email,
COUNT(*) OVER (PARTITION BY t1.Name) as cnt
FROM Table1 t1 JOIN
Table2 t2
ON t1.ID = t2.PersonID
) n
WHERE cnt > 3
ORDER BY Name;

How to get Top 1 with Sort from Join

Instead of doing something like this:
SELECT
t1.*,
(SELECT TOP 1 t2.Name FROM Table2 t2 Order By t2.Number) Val1
FROM Table1 t1
How would it be done with a Join instead?
SELECT
t1.*,
TOP 1 t2.Name
FROM Table1 t1
JOIN Table2 t2 Order By t2.Number
or is it even possible?
select top 1 * from (SELECT t1.*, t2.Number num FROM table1 t1 JOIN table2 t2 on t1.commoncolumn=t2.commoncolumn ) t order by t.num
Try this (id is the join field)
with Table2Data as
(
SELECT Id , Name ,row_number() over(partition by Id order by number) rowNumber FROM Table2
)
select t1.* ,t2.name from table1 t1
inner join Table2Data t2 on t1.Id=t2.Id and t2.rowNumber=1
The following accomplishes the same thing.
SELECT t1.*, MIN(t2.Name) OVER() val1 FROM Table1 INNER JOIN Table2 t2 on t1.id = t2.id
It can be done with a CROSS APPLY. Assuming Id is the common field:
SELECT t1.*
, t3.Name
FROM Table1 t1
CROSS APPLY (
SELECT TOP 1 t2.Name
FROM Table2 t2
WHERE t1.Id = t2.Id
ORDER BY t2.Number
) t3

How to join a table in mysql

My requirement is like :
select (*ALL columns of table1, [count of rows of TABLE2 where TABLE1.id = Table2.id]
from TABLE1, TABLE2
where TABLE1.status = 'A'
Am now to SQL, please pardon if the question is too simple
select t1.col1, t1.col2, t1.col3, count(t2.id)
from table1 t1
left join TABLE2 t2 on t1.id = t2.id
where t1.status = 'A'
group by t1.col1, t1.col2, t1.col3
Using correlated subquery:
SELECT t1.*
,(SELECT COUNT(*) FROM TABLE2 t2 WHERE t1.id = t2.id) AS Counter
FROM TABLE1 t1
WHERE t1.status = 'A';

PostgreSQL LEFT JOIN not working properly

I have 2 tables:
T1 (id, flag1)
T2 (id, amount, date, flag2, t1_id);
I have the following query:
SELECT T1.id, ROUND(COALESCE(SUM(T2.amount), 0), 2) AS spent_amount
FROM T1
LEFT JOIN T2 ON T2.t1_id = T1.id
WHERE T2.date <= '2014-01-01' AND T2.flag2 = 't' AND T1.flag1 = 't'
GROUP BY T1.id
The problem is that I want to have a row in the result such as: id = 123, spent_amount = 0 in case where I have an entrance in T1, but it has no connected rows in T2.
Having a WHERE clause on your T2 it will filter out all NULLS:
SELECT T1.id, ROUND(COALESCE(SUM(T2.amount), 0), 2) AS spent_amount
FROM T1
LEFT JOIN T2
ON T2.t1_id = T1.id
AND T2.date <= '2014-01-01'
AND T2.flag2 = 't'
WHERE T1.flag1 = 't'

Adding a variable to a join table

I'm struggling to figure out how I can add a convenience data column to a join table to use for evaluation.
Sorry if the terminology is incorrect, I'm a bit of an SQL newbie.
For this query structure:
SELECT t1.id
FROM
table1 t1
INNER JOIN
table2 t2 ON t1.c2 = t2.c1
WHERE
t1.c5=5;
...I would like to add a column in the join table that is calculated using a function of a few of t1s columns. For example, the sum of t1.x, t1.y, t1.z in a variable called score that can then be referenced in the WHERE clause.
The reason behind wanting to do this is it will be referenced multiple times and will reduce verbosity and help readability.
I presume it will look something like:
SELECT t1.id
FROM
table1 t1
INNER JOIN
table2 t2 ON t1.c2 = t2.c1
-- function(t1.x, t1.y, t1.z) as score
WHERE
t1.c5=5;
--AND score ...
Using PostgreSQL 9.3
If the score is calculated from the t1 columns, then use a subquery on t1:
SELECT t1.id
FROM (select t1.*, function(t1.x, t1.y, t1.z) as score
from table1 t1
) t1 INNER JOIN
table2 t2
ON t1.c2 = t2.c1
WHERE t1.c5 = 5 AND
t1.score . . .
A good option would be to use the having clause.
Select t1.id, SUM(t1.x + t1.y + t1.z)
FROM table1 t1 inner join table2 t2 on t1.c2 = t2.c1
WHERE t1.c5 = 5
GROUP BY t1.id
Having SUM(t1.x + t1.y + t1.z) = ...
Or, you could do a sub-query in the from clause. The below code makes some assumptions on structure.
SELECT t1.id
FROM
(Select SUM(t1.x + t1.y + t1.z) as score, t1.c5, t1.c2, t1.id
FROM table1 t1
GROUP BY t1.id
) t1
INNER JOIN table2 t2 on t1.c2 = t2.c1
WHERE
t1.c5=5
And t1.score = ...