Select column based on another column value - sql

I have a query that I would like to select a different column based on a value billback_id. If billback_id = 3 then I want to select bhh.ctv otherwise I want bsih.total. Is this possible in a join?
select
bsih.invoice_amount,
case when bhh.billback_id = 3 then bhh.ctv else bsih.total end as bsih.total
from [movement].[dbo].[billback_header_history] as bhh
left join
(select invoice_number, invoice_extension,store_number, sum(ctv) as total
from [movement].[dbo].[bsih]
where ctv <> 0
group by invoice_number, invoice_extension, store_number
) as bsih
on bhh.invoice_number = bsih.invoice_number
where last_movement_update between '2022/01/08' and '2022/01/14'
error
Msg 156, Level 15, State 1, Line 111 Incorrect syntax near the keyword
'if'. Msg 156, Level 15, State 1, Line 111 Incorrect syntax near the
keyword 'then'. Msg 156, Level 15, State 1, Line 119 Incorrect syntax
near the keyword 'as'.

In SQL, you can use case (instead of if) in the select.
The statement is:
case when {condition1} then {result1} when {condition2} then {result2} else {result3} end.
So, we could have:
select
bsih.invoice_amount,
case when bhh.billback_id = 3 then bhh.ctv
else bhih.total end as col2
from [movement].[dbo].[billback_header_history] as bhh
left join (
select invoice_number, invoice_extension,store_number, sum(ctv) as total
from [movement].[dbo].[bsih]
where ctv <> 0
group by invoice_number, invoice_extension, store_number
) as bsih on bhh.invoice_number = bsih.invoice_number
where last_movement_update between '2022/01/08' and '2022/01/14'

Related

SQL Update field Query error

I receive the following error message when trying to setup a simple query to update any '0's to '1's.
Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'UPDATE'.
Msg 156, Level 15, State 1, Line 17
Incorrect syntax near the keyword 'AS'.
SELECT *
FROM
UPDATE Table Policies_with_claims
SET Policies_with_claims.Claim_Count=1
FROM
(
SELECT DISTINCT [Sheenal_Policy_Data].[PolicyIdentifier],
COUNT([Sheenal_Claims_Data].[PolicyIdentifier]) AS Claim_Count
FROM (
[Medical Malpractice].[dbo].[Sheenal Policy Data] AS Sheenal_Policy_Data
LEFT JOIN [Medical Malpractice].[dbo].[Sheenal Claims Data] AS Sheenal_Claims_Data ON
(Sheenal_Policy_Data.PolicyIdentifier = Sheenal_Claims_Data.PolicyIdentifier)
)
GROUP BY [Sheenal_Policy_Data].[PolicyIdentifier]
) AS Policies_with_Claims
WHERE Policies_with_claims.Claim_Count=0

Can you do a select top in a case inside a subquery with a top one?

