replace null sql with 0 - sql

Select t.a,sum(s.value) e, sum(r.value) i
CASE when e is NULL then '0' else i end
from table1 t
LEFT JOIN table2 s
ON t.name = s.in
LEFT JOIN table2 r
ON t.name = r.out
group by a
ORDER BY a
My query has error state like 42601, how can i solve this problem to replacing null values with '0'?

You cannot use the ALIAS in the same level it was created. You have to use the aggregated form:
Select t.a,
sum(s.value) e,
sum(r.value) i,
CASE when sum(s.value) is NULL then 0 else sum(r.value) end
from table1 t
LEFT JOIN table2 s ON t.name = s.in
LEFT JOIN table2 r ON t.name = r.out
group by a
ORDER BY a
Otherwise, wrap it in a subquery if you still wanted to use ALIAS
SELECT a, e, i,
CASE when e is NULL then 0 else i end
FROM
(
Select t.a,
sum(s.value) e,
sum(r.value) i
from table1 t
LEFT JOIN table2 s ON t.name = s.in
LEFT JOIN table2 r ON t.name = r.out
group by a
) subquery
ORDER BY a

you can use ISNULL
Select t.a,sum(ISNULL(s.value,0)) e, sum(r.value) i
from table1 t
LEFT JOIN table2 s
ON t.name = s.in
LEFT JOIN table2 r
ON t.name = r.out
group by a
ORDER BY a

Related

Join to only work if a record present in SQL

I have 2 tables - table1 and table2.
In table2, there are multiple records matching criteria for table1 based on:
c_type, h_level, loop, e_id
I want records from RIGHT table ONLY if there is EXACT 1 match. If not, element_nm should be NULL, so I have the exact same records in the output as left table.
SELECT a.*,
b.element_nm
FROM table1 a
LEFT JOIN table2 b ON
a.c_type = b.c_type
AND a.h_level = b.h_level
AND a.loop = b.loop
AND a.e_id = b.e_id
ORDER BY a.file_name,
a.line_num asc;
As this is about one value only, you can use a Sub query in the select clause. Otherwise you'd use a subquery in a LEFT OUTER JOIN or use OUTER APPLY.
SELECT
t1.*,
(
SELECT MIN(t2.element_nm)
FROM table2 t2
WHERE t2.c_type = t1.c_type
AND t2.h_level = t1.h_level
AND t2.loop = t1.loop
AND t2.e_id = t1.e_id
HAVING COUNT(*) = 1
) AS element_nm
FROM table1 t1
ORDER BY t1.file_name, t1.line_num;
Thorsten's answer works when you want only one column from the second table. But if you want multiple columns, it is a bit cumbersome.
Alternatively:
SELECT a.*, b.*
FROM table1 a LEFT JOIN
(SELECT b.*,
COUNT(*) OVER (PARTITION BY b.c_type, b.h_level, b.loop, b.e_id) as cnt
FROM b
) b
ON a.c_type = b.c_type AND
a.h_level = b.h_level AND
a.loop = b.loop AND
a.e_id = b.e_id AND
b.cnt = 1
ORDER BY a.file_name, a.line_num asc;
you should use Row_Number , like this :
WITH cte AS (
SELECT ROW_NUMBER() OVER(PARTITION BY a.c_type ,a.h_level,a.loop ,a.e_id ) rnum
,a.c_type ,a.h_level,a.loop ,a.e_id FROM table1 a
LEFT JOIN table2 b ON a.c_type = b.c_type AND a.h_level = b.h_level AND a.loop = b.loop AND a.e_id = b.e_id
)
,cte2 AS (SELECT * FROM cte WHERE rnum = 2)
SELECT a.*,
CASE WHEN cte2.element_nm IS NULL then b.element_nm ELSE NULL END element_nm
FROM table1 a
LEFT JOIN table2 b ON a.c_type = b.c_type AND a.h_level = b.h_level AND a.loop = b.loop AND a.e_id = b.e_id
LEFT JOIN cte2 ON a.c_type = cte2.c_type AND a.h_level = cte2.h_level AND a.loop = cte2.loop AND a.e_id = cte2.e_id

How GROUP BY with two inner join in postgresql

