SQL stored procedure with CASE - sql

Hello i have a quation with my stored procedure , i use 2 cases here , the first case it shows me right values and its OK, the second shows me only Null values in the field TirType , i don't understand what is the problem
CREATE VIEW dbo.YUITY
SELECT CAST(dbo.SC5116.CODE AS int) AS код, dbo.SC5116.DESCR AS Наименование,
CAST(dbo.SC3420.CODE AS int) AS TIR, dbo.SC3420.SP4947 AS Date,
CASE WHEN SC3420.SP4949 <> ' 0 ' THEN 'ПовышСтрах' ELSE 'ОснСтрах' END AS VID,
CASE WHEN dbo.SC3420.SP9214 = '714' THEN '4v' END AS TirType
FROM dbo.SC3420 INNER JOIN
dbo.SC5116 ON dbo.SC3420.SP3422 = dbo.SC5116.ID
WHERE (dbo.SC3420.SP4947 <> '01.01.1753')

Don't forget END after CASE. It will return NULL if no ELSE is specified and value is not '714'.
CREATE VIEW dbo.YUITY
AS
SELECT CAST(dbo.SC5116.CODE AS int) AS код, dbo.SC5116.DESCR AS Наименование,
CAST(dbo.SC3420.CODE AS int) AS TIR, dbo.SC3420.SP4947 AS Date,
CASE WHEN SC3420.SP4949 <> ' 0 ' THEN 'ПовышСтрах' ELSE 'ОснСтрах' END AS VID,
CASE WHEN dbo.SC3420.SP9214 = '714' THEN '4v' ELSE '' END AS TirType
FROM dbo.SC3420 INNER JOIN
dbo.SC5116 ON dbo.SC3420.SP3422 = dbo.SC5116.ID
WHERE (dbo.SC3420.SP4947 <> '01.01.1753')

Related

join two cases to create a new field

I have a table with fields man_phone and head_phone, and in the selects there are such cases for obtaining them:
case
when p.dep_code <> 'MM' then
case
when p.man_id = '%%%%%%%' then
'Man Spec-100'
when m.man_l is not null then
m.man_phone
else
case
when m2.man_l is not null then
m2.man_phone
else
m3.man_phone
end
end
else
o.cont_phone
end as man_phone,
case
when p.dep_code <> 'MM' then
case
when p.man_id = '%%%%%%%' then
'Man Spec-100'
when m2.man_l is not null then
m2.man_phone
else
m3.man_phone
end
else
o.head_phone
end as head_phone
I want to add a new field called head_login with a case if man_phone = head_phone, then it will output man_l as head_login and I tried to push these two existing cases into only one case, what would be the right way to do this?
case
case
when p.dep_code <> 'MM' then
case
when p.man_id = '%%%%%%%' then
'Man Spec-100'
when m.man_l is not null then
m.man_phone
else
case
when m2.man_l is not null then
m2.man_phone
else
m3.man_phone
end
end
else
o.cont_phone
end as man_phone,
case
when p.dep_code <> 'MM' then
case
when p.man_id = '%%%%%%%' then
'Man Spec-100'
when m2.man_l is not null then
m2.man_phone
else
m3.man_phone
end
else
o.head_phone
end as head_phone
when p.department_code <> 'MM' then
case
when man_phone = head_phone
m2.man_l
else
m3.man_l
end
end as head_login
I want to add a new field called head_login with a case if man_phone = head_phone, then it will output man_l as head_login and I tried to push these two existing cases into only one case.
Do not try to merge the two cases into a third case. Evaluate them in a sub-query and then put the CASE which compares them into an outer query:
SELECT man_phone,
head_phone,
CASE
WHEN man_phone = head_phone
THEN man_l2
ELSE man_l3
END AS head_login
FROM (
SELECT CASE <your case> END AS man_phone,
CASE <your case> END AS head_phone,
m2.man_l AS man_l2,
m3.man_l AS man_l3
FROM table_name
)

Declare variable PostgreSQL

