SQL CASE comparing Text Datarow with Int Datarow - mysql

In my table, there is the Datarow Comment, which is stored as Text Datatype.
And I have the Datarow idPersonalstamm which is stored as an int.
My Problem is, sometimes it happens that the Data from Personalstamm gets copied in comment.
So I made a case statement which should cast both to varchar and then compare them and if comment equals personalstamm it should set comment as Null if not it should give that comment.
But it always goes to the else statement. What's my fault or how can I achieve my goal in a different way?
The whole thing is part of an Powershell Script which pulls this query once a week from a visitor terminal.
> My output looks like this
> Nr. id. Comment.
>1---2----2
> 2---3---some text
> 3---4---
> it should look like this
>Nr. id. Comment.
>1---2---
>2---3---some text
>3---4---
<pre><code>SELECT ROW_NUMBER() OVER(ORDER BY vb.Arrival ASC) AS Nr,
p.idPersonalstamm,
CASE vr.rating
WHEN 3 THEN 'Sehr Gut'
WHEN 2 THEN 'Gut'
WHEN 1 THEN 'Nicht so Gut'
ELSE 'Sonstiges'
END AS Bewertung,
<b>Case
WHEN CAST(vr.Comment as varchar (20)) = CAST(p.idPersonalstamm as varchar (20)) Then null
ELSE vr.Comment
END as Comment,</b>
vb.Arrival,
vb.Departure,
p.Name,
p.FirstName,
p.Company,
p.Stadt
From Personalstamm p
left join (
select vr.*,
row_number() over (partition by idPersonalstamm order by idPersonalstamm) as seqnum
from VisitRating vr
) vr on p.idPersonalstamm = vr.idPersonalstamm
left join (
select vb.*,
row_number() over (partition by idVisitor order by idVisitor) as seqnum
from VisitorBooking vb
) vb on p.idPersonalstamm = vb.idVisitor and vb.seqnum = vr.seqnum
where p.idPersonalstamm is not null</code></pre>

Could you try:
Case
WHEN LTRIM(CAST(vr.Comment as varchar (20))) = CAST(p.idPersonalstamm as varchar (20)) Then null
ELSE vr.Comment
END as Comment
or:
Case WHEN CAST(p.idPersonalstamm as varchar (20)) like '%' + CAST(vr.Comment as varchar (20)) + '%' Then null
ELSE vr.Comment
END as Comment

Related

mysql date range query, when the field is not sent anything for the 'between' statement

I have this query, which returns me 7 records
Now if I add this line 'AND date_format (ds.date_emission,'% Y-% m-% d ') BETWEEN' 'AND' '', nothing returns me, I want that when the date range is empty I need that I also returned the 7 records
Please refer answer below.
I have added two variable replace inStartDate variable with a range start date and inEndDate with range end date.
SELECT ds.id
FROM documentos_salida ds
WHERE ds.terceros_id = 329
AND ds.estado =0
AND ds.tipo_documento ='Factura'
AND ds.saldo_pendiente_factura >0
AND (ds.fecha_vencimiento > now() or
ds.fetcha_vencimiento = NOW())
AND ds.numero_documento LIKE '%%'
AND ( CASE WHEN (inStartDate = '' AND inEndDate = '')
THEN TRUE
ELSE (date_format(ds.fetcha_emision, '%Y-%m-%d') BETWEEN inStartDate AND inStartDate)
END)
ORDER BY ds.fecha_emision DESC;

sql rank row results

