SQL Query to fetch count for Parent-Child data relationship - sql-server-2008-r2

I have a requirement to get count of parent-child relationship.
QuestionID ParentQuestionID
207 NULL
208 NULL
209 207
210 208
211 209
212 210
For example, question id 207 has child id 209 & 209 has child id 211. so totally 207 has two child ids. So i want to return count as 2. How can i do that. Can some one help?

Try this:
;with cte as
(
select QuestionID, ParentQuestionID, 0 as lvl
from questiontable
where QuestionID = 207
union all
select q.QuestionID, q.ParentQuestionID, lvl+1
from questiontable q
inner join cte c on c.QuestionID= q.ParentQuestionID
)
select count(*) from cte
where QuestionID <> 207
You can use a parameter instead of hard-coded value 207 to make it dynamic for any QuestionID.
Demo

Related

PostgreSQL Lag Function between of Two Tables

I am new to Postgresql/Python, so please bear with me!
Assuming we have two tables:
item table having a itemid, price, time.
user table having colums userid, itemid, timecreated, quantity, firstprice, lastprice, difference.
Table examples like :
item table:
itemid price time
RBK 92 1546408800
LBV 51 1546408800
ZBT 49 1546408800
GLS 22 1546408800
DBC 17 1546408800
RBK 91 1546495200
LBV 55 1546495200
ZBT 51 1546495200
GLS 24 1546495200
DBC 28 1546581600
RBK 108 1546581600
LBV 46 1546581600
ZBT 49 1546581600
GLS 21 1546581600
DBC 107 1546581600
In item table all those values comes up with api.
and user table:
userid itemid timecreated quantitty firstprice currentprice difference
1 RBK 1546408800 20
2 RBK 1546408800 15
3 RBK 1546408800 35
3 GLS 1546408800 101
3 DBC 1546495200 140
1 RBV 1546495200 141
2 RBK 1546495200 25
2 RBV 1546581600 31
User table is djangobased table which is user can register\add new items to follow prices.
My struggle access the item table to fetch first price which is having a same timestamp. In that example userid 1 RBK First price (1546408800) must be filling with 92
I did some trick with postgresql with (lag) But this does not seems to be working:
update user
set firstprice = tt.prev_price
from (select item.*,
lag(price) over (partition by itemid order by time) as prev_price
from item
) tt
where tt.id = item.id and
tt.prev_close is distinct from item.price;
I can call current price from the api but didnt find out the way to filling firstprice from the item table. I will be making for a trigger for this query. I searched on google and on stackoverflow but I couldn't find anything that could help me. Thanks in advance.
I can advice next approach (may be not fastest):
update "user" set firstprice = (
select price from "item" i
where i.itemid = "user".itemid and i.time >= "user".timecreated order by i.time limit 1
);
It calculate firstprice using sub-query. Test this SQL here

Problem counting items in an individual row without duplication

