Get records which satisfy particular condition and exclude others - sql-server-2008-r2

I have the following data with client account decision and status
Client fields value
111 Decision accept
111 Status deposited
112 decision accept
113 Decision accept
114 Decision accept
114 Status contract
115 Decision Conditional Accept
115 Status deposited
116 Decision Conditional Accept
117 Decision Conditional Accept
118 Status contract
118 Decision Conditional Accept
I would like to get
records whose decision is 'accept' and also with status ('deposited', 'contract')
and get the records whose decision is 'conditional accept' and also with status ('deposited', 'contract')
in two different columns.
please help in writing case statements for this.
My output should be like:
My output should be like Client fields value newcol1 newcol2
111 Decision accept
111 Status deposited 111
112 decision accept
113 Decision accept
114 Decision accept
114 Status contract 114
115 Decision Conditional Accept
115 Status deposited 115
116 Decision Conditional Accept
117 Decision Conditional Accept
118 Status contract 118
118 Decision Conditional Accept

Using conditional aggregation:
SELECT Client
FROM yourTable
GROUP BY Client
HAVING (SUM(CASE WHEN fields = 'Decision' AND value = 'accept'
THEN 1 ELSE 0 END) > 0 AND
SUM(CASE WHEN fields = 'Status' AND value IN ('deposited', 'contract')
THEN 1 ELSE 0 END) > 0)
OR
(SUM(CASE WHEN fields = 'Decision' AND value = 'Conditional Accept'
THEN 1 ELSE 0 END) > 0 AND
SUM(CASE WHEN fields = 'Status' AND value IN ('deposited', 'contract')
THEN 1 ELSE 0 END) > 0)

Related

Is there a way to update a table using random values in each row in a table with multiple primary keys?

We have a databade in postgresql ver 11.7 on the pgAdmin4 which has a table with 3 primary keys .We want to update each row with a random value between 1 and 10 .
We have found the desirable elements but we need a way to update only 1 row each time
the conditions are as follow :
1)The subjects much be in a specific semester which can be found in the table called "Courses"
2)then we must find the amkas of students who are registered ( register_status = 'approved')
and we need to upade the table called "Register" with random exam_grades with those conditions assuming the exam_grade is null
Update "Register" SET exam_grade = (SELECT floor(random() * 10 + 1)::int) WHERE course_code IN
(SELECT DISTINCT course_code FROM "Course" WHERE typical_year = 2 AND typical_season = 'spring' ) AND register_status = 'approved' AND exam_grade is null ;
Just that .Somehow we need the update to just be used on only one row and then just use a for loop to take it one by one.If there is any other information i should include ,please tell me
So the tables are as follows
Register:
amka(PK) serial_number(PK) course_code(PK) exam_grade final_grade lab_grade register_status
19 5 ΕΝΕ 501 null null null proposed
13 15 ΤΗΛ 417 2 2 null fail
13 15 ΤΗΛ 411 10 8.4 null pass
47 22 ΜΠΔ 433 6 null 9 approved
:
Course:
course_code(PK) typical_year typical_season units ects weight obligatory
ΑΓΓ 201 2 winter 2 4 1 true
ΜΑΘ 201 1 winter 4 6 1.5 true
ΜΑΘ 208 1 winter 3 5 1.5 false
The results that i want are
mka(PK) serial_number(PK) course_code(PK) exam_grade final_grade lab_grade register_status
19 5 ΕΝΕ 501 random null null approved
13 15 ΤΗΛ 417 random 2 null approved
13 15 ΤΗΛ 411 random 8.4 null approved
47 22 ΜΠΔ 433 random null 9 approved
new random in each row
but i only get 1 number which fills all of them
i hope with this edit things became clearer

Stuck in recursive query with postgresql

INSERT INTO complaintstatus(complaintid,currentstatus,reason,photo,datetime,open_reopen_status,actiontaken,parent_id)
select complaint_id,status,(CASE WHEN (rejectionreason IS NULL OR rejectionreason = '') THEN reopen_reason ELSE rejectionreason END),photo_name,server_time,completion_date,reopen,actiontaken,parent_complaint_id
from complaint_details_v2
where status !=0
order by parent_complaint_id
I want to add data as per the status fetch by select query if status
=3 then insert into complaintstatus with date selected as server_time and insert into only one record but if status is 5 then insert
multiple insertion on behalf of completion ans server_time because
same data is repeated only status, reopen, server_time and
completion_date is vary according to the status.
select complaint_id,status,
(CASE WHEN (rejectionreason IS NULL OR > rejectionreason = '')
THEN reopen_reason ELSE rejectionreason
END),
photo_name,
(CASE WHEN (status='3') THEN server_time
ELSE completion_date
END),
reopen,
actiontaken,
parent_complaint_id
from
complaint_details_v2
where status !=0
order by parent_complaint_id
output of this query:
ComplaintId Status rejectionreason Photoname server_time completiondate reopen actiontaken parent_complaint_id
194 3 sitce picture abc.jpg 2017-09-15 0 109
is not available
195 5 xyz.jpg 2017-09-15 2017-09-15 0 195
08:22:44 00:00:00
214 5 asd.jpg 2017-09-19 1 repair done 214
11:23:11
1599 5 abc.jpg 2017-09-25 2017-10-18 0 complete 214
In the complaintstatus table I have to insert data as per the
above 0utput on the basis of status and reopen data and select
servertime and completion date as per the status
Status rejectionreason Photoname datetime reopen actiontaken parent_complaint_id
3 site picture xyz.jpg 2017-09-15 0 194
are not available 07:59:59.582
5 abc.jpg 2017=09-15 0 repair done 195
08:22:44.247
5 asd.jpg 2017-09-19 0 repairdone 214
11:23:11.274
0 2017-09-25 0 214
18:23:07.181
5 asd.jpg 2017-10-18 0 complete 214

SQL Query to fetch count for Parent-Child data relationship

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

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.

Derived Table(View) is required [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the below table in database:
"ID Number" "Balance Amount Type" "Balance Amount"
234 20 94
234 21 102
234 22 100
212 20 40
212 21 50
212 22 60
I want to create a below derived table(which is just like views in database) from the above table Universe having below fields :
"ID" "BalAmount if Amount Type=20" "BalAmount if Amount Type=21" "BalAmount if Amount Type=22"
234 94 102 100
212 40 50 60
Can you please help me in writing SQL for this derived table, (Database is DB2) ?
A derived table is nothing more than a regular SELECT:
SELECT
ID,
MAX(CASE WHEN amount_type=20 THEN balamount END) type_20_amt,
MAX(CASE WHEN amount_type=20 THEN balamount END) type_21_amt,
MAX(CASE WHEN amount_type=20 THEN balamount END) type_22_amt
FROM
table
GROUP BY
ID
EDIT added in response to comment:
The max() function is required in order to put all values on one row. Using the sample data from the question, a query without max() would produce:
id type_20_amt type_21_amt type_22_amt
-- ----------- ----------- -----------
234 94
234 102
234 100
212 40
212 50
212 60
Using max(), however, puts them on one row:
id type_20_amt type_21_amt type_22_amt
-- ----------- ----------- -----------
234 94 102 100
212 40 50 60