Case (or alternative) statement to select which query criteria to use - sql

I want to use a conditional statement to determine which set of query criteria to use. Open to any suggestions. Currently I have
Select p.1, p.2, p.3, Count(p.3) ASDF
from table1 t1
left join table2 t2 on t2.a = t1.a
inner join table3 t3 on t3.b = t1.b
where
case t1.abc
when 'x' then
t2 in ('1111','2222')
else t3.123 in ('asdf','qwer')
and t2.ad = '123'
and t1.bn = '456'
and t3.mk = '678'
group by p.1, p.2, p.3
I'm getting "Incorrect syntax near" the first 'in' and the following 'else'. Is what I am asking possible? If not, what is a good way to do this?

This should work as you want:
SELECT p.1, p.2, p.3, COUNT(p.3) ASDF
FROM table1 t1
LEFT JOIN table2 t2
ON t2.a = t1.a
INNER JOIN table3 t3
ON t3.b = t1.b
WHERE ((t1.abc = 'x' AND t2.somecolumn IN ('1111','2222'))
OR t1.abc <> 'x' AND t3.123 in ('asdf','qwer'))
AND t2.ad = '123'
AND t1.bn = '456'
AND t3.mk = '678'
GROUP BY p.1, p.2, p.3

Select p.1, p.2, p.3, Count(p.3) ASDF
from table1 t1
left join table2 t2 on t2.a = t1.a
inner join table3 t3 on t3.b = t1.b
where (t1.abc = 'x'AND t2 in ('1111','2222')) OR
(t1.abc <> 'x' AND t3.123 in ('asdf','qwer'))
and t2.ad = '123'
and t1.bn = '456'
and t3.mk = '678'
group by p.1, p.2, p.3

Related

If exists sql error combine 2 select statement

When a subquery is not introduced with EXISTS, only one expression can be specified in the selection list.error. Not sure how to do the if exist statement.
SELECT DISTINCT
t1.id,t1.scn,t1.vsl_name,case when t1.id = t1.id THEN 'IMPORT' ELSE '0' END AS import_export,
dbo.fn_format_datetime(t1.act_arr_dt_tm) AS act_arr_dt_tm,
dbo.fn_format_datetime(t1.act_dept_dt_tm) AS act_dept_dt_tm,
( SELECT t1.scn, COUNT(t1.id) AS total_count_bl_status_c
FROM ccosbl t1
INNER JOIN vesvoy t2 ON t2.scn= t1.scn
WHERE t1.status_ind= 'C'
GROUP BY t1.scn)
FROM vesvoy t1
INNER JOIN ccosbl t2 ON t2.scn = t1.scn
WHERE t2.status_ind = 'C'
GROUP BY t1.scn,t1.vsl_name,t1.act_arr_dt_tm,t1.act_dept_dt_tm,t1.id
I need to combine both select statement as one. Is there any other ways to do that in sql?
If you insist on using a correlated subquery then you need to make sure that only one column is included into the select:
SELECT DISTINCT
t1.id,t1.scn,t1.vsl_name,case when t1.id = t1.id THEN 'IMPORT' ELSE '0' END AS import_export,
dbo.fn_format_datetime(t1.act_arr_dt_tm) AS act_arr_dt_tm,
dbo.fn_format_datetime(t1.act_dept_dt_tm) AS act_dept_dt_tm,
( SELECT COUNT(t4.id) AS total_count_bl_status_c
FROM ccosbl t4
INNER JOIN vesvoy t5 ON t4.scn= t5.scn
WHERE t1.status_ind= 'C' and t4.scn = t2.scn)
FROM vesvoy t1
INNER JOIN ccosbl t2 ON t2.scn = t1.scn
WHERE t2.status_ind = 'C'
GROUP BY t1.scn,t1.vsl_name,t1.act_arr_dt_tm,t1.act_dept_dt_tm,t1.id
However, the better approach will be to use a subquery with the join:
SELECT DISTINCT
t1.id,t1.scn,t1.vsl_name,case when t1.id = t1.id THEN 'IMPORT' ELSE '0' END AS import_export,
dbo.fn_format_datetime(t1.act_arr_dt_tm) AS act_arr_dt_tm,
dbo.fn_format_datetime(t1.act_dept_dt_tm) AS act_dept_dt_tm,
t3.total_count_bl_status_c
FROM vesvoy t1
INNER JOIN ccosbl t2 ON t2.scn = t1.scn
LEFT JOIN
( SELECT t4.scn, COUNT(t4.id) AS total_count_bl_status_c
FROM ccosbl t4
INNER JOIN vesvoy t5 ON t4.scn= t5.scn
WHERE t4.status_ind= 'C'
GROUP BY t4.scn) t3
ON t2.scn = t3.scn
WHERE t2.status_ind = 'C'
GROUP BY t1.scn,t1.vsl_name,t1.act_arr_dt_tm,t1.act_dept_dt_tm,t1.id
Try this:
SELECT t3.id t3.scn,total_count_bl_status_c,act_arr_dt_tm,act_dept_dt_tm,vsl_name,case when t3.id = t3.id THEN 'IMPORT' ELSE '0' END AS import_export,
dbo.fn_format_datetime(t3.act_arr_dt_tm) AS act_arr_dt_tm,
dbo.fn_format_datetime(t3.act_dept_dt_tm) AS act_dept_dt_tm from
(SELECT t2.scn, COUNT(t2.id) AS total_count_bl_status_c
FROM ccosbl t1
INNER JOIN
vesvoy t2
ON t2.scn= t1.scn
WHERE t1.status_ind= 'C'
GROUP BY t1.scn)as t3
inner join
(SELECT * from ccosbl) as t4 on
ON t4.scn = t3.scn

