Inner join not show result - sql

I want to do an inner join but I do not have any result:
select
t5.tcdv_id, t5.gps_coordx, t5.gps_coordy,
t6.status
from
(
select
t3.tcdv_id tcdv_id, t3.reg_date reg_date,
t4.CONTROLLERCOMM_STATUS status
from
SCS_L_SCSCONTROLLERSTATUS t4
inner join
(
select
t1.tcdv_id tcdv_id,
max(reg_date) reg_date
from
SCS_L_SCSCONTROLLERSTATUS t2
inner join
(
select distinct
tcdv_id
from
SCS_L_SCSCONTROLLERSTATUS
where
tcdv_id not in('C1010000','C10100000')
) t1
on
t1.tcdv_id=t2.tcdv_id
group by
t1.tcdv_id
) t3
on
t3.tcdv_id=t4.tcdv_id and
t3.reg_date=t4.reg_date
) t6
inner join
(
select
t.tcdv_id tcdv_id, t.int_ip int_ip,
b.gps_coordx gps_coordx, b.gps_coordy gps_coordy
from
bits.tms_m_node b
inner join
tscs.scs_m_intlc#"BITS_TSCS.CORE.TMC.LOCAL" t
on
t.node_id=b.node_id
) t5
on
t5.tcdv_id=t6.tcdv_id;

Related

BigQuery Deduplicate rows - No unique columns

I have a bigquery table which is a result of multiple left join tables.
The results are duplicated because of the left join (cartesian product)
How do I de-duplicate the rows so I only see one record?
SELECT T1.Col1,T1.Col2,........
T2.Col1,T2.Col2,........
T3.Col1,T3.Col3,........
T5.Col1,T5.Col2,........
T7.Col1.......
FROM `TABLE1` as T1
LEFT JOIN
`TABLE`as T2 ON T1.CUSTOMER_CODE = T2.CUSTOMER_CODE
LEFT JOIN
`TABLE3` as T3 ON (T1.MIAL_CODE) = T3.MIAL_CODE
LEFT JOIN
`TABLE5` as T5
ON T1.WORK_CODE = T5.WORK_CODE
LEFT JOIN
`TABLE7` as T7
ON T1.CA_DATE = T7.date
ORDER BY CA_DATE
Use DISTINCT:
select distinct *
from mytable
or create a new table:
create or replace table my_new_table
as
select distinct *
from mytable
I used GROUP BY and it worked fine to get rid of duplicates
SELECT T1.Col1,T1.Col2,........
T2.Col1,T2.Col2,........
T3.Col1,T3.Col3,........
T5.Col1,T5.Col2,........
T7.Col1.......
FROM `TABLE1` as T1
LEFT JOIN
`TABLE`as T2 ON T1.CUSTOMER_CODE = T2.CUSTOMER_CODE
LEFT JOIN
`TABLE3` as T3 ON (T1.MIAL_CODE) = T3.MIAL_CODE
LEFT JOIN
`TABLE5` as T5
ON T1.WORK_CODE = T5.WORK_CODE
LEFT JOIN
`TABLE7` as T7
ON T1.CA_DATE = T7.date
GROUP BY T1.Col1,T1.Col2,........
T2.Col1,T2.Col2,........
T3.Col1,T3.Col3,........
T5.Col1,T5.Col2,........
T7.Col1.......
ORDER BY CA_DATE

Achieve same results without common table expression