I'm trying to declare a variable which I will pass a value, but even with a single example, I got an different errors. For example: unexpected end of function definition at end of input.
I assume that the reason is that I am passing the value to a temporary table, but I don't know how to fix it
DO $$
DECLARE var2 varchar(20) := 'dzien';
BEGIN
SELECT
CASE WHEN var2 = 'dzien' THEN to_date((to_char(czasRozmowy, 'YYYY/MM/DD' )),'YYYY/MM/DD') ELSE to_date('0000/00/00', 'YYYY/MM/DD')
END as data_,
COUNT(DISTINCT(lekta_call_id)) iloscRozmow
,COUNT(DISTINCT(czasProcesu)) iloscProceso
,SUM(czySukces) iloscRozmowSukces
,SUM(czyTransfer) iloscRozmowTransfer
,SUM(czyRozlaczone) iloscRozmowRozlaczonych
,SUM(czyWsylanySMS) iloscSMS
FROM
(
SELECT
c.start_time czasRozmowy
,d.process_start_time czasProcesu
,c.l_call_id l_call_id
,CASE WHEN end_call_status=3 THEN 1 ELSE 0 END czySukces
,CASE WHEN end_call_status=2 THEN 1 ELSE 0 END czyTransfer
,CASE WHEN end_call_status=1 THEN 1 ELSE 0 END czyRozlaczone
,CASE WHEN send_sms=true THEN 1 ELSE 0 END czyWsylanySMS
FROM "Conversat" C
left join dialogues_details d on c.l_call_id=d.l_call_id
) as tabelka
group by data_
END $$;
Try this
CREATE OR REPLACE FUNCTION _get_result(
_var2 VARCHAR DEFAULT NULL
) RETURNS TABLE (
iloscRozmow INTEGER,
iloscProceso INTEGER,
iloscRozmowSukces INTEGER,
iloscRozmowTransfer INTEGER,
iloscRozmowRozlaczonych INTEGER,
iloscSMS INTEGER,
) AS $$
BEGIN
RETURN QUERY SELECT
CASE WHEN _var2 = 'dzien' THEN to_date((to_char(czasRozmowy, 'YYYY/MM/DD' )),'YYYY/MM/DD') ELSE to_date('0000/00/00', 'YYYY/MM/DD')
END as data_,
COUNT(DISTINCT(lekta_call_id)) iloscRozmow,
COUNT(DISTINCT(czasProcesu)) iloscProceso,
SUM(czySukces) iloscRozmowSukces,
SUM(czyTransfer) iloscRozmowTransfer,
SUM(czyRozlaczone) iloscRozmowRozlaczonych,
SUM(czyWsylanySMS) iloscSMS
FROM
(
SELECT
c.start_time czasRozmowy,
d.process_start_time czasProcesu,
c.l_call_id l_call_id,
CASE WHEN end_call_status=3 THEN 1 ELSE 0 END czySukces,
CASE WHEN end_call_status=2 THEN 1 ELSE 0 END czyTransfer,
CASE WHEN end_call_status=1 THEN 1 ELSE 0 END czyRozlaczone,
CASE WHEN send_sms=true THEN 1 ELSE 0 END czyWsylanySMS
FROM "Conversat" C
left join dialogues_details d on c.l_call_id=d.l_call_id
) as tabelka
group by data_;
END
$$ LANGUAGE 'plpgsql';
SELECT * FROM _get_result('dzien');

create sql view with case

I have a table named VWDRSSTA and it has the following fields
SYSTEM,
EREIGNIS;
DATUM_ZEIT,
ANTRAGSNUMMER,VORGANGSNUMMER,VERS_NR_INT,
DOK_ART,
DUNKEL
I am looking to create a view of this table with a filter in two fields DOK_ART and DUNKEL using CASE statement. Here is what I tried doing
CREATE VIEW VWDRSSTA_VIEW As
SELECT SYSTEM, EREIGNIS, DATUM_ZEIT, ANTRAGSNUMMER, VORGANGSNUMMER, VERS_NR_INT,
CASE
WHEN EREIGNIS = 'EIN-ES' AND DOK_ART = 'EN' Then 'EN'
ELSE ''
END
CASE
WHEN EREIGNIS = 'POL_AN' AND DUNKEL = 1 Then 1
ELSE ''
END
FROM VWDRSSTA;
Which is going wrong. How can I achieve the above?
Try this:
CREATE OR REPLACE FROCE VIEW VWDRSSTA_VIEW As
SELECT SYSTEM, EREIGNIS, DATUM_ZEIT, ANTRAGSNUMMER, VORGANGSNUMMER, VERS_NR_INT,
CASE
WHEN EREIGNIS = 'EIN-ES' AND DOK_ART = 'EN'
THEN 'EN'
ELSE ''
END AS DOK_ART,
CASE
WHEN EREIGNIS = 'POL_AN' AND DUNKEL = 1
THEN 1
ELSE ''
END AS DUNKEL
FROM
VWDRSSTA;

