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
Related
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'
WITH nl_sentcount
AS(
Select nl_id, count(user_id) as sent_num
from sheet1$
Where event_type='nlsent'
group by nl_id
)
Why the error shows as
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near ')'.
You need a select statement after the WITH to do something:
WITH nl_sentcount AS (
Select nl_id, count(user_id) as sent_num
from sheet1$
Where event_type='nlsent'
group by nl_id
)
select *
from nl_sentcount;
I don't know why the following query is not working:
SELECT whs_code, pdt_code,case_dt_yyyymmdd, fresh_frozen_status,
SUM(qty_cases_on_hand)-Qty, SUM(qty_weight_on_hand)-Wt, operation
FROM
(
SELECT whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation,SUM(qty_cases_on_hand) AS Qty, SUM(qty_weight_on_hand) AS Wt
FROM tbl_inventory_activity_rpt1
WHERE operation ='RU'
GROUP BY whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation
)
WHERE operation='SU'
GROUP BY whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation`
The error is :
Msg 156, Level 15, State 1, Line 4 Incorrect syntax near the keyword 'WHERE'.
To make it simple to understand what i am trying to do here, please see the example
I need data as result of
(SELECT x FROM tbl_table Where column y='SU')-(SELECT x FROM tbl_table Where column y='RU')
SELECT ru.whs_code,
ru.pdt_code,
ru.case_dt_yyyymmdd,
ru.fresh_frozen_status,
ru.operation,
ru.Qty - su.Qty AS Qty_Diff,
ru.Wt - su.Wt AS Wt_Diff
FROM
(
SELECT whs_code,
pdt_code,
case_dt_yyyymmdd,
fresh_frozen_status,
operation,
SUM(qty_cases_on_hand) AS Qty,
SUM(qty_weight_on_hand) AS Wt
FROM tbl_inventory_activity_rpt1
WHERE operation ='RU'
GROUP BY whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation
) ru,
(
SELECT whs_code,
pdt_code,
case_dt_yyyymmdd,
fresh_frozen_status,
operation,
SUM(qty_cases_on_hand) AS Qty,
SUM(qty_weight_on_hand) AS Wt
FROM tbl_inventory_activity_rpt1
WHERE operation ='SU'
GROUP BY whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation
) su
WHERE ru.whs_code = su.whs_code
AND ru.pdt_code = su.pdt_code
AND ru.case_dt_yyyymmdd = su.case_dt_yyyymmdd
AND ru.fresh_frozen_status = su.fresh_frozen_status
AND ru.operation = su.operation;
I'm trying to convert a PostgreSQL into SQL Server. But this query doesn't work.
What am I doing wrong? I tried to add a semicolon before WITH but no luck.
SELECT
member_a AS you, member_b AS mightknow, shared_connection,
CASE
WHEN (n1.member_job_country = n2.member_job_country AND n1.member_job_country = n3.member_job_country) THEN 'country in common'
WHEN (n1.member_unvan_id = n2.member_unvan_id AND n1.member_unvan_id = n3.member_unvan_id) THEN 'unvan in common'
ELSE 'nothing in common'
END AS reason
FROM (
WITH transitive_closure(member_a, member_b, distance, path_string, direct_connection) AS
(SELECT
member_a, member_b, 1 AS distance,
CAST(member_a as varchar(MAX)) + '.' + CAST(member_b as varchar(MAX)) + '.' AS path_string,
member_b AS direct_connection
FROM Member_Contact_Edges
WHERE member_a = 45046 -- set the starting node
UNION ALL
SELECT
tc.member_a, e.member_b, tc.distance + 1,
CAST(tc.path_string as varchar(MAX)) + CAST(e.member_b as varchar(MAX)) + '.' AS path_string,
tc.direct_connection
FROM Member_Contact_Edges AS e
JOIN transitive_closure AS tc ON e.member_a = tc.member_b
WHERE tc.path_string NOT LIKE '%' + CAST(e.member_b as varchar(MAX)) + '.%'
AND tc.distance < 2
)
SELECT
member_a, member_b,direct_connection AS shared_connection
FROM transitive_closure
WHERE distance = 2
) AS youmightknow
LEFT JOIN Members AS n1 ON youmightknow.member_a = n1.memberID
LEFT JOIN Members AS n2 ON youmightknow.member_b = n2.memberID
LEFT JOIN Members AS n3 ON youmightknow.shared_connection = n3.memberID
WHERE (n1.member_job_country = n2.member_job_country
AND n1.member_job_country = n3.member_job_country)
OR (n1.member_unvan_id = n2.member_unvan_id
AND n1.member_unvan_id = n3.member_unvan_id);
Error I get:
Msg 156, Level 15, State 1, Line 11
Incorrect syntax near the keyword 'WITH'.
Msg 319, Level 15, State 1, Line 11
Incorrect syntax near the keyword 'with'. If this statement is a
common table expression, an
xmlnamespaces clause or a change
tracking context clause, the previous
statement must be terminated with a
semicolon.
Msg 102, Level 15, State 1, Line 34
Incorrect syntax near ')'.
Here is the reference; Graphs in the Database - SQL Meets Social Networks - Look at the facebook suggestion part at the bottom of the article.
Thanks in advance
The CTE declaration needs to go at the top. You can also have multiple CTEs declared and joined by commas rather than mixing CTEs and derived tables.
Try
;WITH
/*First CTE declaration*/
transitive_closure(member_a, member_b, distance, path_string, direct_connection)
AS
(
...
),
/*Second CTE declaration*/
youmightknow AS
(
SELECT member_a, member_b,direct_connection AS shared_connection
FROM transitive_closure
WHERE distance = 2
)
SELECT member_a AS you,
...
FROM youmightknow
Move your CTE definition to the beginning of your SQL statement. The keyword WITH appears once at the beginning of your statement to introduce one or more CTEs, which may then be referred to in the following SQL. Take a look at this CTE example, it will clear it up for you.
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.