I"m trying to add a new col that shows the rank (or sequence) of row results by date.
I've written:
SELECT
#row_number:=(CASE
WHEN #member_id = lh.member_id and lc.ladder_advocacy is not null
THEN #row_number + 1
when #member_id = lh.member_id and lc.ladder_advocacy is null then "null"
ELSE 1 /* there is an error here - i need it to return a 1 if not null, then 2 for the 2nd instance, etc */
END) AS rank_advocacy,
#member_id:=lh.member_id AS member_id,
lh.ladder_change,
lc.name,
lc.ladder_advocacy,
lc.ladder_elected,
lc.ladder_policy,
lc.ladder_engagement,
lc.ladder_newventure,
lc.ladder_collective,
lc.is_trigger
FROM
leenk_ladder_history AS lh
LEFT JOIN
leeds_so.leenk_ladder_config AS lc ON lh.ladder_config_id = lc.id
WHERE
ladder_change = 1 AND trigger_active = 1
ORDER BY member_id, trigger_event_date DESC;
There is an error at row 4, and I'm not sure how to fix it. For the first result, I want to return 1. for the second results, I want to return #row_number + 1. Third result, #row_number+2 (etc).
How do I achieve this?
I don't understand how the condition lc.ladder_advocacy is not null is being used. However, the basic structure is:
SELECT (#row_number = IF(#member_id = lh.member_id, #row_number + 1
IF(#member_id := lh.member_id, 1, 1)
)
) as rank_advocacy,
lh.ladder_change,
. . .
Some really important points:
You need to assign #member_id and #row_number in the same expression. MySQL (as with all other databases) does not guarantee the order of evaluation of expressions.
In more recent versions of MySQL, I think the ORDER BY needs to go in a subquery, with the variable expressions in the outer query.

Table Insertion - Multiple MySQL Databases

I am using the below Query to try to insert a few rows from table2 in Database2 into table1 from Database1. Both these tables have different schemas.
Both the tables have same structure. I did check other posts with a similar query to no avail. Used the below basic approach :
INSERT INTO remoteDATABASE.remoteSCHEMA.remoteTABLE
SELECT * FROM localTABLE
INSERT INTO 'eno*****.com'.asdf_stage.temp
SELECT i.artf_id as id,
title as description,
(case
when i.assigned_group = 'asdfk' then 'dfg'
when i.assigned_group = 'dfgdg' then 'dgdk'
when i.assigned_group = 'dfghdgf' then 'igo'
when i.assigned_group = 'dfgh' then 'eMgem'
when i.assigned_group = 'Edsfg' then 'esgd'
end ) as area,
"N/A" as disposition,
"********" as flavor,
"ipf" as link,
"*************" as vendor,
DATE(date_created) date,
DATE(close_date) as completed
FROM ******com.lkjhg_stage.issues i
where date(date_created) >'2018-04-01' AND status!='Rejected'
I am getting syntax error, need to know what is the correct syntax to mention the DB name , Schema name and the table name at both the places.
Also, the table has around 10-15 columns , do I need to specify all of them ?
Replace the double quotes with single quotes. DATE is not a function, and the reserved word date is not a good column name.
Reference your table as SERVER.DATABASE.OWNER.OBJECT
INSERT INTO SERVER1.DATABASE1.OWNER1.OBJECT1
SELECT * FROM localTABLE
INSERT INTO SERVER1.DATABASE1.OWNER1.OBJECT1
SELECT i.artf_id as id,
title as description,
(case
when i.assigned_group = 'asdfk' then 'dfg'
when i.assigned_group = 'dfgdg' then 'dgdk'
when i.assigned_group = 'dfghdgf' then 'igo'
when i.assigned_group = 'dfgh' then 'eMgem'
when i.assigned_group = 'Edsfg' then 'esgd'
end ) as area,
'N/A' as disposition,
'********' as flavor,
'ipf' as link,
'*************' as vendor,
date_created,
close_date as completed
FROM SERVER2.DATABASE2.OWNER2.OBJECT2 i
WHERE date_created > '2018-04-01' AND status != Rejected'

Subquery returned more than 1 value.4...Different query

