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'
Related
I have three tables
t1 t2 t3
I want to select data from only two of the tables but need the ids and columns from other tables for reference.
How to select data from t3 and t2
WHERE t1.id = t2.id = t3.id
AND t1.fid = t2.fid = t3.fid
AND t1.type = 'abc'
id column will be the same value for all tables. fid column will have incremental fid's but need the ones where t1.type = 'abc' also
Would this work?
select data
from t3
select data
from t2
join on t1.id = t2.id and t2.id = t3.id
join on t1.fid = t2.fid and t2.fid = t3.fid and t1.type = 'abc'
where id = 1
Generally, this kind of problem can be solved using EXISTS as follows:
select data
from t2
join t1 on t1.id = t2.id and t1.fid = t2.fid
where exists (select 1 from t3 where t2.id = t3.id and t2.fid = t3.fid)
and t1.type = 'abc'
and t1.id = 1
You could try something like this:
select columns_that_you_want
from t1 x
inner join t2 y on x.id = y.id
inner join t3 z on y.id = z.id
where x.type = 'abc'
;
This will join the three tables, and filter just the type 'abc' that you want from table t1. Also you can add more filter on the where clause in case you need to filter more the result.
The query is compiled correctly. Prompt how to make correctly. You need to get three variables and check them out. Depending on the selected test date.
after when is not written correctly
select sum(t.sum),t3.cost, t.status
from Credit t inner join cost t3 on t.code= t3.code1
where t.id='1' and
t.date >= case (SELECT t1.mark1, t1.mark2, t2.mark3
FROM marks t1 INNER JOIN marksagain t2 ON (t1.id = t2.id)
WHERE t1.id = t.id)
when (0,0,6) then (INTNX('month',date(),6,'same'))
else(INTNX('month',date(),3,'same')) end
group by t.sum, t3.cost, t.status;`
Try out this:
select sum(t.sum),t3.cost, t.status
from Credit t inner join cost t3 on t.code= t3.code1
where t.id='1' and t.date >=
case when (SELECT t1.mark1 FROM marks t1 INNER JOIN marksagain t2 ON (t1.id = t2.id) WHERE t1.id = t.id) = 0
AND (SELECT t1.mark2 FROM marks t1 INNER JOIN marksagain t2 ON (t1.id = t2.id) WHERE t1.id = t.id) = 0
AND (SELECT t2.mark3 FROM marks t1 INNER JOIN marksagain t2 ON (t1.id = t2.id) WHERE t1.id = t.id) = 6
then (INTNX('month',date(),6,'same')) else(INTNX('month',date(),3,'same'))
end
group by t.sum, t3.cost, t.status;
I have two looK up tables, and need is to fetch value by correlating the both tables.
As of now I am doing this with multiple sub queries and trying to find a easiest way.
SELECT
(SELECT TYPE_NAME FROM T1 where FK_T2 = (SELECT PK FROM T2 WHERE T2.ID = 'A') AND T1.ID = 'AA')as A,
(SELECT TYPE_NAME FROM T1 where FK_T2 = (SELECT PK FROM T2 WHERE T2.ID = 'B') AND T1.ID = 'BB')AS B,
(SELECT TYPE_NAME FROM T1 where FK_T2 = (SELECT PK FROM T2 WHERE T2.ID = 'C') AND T1.ID = 'CC')AS C,
(SELECT TYPE_NAME FROM T1 where FK_T2 = (SELECT PK FROM T2 WHERE T2.ID = 'D') AND T1.ID = 'DD')AS D,
(SELECT TYPE_NAME FROM T1 where FK_T2 = (SELECT PK FROM T2 WHERE T2.ID = 'E') AND T1.ID = 'EE')AS E,
(SELECT TYPE_NAME FROM T1 where FK_T2 = (SELECT PK FROM T2 WHERE T2.ID = 'F') AND T1.ID = 'FF')AS F
FROM MYTABLE;
I assume you need something like this.
SELECT MAX ( CASE WHEN T1.ID = 'AA' AND T2.ID = 'A' THEN TYPE_NAME END ) as A,
MAX ( CASE WHEN T1.ID = 'BB' AND T2.ID = 'B' THEN TYPE_NAME END ) as B,
MAX ( CASE WHEN T1.ID = 'CC' AND T2.ID = 'C' THEN TYPE_NAME END ) as C,
..
FROM T1 t1
INNER JOIN T2 t2
ON t1.FK_T2 = t2.PK
I'll try something like:
SELECT
T1.TYPE_NAME,
T1.ID
FROM T1 t1
INNER JOIN T2 t2
ON t1.FK_T2 = t2.PK
WHERE
T1.ID||T1.ID = T2.ID
You could try rephrasing these queries as a single query consisting of a join between the two tables:
SELECT
T1.TYPE_NAME,
T1.ID
FROM T1 t1
INNER JOIN T2 t2
ON t1.FK_T2 = t2.PK
WHERE
(T1.ID = 'AA' AND T2.ID = 'A') OR
(T1.ID = 'BB' AND T2.ID = 'B') OR
(T1.ID = 'CC' AND T2.ID = 'C') OR
(T1.ID = 'DD' AND T2.ID = 'D') OR
(T1.ID = 'EE' AND T2.ID = 'E') OR
(T1.ID = 'FF' AND T2.ID = 'F');
You can keep track of to which subquery each returned record corresponds by checking the value of the ID column in the first table. This would serve as a marker for the source subquery.
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;
I have two SQL SELECT COUNT statements:
SELECT COUNT(*) FROM table1 t1
INNER JOIN table2 t2 ON t2.id = t1.rowid
WHERE t1.flag1 = false AND t2.flag2 = true;
SELECT COUNT(*) FROM table1 t1
INNER JOIN table2 t2 ON t2.id = t1.rowid
WHERE t1.flag1 = true AND t2.flag2 = false;
As can be seen the only difference of these two statements are the flipped condition.
But what I want to do is to combine the two statements into one so that the output becomes one table with two columns, the first column contains the result of the first statement and the second column contains the result of the second statement. Something like:
count1 | count 2
-------------------------
3456 | 9864
I use PostgreSQL by the way. Can someone let me know how to do this?
Many thanks
This should do it for you. I'm not to familiar with PostgreSql but I think it will work.
SELECT
SUM(CASE WHEN t1.Flag1 = false AND t2.flag2 = true THEN 1 ELSE 0 END) Count1,
SUM(CASE WHEN t1.Flag1 = true AND t2.flag2 = false THEN 1 ELSE 0 END) Count2
FROM
table1 t1
INNER JOIN table2 t2 ON t2.id = t1.rowid
If you really need it this way (use two sql queries and combine them) then:
select * from
(SELECT COUNT(*) FROM table1 t1 INNER JOIN table2 t2 ON t2.id = t1.rowid WHERE t1.flag1 = false AND t2.flag2 = true) a,
(SELECT COUNT(*) FROM table1 t1 INNER JOIN table2 t2 ON t2.id = t1.rowid WHERE t1.flag1 = true AND t2.flag2 = false) b
Based on your SQL, this would be better solution:
select
sum (case when not t1.flag1 and t2.flag2 then 1 else 0 end) as count1,
sum (case when t1.flag1 and not t2.flag2 then 1 else 0 end) as count2
FROM
table1 t1
INNER JOIN table2 t2 ON t2.id = t1.rowid
You can also cast boolean type to integer and shorten the sql (true::int = 1, false::int = 0):
select
sum((flag1::int<flag2::int)::int) count1,
sum((flag1::int>flag2::int)::int) count2
from
table1 t1
join table2 t2 ON t2.id = t1.rowid
And because true > false and false < true (at least in PostgreSQL) you can write:
select
sum((flag1 < flag2)::int) count1,
sum((flag1 > flag2)::int) count2
from
table1 t1
join table2 t2 ON t2.id = t1.rowid
Select * from
(
SELECT COUNT(*) FROM table1 t1
INNER JOIN table2 t2 ON t2.id = t1.rowid
WHERE t1.flag1 = false AND t2.flag2 = true) tab1,
(
SELECT COUNT(*) FROM table1 t1
INNER JOIN table2 t2 ON t2.id = t1.rowid
WHERE t1.flag1 = true AND t2.flag2 = false) tab2