SQL - Joining multiple selects/tables

My first query...
SELECT
t1.a, t1.b, t1.c,
t2.a, t2.b, t2.c
FROM t1
LEFT JOIN t2 ON t1.a = t2.a
WHERE t1.b = '000000'
AND LENGTH(t1.a) > '5'
AND t1.c <> 'Y';
My second query...
SELECT
t1.a,
t3.b as testMe
FROM t1
LEFT JOIN t3 ON t1.a = '0' + LEFT(t3.a, 5)
WHERE t1.a = '017941';
Both of these queries work fine by themselves, but I need them combined into one result set. Worth noting is that the where clause in the second query is there for testing purposes, but when I remove it the whole thing crashes. Not sure if that means I need something to filter by, or it's timing out? The database I'm using is Pervasive.
My failed query...
SELECT
t1.a, t1.b, t1.c,
t2.a, t2.b, t2.c,
t3.b as testMe
FROM t1
LEFT JOIN t2 ON t1.a = t2.a
LEFT JOIN t3 ON t1.a = '0' + LEFT(t3.a, 5)
WHERE t1.b = '000000'
AND LENGTH(t1.a) > '5'
AND t1.c <> 'Y';
I've shorted my code to better show what I have going on, but the actual code can be found here: http://codeshare.io/A2aB9
You can try to use those two sets as subselect statements and join them together. I pulled your join condition between the two queries out as a separate column in order to keep testMe in the result set.
SELECT *
FROM (SELECT t1.a t1_a
, t1.b t1_b
, t1.c t1_c
, t2.a t2_a
, t2.b t2_b
, t2.c t2_c
FROM t1
LEFT JOIN t2
ON t1.a = t2.a
WHERE t1.b = '000000'
AND LENGTH(t1.a) > '5'
AND t1.c <> 'Y') a
LEFT JOIN (SELECT t1.a t1_a
, t1.b t1_b
, t1.c t1_c
, t3.a t3_a
, t3.b t3_b
, '0' + LEFT(t3.c, 5) t3_c
, LEFT(t3.c, 5) AS testMe
FROM t1
LEFT JOIN t3
ON t1.a = '0' + LEFT(t3.c, 5)) b
ON a.t1_a = c.t3_c;

Oracle SQL correlated update

I got three tables:
t1.columns: a,c
t2.columns: a,b
t3.columns: b,c,d
Now what I want is to update t1.c with t3.d. But I can't just update t1 from t3 using t1.c = t3.c I also have to go though t3.b = t2.b and t1.a = t2.a.
I've tried something like this:
UPDATE table1 t1
SET t1.c = (select t3.d
from table2 t2, table3 t3
where t2.b = t3.b and t1.a = t2.a)
WHERE EXISTS ( SELECT 1 FROM table2 t2, table3 t3 WHERE t1.c = t3.c and t1.a = t2.a);
This code generates error-msg: ORA-01427: single-row subquery returns more than one row
If there is a one-to-many relationship between t1 and t2 or between t2 and t3 you will get many matches for each row in t1. If you know that all rows in t3 that belong to the same row in t1 have the same value in d, then you can use DISTINCT to remove (identical) duplicates.
UPDATE table1 t1
SET t1.c = (select DISTINCT t3.d
from table2 t2, table3 t3
where t2.b = t3.b and t1.a = t2.a)
WHERE EXISTS ( SELECT 1 FROM table2 t2, table3 t3 WHERE t1.c = t3.c and t1.a = t2.a);
You have a subquery that is returning more than one row. Use rownum to get just one row:
UPDATE table1 t1
SET t1.c = (select d
from (select t3.d
from table2 t2 join table3 t3
on t2.b = t3.b
where t1.a = t2.a
) t
where rownum = 1
)
WHERE EXISTS ( SELECT 1 FROM table2 t2, table3 t3 WHERE t1.c = t3.c and t1.a = t2.a);
Sorry for the confusion but I solved it:
UPDATE table t1
SET t1.c = (select t3.d from table3 t3, table2 t2
where t1.a = t2.a and t2.b = t3.b and t3.c = t1.c)
WHERE EXISTS ( SELECT 1 FROM table1 t1, table2 t2 WHERE t1.a = t2.a and t2.b = t3.b and t3.c = t1.c)
UPDATE table1 t1
SET t1.c = (select MAX(t3.d)
from table2 t2, table3 t3
where t2.b = t3.b and t1.a = t2.a)
WHERE EXISTS ( SELECT 1 FROM table2 t2, table3 t3 WHERE t1.c = t3.c and t1.a = t2.a);