I'm trying to write a query that will include a count for the primary and secondary activity only when Group ID = 260 and Item id in(1302,1303,1305,1306) for each individual RecordID. So far I have been able to single out the rows with those conditions, but I only want to count the primary and secondary activities once(because the Primary and Secondary activities are the same for their corresponding RecordID regardless of how many rows there are), if they aren't null, regardless of how many RecordID's match those conditions.
RecordID: GroupID: ItemID: PrimActivity: SecActivity:
320 260 1302 36 0
320 260 6456 36 0
320 312 1303 36 0
560 400 1302 46 48
560 312 1305 46 48
460 260 1305 45 56
460 260 1302 45 56
Result I'm getting:
RecordID: Count:
320 2
460 4
Expected result:
RecordID: Count:
320 1
460 2
SELECT dfr.RecordID,
COUNT(CASE WHEN dfr.PrimActivity <> 0 and a.GroupID =260 then 1
ELSE NULL END) +
COUNT(CASE WHEN dfr.SecActivity <> 0 and a.GroupID =260 then 1 ELSE
NULL END) AS Count
From ActivityItem ai
Join DailyRecord dfr on ai.PrimActivity = dfr.PrimActivity
Join AreaInfo af on af.AreaInfoID = dfr.AreaInfoID
Join Information a on dfr.RecordID = a.RecordID
Join Lookup lp on lp.ItemID = a.ItemID
WHERE a.GroupID like '260' and EXISTS(
SELECT b.RecordID, b.GroupID, b.ItemID
FROM Areainfo b
where a.RecordID=b.RecordID and b.ItemID IN(1302,1303,1305,1306)
GROUP BY dfr.RecordID
You should be more clear when you explain the structure of tables you are using. However, I reach the expected result starting from your sample table doing this:
SELECT RecordID,COUNT(*) as Count
FROM (SELECT DISTINCT RecordID,ItemID,PrimActivity,SecActivity
FROM [TABLE YOU POSTED]
WHERE GroupID = 260 and ItemID in (1302,1303,1305,1306) ) A
GROUP BY RecordID

SELECT FROM VALUES used a bit like a CASE statement - but possibly more powerful

I just found myself writing the code below - which works.
Interesting, but is it necessarily the best method?
the syntax allows the TRY_CAST to only be performed once.
Note "Atextfield" can contain valid numbers and invalid numbers.
SELECT *
FROM call
WHERE
EXISTS ( SELECT 1
FROM ( VALUES( TRY_CAST(call.[Atextfield] AS int) )
) AS Table1(num)
WHERE
(Table1.num BETWEEN 124 AND 140 )
OR (Table1.num BETWEEN 143 AND 146 )
OR (Table1.num BETWEEN 148 AND 149 )
OR (Table1.num BETWEEN 160 AND 169 )
OR (Table1.num BETWEEN 181 AND 189 )
)
;
2 .Could this be re-written as follows?
SELECT *
FROM [call]
WHERE TRY_CAST([call].AtextField AS TINYINT) BETWEEN 124 AND 189
AND TRY_CAST([call].AtextField AS TINYINT) NOT IN (141,142,147)
AND TRY_CAST([call].AtextField AS TINYINT) NOT BETWEEN 150 AND 159
AND TRY_CAST([call].AtextField AS TINYINT) NOT BETWEEN 170 AND 180
Note I'm new to CASE in t-sql...
2A. Is the TRY_CAST(...) evaluated more than once?
Which of the above will be quicker?
Is there a better way to write this?
Is the first method useful when the criteria get more involved and complex.
Is this an acceptable approach?
Harvey
There's no need to use exists or 1 = CASE...
Just put your logic in the where clause directly. I'd probably do something like this:
SELECT *
FROM [call]
WHERE TRY_CAST([call].AtextField AS TINYINT) BETWEEN 124 AND 189
AND TRY_CAST([call].AtextField AS TINYINT) NOT IN (141,142,147)
AND TRY_CAST([call].AtextField AS TINYINT) NOT BETWEEN 150 AND 159
AND TRY_CAST([call].AtextField AS TINYINT) NOT BETWEEN 170 AND 180
Cross Apply Method:
SELECT *
FROM [call]
CROSS APPLY (SELECT CAST(PersonID AS TINYINT)) CA(intField)
WHERE intField BETWEEN 124 AND 189
AND intField NOT IN (141,142,147)
AND intField NOT BETWEEN 150 AND 159
AND intField NOT BETWEEN 170 AND 180
My guess is that your query and mine queries will be pretty similiar. If you want to check performance, try running this first and then running each query and recording the logical reads and times.
SET STATISTICS IO ON
SET STATISTICS TIME ON

Recursive CTE with multiple valid same parent child relationships

I have an equipment inventory application I am working on. The piece of equipment is my top level and it contains assemblies, sub-assemblies and parts. I am trying to use recursive CTE to display the parent/child relationships. The issue I am having is that some assemblies can have multiple sub-assemblies that are the same, meaning there is not difference in the part numbers. This is causing my query to not show the correct relationship based on my order by statement. This is the first time I have used CTE so I have be using a lot learned on the web.
PartNumberID 174 is used twice in this assembly.
Sample Table
equipmentID parentPartNumberID partNumberID
17 1 281
17 281 156
17 156 161
17 161 224
17 281 174
17 174 192
17 192 56
17 174 193
17 281 174
17 174 192
17 192 56
17 174 193
17 281 283
17 ` 283 183
17 283 277
17 283 173
Results of Query
PARENT CHILD PARTLEVEL HIERARCHY
1 281 0 281
281 156 1 281.156
156 161 2 281.156.161
161 224 3 281.156.161.224
281 174 1 281.174
281 174 1 281.174
174 192 2 281.174.192
174 192 2 281.174.192
192 56 3 281.174.192.56
192 56 3 281.174.192.56
174 193 2 281.174.193
174 193 2 281.174.193
281 283 1 281.283
283 173 2 281.283.173
283 183 2 281.283.183
283 277 2 281.283.277
As you can see the hierarchy is created correctly but I it is not being returned correctly because there is nothing unique for these 2 assemblies for the order by statement.
The Code:
with parts(PARENT,CHILD,PARTLEVEL,HIERARCHY) as (select parentPartNumberID,
--- Used to get rid of duplicates
CASE WHEN ROW_NUMBER() OVER (PARTITION BY partNumberID ORDER BY partNumberID) > 1
THEN NULL
ELSE partNumberID END AS partNumberID,
0,
CAST( partNumberID as nvarchar) as PARTLEVEL
FROM db.tbl_ELEMENTS
WHERE parentPartNumberID=1 and equiptmentID=17
UNION ALL
SELECT part1.parentPartNumberId,
--- Used to get rid of duplicates
CASE WHEN ROW_NUMBER() OVER (PARTITION BY parts1.partNumberID ORDER BY parts1.partNumberID) > 1
THEN 10000 + parts1.partNumberID
ELSE parts1.partNumberID END,
PARTLEVEL+1,
cast(parts.hierarchy + '.' + CAST(parts1.partNumberID as nvarchar) as nvarchar)
from dbo.tbl_BOM_Elements as parts1 inner
join parts onparts1.parentPartNumberID=parts.CHILD
where id =17)
select CASE WHEN PARENT > 10000
THEN PARENT - 10000
ELSE PARENT END AS PARENT,
CASE WHEN CHILD > 10000
THEN CHILD - 10000
ELSE CHILD END AS CHILD,
PARTLEVEL,HIERARCHY
from parts
order by hierarchy
I tried to create a unique ID to order but was not successful. Any suggestions would be greatly appreciated.
I'll start by just answering the part about getting a sequential id.
If you have control you could just a unique Id to your source table. Having a surrogate primary key would be pretty typical here.
You could instead use a second CTE before the recursive one and add the row numbers there using ROW_NUMBER() OVER BY (ORDER BY equipmentID, parentPartNumberID, partNumberID). Then build your recursive CTE off of that rather than the source table directly.
Better might be to use the first CTE to instead GROUP BY equipmentID, parentPartNumberID, partNumberID and add a COUNT(1) field. This would let you instead use the count in you hierarchy rather than getting the duplicates. Something like 281.283.277x2 or whatever.

Ordering row having same values in two columns on top

I have data similar to one given below:
ID UserID PlayerID Name
1 56 21 A
2 57 34 B
3 77 77 C
4 65 23 D
5 77 77 E
I want the rows with same value in UserID and PlayerID column to be at the top.
I have currently done this:
select * from tblTest
order by abs(UserID - PlayerID ) asc
Any better way to achieve this result?
Try this
SELECT * From tblTest
Order By Case When UserID = PlayerID Then 0 Else 1 End