I am new in SQL, I have a basic question. I have 3 tables and 2 inner joins.
This is my SQL code:
select c.name, case when e.time > NOW() then 'Yes' else 'No' end AS "time"
from table1 as e
INNER JOIN table2 as a on e.id = a.id
INNER JOIN table3 as c on a.id = c.id
where e.conty= 'SAD'
GROUP BY c.name;
Error: must appear in the GROUP BY clause or be used in an aggregate function
Any field you want to retrieve must be contained in the GROUP BY clause of that query. You should include "time" in the GROUP BY clause or exclude it from the query.
select c.name, case when e.time > NOW() then 'Yes' else 'No' end AS "time"
from table1 as e
INNER JOIN table2 as a on e.id = a.id
INNER JOIN table3 as c on a.id = c.id
where e.name = 'JOHN'
GROUP BY c.name, time;
Use the e.time attribute in the GROUP BY clause.
select c.name, case when e.time > NOW() then 'Yes' else 'No' end AS "time"
from table1 as e
INNER JOIN table2 as a on e.id = a.id
INNER JOIN table3 as c on a.id = c.id
where e.name = 'JOHN'
GROUP BY c.name, e.time;
I am guessing that you want aggregation -- and the maximum time:
select c.name,
(case when max(e.time) > NOW() then 'Yes' else 'No' end) AS "time"
from table1 e join
table2 a
on e.id = a.id join
table3 c
on a.id = c.id
where e.name = 'JOHN'
group by c.name;
You don't need to group by name since you select only the rows with name = 'JOHN'.
If you want to select the row with the maximum time, do this:
select
'JOHN' name,
case when max(e.time) > NOW() then 'Yes' else 'No' end AS "time"
from table1 as e
INNER JOIN table2 as a on e.id = a.id
INNER JOIN table3 as c on a.id = c.id
where e.name = 'JOHN'

Alternative for union in Oracle

Is there any alternate way to fetch the following data without using union?
select A.name,A.age,B.Address,C.phoneNo from table1 A,Table2 B,Table3 C where a.pkId = b.FkId and b.pkId = c.FkId
union
select A.name,A.age,B.Address,C.phoneNo from table4 A,Table5 B,Table3 C where a.pkId = b.FkId and b.pkId = c.FkId
I am using this in Hibernate and unfortunately hibernate doesnt support Union. I was just wondering if there is any other way to achieve it else ill have to write it in a procedure and save the data in temp table and fire a sql to read data from that temp table
There is an alternative for union, but it is not pretty:
select distinct coalesce(x1.name, x2.name) as name,
coalesce(x1.age, x2.age) as age,
coalesce(x1.Address, x2.Address) as age,
coalesce(x1.phoneNo, x2.phoneNo) as age,
from (select A.name, A.age, B.Address, C.phoneNo
from table1 A join
Table2 B
on a.pkId = b.FkId join
Table3 C
on b.pkId = c.FkId
) x1 full outer join
(select A.name, A.age, B.Address, C.phoneNo
from table4 A join
Table5 B
on a.pkId = b.FkId join
Table3 C
on b.pkId = c.FkId
) x2
on 1 = 0; -- always false
I can't imagine why you would want to express a union like this. I would highly recommend, though, that you start using proper, explicit join syntax.
is this working ?
SELECT
CASE DISTINCT_FLG WHEN 1 THEN nameA ELSE nameB END name,
CASE DISTINCT_FLG WHEN 1 THEN ageA ELSE ageB END age,
CASE DISTINCT_FLG WHEN 1 THEN AddressA ELSE AddressB END Address,
CASE DISTINCT_FLG WHEN 1 THEN phoneNoA ELSE phoneNoB END phoneNo
FROM (
SELECT
T1.name AS nameA, T1.age AS ageA, T2.Address AS AddressA, T3.phoneNo AS phoneNoA,
T4.name AS nameB, T4.age AS ageB, T5.Address AS AddressB, T3.phoneNo AS phoneNoB,
ROW_NUMBER() OVER(PARTITION BY T1.name, T1.age, T2.Address, T4.name, T4.age, T5.Address, T3.phoneNo ORDER BY NULL) AS DISTINCT_FLG
FROM
table1 T1,
table2 T2,
table4 T4,
table5 T5,
table3 T3
WHERE
T1.pkId = T2.FkId AND
T4.pkId = T5.FkId AND
(
T2.pkId = T3.FkId OR
T5.pkId = T3.FkId
)
) WHERE DISTINCT_FLG IN (1, 2)
Those two UNION parts have Table3 C in common, so we can join the rest to it. To emulate UNION records from the source table can be replicated through unconditional cross join of an auxiliary table with the required number or rows:
Select Distinct
CASE R.r WHEN 1 THEN A1.name ELSE A2.name END As name ,
CASE R.r WHEN 1 THEN A1.age ELSE A2.age END As age ,
CASE R.r WHEN 1 THEN B1.Address ELSE B2.Address END As Address,
C.phoneNo
From Table3 C, --< Start at common Table3
(Select Rownum r From USER_TABLES Where Rownum < 3) R --< Two rows to replicate Table3 C
-- Any table with more than one row will do
-- USER_TABLES should have enough rows in this particular case
Left Join Table2 B1 On R.r = 1 AND B1.pkId = C.FkId --< Left Join branch one
Left Join table1 A1 On R.r = 1 AND A1.pkId = B1.FkId
Left Join Table5 B2 On R.r = 2 AND B2.pkId = C.FkId --< Left Join branch two
Left Join table4 A2 On R.r = 2 AND A2.pkId = B2.FkId
Where (R.r = 1 AND A1.pkId Is NOT NULL) --/ Make sure we have values
OR (R.r = 2 AND A2.pkId Is NOT NULL) --\ for the branch
But really, consider a view.

