If statement inside an sql query - sql

I have a function in PL/SQL which has two SQL statements in it, they are as follows :
select access_level into v_access from users where user_ID = APEX_UTIL.get_username( APEX_UTIL.get_current_user_id );
and
for row in ( select distinct sub_category_lu
, f_get_display('SUB_CATEGORY', sub_category_lu) display
from files
where active = 'Y'
and publish_date <= sysdate
and category_lu = p_category
and key in ( 0, snl_key )
order by substr(sub_category_lu,1,2)
) loop
Now based on the output from the first sql statement, I want to add another 'and' condition in the second sql statement which is in the for loop.
like if v_access is 'xyz' then I want to add a condition in the for loop sql statement as
AND sub_category_lu NOT LIKE '03%' AND sub_category_lu NOT LIKE '04%'
else dont add this condition and keep the loop as above. How can I add an if statement according to this in the loop.
Thanks in advance.

Simply adding only:
(...) AND (v_access != 'xyz'
OR (sub_category_lu NOT LIKE '03%' AND sub_category_lu NOT LIKE '04%'))

Related

How to execute two queries based on case condition in sql server

I am trying to execute queries based on condition within single table,here is my query in sql server.
where PATTERN is column name and is set to 0 as default value
I want to display result into only one cell of excel sheet. I linked SQL server and Excel sheet.
IF ([PATTERN] = 1)
BEGIN
SELECT PATTERN,COLOR,SHIFT FROM [DEFECT_RESULTS]
END
ELSE
SELECT MODEL,COLOR FROM [DEFECT_RESULTS]
pattern column is present then still following error is displaying
error: Invalid column name 'PATTERN'.
You need to perform a query in the if condition. SQL doesn't know what table/function you're referring to as it stands.
Something like
if exists (select 1 from DEFECT_RESULTS where PATTERN = 1)
begin
…
Which will do the true side of the if if any row in DEFECT_RESULTS matches. You may need a more specific condition.
declare #pattern int
if (#pattern=1)
begin
SELECT PATTERN,COLOR,SHIFT FROM [DEFECT_RESULTS]
end
else if(#pattern=0)
begin
SELECT MODEL,COLOR FROM [DEFECT_RESULTS]
end
A single query cannot return 2 columns sometimes and 3 columns at other times. Perhaps you just want two queries:
SELECT PATTERN, COLOR, SHIFT
FROM [DEFECT_RESULTS]
WHERE PATTERN = 1;
SELECT MODEL, COLOR
FROM [DEFECT_RESULTS]
WHERE PATTERN = 0;

SQL Select Case Statement With Where Condition In Nested Table

I need to insert a SELECT CASE statement to add a column "acct_profile_ext.acct_pay_terms", to a nested condition table. So the only table that can link with this table is "acct_profile", which is "acct_profile.acct_id = acct_profile_ext.acct_id". Problem is this table "acct_profile" is heavily linked to other table as well, so I need to add this column without effect the result of other table.
So now I've insert the statement at the column part, but I'm not sure how to add the column with WHERE condition inside the SELECT CASE statement.
(SELECT CASE acct_profile_ext.acct_pay_terms
WHEN 'CASH' THEN
(SELECT acct_profile_ext.acct_pay_terms FROM acct_profile_ext, acct_profile
WHERE acct_profile.acct_id = acct_profile_ext.acct_id)
WHEN 'CHEQUE' THEN
(SELECT acct_profile_ext.acct_pay_terms FROM acct_profile_ext, acct_profile
WHERE acct_profile.acct_id = acct_profile_ext.acct_id)
ELSE NULL
END
FROM acct_profile_ext)
Please advise and help. Thanks.

How to use a select statment inside a case statement

I will do a select statement inside my case statement like this:
CASE
WHEN d.dependent_speed_type = 4 THEN (SELECT column FROM tablename)
END
But this is not working.
Can I realize a select statement inside my case statement? If the value 4 is inside my column "d.dependent_speed_type" then it should be select a special value from another table. How is the right syntax?
You have to use something that will always return one value, like SELECT MAX(column) FROM tablename) for example.

Querying the Result set of a Previous Query

I have a query for example Query1 = Select Name from table where some Criteria.
Now this query returns a result set of course, what I want is to query the result set of this query, for example I only want the unique Names from the above query select Distinct(Name) from Query1. I should mention that I know I can just use distinct in Query1 but this is just an example my real scenario is somewhat different, what I want to know is whether it's possible to query the result set of a previous query.
I am using SQL Server 2012.
There are several ways to solve this:
1: create a view from the first query and run the second query on the view.
2: nest both queries, like this:
SELECT DISTINCT [Name]
FROM (
SELECT [Name]
FROM table
WHERE some Criteria
) As InnerQuery
3: use a temporary table to store the resultset of the first query as suggested by wewesthemenace in the comments.
4: use CTE as suggested the thebreiflabb in the other answer to this post.
Personally, I would probably go with the first or second option, depending if you need to use the first query as stand alone as well.
You can use the WITH clause
WITH SomeClients AS (
SELECT
c.ID
FROM Clients c
WHERE c.Name LIKE '%hello%'
)
SELECT DISTINCT
sc.ID
FROM SomeClients sc
You need WITH clause. The Syntax Is-
WITH someName AS(
//Your Db Query
)
SELECT * FROM someName // OR Whatever you want
you can create a table to store the results temporarily and use that table in a new query
DECLARE #resultset table (
ID int identity(1,1) not null
, name nvarchar(100)
)
Select top 50 application_Name
into resultset
from Applications_ASIS

How to count values in a query result

I want to count the values on a query result but within the same query if that makes sense. the origional query is
SELECT CLOSING_FEE+CLOSING_FEE_INT+CLOSING_INS+CLOSING_INT+CLOSING_OTHER as BAL, total_closing_balance
FROM statement
this returns over 4000 rows. I want to check the two add up. is there a way to use the count function in the same query to count the first two values of the select statement?
Or would i have to use something like a temp table and then count?
try this if total_closing_balance is static
SELECT SUM(CLOSING_FEE+CLOSING_FEE_INT+CLOSING_INS+CLOSING_INT+CLOSING_OTHER) as BAL, MAX(total_closing_balance) AS total_closing_balance FROM isql.VW_300_statement WHERE brand = '1'AND DAT = '2013-01-18 00:00:00.00000'AND INS_TYPE =''group by Brand,DAT
use Select sum(CLOSING_FEE+CLOSING_FEE_INT+CLOSING_INS+CLOSING_INT+CLOSING_OTHER )
it will add up all these details.
In order to count the rows of a query result you have to write a query as following:
select count(*)
from (SELECT CLOSING_FEE+CLOSING_FEE_INT+CLOSING_INS+CLOSING_INT+CLOSING_OTHER as BAL, total_closing_balance
FROM isql.VW_300_statement
WHERE brand = '1'
AND DAT = '2013-01-18 00:00:00.00000'
AND INS_TYPE ='')