SQL Select Case Statement With Where Condition In Nested Table - sql

I need to insert a SELECT CASE statement to add a column "acct_profile_ext.acct_pay_terms", to a nested condition table. So the only table that can link with this table is "acct_profile", which is "acct_profile.acct_id = acct_profile_ext.acct_id". Problem is this table "acct_profile" is heavily linked to other table as well, so I need to add this column without effect the result of other table.
So now I've insert the statement at the column part, but I'm not sure how to add the column with WHERE condition inside the SELECT CASE statement.
(SELECT CASE acct_profile_ext.acct_pay_terms
WHEN 'CASH' THEN
(SELECT acct_profile_ext.acct_pay_terms FROM acct_profile_ext, acct_profile
WHERE acct_profile.acct_id = acct_profile_ext.acct_id)
WHEN 'CHEQUE' THEN
(SELECT acct_profile_ext.acct_pay_terms FROM acct_profile_ext, acct_profile
WHERE acct_profile.acct_id = acct_profile_ext.acct_id)
ELSE NULL
END
FROM acct_profile_ext)
Please advise and help. Thanks.

Related

SQL select statement to change two other column values based on a column that contains null

I would like to use a SQL select statement that has the condition 'where column A is NULL change column B values to be equal to column C values'. How would I be able to incorporate this logic into a SELECT statement (Not an UPDATE statement as I cant change the tables on the server but want to query them from the server).
SELECT final.*
FROM final
The actual table is in the image below, here I want to change column Old to match column DirectUse if the Change column is null.
Try Case statement:
SELECT
Name, NameSimple, DirectUse, Year, Month,
CASE WHEN Change IS NULL THEN DirectUse ELSE Old END AS Old,
CurrentCons, Change
FROM final;
CASE: https://www.w3schools.com/sql/sql_case.asp
Can also be incorporated by UNION ALL:
SELECT Old
FROM final where Change is not null
UNION ALL
SELECT DirectUse
FROM final where Change is null
Use a CASE expression:
SELECT Name, NameSimple, DirectUse, Year, Month,
CASE WHEN Change IS NULL THEN DirectUse ELSE Old END AS Old,
CurrentCons, Change
FROM final;
I think you basically you want:
SELECT
ColumnA
, CASE WHEN ColumnA IS NULL THEN ColumnC ELSE ColumnB END AS ColumnB
, ColumnC
, <any other columns>
FROM Final

stored procedure that synchronizes writeback table to a fact table, throws an error subquery returns more than one query