Search filter on two fields based upon condition

Here is my table
create table Table1 (Id int, ...some fields... , CategoryId int, ProfileId int)
I want to write a SP(stored procedure) which will give me search results from the table based upon the parameters passed to the SP. Here is my procedure
Create proc Search
(
#MediaType1 varchar(1000),
#MediaType2 varchar(1000),
#MediaType3 varchar(1000)
)
as
begin
select * from table
where
case when #MediaType1 = '' then 1 else CategoryId end in
(select case when #MediaType1 = '' then 1 else Splvalue end
from dbo.Split(case #MediaType1 when '0,' then '1,2,3,4' when '' then '1,' else #MediaType1 end,','))
and
case when #MediaType2 = '' then 1 else ProfileId end in
(select case when #MediaType2 = '' then 1 else Splvalue end
from dbo.Split(case #MediaType2 when '0,' then '2,12,13' when '' then '1,' else #MediaType2 end,','))
and
case when #MediaType3 = '' then 1 else ProfileId end in
(select case when #MediaType3 = '' then 1 else Splvalue end
from dbo.Split(case #MediaType3 when '0,' then '1,14,15,16' when '' then '1,' else #MediaType3 end,','))
end
Basically, what I want to achieve is when '0' is passed in #MediaType1 variable, it should return all records which have category (1,2,3,4) else it should only that category which is passed (e.g. 3) else if its blank, it should show all records. Same way for #MediaType2 and #MediaType3 except that they should check for ProfileId. The condition is also that all three or two or one of the parameters could be blank, I need to handle those and show the filtered records.
My above query works only if one parameter is passed and rest all are blank. I also tried
where
(#MediaType1 <> '' and Category in (select Splvalue from dbo.Split(#MediaType1,',')))
or
(#MediaType2 <> '' and ProfileId in (select Splvalue from dbo.Split(#MediaType2 ,',')))
or
(#MediaType3 <> '' and ProfileId in (select Splvalue from dbo.Split(#MediaType3 ,',')))
but even this does not works. Any help would be appreciated
If I just rewrite what you said as conditions:
SELECT * FROM table
WHERE
(#MediaType1 = '' OR (#MediaType1 = 0 AND CategoryId IN (1,2,3,4))
OR #MediaType1 = CategoryId)
AND
(#MediaType2 = '' OR (#MediaType2 = 0 AND ProfileId IN (1,2,3,4))
OR #MediaType2 = ProfileId )
AND
(#MediaType3 = '' OR (#MediaType3 = 0 AND ProfileId IN (1,2,3,4))
OR #MediaType3 = ProfileId )
I just wrote the SP like this and it worked
Create proc Search
(
#MediaType1 varchar(1000),
#MediaType2 varchar(1000),
#MediaType3 varchar(1000)
)
as
begin
if (#MediaType1 = '' and #MediaType2 = '' and #MediaType3 = '')
begin
set #MediaType1 = '0,';
set #MediaType2 = '0,';
set #MediaType3 = '0,';
end
select * from table
where
((#MediaType1 = '0,' and CategoryId in (1,2,3,4)) or (CategoryId in (select Splvalue from dbo.Split(#MediaType1,','))))
or
((#MediaType2 = '0,' and ProfileId in (2,12,13)) or (ProfileId in (select Splvalue from dbo.Split(#MediaType2 ,','))))
or
((#MediaType3 = '0,' and ProfileId in (1,14,15,16)) or (ProfileId in (select Splvalue from dbo.Split(#MediaType3 ,','))))
end

Concatenating multiple CASE statements into one alias

After some previous help on how to approach a problem I am having with some legacy code, it seems like the best approach for my issue is to concatenate case statements to return a value I can parse out in PHP.
I am trying to do something like this, but it is returning many rows, and eventually getting this error:
Maximum stored procedure, function, trigger, or view nesting level
exceeded (limit 32).
SELECT org.org_id,
org.org_name_1,
Datename(YEAR, member.enroll_date) AS enroll_year,
Max(CASE
WHEN board.member_from IS NULL THEN 0
ELSE 1
END) AS board_member,
CASE
WHEN ( org.delete_reason = 'OUT'
AND org.org_delete_flag = 'Y'
AND org.org_status_flag = 'C' ) THEN 'out_of_business|'
ELSE ''
END + CASE
WHEN ( stat.carrier = 'BS'
AND stat.status_id IS NOT NULL
AND stat.termination_date IS NULL
AND stat.flat_dues > 0 ) THEN 'insurance_member|'
ELSE ''
END + CASE
WHEN ( stat.carrier = 'BS'
AND stat.status_id IS NOT NULL
AND stat.termination_date IS NULL
AND stat.flat_dues = 0
AND member.status_flag IN( 'C', 'P' ) ) THEN 'insurance_product|'
ELSE ''
END + CASE
WHEN ( member.enroll_date IS NOT NULL
AND member.status_flag NOT IN( 'C', 'P' ) ) THEN 'member_since|'
ELSE ''
END + CASE
WHEN ( org.org_relationship_parent = 'Y'
AND org.dues_category = 'MBR'
AND org.org_status_flag = 'R' ) THEN 'subsidiary_member|'
ELSE ''
END + CASE
WHEN ( org.org_misc_data_9 = 'PAC' ) THEN 'pac|'
ELSE ''
END + CASE
WHEN ( org.dues_category = 'PART' ) THEN 'partner_member|'
ELSE ''
END + CASE
WHEN ( org.dues_category = 'FREE'
AND org.org_status_flag = 'P' ) THEN 'associate_member|'
ELSE ''
END
--ELSE 'non_member'
--END
AS org_status,
60 AS expires_in,
CASE
WHEN stat.dues_type = 'M' THEN
CASE
WHEN ( stat.termination_date IS NULL ) THEN ( stat.flat_dues )
ELSE 0
END
ELSE
CASE
WHEN ( member.payments = 0 ) THEN member.dues_billed_annual
ELSE member.payments
END
END AS dues_level,
CASE
WHEN ( org.affiliate_code = 'PCCE'
AND org.dues_category = 'MBR'
AND org.org_status_flag = 'R' ) THEN 1
ELSE 0
END AS pcce_membr,
-- '$'+CONVERT(VARCHAR,#dues) AS dues_level,
Ltrim(#product_level) AS product_level,
Ltrim(#involve_level) AS involvement_level
FROM organiz AS org
LEFT JOIN affilbil AS member
ON member.status_id = org.org_id
AND member.dues_category = 'MBR'
LEFT JOIN individu AS ind
ON ind.org_id = org.org_id
LEFT JOIN commembr AS board
ON board.status_id = ind.ind_id
AND board.committee_code = '5'
AND board.member_to IS NULL
LEFT JOIN statinsmorn AS stat
ON stat.status_id = org.org_id
AND stat.carrier = 'BS'
AND stat.planz = 'PCI'
WHERE org.org_id = #org_id
GROUP BY org.org_id,
org.org_name_1,
member.enroll_date,
org.delete_reason,
org.org_status_flag,
org.org_delete_flag,
stat.status_id,
stat.flat_dues,
stat.dues_type,
stat.termination_date,
org.org_misc_data_9,
org_relationship_parent,
org.dues_category,
member.status_flag,
member.dues_billed_annual,
member.payments,
stat.carrier,
org.Affiliate_Code
Well, this is embarrassing.
When I was making my changes to the stored procedure, I had inadvertently placed a call to the same procedure at the bottom. So I was recursively calling the same procedure over and over again. DOH.