My SQL code produce per_month, per_month min, per_month max and per_month standard deviation. But i have done it with CTE. now i want to do without CTE.
;WITH QTY_T AS(
SELECT
YEAR(SHIP_DATE) [Year],
MONTH(SHIP_DATE) [Month],
T1.PLANT AS PLANTS,
WC AS W_C,
T2.SHIP_TO AS SHIP_TO,
T1.PARTS AS PARTS,
SUM([QTY_MII]) AS [QTY_MONTH]
FROM TABLE1 T1
INNER JOIN
TABLE2 T2
ON
T2.OBD = T1.OBD
INNER JOIN
TABLE3 T3
ON T1.OBD=T3.OBD AND T1.ITEM = T3.ITEM AND T1.PLANT = T3.PLANT
INNER JOIN
TABLE4 T4
ON T3.SHIP_LBL = T4.HU_CODE AND T4.STATUS ='SHIPPED'AND T4.PLANT = T3.PLANT
GROUP BY
T1.PLANT,WC,SHIP_TO,T1.PARTS,YEAR(SHIP_DATE),MONTH(SHIP_DATE)
) SELECT
PLANTS,W_C,PARTS,SHIP_TO,
ROUND(AVG(QTY_MONTH),3) AS QTY_SHIPPED_PER_MONTH,
ROUND(MIN(QTY_MONTH),3) AS QTY_SHIPPED_PER_MONTH_MIN,
ROUND(MAX(QTY_MONTH),3) AS QTY_SHIPPED_PER_MONTH_MAX,
ROUND(AVG(QTY_MONTH),3) AS QTY_SHIPPED_PER_MONTH_AVG,
ROUND(STDEV(QTY_MONTH),3) AS QTY_SHIPPED_PER_MONTH_STD
FROM QTY_T
GROUP BY PLANTS,W_C,QTY_T.SHIP_TO,QTY_T.PARTS
you can use subquery without cte
SELECT
PLANTS,W_C,PARTS,SHIP_TO,
ROUND(AVG(QTY_MONTH),3) AS QTY_SHIPPED_PER_MONTH,
ROUND(MIN(QTY_MONTH),3) AS QTY_SHIPPED_PER_MONTH_MIN,
ROUND(MAX(QTY_MONTH),3) AS QTY_SHIPPED_PER_MONTH_MAX,
ROUND(AVG(QTY_MONTH),3) AS QTY_SHIPPED_PER_MONTH_AVG,
ROUND(STDEV(QTY_MONTH),3) AS QTY_SHIPPED_PER_MONTH_STD
FROM (
SELECT
YEAR(SHIP_DATE) [Year],
MONTH(SHIP_DATE) [Month],
T1.PLANT AS PLANTS,
WC AS W_C,
T2.SHIP_TO AS SHIP_TO,
T1.PARTS AS PARTS,
SUM([QTY_MII]) AS [QTY_MONTH]
FROM TABLE1 T1
INNER JOIN TABLE2 T2 ON T2.OBD = T1.OBD
INNER JOINTABLE3 T3 ON T1.OBD=T3.OBD AND T1.ITEM = T3.ITEM AND T1.PLANT = T3.PLANT
INNER JOIN TABLE4 T4 ON T3.SHIP_LBL = T4.HU_CODE AND T4.STATUS ='SHIPPED'AND T4.PLANT = T3.PLANT
GROUP BY
PLANT,WC,SHIP_TO,T1.PARTS,YEAR(SHIP_DATE),MONTH(SHIP_DATE)
) as QTY_T
GROUP BY PLANTS,W_C,QTY_T.SHIP_TO,QTY_T.PARTS

sql union displays duplicates

image of what I want
I have tried the join on x or y and it didn't work, even the group by didn't work.
What almost gave me the result is the query below
SELECT A.Id ,A.AccNo ,A.Name ,B.Id ,B.AccNo1 ,B.AccNo2 ,B.Name
from Table1 as A
left outer join Table2 as B on A.AccNo = B.AccNo1
union
SELECT A.Id ,A.AccNo ,A.Name ,B.Id, B.AccNo1, B.AccNo2, B.Name,
from Table1 as A
left outer join Table2 as B on A.AccNo = B.AccNo2
After getting the query correct I want to show only the exceptions where there was no link between the tables and its kind of difficult if the T1.ID is repeated
You seem to want a left join:
select t1.*, t2.*
from table1 t1 left join
table2 t2
on t1.id in (t2.accno1, t2.accno2);
Try:
SELECT A.Id ,A.AccNo ,A.Name ,B.Id ,B.AccNo1 ,B.AccNo2 ,B.Name
from Table1 as A
left outer join Table2 as B
ON A.AccNo = (CASE WHEN A.AccNo = B.AccNo1 THEN B.AccNo1 ELSE B.AccNo2 END)
You may nest your original query, and then use max aggregate function with grouping :
SELECT Id ,AccNo ,Name, max(Id2) as Id2, max(Name2) as Name2,
max(AccNo1) as AccNo1, max(AccNo2) as AccNo2
FROM
(
SELECT A.Id ,A.AccNo ,A.Name ,B.Id Id2 ,B.AccNo1 ,B.AccNo2 ,B.Name Name2
from Table1 as A
left outer join Table2 as B on A.AccNo = B.AccNo1
union
SELECT A.Id ,A.AccNo ,A.Name ,B.Id Id2, B.AccNo1, B.AccNo2, B.Name Name2
from Table1 as A
left outer join Table2 as B on A.AccNo = B.AccNo2
) q
GROUP BY Id ,AccNo ,Name;
SQL Fiddle Demo
Do a LEFT JOIN to return the table1 values along with matching table2 values (where t2.accno2 = t1.accno):
select t1.*, t2.*
from table1 t1
left join table2 t2
on t1.accno = t2.accno2
Or, perhaps you want table2 values for matching accno1's as well?
select t1.*, t2.*
from table1 t1
left join table2 t2
on t1.accno in (t2.accno1, t2.accno2)
It this way to resolve:
SELECT
t1.id,
t1.accno,
t1.name,
(
SELECT DISTINCT
id
FROM
table2
WHERE
accno2 = t1.accno
),
(
SELECT DISTINCT
name
FROM
table2
WHERE
accno2 = t1.accno
),
(
SELECT DISTINCT
accno1
FROM
table2
WHERE
accno2 = t1.accno
),
(
SELECT DISTINCT
accno2
FROM
table2
WHERE
accno2 = t1.accno
) FROM
table1 t1
LEFT JOIN table2 t2 ON t1.accno = t2.accno1 OR t1.id = t2.id

