SQLite. How to merge two cases in just one column, from a SQL Case function? - sql

I'm using SQLite.
I have this query:
SELECT Customer.contract_number,
Customer.name,
Customer.last,
Customer.phone,
Contrato.desc,
CASE Contract.sinc WHEN 1 THEN 'Sincro' ELSE 'NO sincro' END Status1,
CASE Customer.rev WHEN 1 THEN 'Rev' ELSE 'No rev' END Status2
FROM Customer,
Contract
WHERE Customer.contract_number = Contract.contract_number
UNION ALL
SELECT HandCo.contract_number,
HandCo.name,
HandCo.last,
HandCo.phone,
HandCo.des,
CASE HandCon.tradit WHEN 1 THEN 'Tradit' ELSE NULL END Status3
FROM HandContract;
I want to merge the result from Status1 and result from Status2 in just one column.
How I do that? Thank you.
I'm using a UNION for the query and that is the reason why I want to merge the Case Funtion.
Thank you for respond.

You can concatenate strings with || in sqllite.
SELECT Customer.contract_number, Customer.name, Customer.last, Customer.phone, Contract.desc,
CASE Contract.sinc WHEN 1 THEN 'Sincro' ELSE 'No sincro' END
||
CASE Customer.rev WHEN 1 THEN 'Rev' ELSE 'No Rev' END Status
FROM
Customer, Contract;
Similar topic: How to concatenate strings with padding in sqlite

Perhaps with string concatenation?
SELECT cu.contract_number, cu.name, cu.last, cu.phone, co.desc,
((CASE co.sinc WHEN 1 THEN 'Sincro' ELSE 'No sincro' END) || '-' ||
(CASE cu.rev WHEN 1 THEN 'Rev' ELSE 'No Rev' END)
) as Status
FROM Customer cu JOIN
Contract co
ON co.?? = cu.??;
Note: I removed the , from the FROM clause. I really doubt that is the intention of this query. You need to put the proper JOIN condition in the ON clause, replacing the ?? with the appropriate column names.

Related

How to check unique values in SQL

I have a table named Bank that contains a Bank_Values column. I need a calculated Bank_Value_Unique column to shows whether each Bank_Value exists somewhere else in the table (i.e. whether its count is greater than 1).
I prepared this query, but it does not work. Could anyone help me with this and/or modify this query?
SELECT
CASE
WHEN NULLIF(LTRIM(RTRIM(Bank_Value)), '') =
(SELECT Bank_Value
FROM [Bank]
GROUP BY Bank_Value
HAVING COUNT(*) = 1)
THEN '0' ELSE '1'
END AS Bank_Key_Unique
FROM [Bank]
A windowed count should work:
SELECT
*,
CASE
COUNT(*) OVER (PARTITION BY Bank_Value)
WHEN 1 THEN 1 ELSE 0
END AS Bank_Value_Unique
FROM
Bank
;
It works also, but I found solution also:
select CASE WHEN NULLIF(LTRIM(RTRIM(Bank_Value)),'') =
(select Bank_Value
from Bank
group by Bank_Value
having (count(distinct Bank_Value) > 2 )) THEN '1' ELSE '0' END AS
Bank_Value_Uniquness
from Bank
It was missing "distinct" in having part.

How can I use CASE clause twice in SQL Query