I have stored a procedure that loads data from the writeback table to the fact table. If a user alters only a single row in excel and publishes the write back then the stored procedure synchronizes the writeback table to the fact table properly but if the users alter multiple rows then my stored procedure throws an error saying. subquery returned more than one value. this is not permitted when subquery follows =,!=,<,> or when the subquery is used as an expression. I don't know what to use other than subquery to update multiple rows.
This is the stored procedure;
Create PROC [dbo].[sp_load_purchase]
AS
Merge fact_purchase target
using (
SELECT [ProductKey_6],[Vendor_5]
,year([Date_4]) [year_]
,month([Date_4]) [Month_]
,SUM([Purchased_x0024__1]) Purchased
,sum([ReturnedQTY_2]) ReturnedQTY
,sum([Returned_x0024__3]) Returned
,sum([PurchasedQTY_0]) as PurchasedQTY
FROM [dbo].[WriteTable_Purchase]
GROUP BY [ProductKey_6],[Vendor_5]
,year([Date_4])
,month([Date_4]) ) as Source
ON [Product]=[ProductKey_6]
and vendor=[Vendor_5]`
and year(target.[Date_key])=source.[year_]
and month(target.[Date_key])=source.[Month_]
WHEN MATCHED
THEN UPDATE SET
target.[PurchasedQTY]=Case when source.[PurchasedQTY] is not null Then
(SELECT sum([PurchasedQTY_0])+ISNULL(PurchasedQTY,0) as
FROM [VR].[dbo].[WriteTable_Purchase]
JOIN [dbo].[Fact_Purchase] on [ProductKey_6]=product
WHERE [Date]=#date
GROUP BY PurchasedQTY)
else target.[PurchasedQTY] end ,
target.[Purchased$]= Case when source.Purchased is not null Then
(SELECT sum([Purchased_x0024__1])+ISNULL(Purchased$,0) as Purchased$
FROM [VR].[dbo].[WriteTable_Purchase]
JOIN [dbo].[Fact_Purchase] on [ProductKey_6]=product and [Vendor_5]=[Vendor]
WHERE [Date]=#date
GROUP BY Purchased$)
else target.[Purchased$] end,
Target.[ReturnedQTY]= Case when source.ReturnedQTY is not null then
(SELECT sum([ReturnedQTY_2])+ISNULL([ReturnedQTY],0) as [ReturnedQTY]
FROM [VR].[dbo].[WriteTable_Purchase]
JOIN [dbo].[Fact_Purchase] on [ProductKey_6]=product and [Vendor_5]=[Vendor]
WHERE [Date]=#date
GROUP BY [ReturnedQTY])
else target.[ReturnedQTY] end,
Target.[Returned$]= case when source.Returned is not null then
(SELECT sum([Returned_x0024__3])+ISNULL([Returned$],0) as [Returned$]
FROM [VR].[dbo].[WriteTable_Purchase]
JOIN [dbo].[Fact_Purchase] on [ProductKey_6]=product and [Vendor_5]=[Vendor]
WHERE [Date]=#date
GROUP BY [Returned$] )
else target.Returned$ end
;
TRUNCATE TABLE [dbo].[WriteTable_Purchase]
Try putting all the logic to create the dataset that will update the target in the "using" statement. Ensure that this only generates one record per key (the ON statement).
Then make the UPDATE statement a simple update of the values in your "using" dataset

Why do these two "not in" queries have different results?

We are trying to weed out records that have a duplicate of certain columns. I built this query to show any row that has an 'N' for its Flag, if there is not a matching 'Y' record in the table with the same last/first name.
select * from Table where LName+FName not in
(select LName+FName from Table where FLAG = 'y')
However this comes back with 0 results. The inner query does return expected results. If I run the inner query, and manually paste in the result values like below, it runs with results.
select * from Table where LName+Fname not in ('DoeJohn','AbelAdam')
What exactly is going on here?
I would suggest looking at your data... NOT IN fails when there is any NULL value. You can try either using NOT EXISTS or filtering any null values:
select * from Table where LName+FName not in
(select LName+FName from Table where FLAG = 'y' AND LName+FName IS NOT NULL)

SELECT-CASE-IN-SELECT error: [SQL0115] Comparison operator IN not valid. In query db2

i have a problem in a db2 query
I tried run this query
SELECT t.* ,
CASE WHEN column in (SELECT data FROM otherTable WHERE conditions...)
then 5
else 0
end as 'My new data'
FROM table t
WHERE conditions....
But get error
[Error Code: -115, SQL State: 42601] [SQL0115] Comparison operator IN not valid.
When i change the sub-query to where statement like this
SELECT t.*
FROM table t
WHERE column in (SELECT data FROM otherTable WHERE conditions...)
Works fine
Why not work in the case statement? It is a limitation of db2?
And could make an equivalent behavior?
One way to do this is to left join to the table and check if it is not null.
In most cases this will be the fastest way because SQL servers are optimized to perform joins very quickly (but will depend on a number of factors including data model, indexes, data size, etc).
Like this:
SELECT t.* ,
CASE WHEN othertable.data is not null
then 5
else 0
end as 'My new data'
FROM table t
left join otherTable ON otherTable.column = data
WHERE conditions....
Try with using exists condition as below (put the column value in the where clause of subquery) :
SELECT t.* ,
CASE WHEN exists (SELECT data FROM otherTable WHERE conditions... and column=val)
then 5
else 0
end as 'My new data'
FROM table t
WHERE conditions....

SQL Check column present more than once

Consider a table A. Table A has a column named INNETTEDTXNID which is not the Primary key and neither unique and can be null.
I have a select * on table A based on some condition(not related to column INNETTEDTXNID).
For each of the rows fetched above i have to find a value called isNetted. The concept of isNetted is that if the column INNETTEDTXNID of this row is present more than once in the entire table(including this row) then the value of isNetted would be true for this row.
I hope my question is clear. Thanks for advance in help.
This will return the values of any entries that appear more than once.
SELECT a.INNETTEDTXNID
FROM TableA as a, TableA as b
WHERE
not a.id=b.id
and a.INNETTEDTXNID=b.INNETTEDTXNID
if your trying to do this specifically to check if a single number appears more than once you could do something like
SELECT COUNT(INNETTEDTXNID)
FROM TableA
WHERE INNETTEDTXNID='value'
if it returns one, then its unique, for more than one its not unique. IF you want it to return something like true or false you could use
SELECT
CASE WHEN (
SELECT COUNT(INNETTEDTXNID)
FROM TableA
WHERE INNETTEDTXNID='value'
)>1 THEN 'True'
ELSE 'False'
END
AS isNetted
Probably no-where near the most efficient but it works.
SELECT TableA.INNETTEDTXNID,
CASE
WHEN NetChk.INNETTEDTXNID IS NOT NULL Then 1
ELSE 0
END AS isNetted
FROM TABLEA
LEFT JOIN (
SELECT INNETTEDTXNID
FROM TableA
GROUP BY INNETTEDTXNID
HAVING COUNT(INNETTEDTXNID) >1)
NetChk ON TableA.INNETTEDTXNID = NetChk.INNETTEDTXNID