List menu hierarchy using SQL Server hierarchyid - sql-server

I try to build a menu using the datatype hierarchyid.
I have the root node and the current selected node. now I want to list of all elements that are related wetween root and selected node AND there siblings.
I get all related elements with following sql query
DECLARE #rootNode hierarchyid, #selectedNode hierarchyid
SELECT #rootNode = MenuNode FROM CMS_Menu WHERE MenuItemID = 3;
SELECT #selectedNode = MenuNode FROM CMS_Menu WHERE MenuItemID =15;
SELECT CMS_Menu.MenuNode
FROM CMS_Menu
WHERE #selectedNode.IsDescendantOf(MenuNode) = 1 /*all related elements*/
AND MenuNode.GetLevel() >= #rootNode.GetLevel() /*nothing below root*/
Now I have to do something like MenuNode.GetAncestor(1) = result for each row in the query above.
Does anyone have an idea how to get this in a sql query?
Thanks : )

Not entirely sure I understand the question but could you not do something like the following with the WHERE clause:
WHERE #selectedNode.IsDescendantOf(MenuNode.GetAncestor(1)) = 1
Tom

Related

How to query values when a column has N value

So I have the following table:
And I'm trying to write a Query where I can send the code BR_BN as a variable in my WHERE clause
and if I get BR_BN then I want to retrieve the records with this code AND the records with the Code_FS RB02. On the other side when I get the value AB_CP, I want to include the recordes with the Code_FS RB01.
Here's the Query I've tried so far:
DECLARE #Code_OB VARCHAR(20) = 'BR_BN'
SELECT * FROM Dummy_AV
WHERE FK = 2
OR
(#Code_OB = 'BR_BN' AND Code_FS = 'RB02' AND Code_FS = #Code_OB)
But it doesn't work, it retrieves all the records regardless of the FK, and/or the #Code_FS.
How can I achieve this?
Thanks for the help.
You don't note the FK = 2 being needed, yet you have it in front of an OR in the WHERE clause. I think this is what you're after, if it isn't exactly what you're aiming for hopefully it gets you on the right track. For future questions, always helpful to paste your sample data as data instead of an image.
DECLARE #Code_OB VARCHAR(20) = 'BR_BN'
SELECT * FROM Dummy_AV
WHERE FK = 2 -- you will get all rows where this is true
OR
((#Code_OB = Code_OB AND Code_FS = 'RB02') OR (Code_OB = 'AB_CP' AND Code_FS = 'RB01')) -- you will get all rows where one of these is true

Postgresql/ database - traceout on graph with stop criterium (topology)

I have a undirected graph and want to make a traceout. This works fine with:
WITH RECURSIVE path AS (
SELECT edge_id, start_node, end_node
FROM simulation.edge_data
WHERE start_node = 1 OR end_node = 1
UNION
SELECT e.edge_id, e.start_node, e.end_node
FROM simulation.edge_data e, path s
WHERE s.start_node = e.start_node OR s.start_node = e.end_node OR s.end_node = e.end_node OR s.end_node = e.start_node)
SELECT * FROM path;
Now I want to stop at certain nodes if the corresponding record of the node has the status = closed.
Not further investigate on not reachable nodes.
I tried to add something in the where clause:
(SELECT status FROM getRecord(e.start_node)) = open
This gives me all the correct nodes but not all edges.
I can provide more code if needed dont want to overdo if the answer is quiet simple!

Updating one table's column in SQL Server from another

I have a table of measurements from weather stations, with station names (in Hebrew):
I also have created a table of those weather stations with their latitudes and longitudes:
I've written a query that should update the first table with the lat/longs from the second, but it's not working:
update t1
set t1.MeasurementLat = t2.Latitude,
t1.MeasurementLong = t2.Longitude
from [dbo].[Measurements] as t1
inner join [dbo].[StationCoords] as t2 on t1.StationName like t2.Station
I think there is a problem with the way the station name is being read, and perhaps something to do with encoding, because this query brings back an empty result, too:
SELECT TOP (5) *
FROM [dbo].[Measurements]
WHERE [StationName] = 'אריאל מכללה';
Any ideas?
Your example names are not the same. Perhaps this will work:
update m
set MeasurementLat = sc.Latitude,
MeasurementLong = sc.Longitude
from dbo.[Measurements] m join
dbo.[StationCoords] sc
on m.StationName like sc.Station + '%';

how to add a drop down list

Report needs option to select multiple super product types
Selection of multiple super product types?
IF #superProductType = 'ALL'
INSERT INTO #ProductDetails
SELECT pt_sp_type_c, pt_sb_type_c
FROM product..p_type_p_type
WHERE pt_sp_type_c NOT IN ('EQUITY','OPTEQTY')
AND p_type_use_sp_c= 'RPCDB'
AND p_type_use_sb_c = 'TRD'
AND pt_rel_stat_c = 'ACTIVE'
ELSE
INSERT INTO #ProductDetails
SELECT pt_sp_type_c, pt_sb_type_c
FROM product..p_type_p_type
WHERE pt_sp_type_c NOT IN ('EQUITY','OPTEQTY')
AND pt_sp_type_c IN (#superProductType)
AND p_type_use_sp_c= 'RPCDB'
AND p_type_use_sb_c = 'TRD'
AND pt_rel_stat_c = 'ACTIVE'
I have a parameter here #super product type,can anyone help me how to change this code
Here in this code i need to make some changes: ---parameter is #superProductType
The above code was for the option to select multiple super product types
when i select All and one value from the drop down list like 'ALL','ASKF'
both the above conditions in the code if else will fail
It should not get selected ALL and other ASKF at the same time
either should has to select
How can we differentiate that ALL not selecting ALL from together
if we select ALL remaining values in the drop down list must be deleted
It should not have two values selected together IF ALL only ALL has to select remaining should discard
I am not sure how to eliminate the rest of values in the drop down list
looking for suitable solution,can anybody see the code abnove and tell me what is the change i have to do in the code.
If you have set the SSRS parameter to Multivalue then you should change the query to use IN (#ParamName) syntax. so your query would become:
IF #superProductType = 'ALL'
INSERT INTO #ProductDetails
SELECT pt_sp_type_c, pt_sb_type_c
FROM product..p_type_p_type
WHERE pt_sp_type_c NOT IN ('EQUITY','OPTEQTY')
AND p_type_use_sp_c= 'RPCDB'
AND p_type_use_sb_c = 'TRD'
AND pt_rel_stat_c = 'ACTIVE'
ELSE
INSERT INTO #ProductDetails
SELECT pt_sp_type_c, pt_sb_type_c
FROM product..p_type_p_type
WHERE pt_sp_type_c NOT IN ('EQUITY','OPTEQTY')
AND pt_sp_type_c IN ( #superProductType )
AND p_type_use_sp_c= 'RPCDB'
AND p_type_use_sb_c = 'TRD'
AND pt_rel_stat_c = 'ACTIVE'

merge into matches woron

im trying to merge into a table.
this select doesnt find anything:
select * from dpr where dpr_qot_id=1111;
then i run this merge like the follwing:
MERGE INTO dpr d
USING (select dpr_ts, dpr_qot_id
from dpr
where dpr_qot_id = 1111
and dpr_ts = to_date('30.11.1999', 'DD.MM.YYYY')) s
on (s.dpr_ts = d.dpr_ts and s.dpr_qot_id = d.dpr_qot_id)
when not matched then
insert
(DPR_TS,
DPR_CLOSE,
DPR_OPEN,
DPR_HIGH,
DPR_LOW,
DPR_VOLUME,
DPR_QOT_ID)
values
(to_date('30.11.2010', 'DD.MM.YYYY'),
21.66,
21.75,
22.005,
21.66,
2556.00,
1111)
WHEN MATCHED THEN
UPDATE
set DPR_CLOSE = 21.66,
DPR_OPEN = 21.75,
DPR_HIGH = 22.005,
DPR_LOW = 21.66,
DPR_VOLUME = 2556.00;
and still this select doesn't find anything:
select * from dpr where dpr_qot_id=1111;
What am i doing wrong?
Thank you!
Greetings
Magda
Since there are no dpr rows where dpr_qot_id=1111, the source (USING) query of your MATCH will also contain no rows, so there is no data to be merged - and so nothing is done.

Resources