How to write sql query to get this result - sql

I had two tables as below:
request_id
update_from_id
sw_ref_number
raised_by_user_id
raised_date
1
0
1
3
2019-08-29 15:08:16.000
id
request_id
input_id
value
is_deleted
21
1
1
00001
0
22
1
2
3
0
75
2
1
00002
0
76
2
2
0
My query is:
select req.request_id,
req.sw_ref_number,
reqDet.[value] ,
reqDet.input_id
FROM SOF.tblSOFRequest req
left join SOF.tblSOFRequestDetails reqDet
on req.request_id = reqDet.request_id
where reqDet.input_id = 1
or reqDet.input_id = 2
And after that my result is:
request_id
sw_ref_number
value
input_id
1
1
00001
1
1
1
3
2
2
2
00002
1
2
2
2
I want a result as:
request_id
sw_ref_number
epi_db_no
manuf_no
1
1
00001
3
2
2
00002
Here input_id = 1 means epi_db_no and input_id = 2 means manuf_no.
How can I get this?
Thanks

You can do it using self-join like the following query.
SELECT req.request_id
,req.sw_ref_number
,reqDet.[value] AS epi_db_no
,reqDet2.[value] AS manuf_no
FROM SOF.tblSOFRequest req
LEFT JOIN SOF.tblSOFRequestDetails reqDet ON req.request_id = reqDet.request_id
LEFT JOIN SOF.tblSOFRequestDetails reqDet2 ON reqDet.request_id = reqDet2.request_id
AND reqDet2.input_id = 2
WHERE reqDet.input_id = 1

This seems like a simple pivot. I use conditional aggregation here:
SELECT req.request_id,
req.sw_ref_number,
MAX(CASE reqDet.input_id WHEN 1 THEN reqDet.[value] END) AS epi_db_no,
MAX(CASE reqDet.input_id WHEN 2 THEN reqDet.[value] END) AS manuf_no
FROM SOF.tblSOFRequest req
JOIN SOF.tblSOFRequestDetails reqDet ON req.request_id = reqDet.request_id
WHERE reqDet.input_id IN (1,2)
GROUP BY req.request_id,
req.sw_ref_number;

Related

How to add extra value to select query result

SubjectMaster
Id Subject
1 English
2 History
3 Maths
UserSubjectAssociation
Id Userid SubjectId
1 1 1
2 1 3
3 2 2
Logs
Id Userid SubjectId Examdate Percentage
1 1 1 02/20/2020 50
2 1 0 Null Null
3 2 1 02/20/2020 70
4 2 2 02/20/2020 60
5 3 0 Null Null
6 4 3 02/18/2020 56
These are my sample tables.
I want to show records from log table of zero as well as all assigned subject of user 1
Suppose user 1 has 2 subject 1 and 3.
Show records from logs where subjectid comes in 0 as well as 1,3
Required Output :
Logs
Id Userid SubjectId Examdate Percentage
1 1 1 02/20/2020 50
2 1 0 Null Null
3 2 1 02/20/2020 70
4 3 0 Null Null
5 4 3 02/18/2020 56
Query :
select * from logs where rdatetime >= '' and subjectid in (select id from subjectmaster where userid = 1)
'Or' did not work.It was giving wrong output.How to handle it.
If I understand correctly, you want a correlated subquery and condition like this:
select l.*
from logs l
where l.subjectid = 0 or
exists (select 1
from subjectmaster sm
where sm.userid = l.userid and
sm.subjectid = l.subjectid
);
You can do left join :
select l.*
from logs l left join
subjectmaster sm
on sm.userid = l.userid and
sm.subjectid = l.subjectid
where not (l.subjectid <> 0 and sm.subjectid is null);
This query will give you the desired result:
select *
from logs l
where subjectid = 0
OR
subjectid IN (select subjectid
from UserSubjectAssociation
where Userid = 1)

MS Access merge two tables

I need to create a new table base on two tables. I have a database in ms access and need to migrate.
tableT tableS
ID CustID DATE Exp1 Exp2 ID CustID DATE Tem1 Tem2
-------------------------------- ---------------------------------
1 1 1/1/00 5 5 1 1 1/1/00 3 4
2 2 1/1/00 1 3 2 2 1/1/00 5 0
3 1 3/1/00 3 2 3 1 5/1/00 0 3
4 3 4/1/00 4 1 4 3 6/1/00 0 0
Desired output table tableNew:
ID CustID DATE Exp1 Exp2 Tem1 Tem2
---------------------------------------------
1 1 1/1/00 5 5 3 4
2 2 1/1/00 1 3 5 0
3 1 3/1/00 3 2
4 3 4/1/00 4 1
5 1 5/1/00 0 3
6 3 6/1/00 0 0
If I use outer join, I will not get the output I need.
Any idea or help.
You want a full join. You can emulate this in MS Access using:
select t.CustID, t.DATE, t.Exp1, t.Exp2, s.tem1, s.tem2
from TableT as t left outer join
tableS as s
on t.CustId = s.CustId and t.date = s.date
union all
select s.CustID, s.DATE, null, null, s.tem1, s.tem2
from tableS as s left outer join
tableT as t
on t.CustId = s.CustId and t.date = s.date
where t.CustId is null;

How to add a flag column based on column values and COUNT() of records partitioned by customer in SQL Server