I would like to perform a select statement inside a subquery that looks for the TOP 1, but only if the original result is Null. This is what I Have so far:
,(select TOP 1 Dimension from CUSTINVOICETRANS WHERE invoiceID= #TempCMs.INVOICEID AND [Inv Amt] = LINEAMOUNT
(CASE
WHEN DIMENSION IS NULL THEN
select TOP 1 Dimension from LEDGERTRANS WHERE ledgertrans.voucher = #TempCms.INVOICEID and ABS([Inv Amt])= AMOUNTMST
END)
) AS 'Department'
but i keep getting the following error message
Msg 156, Level 15, State 1, Line 69
Incorrect syntax near the keyword 'CASE'.
Msg 102, Level 15, State 1, Line 73
Incorrect syntax near ')'.
I have no idea how to get past it
For a literal implementation of what you're trying to achieve, you can try
,ISNULL((select TOP 1 Dimension from CUSTINVOICETRANS
WHERE invoiceID= #TempCMs.INVOICEID AND [Inv Amt] = LINEAMOUNT)
, (select TOP 1 Dimension from LEDGERTRANS
WHERE ledgertrans.voucher = #TempCms.INVOICEID and ABS([Inv Amt])= AMOUNTMST))
AS 'Department'

Can I use IIF in th update query

I have an access query which I am trying to convert to SQL but getting Msg 102...
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '='.
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near 'r'.
can I know what's wrong in this ?
UPDATE r
SET r.Estimate_date_collected = (IIf([LAB].[nationality]='MD',
DateAdd("m",-1,[lab].[Date Received]),
DateAdd("m",-2,[lab].[Date Received])))
From temp as r INNER JOIN temp1 as l
ON r.[ID #] = l.[ID #]
WHERE ((((r.Estimate_date_collected) Is Null)) AND ((r.[Date Coll't]) Is Null))) ;

Title: CTE error

I am getting these errors while executing the following SQL please help me
;WITH myCTE AS
(Select mcisi.* from
coke_packaged_item AS spi
JOIN coke_item AS si
ON si.coke_id = spi.coke_id
AND si.coke_item_id = spi.coke_item_id
AND si.shipper_flag = 'n'
JOIN merch_cat_import_coke_item AS mcisi
ON mcisi.coke_id = si.coke_id
AND mcisi.resolved_coke_item_id = si.coke_item_id
AND mcisi.cat_import_event_id = #cat_import_event_id
AND mcisi.accept_flag = 'y')
UPDATE coke_packaged_item
SET packaged_in_uom_id = (select resolved_packaged_unit_of_measure_id from myCTE where myCTE.coke_id = coke_id)
priced_in_uom_id = COALESCE((select resolved_weight_unit_of_measure_idfrom myCTE.coke_id = coke_id), #each_uom_id),
name = (select packaged_item_name from myCTE where myCTE.coke_id = coke_id),
package_weight = (select package_weight from myCTE where myCTE.coke_id = coke_id) ,
status_code = (select status_code from myCTE where myCTE.coke_id = coke_id) ,
last_modified_user_id = (select CASE WHEN last_modified_user_id = 42 THEN #posting_user_id ELSE last_modified_user_id END from myCTE where myCTE.coke_id = coke_id),
last_modified_timestamp = CURRENT_TIMESTAMP
and exists (select coke_id from coke_item AS si
where si.coke_id = spi.coke_id
AND si.coke_item_id = spi.coke_item_id
AND si.shipper_flag = 'n')
and exists (select coke_id from merch_cat_import_coke_item AS mcisi)
where mcisi.coke_id = si.coke_id
AND mcisi.resolved_coke_item_id = si.coke_item_id
AND mcisi.cat_import_event_id = #cat_import_event_id
AND mcisi.accept_flag = 'y'
The error message is:
Msg 102, Level 15, State 1, Procedure Sp_Name, Line 44
Incorrect syntax near 'priced_in_uom_id'.
Msg 102, Level 15, State 1, Procedure Sp_Name, Line 44
Incorrect syntax near '.'.
Msg 102, Level 15, State 1, Procedure Sp_Name, Line 45
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Procedure Sp_Name, Line 46
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Procedure Sp_Name, Line 47
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Procedure Sp_Name, Line 48
Incorrect syntax near ','.
Msg 156, Level 15, State 1, Procedure Sp_Name, Line 54
Incorrect syntax near the keyword 'and'.
Msg 156, Level 15, State 1, Procedure Sp_Name, Line 55
Incorrect syntax near the keyword 'where'.
My friend, I've been looking # your code, and so far, all I've really found are typing errors.
For instance,
* in the set clause, there is no comma between the set for packaged_in_uom_id and priced_in_uom_id
* in the subquery for priced_in_uom_id, there is no space in front of the from clause.
My advice is to adopt a stylized pattern for how you write you sql. The pattern I use says that
* each clause goes on its own line - select, from where, order by, group by
* each column goes on its own line
* list commas go BEFORE the item
when I began to format your code using these patterns, I began to see the problems.
I enforce these patterns using RedGate's SQL Refactor. It's the best SQL code formatter I've found.
Looks like you're missing a comma at the end of the first SET line
SET packaged_in_uom_id = (select resolved_packaged_unit_of_measure_id from myCTE where myCTE.coke_id = coke_id),
You're missing a Comma at line 44 after myCTE.coke_id = coke_id)
You're missing a comma here
SET packaged_in_uom_id = (select resolved_packaged_unit_of_measure_id from myCTE where myCTE.coke_id = coke_id) ,
Also on the next line down you're missing a space before the from and a where
select resolved_weight_unit_of_measure_idfrom myCTE.coke_id = coke_id
there might be other errors I'll leave you to find them
#SmartestVEGA You've posted loads of these sort of questions today. I suggest getting hold of a copy of SQL2008 Management Studio with its syntax checking. I pasted your query in and could see the issue very quickly.

SQL [Hard query - to make or to avoid]

SELECT Name,
( NOT (ID_ListGroupParIzm
IN (SELECT ID_Param
FROM TbUserParam
WHERE ID_User=:ID_User
)
)
) Visi
FROM CfgListParIzm
WHERE ID_ListGroupParIzm=:ID_ListGroupParIzm
Errors :
Message 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword "NOT".
Message 102, Level 15, State 1, Line 2
Incorrect syntax near the construction ":".
added :
I'll try to explain what I want with it.
I need a name from one table and a BOOL value for each Node wich will be false if ID_ListGroupParIzm IN (SELECT ID_Param FROM TbUserParam
WHERE ID_User=:ID_User
And ID_ListGroupParIzm (from CfgListParIzm ) = ID_Param (from TbUserParam)
forget to say :(
btw : looking like select can't return logics value . . .
How to get my purpose then :(
added a try :
SELECT Name,
COALESCE(
(
SELECT TOP 1 0
FROM TbUserParam
WHERE TbUserParam.ID_User = :ID_User
AND TbUserParam.ID_Param = CfgListParIzm.ID_ListParIzm
), 1) Visi
FROM CfgListParIzm
WHERE CfgListParIzm.ID_ListGroupParIzm = :ID_ListGroupParIzm
error :
Message 102, Level 15, State 1, line 6
Incorrect syntax near the construction ":".
But ... sure >_< I need to remake it as a Procedure, Thank you.
SELECT Name,
COALESCE(
(
SELECT TOP 1 0
FROM TbUserParam
WHERE ID_User = :ID_User
AND ID_ListGroupParIzm = :ID_ListGroupParIzm
), 1) Visi
FROM CfgListParIzm
WHERE ID_ListGroupParIzm = :ID_ListGroupParIzm