SQL displaying wrong records in OR condition - sql

I am struggling to write query for the below scenario, can any one help me please?
I have two column error and priority both are numeric fields. need to fetch based on the condition. If i select the value 3 for example from UI screen, it has to look for record in the table.
if the value match in any of the column i have to return the record i have written like this
Select *
FROM WorkItems
WHERE [System.Title] like '%defect%'
AND [System.WorkItemType] in ('Incident','bug')
AND [ErrorClass] =3
OR [Microsoft.VSTS.Common.Priority] =3
AND [Customer] = 'XYZ'
order by [System.WorkItemType]
Its giving false record and fetching all the records with priority or errorclass = 3, i need the records only workitem type = incident or bug for error class=3 or priority = 3
its returning all the records from the table.
If i put and condition like this [ErrorClass] = 3 and [Microsoft.VSTS.Common.Priority] = 3 its returning only if both the value are 3. I need all the records which match 3 along with above condition

try
Select *
FROM WorkItems
WHERE System.Title like '%defect%'
AND System.WorkItemType in ('Incident', 'bug')
AND (ErrorClass = 3 OR Microsoft.VSTS.Common.Priority = 3)
AND Customer = 'XYZ'
order by System.WorkItemType

Related

How can I update if value is different or empty

I want to update my column if the vlaue is different from last value or its empty. I came up with this sql but it gives this error:
missing FROM-clause entry for table "box_per_pallet"
SQL:
UPDATE products AS p
SET box_per_pallet[0] = (CASE WHEN p.box_per_pallet.length = 0 THEN 0 ELSE p.box_per_pallet[0] END)
WHERE sku = 'A' AND store_id = 1
This is what I came up with based on your input. ARRAY_LENGTH takes the array and the dimension you want to check the length of as parameters. This missing from clause is because Postgres thinks that p.box_per_pallet is something other than an array and it can't find that anywhere in the query. You can't use the dot operator on arrays like p.box_per_pallet.length. It's like saying, "find the length field on table box_per_pallet in schema p".
UPDATE products
SET box_per_pallet[0] = CASE WHEN ARRAY_LENGTH(box_per_pallet, 1) = 0
OR box_per_pallet IS NULL
OR box_per_pallet[0] <> 0 -- your new value?
THEN 0
ELSE box_per_pallet[0]
END
WHERE sku = 'A'
AND store_id = 1
;
Here is a link to a dbfiddle showing the idea.

Only return 1 value from SQL Server query

I use the following query that returns the following data set.
SELECT
DISTINCT (a.changelog_Histories_created) AS DateOfStatusChange,
a.Issue_Key AS IssueKey,
a.Changelog_Histories_author_displayName AS ChangeLogUserName,
b.Items_fromString AS StatusChangedFrom,
b.Items_toString AS StatusChangedTo
FROM
[Jira].[Platform.Api_Issue_Changelog_Histories] a
JOIN [Jira].[Platform_Items] b
ON a.Issue_key = b.Issue_key
AND a.Changelog_Histories_index = b.Changelog_Histories_index
WHERE
a.Issue_key = 'TAP-944'
AND b.items_field = 'Status'
AND b.Items_fromString IS NOT NULL
GROUP BY
a.Issue_Key,
a.Changelog_Histories_author_displayName,
b.Items_fromString,
b.Items_toString,
b.Items_field,
a.changelog_Histories_created
ORDER BY
a.changelog_Histories_created DESC;
Data set:
I want to only see 1 status changed from and 1 status changed to distinct value.
For example looking at the screenshot, i only want to see this specific issue_key as status Open, In progress, Code review and closed not multiple times.
How does one achieve this?
Desired output

Complex INSERT INTO SELECT statement in SQL

I have two tables in SQL. I need to add rows from one table to another. The table to which I add rows looks like:
timestamp, deviceID, value
2020-10-04, 1, 0
2020-10-04, 2, 0
2020-10-07, 1, 1
2020-10-08, 2, 1
But I have to add a row to this table if a state for a particular deviceID was changed in comparison to the last timestamp.
For example this record "2020-10-09, 2, 1" won't be added because the value wasn't changed for deviceID = 2 and last timestamp = "2020-10-08". In the same time record "2020-10-09, 1, 0" will be added, because the value for deviceID = 1 was changed to 0.
I have a problem with writing a query for this logic. I have written something like this:
insert into output
select *
from values
where value != (
select value
from output
where timestamp = (select max(timestamp) from output) and output.deviceID = values.deviceID)
Of course it doesn't work because of the last part of the query "and output.deviceID = values.deviceID".
Actually the problem is that I don't know how to take the value from "output" table where deviceID is the same as in the row that I try to insert.
I would use order by and something to limit to one row:
insert into output
select *
from values
where value <> (select o2.value
from output o2
where o2.deviceId = v.deviceId
order by o2.timestamp desc
fetch first 1 row only
);
The above is standard SQL. Specific databases may have other ways to express this, such as limit or top (1).

How to replace a value in one field by a value from another field (different column) within the same view table (SQL)?

I'd like to know if it is possible to replace a value from one field by using a value from another column and a different row.
For Example, click this to view the table image.
I'd like the SumRedeemed value 400 in row 15 to be replaced by the value -1*(-395); the value -395 comes from the EarnPointsLeft in row 6 (both have the same CID meaning that they are the same person). Any suggestions?
You need this update statement:
update t
set t.sumredeemed = (-1) * (select earnpointsleft from FifoPtsView where cid = t.cid)
from FifoPtsView t
where t.cid = 5000100008 and t.earnpointsleft = 0
This will work if the select statement will return only 1 row.
you can simply update your table
update t
set t.sumredeemed = ABS(t2.earnpointsleft )
from FifoPtsView t join FifoPtsView t2 on t.cid = t.cid and isnull(t2.earnpointsleft,0)>0
if you want negative values you can remove ABS ,
please give me your feedbacks

sql query is not working properly

i am trying to get non matching records from two table by comparing some columns which are common in both tables.i am using sql query to get the result. my first table is snd_marketvisits this table have properties like id ,pjpCode , section code, popCode .pop_name and landmark similary my 2nd table have pjpcode , section code, popcode popname are common and there are some other fields.i want to get the names of the pop which are not in second table but present in snd_marketvisit table by comparing popcode, sectioncode and pjpcode in both tables.
SELECT *
FROM snd_marketvisits sm
LEFT JOIN snd_marketvisit_pops sp ON
sm.distributorCode = sp.distributor AND
sm.pjpCode = sp.pjp AND
sm.sectionCode = sp.sectionCode AND
sm.popCode = sp.popCode
WHERE
sm.sectionCode = '00016' AND
sm.pjpCode = '0001' AND
sm.distributorCode = '00190A'
It depends on the database, as far as I know, but if you ask for NULL inside your yoined fields you should get only the rows without a match.
SELECT *
FROM snd_marketvisits sm
LEFT JOIN snd_marketvisit_pops sp ON
sm.distributorCode = sp.distributor AND
sm.pjpCode = sp.pjp AND
sm.sectionCode = sp.sectionCode AND
sm.popCode = sp.popCode
WHERE
sm.sectionCode = '00016' AND
sm.pjpCode = '0001' AND
sm.distributorCode = '00190A'
AND sp.distributor IS NULL