As title says, how can I swap all the values of a column that can be 'A' or 'B', making all columns with 'A' have a 'B' and all columns with a 'B' have an A?
I'm not sure how if making it with UPDATE and SET will change all the A's into B's and then, when all the columns have a 'B', they will change into As.
You can use case like this:
update tablename
set col =
case col
when 'A' then 'B'
when 'B' then 'A'
end
where col in ('A', 'B')
See the demo.
Related
I have the following code in a trigger which works designed. I would like to take this trigger rule further by only setting the CommissionAmount to 0.00 IF the invoice has 1 invoice detail row and changing the #IfMenu to a different letter.
IF the invoice detail like has more than 1 row, the CommissionAmount should be set to 0.00 and the #IfMenu can remain as 'C'.
When the records are are processed the IfMenu will be 'C'. This should change to 'M' for invoices that only have 1 InvoiceID in the InvoiceDetails table. When an InvoiceID has 2 or more InvoiceID's in the InvoiceDetails table the IfMenu can remain as C.
The issue I am having is when SQL processes rows in the InvoiceDetails table and InvoiceID '123' has 3 rows the first 2 rows will change to IfMenu 'M' and then revert back to 'C' when the 3rd row is processed due to the last row not meeting the criteria of the IF statement.
I would like to know how I can group the rows in the InvoicesDetails table so that if InvoiceID = '123' and there are are more than 2 the IfMenu should remain as 'C'
I have tried using a cursor, although I cannot use a cursor in this database due to performance.
Also tried using ##ROWCOUNT and had no luck!
if (#SubmitTo = 2) and #ProductCode in ('3', '14') and #IfMenu in ('C','D') and #Branch = '9' and #CAmount <> 0.00 and #VendorNumber not in ( 'DEFAULT', '') and #VendorNumber is not null
begin
update top (1) IfInvoiceDetails
set CommissionAmount = 0.00
where InvoiceDetailID = #InvoiceDetailID
insert into tblInterfaceOverrides (InvoiceID,InvoiceDetailID,ActionTaken,OriCAmount, JobNumber)
values (#InvoiceID,#InvoiceDetailID,'Set Commission Amount to 0.00',#CAmount, 7)
end
update top (1) IfInvoices
set IfMenu = 'M', Record = (select COALESCE(max(Record),0)+1 from IfInvoices where IfMenu = 'M')
where IfInvoices.InvoiceID = #InvoiceID
Is there a work-around for this?
SELECT
CASE #GROUP
WHEN 'A' THEN GRADE1, GRADE2
WHEN 'B' THEN GRADE1, GRADE4, GRADE5
WHEN 'C' THEN GRADE3, GRADE6
WHEN 'D' THEN GRADE2, GRADE5, GRADE6
END
FROM QuizBeeRep
I want to have multiple THEN in order to filter which columns to select based on the #GROUP.
Please help.
Thanks in advance.
You can't use case expression in such a manner.
Alternatively you can do something like:
if #GROUP = 'A'
select GRADE1, GRADE2
FROM QuizBeeRep
else if #GROUP = 'B
select GRADE1, GRADE4, GRADE5
FROM QuizBeeRep
... and so on
If you know in advance the number of column (and it's fixed) you can do something like this (without using dynamic SQL). It works if the columns have same dataype (otherwise, in some cases, you should add proper CAST):
SELECT
CASE #GROUP
WHEN 'A' THEN GRADE1
WHEN 'B' THEN GRADE1
WHEN 'C' THEN GRADE3
WHEN 'D' THEN GRADE2
END AS COL1
, CASE #GROUP
WHEN 'A' THEN GRADE2
WHEN 'B' THEN GRADE4
WHEN 'C' THEN GRADE6
WHEN 'D' THEN GRADE5
END AS COL2
,CASE #GROUP
WHEN 'A' THEN NULL
WHEN 'B' THEN GRADE5
WHEN 'C' THEN NULL
WHEN 'D' THEN GRADE6
END AS COL3
FROM QuizBeeRep
Considering a simple view like:
JOB TYPE DAYS
1 A 10
2 B 2
3 A 6
4 C 8
I want to add a new column (which do not exist on the original table), called ADP. This column will be filled with values depending on the TYPE column. If that value is A, then ADP column will be filled with a CMM value, if its B with a CM, and if it's C with a PMM value.
The result would be like that:
JOB TYPE DAYS ADP
1 A 10 CMM
2 B 2 CM
3 A 6 CMM
4 C 8 PMM
Use a CASE expression.
select JOB,TYPE,DAYS,
case when TYPE = 'A' then 'CMM'
when TYPE = 'B' then 'CM'
when TYPE = 'C' then 'PMM'
else null
end as ADP
from tablename;
SQL Fiddle
You can use CASE:
SELECT
JOB,
TYPE,
DAYS,
ADP = CASE [TYPE]
WHEN 'A' THEN 'CMM'
WHEN 'B' THEN 'CM'
WHEN 'C' THEN 'PMM'
ELSE NULL
END
FROM your_table;
Or you can create lookup table:
SqlFiddleDemo1&2
CREATE TABLE lookup([Type] NVARCHAR(1), Val NVARCHAR(10), UNIQUE([Type]));
INSERT INTO lookup([Type], [Val])
VALUES ('A' , 'CMM'), ( 'B' ,'CM'),( 'C', 'PMM');
SELECT
JOB,
t.[TYPE],
DAYS,
ADP = l.[val]
FROM your_table t
LEFT JOIN lookup l
ON t.[type] = l.[type];
This solution may be useful if you will need this type value in many places at your system.
I try to insert values into my table in stored procedure based on the conditions:
alter procedure service_report_st2
as
insert into Service_report_step1(appointment_or_house)
select
case
when Service_report_step1.card_uid in(select card_uid from visits where vidpos_kod = 'A')
then '1'
when Service_report_step1.card_uid in(select card_uid from visits where vidpos_kod = 'B')
then '2'
else '3'
end
The error is in Service_report_step1.card_uid it is not found, however this is the first column in Service_report_step1 table. appointment_or_house is the second column, which should contain '1', '2' or '3' if visits table contains card_uid in Service_report_step1.
I'd appreciate any help!
Short example:
Visits table has 2 columns: Card_uid and vidpos_kod
card_uid vidpos_kod
111-111 A
222-222 B
333-333 A
444-444 C
now Service_report_step1 table has card_uid (column that contains all possible cards) and appointment_or_house column
card_uid appointment_or_house
111-111 1
222-222 2
333-333 1
444-444 1
So, If I have the same card_uids in Service_report_step1 as in Visits I determine appointment_or_house column based on vidpos_kod in Visits (A, C is '1', B is '2')
I need to insert into Service_report_step1 all data correctly as in example.
You are using a select statement, but without a FROM clause
You need to specify
INSERT INTO ...
SELECT *
FROM ...
Or something like that.
The other option would be to use Table Value Constructor (Transact-SQL)
insert into Service_report_step1(appointment_or_house)
select
case vidpos_kod
when 'A' then '1'
when 'B' then '2'
when 'C' then '3'
end
from visits
where vidpos_kod like '[ABC]'
I have a table TableX. It contains three columns: A INT , B INT and C VARCHAR(1) (valid values for column C being the name of the columns 'A' or 'B'). I need to count the number of occurrences where either of the following conditions are met:
When C is NULL and does contains values in either A or B greater than zero. or
When C is NOT NULL (i.e. 'A' or 'B') and the value in the specified column (A or B) is either zero or NULL.
My current stored procedure looks like
CREATE PROCEDURE ispcSomeName #NumOcc INT OUTPUT
AS
SELECT COUNT(*) AS [NumOcc]
FROM TableName
WHERE (C IS NULL
AND ((A IS NOT NULL OR A > 0) OR
(B IS NOT NULL OR B > 0)))
OR (CritCarType IS NOT NULL
AND (CASE SET #TmpColumnName = CritCareType
WHEN N'A' THEN (A IS NULL OR A <= 0)
WHEN N'B' THEN (B IS NULL OR B <= 0)))
GO
The problem is that I need to check the column that C references. That is if C = 'A', I need to check if A IS NOT NULL OR A <= 0. There is clearly an issue with the SET in the final WHERE. I have got a few of these types of SPs to author and I want to avoid complex TSQL if I can help it...
How do I dynamically get reference to the relevant column in the WHERE clause?
Thanks for your time.
Select Count(*)
from tab
where
(
C is null and (Coalesce(A,0)>0 or Coalesce(B,0)>0)
)
or
(
C is not null and Case when C='A' then Coalesce(A,0)
when C='B' then Coalesce(B,0) end <=0
)