how to filter data in sql - sql

I am trying to filter the data according to several criteria, and based on these criteria, the record record number 7 (M08) should not appear because the "NewSiteID" column is not null and does not belong to the number 6, but the problem is that this record appears even though I used the conditions that appear in the query.
tbMachines.MachIsTransfered, tbMachines.NewSiteID
FROM tbMachines
WHERE BranchID = 6 AND MachIsTransfered = 0
OR BranchID = 6 AND MachIsBorrowed = 1
OR BranchID = 6 AND MachIsloaning = 1
AND NewSiteID = 6 OR NewSiteID = '' ```
[enter image description here][1]
[1]: https://i.stack.imgur.com/yhkUP.png

You need to use parenthesis around your conditions. In this case it can also be simplified quite a bit. If I understand what you are trying to do it could be something like this.
WHERE BranchID = 6
AND
(
MachIsTransfered = 0
OR
MachIsBorrowed = 1
OR
MachIsloaning = 1
)
AND NewSiteID in (6, '')

Related

Loop Back to Same Table

Is there a way to combine these two queries into a single query - just one trip to the database?
Both queries hit the same table, but the first is looking for Total Active Circuits, while the second is looking for Total Circuits.
I am hoping to display results like this...
4/15, 12/34, 2/21 (where the first number is ActiveCircuits and the second number is TotalCircuits)
SELECT COUNT(CircuitID) AS ActiveCircuits
FROM Circuit
WHERE StateID = 5
AND Active = 1
SELECT COUNT(CircuitID) AS TotalCircuits
FROM Circuit
WHERE StateID = 5
Use conditional aggregation:
SELECT COUNT(*) AS TotalCircuits,
SUM(CASE WHEN Active = 1 THEN 1 ELSE 0 END) as ActiveCircuits
FROM Circuit
WHERE StateID = 5;
This assumes that CircuitId is never NULL, which seems quite reasonable in a table called Circuit.
You can use case when to have a 1 wherever it's active and then take the sum to get the total # of 1's or activecircuits.
SELECT COUNT(CIRCUITID) AS TOTALCIRCUITS,
SUM(CASE WHEN ACTIVE = 1 THEN 1 ELSE 0 END) AS ACTIVECIRCUITS
FROM CIRCUIT
WHERE STATEID = 5

sql server query for update rows which are multiple of 2

I've a table with many rows. I've to meet the requirement that those rows which are multiple of 2 should only be updated. e.g.
update [DBO].[ZZZ_FKP_FEMALE_FULLNAME_TBL]
set remarks = 'multiple of TWO'
--- update only those rows which are multiple of 2.
--- where ID = MULTIPLE OF 2
here ID column is primary key with auto increment
How can I solve this?
You can use modulo as #jarlh said, here is the code:
UPDATE T SET T.remarks = 'multiple of TWO'
FROM [DBO].[ZZZ_FKP_FEMALE_FULLNAME_TBL] AS T
WHERE ID % 2 = 0
update [DBO].[ZZZ_FKP_FEMALE_FULLNAME_TBL] set remarks = 'multiple of TWO'where ID % 2 = 0

Can a CASE expression have 2 resultant values

I have to write a case expression in SQL which goes like this,
case condition
if (T_CD = 'Y')
Case C_CD = 'H3'
set R_ID = 3 and RS_ID = 25
CASE A_FLG = 'N' and Mod = 'D'
set R_ID = 3 and RS_ID = 31
Both R_ID and RS_ID populate columns in a different table and have to be derived as per condition above.
My question is - Since I want 2 separate fields out of my case expression, will a single Case give out 2 resultant field values for me. Or Do I have to write 2 different case expressions for it.
If your dbms supports row types, maybe this works for you:
select case when a = 1 then (1,2) else (3,4) end from testtable;
The SQL Validator says:
The following feature outside Core SQL-2003 is used:
T051, "Row types"

Transform an Int "Id" to a Specific Text

i have a question, I want to display in a Datagrid in Flex some information of this Combobox: http://puu.sh/bU6ht/ee36ed7fd1.png
N/A = 0
B = 1
M = 2
NT = 3
FS = 4
ER = 5
SSE = 6
And is saved in the Database as int, but I want in the Datagrid display the information as String
http://puu.sh/bU6cR/3c112d85e3.png
I have a solution and is doing a Case in SQL, but i dont know if is the correct solution.
CASE insp._manguera
WHEN 0 THEN 'N/A'
WHEN 1 THEN 'B'
WHEN 2 THEN 'M'
WHEN 3 THEN 'NT'
WHEN 4 THEN 'FS'
WHEN 5 THEN 'ER'
WHEN 6 THEN 'SSE'
END as _manguera,
And do that for every field in the database
You could also have a table in which you could associate the values to letters and do a join on that table.
That way, you wouldn't need to repeat that code anywhere and you could always modify the table. Good for long time use and reuse.
tell me if you need an example :)

Completing the where clause in sql server

I have a table kyc3 where there are walletno, status and rank columns present. rank columns are currently filled with 0. While status column has following data: accepted, rejected, registered and scanned. I want to put value for each status in rank column where
accepted = 1, rejected = 2, registered = 3 and scanned = 4
I wrote following query but do not understand how to complete it:
INSERT INTO kyc3([rank]) SELECT status_ FROM kyc3
I understand I need to put a where clause that will indicate my logic for data population. But what should I write?
If the table is populated and you want to change the rank field, you want to use an UPDATE statement, as INSERT is for adding new rows to your table:
UPDATE kyc3
SET rank = CASE WHEN status = 'accepted' THEN 1
WHEN status = 'rejected' THEN 2
WHEN status = 'registered' THEN 3
WHEN status = 'scanned' THEN 4
END
You can use update to fill a cell of an existing row.
update kyc3
set rank = CASE WHEN status = 'accepted' THEN 1
WHEN status = 'rejected' THEN 2
WHEN status = 'registered' THEN 3
WHEN status = 'scanned' THEN 4
END
Use insert only for creating new rows.