SQL Select rows based on parameters - sql

so I created a customer table that looks a bit like this
Each customer is either new, recurring, or reactivated, and each customer can only be one type so whatever type it is has a 1 in that field and a 0 in all other fields.
I am created a report where I want to have a parameter called customer type that lets you select new, recurring, reactivated, and all, and it will show you only the customer type that you select.
I am not sure how to do this.
I have the whole query done, I just need some help with the where clause.
The psuedo code would be something like this
Case when #CustomerType = 'NEW'
THEN where cs.brandnewcustomer = 1
Case when #CustomerType = 'RECURRING'
THEN where cs.recurringcustomer= 1
Case when #CustomerType = 'REACTIVATED'
THEN where cs.reactivatedcustomer= 1
else case when #CustomerType = 'All' end
Does anyone have any tips on how to turn this into actual code?

You can form your conditions as:
WHERE (cs.brandnewcustomer = 1 AND #CustomerType = 'NEW')
OR
(cs.recurringcustomer = 1 AND #CustomerType = 'RECURRING')
OR
(cs.reactivatedcustome = 1 AND #CustomerType = 'REACTIVATED')
OR
(#CustomerType = 'ALL')

It would need to be structured like this:
SELECT ... FROM ...
WHERE cs.brandnewcustomer = CASE WHEN #CustomerType = 'NEW' THEN 1 ELSE 0 END
AND cs.recurringcustomer = CASE WHEN #CustomerType = 'RECURRING' THEN 1 ELSE 0 END
AND cs.reactivatedcustomer = CASE WHEN #CustomerType = 'REACTIVATED' THEN 1 ELSE 0 END
OR 1 = CASE WHEN #CustomerType = 'All' THEN 1 ELSE 0 END

Put this CASE statement in the WHERE clause:
select * from customers
where 1 = case #CustomerType
when 'NEW' then brandnewcustomer
when 'RECURRING' then recurringcustomer
when 'REACTIVATED' then reactivatedcustomer
when 'All' then 1
end
In case the parameter does not match any of the 4 values then nothing will be returned.
See the demo.

I think this needs to be normalized
Customer types needs to be in a table with a primary key tb_CustomerType
enter image description here
Customer table needs to have a foreign key to the customer type tb_Customer
enter image description here
you do not have to have a case statement if you have this kind of a table structure

Related

SQL query to fetch the result set with same status

The table 1 is as follows,
ID
FK1_ID
FK2_ID
1
1
1
2
1
2
3
1
3
The table with FK2 is as follows,
ID
Type
Status
1
Type1
True
2
Type2
True
3
Type1
False
The FK2_ID column is the ID column of table 2.
The expected result is, for any FK1_ID(which I have as a list of IDs), need to check all its FK2 entries in the 2nd table of Type1 and status True.
For example:
Here, I want to return YES, if all the Type1 entries are True for the specific FK1_ID. Else NO.
So, for FK1_ID with 1, the FK2 table has 3 records. Of which Type1 has 2 records. I should return YES, if both Type1 records are True, else NO.
I want accomplish this using SQL.
Any help is appreciated?
Looks like you just need to compare a conditional count of Status to the full count, with a CASE for the final result.
SELECT
t1.FK1_ID,
Result = CASE WHEN COUNT(*) = COUNT(CASE WHEN FK2.Status = 'True' THEN 1 END)
THEN 'Yes'
ELSE 'No' END
FROM table1 t1
JOIN FK2 ON FK2.ID = t1.FK2_ID
AND FK2.Type = 'Type1'
GROUP BY
t1.FK1_ID;
A slightly shorter but less understandable version
CASE WHEN COUNT(*) = COUNT(NULLIF(FK2.Status, 'False'))
Alternatively
CASE WHEN COUNT(NULLIF(FK2.Status, 'True')) = 0
I'm not totally following your logic (how these two tables are joined) but sounds like you want to compare a total count with a conditional count so maybe something like
with t as (select type, count(status) as cnt,
sum(case when status ='True' then 1 else 0 end) as truecnt
from FK2
group by type)
select type, case when truecnt > 0 and cnt = truecnt then 'Yes' else 'No' end as MyResult
from t

If SCI.SOURCE="CC" , and SCI.CATEGORY should not be 'not awaiting'. I want to return the records based on this additional condition

SELECT DISTINCT sci.le_id AS le_id,
sci.sub_profile_id AS sub_profile_id,
sci.GROUP_ID AS GROUP_ID,
sci.cdd_id,
sci.crm_id,
sci.grp_crm_id,
sci.source,
sci.category
FROM table_name sci
WHERE sci.crm_id IN (SELECT master.x_ref_id
FROM biz_master master, biz_pr_master pr
WHERE master.x_latest_txn_id = pr.x_txn_id
AND pr.x_leid IS NULL
AND pr.GROUP_ID IS NULL)
I'm thinking of something like CASE:
and sci.category <> case when sci.source = 'CC' then 'not awaiting'
else '???
end
Then add it in the WHERE clause as follows:
WHERE ......
AND (sci.source <> 'CC' -- this condition will be true for all other sci.source
OR sci.category <> 'not awaiting') -- This condition will be checked if sci.source = CC

Issue with case null in sql

SELECT Id
FROM dbo.OWL_AddlDataFields
WHERE CustomerId = #BaseCustomerId
AND AddlDataFieldCategoryId = #AddlDataFieldCategoryId
AND AddlDataFieldGroupId = CASE
WHEN #AddlDataFieldGroupId = 0 THEN NULL
ELSE #AddlDataFieldGroupId
END
AND Name = #DataFieldName
The above query does not returns any result. I think above query has issue with the 'AddlDataFieldCategoryId =' and the null from the case. Can anybody correct the query?
The issue with your CASE expression is that you are comparing a column to a predicate which might be NULL using the equals operator. This won't behave as expected, because NULL comparisons should use IS NULL instead of equals. One possible workaround would be to coalesce AddlDataFieldGroupId to a numeric value. Then, we can be certain that the CASE expression would only be comparing one number to another.
WHERE ... AND
COALESCE(AddlDataFieldGroupId, 0) = CASE WHEN
#AddlDataFieldGroupId = 0
THEN 0 ELSE #AddlDataFieldGroupId END
If your AddlDataFieldGroupId cannot be zero, then you don't need the CASE at all. This will do the job:
SELECT Id
FROM dbo.OWL_AddlDataFields
WHERE CustomerId = #BaseCustomerId
AND AddlDataFieldCategoryId = #AddlDataFieldCategoryId
AND AddlDataFieldGroupId = #AddlDataFieldGroupId
AND Name = #DataFieldName
If AddlDataFieldGroupId can be zero, but cannot be negative, you can do this:
SELECT Id
FROM dbo.OWL_AddlDataFields
WHERE CustomerId = #BaseCustomerId
AND AddlDataFieldCategoryId = #AddlDataFieldCategoryId
AND AddlDataFieldGroupId = CASE WHEN
#AddlDataFieldGroupId = 0 THEN -1 ELSE #AddlDataFieldGroupId END
AND Name = #DataFieldName
Alternatively, this will work regardless of what AddlDataFieldGroupId can be:
SELECT Id
FROM dbo.OWL_AddlDataFields
WHERE CustomerId = #BaseCustomerId
AND AddlDataFieldCategoryId = #AddlDataFieldCategoryId
AND AddlDataFieldGroupId = CASE WHEN
#AddlDataFieldGroupId = 0 THEN AddlDataFieldGroupId - 1 ELSE #AddlDataFieldGroupId END
AND Name = #DataFieldName
Note: all these solutions assume that AddlDataFieldGroupId cannot be NULL.

SQL, Select if or select case

I am not sure what I am trying is achievable or not!!
I am trying to write a SQL query will will do select statement based on user input.
so if user input = 1 then I want it to select from actual table.
if user input = 0 then I want it do select 0 or null from dual. (if this is possible).
so Here is Parameter which will used to get input from user. ?i_userkey:'':null?
if user input's 1 then it will change null to 1.
I want to write a query using this parameter. something like this.
below is the logic.
IF i_userkey = 1 then
select ID,Gender,Age from TableA
If i_userkey = 0 then
select 0 or null from dual.
is this possible?
How about this:
SELECT
CASE WHEN i_userkey = 1 THEN ID ELSE NULL END AS ID
CASE WHEN i_userkey = 1 THEN Gender ELSE NULL END AS Gender
CASE WHEN i_userkey = 1 THEN AGE ELSE NULL END AS Age
FROM TableA
This will at least give you a consistent three-column result set you can work with. Having the query return differing column counts is not going to work.
select ID,Gender,Age
from TableA
where i_userkey = 1
union all
select 0, 0, 0
from dual
where i_userkey = 0
You might have to adjust the datatypes in the dual-select to match TableA

Case sentence using SQL

I am using double case sentence to get a value from column in a table based on 2 conditions that are available in 2 other columns in same table , and else (otherwise) the function should give null when it is null or 0 when it is 0 .
Example of code is below :
CASE CODE
WHEN 'ABC'
CASE NAME WHEN 'XYZ'
THEN 'VALUE'
ELSE NULL
END
ELSE NULL
END
The problem is if I use NULL after else then it gives all ( both null values and values with 0 ) as NULL , or if I use 0 instead of NULL after ELSE then both null and 0 values are given as 0 .
I have tried to write the sentence in many ways but I dont know its not working . Hopefully somebody can give me some good solution regarding this.
CASE WHEN CODE = 'ABC' AND NAME = 'XYZ' THEN 'VALUE' ELSE NULL END
I believe you want:
CASE WHEN CODE = 'ABC' AND NAME = 'XYZ' THEN 'VALUE' ELSE NULL END
This structure of the CASE statement is more flexible than sticking the field name before WHEN.
Are you trying to do something like this?
(CASE when CODE = 'ABC' and NAME = 'XYZ'
THEN 'VALUE'
when code = '0' or name = '0'
then '0'
ELSE NULL
end)
Or perhaps it is this (based on the fact that you are trying to get something from a column):
(CASE when CODE = 'ABC' and NAME = 'XYZ'
THEN value
when value is NULL or value = 0
then value
ELSE NULL
end)
Based on your comment, I think this will work:
(CASE when CODE = 'ABC' and NAME = 'XYZ'
THEN value
ELSE NULL
end)
Or is it this:
(CASE when CODE = 'ABC' and NAME = 'XYZ' and value <> '0'
THEN 'value'
when CODE = 'ABC' and NAME = 'XYZ' and value = '0'
then '0'
ELSE NULL
end)
However, I think this is equivalent to your original nested two-case version.