My SQL Server table looks like this:
DayNo. Customer AgentsInvolved CallID DesiredFlag
-------------------------------------------------------
0 AAA 1 1858 0
0 AAA 3 1859 0
2 AAA 1 1860 0
0 BBB 2 1862 0
0 CCC 1 1863 1
0 DDD 3 1864 0
9 DDD 1 1865 0
9 DDD 4 1866 0
How do I add a new column to say;
WHEN (DayNo. = 0, and AgentsInvolved = 1) AND COUNT(callID) = 1 (grouped by customer)
THEN 1
ELSE 0
The column values I understand can be wrapped into a case statement but not sure how to add the condition for the COUNT(CallID). See the DesiredFlag column to see the result I'm trying to achieve.
Any help would be great.
I would use NOT EXISTS :
SELECT t.*,
(CASE WHEN NOT EXISTS (SELECT 1
FROM table t1
WHERE t1.Customer = t.Customer AND t1.CallID <> t.CallID
) AND t.DayNo = 0 AND t.AgentsInvolved = 1
THEN 1
ELSE 0
END) AS DesiredFlag
FROM table t;
with SAMPLE DATA:
declare #T Table (DayNo int,Customer varchar(10),AgentsInvolved int,CallID int)
insert into #T values
(0,'AAA',1,1858),
(0,'AAA',3,1859),
(2,'AAA',1,1860),
(0,'BBB',2,1862),
(0,'CCC',1,1863),
(0,'DDD',3,1864),
(9,'DDD',1,1865),
(9,'DDD',4,1866)
you can get your result (as i understand correctly) by:
;with summation as
(
select Customer, count(1) as qty
from #T
group by Customer
)
select
T.*,
coalesce(F.flag, 0) as flag
from
#T T
inner join summation s on s.Customer = T.Customer
outer apply (select 1 as flag where DayNo = 0 and AgentsInvolved = 1 and qty = 1) F
RESULTS:
DayNo Customer AgentsInvolved CallID flag
----------- ---------- -------------- ----------- -----------
0 AAA 1 1858 0
0 AAA 3 1859 0
2 AAA 1 1860 0
0 BBB 2 1862 0
0 CCC 1 1863 1
0 DDD 3 1864 0
9 DDD 1 1865 0
9 DDD 4 1866 0

Unable to figure out Inventory current stock calculation

I have 2 tables whose structure is as follows:
tblBookMst
Id bk_isbn bk_title bk_author
----------------------------------------------------
1 ISBN_001 ABC Book AA
2 ISBN_002 DEF Book BB
3 ISBN_003 GHI Book CC
4 ISBN_004 JKL Book DD
and
tblBookId
b_id id lib_id inv_stat
----------------------------------------------------
1 1 BK/LIB/01 1
2 1 BK/LIB/02 2
3 1 BK/LIB/03 2
4 2 BK/LIB/04 1
5 2 BK/LIB/05 1
6 3 BK/LIB/06 1
('inv_stat' legends: 1=> In Stock & 2 => In Circulation)
Using the above 2 tables, i want to write a query which will give me output as shown below
bk_title bk_author tot_copies in_stock in_circulation
ABC Book AA 3 1 2
DEF Book BB 2 2 0
GHI Book CC 1 1 0
Till now i have been unable to figure out how to calculate the 'in_stock' & 'in_circulation'.I am using the below mentioned sql query.
SELECT a.id,a.bk_title,a.bk_author,count(b.lib_id) as tot_copies
FROM tblBookMst a
JOIN tblBookId b ON a.id = b.id
GROUP BY a.id,a.bk_title,a.bk_author
ORDER BY a.bk_title
I hope you understand my question.Please advice with example
You are close! You just need some Case Statements:
SELECT a.id,
a.bk_title,
a.bk_author,
count(b.lib_id) as tot_copies
SUM(CASE WHEN b.inv_stat = 1 THEN 1 ELSE 0 END) as in_stock,
SUM(CASE WHEN b.inv_stat = 2 THEN 1 ELSE 0 END) as in_circulation
FROM tblBookMst a
JOIN tblBookId b ON a.id = b.id
GROUP BY a.id,a.bk_title,a.bk_author
ORDER BY a.bk_title

MSSQL DB get records that don't have a specific status

I have a DB with 3 tables:
Alarm
ID Message
-------------------
1 Server01 Down
2 Switch01 Port 2 down
3 Webserver Down
ListAlarmStates
ID StateName
------------------
1 Raised
2 RaisedNotified
3 Cleared
4 ClearedNotified
5 ForceClear
AlarmStates
ID AlarmId ListAlarmStatesId
-----------------------------------------
1 1 1
2 1 2
3 1 3
4 1 4
5 2 1
6 2 5
7 3 1
Now I would like to know all alarms that don't have the status ClearedNotified but do have the status cleared (the status cleared I could catch in code)
Thanks in advance!
SELECT AlarmId FROM
AlarmStates AS
INNER JOIN Alarm A
ON (AS.AlarmID = A.ID)
INNER JOIN ListAlarmStates LA
ON ( AS.ListAlarmStatesId = LA.ID)
GROUP BY AS.AlarmID
HAVING COUNT(CASE WHEN LA.StateName = 'ClearedNotified' THEN 1 ELSE NULL END) = 0
AND COUNT(CASE WHEN LA.StateName = 'Cleared' THEN 1 ELSE NULL END) > 0)