I am trying to apply two conditions in one SQL Query.
(select DISTINCT (
CASE WHEN (
ABC.GemUserID = '99' )
OR ABC.GemUserID != '99'
THEN 'Yes'
ELSE 'No'
END)) AS AllWell
This gives me output as "Yes" where as the CASE is true only for 1 file like below :
Current Result:
99 , Yes
99 , Yes
99 , Yes
Expected Result:
99 , No
99 , No
99 , Yes
I am using the below query but the SQL Query Intellisence is identifying it as wrong.
Wrong Query:
(select DISTINCT (
CASE WHEN ( ABC.GEMUserID = '99' THEN 'Yes' else 'No'
CASE WHEN ( ABC.GEMUserID != '99' THEN 'No' else 'Yes'
END)) AS AllWell
After fixing the above Wrong Query:
(select DISTINCT
(CASE WHEN ABC.GemUserID = '99' THEN 'Yes' else 'No' END),
(CASE WHEN ABC.GemUserID != '99' THEN 'No' else 'Yes' END))
AS AllWell
But I am getting error:
Msg 116, Level 16, State 1, Line 17 Only one expression can be
specified in the select list when the subquery is not introduced with
EXISTS.
How to fix this?
select distinct is -- itself -- part of SQL syntax. The distinct is not a function. It should not be followed by parentheses. So, if I understand your question:
select DISTINCT
( CASE WHEN ABC.GEMUserID = '99' THEN 'Yes' else 'No' END),
( CASE WHEN ABC.GEMUserID <> '99' THEN 'No' else 'Yes' END) as AllWell
Do you plan on giving the first column a name?
select DISTINCT
CASE WHEN ABC.GEMUserID = '99' THEN 'Yes'
ELSE 'No' -- This is automatically When ABC.GEMUserID <> '99'
END AS AllWell
According to the error, your query is a subquery (probably behind IN?) in a larger SQL command. Therefore, it is not possible for such subquery to return more than one column.
So your first query, you've said:
CASE WHEN userID = 99 OR userID != 99
In other words:
CASE WHEN 1=1
This is why it returns yes for everything (not sure what the difference between your current and expected result should be considering that the userID is 99 for all rows).
For your erroneous query, seems you're returning that select in the middle of another select (since you alias it at the end). Due to that, you cannot return more than one column in your nested select. You do not need the second CASE statement, simply change your query to:
(select DISTINCT
CASE WHEN ABC.GemUserID = '99' THEN 'Yes' Else 'No' End) AS AllWell
Assuming that you hold the missing pieces to the query such as the FROM.

sql case statement IN with group by

I have a 2 column table with the columns : "user_name" and "characteristic". Each user_name may appear multiple times with a different characteristic.
The values in characteristic are:
Online
Instore
Account
Email
I want to write a sql statement that goes like this - but obviously this isn't working:
SELECT user_name,
case
when characteristic in ("online","instore") then 1
else 0
END as purchase_yn,
case
when characteristic in ("online","instore") and
characteristic in ("email",'account') then 1
else 0
END as purchaser_with_account
FROM my_table
GROUP BY user_name;
Essentially the first is a flag where I check for the presence of either value for that user_name.
The Second field is that they meet this criteria AND that they meet the criteria for having either 'email' or 'account'
An example the structure of your data would help better understand what you are trying to accomplish. But I think I get what you are trying to do.
You have to use an aggregate function in order to use a group by.
Something like SUM or AVG.
But you need first to build a pivot of your data and then you could use that pivot to check for your criterias:
This would create a table pivot that shows for each record what criterias are met:
SELECT
user_name,
case when characteristic = "online" then 1 else 0 end as online_yn,
case when characteristic = "instore" then 1 else 0 end as instore_yn,
case when characteristic = "account" then 1 else 0 end as account_yn,
case when characteristic = "email" then 1 else 0 end as email_yn,
FROM my_table
Now what you might wanted to do is to create an averaged version of these entries grouped by user_name and use those averages to create the fields you wanted. For that you need to use the same statement created earlier as an inline table :
Select
user_name,
case when avg(online_yn + instore_yn) >= 1 then 1 else 0 end as purchase_yn,
case when avg(online_yn + instore_yn) >= 1 and avg(email_yn + account_yn) >= 1 then 1 else 0 end as purchaser_with_account
From
(SELECT
user_name,
case when characteristic = "online" then 1 else 0 end as online_yn,
case when characteristic = "instore" then 1 else 0 end as instore_yn,
case when characteristic = "account" then 1 else 0 end as account_yn,
case when characteristic = "email" then 1 else 0 end as email_yn,
FROM my_table) avg_table
group by
user_name;
This should help.
It may not be efficient in terms of performance but you'll get what you want.
You just have to enclose the CASE expressions in COUNT aggregates:
SELECT user_name,
COUNT(case when characteristic in ("online","instore") then 1 END) as purchase_yn,
COUNT(case when characteristic in ("email",'account') then 1 END) as user_with_account
FROM my_table
GROUP BY user_name
If purchase_yn > 0 then you first flag is set. If purchase_yn > 0 and user_with_account > 0 then you second flag is set as well.
Note: You have to remove ELSE 0 from the CASE expressions because COUNT takes into account all not null values.
You haven't mentioned a specific RDBMS, but if SUM(DISTINCT ...) is available the following is quite nice:
SELECT
username,
SUM(DISTINCT
CASE
WHEN characteristic in ('online','instore') THEN 1
ELSE 0
END) AS purchase_yn,
CASE WHEN (
SUM(DISTINCT
CASE
WHEN characteristic in ('online','instore') THEN 1
WHEN characteristic in ('email','account') THEN 2
ELSE 0 END
)
) = 3 THEN 1 ELSE 0 END as purchaser_with_account
FROM
my_table
GROUP BY
username
If I correctly understand, if user have 'online' or 'instore', then for this user you want 1 as purchase_yn column, and if user also have 'email' or 'account', then 1 as purchaser_with_account column.
If this is correct, then one way is:
with your_table(user_name, characteristic) as(
select 1, 'online' union all
select 1, 'instore' union all
select 1, 'account' union all
select 1, 'email' union all
select 2, 'account' union all
select 2, 'email' union all
select 3, 'online'
)
-- below is actual query:
select your_table.user_name, coalesce(max(t1.purchase_yn), 0) as purchase_yn, coalesce(max(t2.purchaser_with_account), 0) as purchaser_with_account
from your_table
left join (SELECT user_name, 1 as purchase_yn from your_table where characteristic in('online','instore') ) t1
on your_table.user_name = t1.user_name
left join (SELECT user_name, 1 as purchaser_with_account from your_table where characteristic in('email', 'account') ) t2
on t1.user_name = t2.user_name
group by your_table.user_name

Case SQL If there is no record between dates

I'm new to SQL and am struggling with a case.
I would like to make the case where if an account (account_ID) doesn't have a record (ON billing_id) between current_date-302 and current_date-62 THEN MARK WITH A "1"
Query below:
Thanks in advance
SELECT
billing_date_local_time
,account_id
,contract_owner_name
,date_first_feature_partner
,deal_starts_at
,contract_id
,new_partner_type
,sum(voucher_sold) AS Vouchers
,sum(gross_bookings_local) AS GB
,sum(gross_revenue_local) AS GR
,is_G2
,Case when billing_date_local_time between current_date-302 and current_date-62 = 0 THEN 'YES' ELSE 'NO' End
FROM EMEA_ANALYTICS.eu_deal_flat
WHERE
country_id = 206
and billing_date_local_time between current_date-400
and current_date-2
GROUP BY 1,2,3,4,5,6,10,11
You'll need to do a correlated subquery; something like this:
select
a.billing_date_local_time
,a.account_id
,...
, CASE WHEN EXISTS (SELECT * FROM EMEA_ANALYTICS.eu_deal_flat b WHERE a.account_id = b.account_id AND b.billing_date_local_time between current_date-302 and current_date-62 ) THEN 'YES' ELSE 'NO' END
from
FROM EMEA_ANALYTICS.eu_deal_flat a
WHERE ...
You need to apply an aggregate function like this:
min(case when billing_date_local_time
between current_date-302 and current_date-62
then 0
else 1
end)

adding condition when case statement is true in postgresql

I have a query that where i need to put condition when case statement is true.I have tried like this but not geeting the correct value.
SELECT
name,count(CASE WHEN date_part('year',time_stamp) = 2016 THEN answ_count end) AS Year15
FROM
companies companies
where
(CASE when no_answer='f' then value_s IS not NULL or value_n IS not NULL end )
SELECT name,
count(CASE WHEN date_part('year',time_stamp) = 2016 THEN answ_count end) AS Year15
FROM
companies companies
where
CASE when no_answer='f' then value_s ELSE '1' end IS not NULL
OR CASE when no_answer='f' then value_n ELSE '1' end IS not NULL
CASE is an expression, you can only specify a value after the THEN part, not a condition like you did THEN value_s IS NOT NULL
You can't use case like that.
It's hard to figure out what you are trying to do, but test the result of the case, rather than putting the test inside the case, like this:
...
where CASE when no_answer='f' then value_s else value_n end
IS not NULL