SQL Select MAX w/ Left Join

SELECT a.first, a.last, b.number, c.last_login
FROM Table1 a
LEFT JOIN Table2 b ON a.first = b.first AND a.last = b.last
LEFT JOIN Table3 c ON b.number = c.number
Sometimes their are multiple entries for the number and I need to only select the max number from table b, however, when i try
SELECT a.first, a.last, MAX(b.number), c.last_login
I get an error. What can I do to select the maximum b.number?
EDIT : Many years I haven't done SQLServer, but maybe the Analytic function can do the trick
SELECT v.first, v.last, c.number, c.last_login
FROM (
SELECT a.first, a.last, LAST_VALUE(b.number) OVER (PARTITION BY a.first, a.last ORDER BY a.first, a.last, b.number) AS yourmax
FROM Table1 a
LEFT JOIN Table2 b ON a.first = b.first AND a.last = b.last
GROUP BY a.first, a.last
) v
INNER JOIN Table3 c ON v.yourmax = c.number
Can you confirm this? I haven't sqlserver, but on oracle the MAX() OVER (...) exists.
LAST_VALUE isn't the only keyword applying to OVER analitics
If you want the max number you can use
SELECT a.first, a.last, b.MaxNumber, c.last_login
FROM Table1 a
LEFT JOIN (select tmp.first, tmp.last, max(tmp.number) MaxNumber from Table2 tmp
group by tmp.first, tmp.last
) b
ON a.first = b.first AND a.last = b.last
LEFT JOIN Table3 c ON b.MaxNumber = c.number

how to update table from select statement in sql server

How can i update a table from select statement results. Here is my select statement:
SELECT count(distinct r.[ID])as Total
FROM Table1 r left join
Tabel2 a
on r.ID = a.ID
where a.Status is null
and here is like what i want to do and i know it is wrong:
update MyTable
set mycol = total
from
(
SELECT count(distinct r.[ID])as Total
FROM Table1 r left join
Tabel2 a
on r.ID = a.ID
where a.Status is null)
Use the subquery in the set:
update MyTable
set mycol = (
SELECT count(distinct r.[ID])
FROM Table1 r left join
Tabel2 a
on r.ID = a.ID
where a.Status is null
)
All you have to do is make a couple tiny changes. Below is the code you will need to use:
update MyTable
set mycol = (SELECT count(distinct r.[ID])as Total
FROM Table1 r left join
Tabel2 a
on r.ID = a.ID
where a.Status is null)
In the case that, as I assume, you have multiple rows in both tables and you want to update the first table row-by-row with related results from your subquery, you'll want to add a join (assuming that both datasets will have what i am calling 'identifyingfield' below) :
Update MyTable
set mycol = b.total
from
MyTable a
inner join
(
SELECT identifyingfield, count(distinct r.[ID])
FROM Table1 r left join
Tabel2 a
on r.ID = a.ID
where a.Status is null
group by identifyingfield
) b
ON a.identifyingfield = b.identifyingfield
you may try something like this:
with "sums"
as
(
select
F."id"
, "sum" = sum( F."value" ) over ( partition by F."id" )
from
"foo" F
)
update
B
set
B."totals" = S."sum"
from
"bar" B
inner join "sums" S
on S."id" = B."id";
see sql-fiddle here