Hello I have this query that i am trying to execute and i keep getting this error "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.", Kindly help please.
DECLARE #NUMCOUNT BIT
Select #NUMCOUNT = (SELECT
CASE WHEN
(SELECT COUNT(R5REQUISLINES.RQL_REQ)
WHERE R5REQUISLINES.RQL_STATUS IN ('A')
) IN
(SELECT COUNT(R5REQUISLINES.RQL_REQ)
WHERE R5REQUISLINES.RQL_STATUS IN ( 'A','C') ) THEN 1 else 0 END AS NUMCOUNT1
FROM R5REQUISLINES JOIN
R5REQUISITIONS ON R5REQUISLINES.RQL_REQ = R5REQUISITIONS.REQ_CODE
GROUP BY R5REQUISLINES.RQL_REQ, R5REQUISITIONS.REQ_CODE,R5REQUISLINES.RQL_STATUS
)
IF #NUMCOUNT = '1'
begin
UPDATE R5REQUISITIONS
SET R5REQUISITIONS.REQ_STATUS = 'CP'
end
Ok, it sounds like what you actually want to do is update R5REQUISITIONS when there is no RQL_STATUS = 'C' in R5REQUISLINES, since you said you want to count the records where the RQL_STATUS is A and where it's A or C, and then do the update if the counts are the same.. You can greatly simplify this task with the following query:
UPDATE r5
SET r5.REQ_STATUS = 'CP'
FROM R5REQUISITIONS r5
WHERE NOT EXISTS (SELECT 1 FROM R5REQUISLINES r5q WHERE r5q.RQL_REQ = r5.REQ_CODE AND r5q.RQL_STATUS = 'C')
Your 'SELECT CASE' is returning more than 1 record, so it can't be assigned to #NUMBER. Either fix the sub-query to only return the record your looking for or hack it to return only 1 with a 'LIMIT 1' qualification.
I don't know what your data looks like so I can't tell you why your case subquery returns more records than you think it should.
Try running this and see what it returns, that will probably tell you wall you need to know:
SELECT
CASE WHEN
(SELECT COUNT(R5REQUISLINES.RQL_REQ)
WHERE R5REQUISLINES.RQL_STATUS IN ('A')
) IN
(SELECT COUNT(R5REQUISLINES.RQL_REQ)
WHERE R5REQUISLINES.RQL_STATUS IN ( 'A','C')
)
THEN 1
ELSE 0
END AS NUMCOUNT1
FROM R5REQUISLINES JOIN
R5REQUISITIONS ON R5REQUISLINES.RQL_REQ = R5REQUISITIONS.REQ_CODE
GROUP BY R5REQUISLINES.RQL_REQ, R5REQUISITIONS.REQ_CODE,R5REQUISLINES.RQL_STATUS
If there is more than 1 row returned, that's where your problem is.

How to get the 1st transaction date then separate it to another column

I want to separate the 1st transaction date of every client to another column. Im not good at explaining. Hope the image below is enough to clarify my question..
I want to generate a result like in the picture. **I just edited this picture for the reference of the result that I want.
this my sqlfiddle ---> http://sqlfiddle.com/#!9/bebf5/1
I believe PN_NO represents client. We would need to perform ordering of rows based on PN_NO and Pay Date columns. It is needed to identify first transaction date. Please try below query.
SELECT
SCHED_NO,
PN_NO,
PAY_PRIN,
PAY_DATE,
CASE WHEN PN_NO_Rank=1 THEN PAY_DATE ELSE '' END AS BEGIN,
CASE WHEN PN_NO_Rank <>1 THEN PAY_DATE ELSE '' END AS CR
FROM
(
SELECT
SCHED_NO,
PN_NO,
PAY_PRIN,
PAY_DATE,
#PN_NO_Rank := IF(#current_PN_NO = PN_NO, #PN_NO_Rank + 1, 1) AS PN_NO_Rank,
#current_PN_NO := PN_NO
FROM PAY_SCHED
ORDER BY PN_NO, PAY_DATE ASC
) AS Temp
That should help:
SELECT SCHED_NO, PN_NO, PAY_PRIN, PAY_DATE,
CASE WHEN pay_date = (select min(pay_date) from pay_sched where pn_no=ps.pn_no) THEN pay_date ELSE NULL END as "BEGIN",
CASE WHEN pay_date = (select min(pay_date) from pay_sched where pn_no=ps.pn_no) THEN NULL ELSE pay_date END as "CR"
FROM PAY_SCHED ps order by pn_no, pay_date
http://sqlfiddle.com/#!9/bebf5/26/0