multiple condition in join

how can i use two or more condition on join?
i want to replace this query with join version of it:
select *
from t1,t2
where t1.a=t2.b and t1.c=t2.d and t1.e=t2.f
how could it be?
This should work
select *
from t1
join t2
on t1.a = t2.b and t1.c = t2.d and t1.e = t2.f

Sql Server : How to use an aggregate function like MAX in a WHERE clause

I want get the maximum value for this record. Please help me:
SELECT rest.field1
FROM mastertable AS m
INNER JOIN (
SELECT t1.field1 field1,
t2.field2
FROM table1 AS T1
INNER JOIN table2 AS t2 ON t2.field = t1.field
WHERE t1.field3=MAX(t1.field3)
-- ^^^^^^^^^^^^^^ Help me here.
) AS rest ON rest.field1 = m.field
As you've noticed, the WHERE clause doesn't allow you to use aggregates in it. That's what the HAVING clause is for.
HAVING t1.field3=MAX(t1.field3)
You could use a sub query...
WHERE t1.field3 = (SELECT MAX(st1.field3) FROM table1 AS st1)
But I would actually move this out of the where clause and into the join statement, as an AND for the ON clause.
The correct way to use max in the having clause is by performing a self join first:
select t1.a, t1.b, t1.c
from table1 t1
join table1 t1_max
on t1.id = t1_max.id
group by t1.a, t1.b, t1.c
having t1.date = max(t1_max.date)
The following is how you would join with a subquery:
select t1.a, t1.b, t1.c
from table1 t1
where t1.date = (select max(t1_max.date)
from table1 t1_max
where t1.id = t1_max.id)
Be sure to create a single dataset before using an aggregate when dealing with a multi-table join:
select t1.id, t1.date, t1.a, t1.b, t1.c
into #dataset
from table1 t1
join table2 t2
on t1.id = t2.id
join table2 t3
on t1.id = t3.id
select a, b, c
from #dataset d
join #dataset d_max
on d.id = d_max.id
having d.date = max(d_max.date)
group by a, b, c
Sub query version:
select t1.id, t1.date, t1.a, t1.b, t1.c
into #dataset
from table1 t1
join table2 t2
on t1.id = t2.id
join table2 t3
on t1.id = t3.id
select a, b, c
from #dataset d
where d.date = (select max(d_max.date)
from #dataset d_max
where d.id = d_max.id)
SELECT rest.field1
FROM mastertable as m
INNER JOIN table1 at t1 on t1.field1 = m.field
INNER JOIN table2 at t2 on t2.field = t1.field
WHERE t1.field3 = (SELECT MAX(field3) FROM table1)
yes you need to use a having clause after the Group by clause ,
as the where is just to filter the data on simple parameters ,
but group by followed by a Having statement is the idea to group the data and filter it on basis of some aggregate function......
But its still giving an error message in Query Builder. I am using SqlServerCe 2008.
SELECT Products_Master.ProductName, Order_Products.Quantity, Order_Details.TotalTax, Order_Products.Cost, Order_Details.Discount,
Order_Details.TotalPrice
FROM Order_Products INNER JOIN
Order_Details ON Order_Details.OrderID = Order_Products.OrderID INNER JOIN
Products_Master ON Products_Master.ProductCode = Order_Products.ProductCode
HAVING (Order_Details.OrderID = (SELECT MAX(OrderID) AS Expr1 FROM Order_Details AS mx1))
I replaced WHERE with HAVING as said by #powerlord. But still showing an error.
Error parsing the query. [Token line number = 1, Token line offset = 371, Token in error = SELECT]