How to create a temp table in PostgreSQL?

I'm trying to use temp table to simplify my query. At the beginning I used WITH, which was not recognized if I'm not joining each table specifically. What's the best way to approach this query? what's wrong with this syntax?
For the account that purchased the most (in total over their lifetime as a customer) standard_qty paper, how many accounts still had more in total purchases?
create temp table t1 as (
SELECT
a.id as account_id,
SUM(o.standard_qty) as all_std_qty
FROM
accounts a
JOIN orders o ON (a.id = o.account_id)
GROUP BY
1
order by
2 desc
limit
1
)
create temp table t2 as (
SELECT
a.id as account_id,
SUM(o.total) as total_purchases
FROM
accounts a
JOIN orders o ON (a.id = o.account_id)
GROUP BY
1
)
create temp table t3 as (
SELECT
t1.account_id,
t2.total_purchases as total_pur FROM
t1
JOIN t2
ON (t1.account_id = t2.account_id)
)
SELECT
count(a.id) as count_ids
FROM
accounts a
JOIN orders o ON (a.id = o.account_id)
WHERE
o.total > t3.total_pur
I think you missed a join with table t3 and you used it on where clause thats the problem ,can you please try with below query
WITH t1 as (
SELECT
a.id as account_id,
SUM(o.standard_qty) as all_std_qty
FROM
accounts a
JOIN orders o ON (a.id = o.account_id)
GROUP BY
1
order by
2 desc
limit
1
), t2 as (
SELECT
a.id as account_id,
SUM(o.total) as total_purchases
FROM
accounts a
JOIN orders o ON (a.id = o.account_id)
GROUP BY
1
), t3 as (
SELECT
t1.account_id,
t2.total_purchases as total_pur FROM
t1
JOIN t2
ON (t1.account_id = t2.account_id)
)
SELECT
count(a.id) as count_ids
FROM
accounts a
JOIN orders o ON (a.id = o.account_id)
inner join t3 on a.id=t3.account_id
WHERE
o.total > t3.total_pur

SQL Join multiple tables using where clause

I want to write a sql statement:
Select * FROM table1
inner join table2 AS t2
ON
inner join table3 AS t3
ON
inner join table4 AS t4
ON
Where FK_Client_ID = 4
All tables have the client ID in common. So Not sure what to do on the ON.
Will it be something like ON t2.FK_Client_ID = ...... not sure.
So I just want all the data from those tables that has FK_Client_ID in common.
Select *,
(Select FK_Client_ID from table2 where FK_Client_ID = t1.FK_Client_ID )As TID1,
(Select FK_Client_ID from table3 where FK_Client_ID = t1.FK_Client_ID )As TID2,
(Select FK_Client_ID from table4 where FK_Client_ID = t1.FK_Client_ID )As TID3
FROM table1 t1
Select * FROM table1 t1 inner join table2 t2
ON t1.FK_Client_ID = t2.FK_Client_ID
inner join table3 t3
ON t1.FK_Client_ID = t3.FK_Client_ID
inner join table4 t4
ON t1.FK_Client_ID = t4.FK_Client_ID
Where t1.FK_Client_ID = 4
Try this
Select * FROM table1 t1
inner join table2 AS t2
ON t2.FK_Client_ID = t1.FK_Client_ID
inner join table3 AS t3
ON t3.FK_Client_ID = t1.FK_Client_ID
inner join table4 AS t4
ON t4.FK_Client_ID = t1.FK_Client_ID
Where t1.FK_Client_ID = 4
if your foreign field name is "FK_Client_ID" and primary key in table1 is "Client_ID"
Select * FROM table1
inner join table2 AS t2
ON t2.FK_Client_ID = table1.Client_ID
inner join table3 AS t3
ON t3.FK_Client_ID = table1.Client_ID
inner join table4 AS t4
ON t4.FK_Client_ID = table1.Client_ID
Where table1.Client_ID = 4
Since you are using inner join no matter you join them with table1 or any other of them