SQL Update query on query result - sql

I have 2 tables like:
Table 1
SID Sdefinition CValue
4057 s1 32
4058 s2
4059 s3 6
4060 s4
Mapping_tbl
SID SINID ECFID SID-SINID
4057 1099 4027e 1099_4057
4058 1099 4027e 1099_4058
4059 1121 4003e 1121_4059
4060 1121 4003e 1121_4060
Query1
SELECT Mapping_tbl.SID, Table1.Sdefinition, Table1.CValue
FROM Table1 INNER JOIN Mapping_tbl ON Table1.SID= Mapping_tbl.SID;
Query1(Result)
SID Sdefinition CValue
4057 s1 32
4058 s2
4059 s3 6
4060 s4
I have a situation that I wanted to update query table (Query1) i.e
set field(Cvalue) to 0 if it contains null. I am using update query like
Update Query1 Set CValue = 0 Where CValue Is Null;
The query table (query1) gets updated and sets Cvalue to 0 if it
contains nulls, and it also updates(set 0) Table1 where Cvalues are
null.
How can I avoid updating Table1? Any suggestions.

It seems you don't want to change the values stored in your table, and you want the query to display zero when CValue is Null. So you can have the query substitute zero for Null in its result set only (without changing values in the table) ...
SELECT m.SID, t.Sdefinition, IIf(t.CValue Is Null, 0, t.CValue) AS [CValue]
FROM Table1 AS t INNER JOIN Mapping_tbl AS m
ON t.SID= m.SID;
For a query in an Access session, you could use the VBA Nz() function ...
SELECT m.SID, t.Sdefinition, Nz(t.CValue Is Null, 0) AS [CValue]
FROM Table1 AS t INNER JOIN Mapping_tbl AS m
ON t.SID= m.SID;
I used aliases for the table names. But you don't need them for Null substitution to work. I just prefer aliases.

Related

Have a table named TEST with 3 columns. 2 columns has big queries as a cell value. I want to update the column data using reg expression in oracle sql

~Test_num ~src_qry ~tgt qry
---------------------------------------
~123 ~sql query1 ~sql_query2
~124 ~sql query3 ~sql query6
~125 ~sql query4 ~sql query7
~126 ~sql query5 ~sql query8
sql query: it is a Oracle sql query(testing queries stored in table.), used to test data in the table(joins, sub queries are used. ex: customer, relation, transaction and partition_table is used)
change: if partition_table is used in queries of src_qry and tgt qry column, then partition_table.frequency column condition from should be removed from the query.
Output:
as a res
ult i want to check which record is using partition_table.frequency column, if column is used in src_qry and tgt qry column, then i need to remove that condition data( by removing frequency='D' or frequency='M').
before update(sample):
select c1,c2 from T1
leftjoin t2 on t2.c1=t1.c1
and t2.part_num=(select part_num from *abc.partition_table pt where pt.frequency='M' and type_code='cp'* )
where t1.c3='xyz'
and t1.part_num=(select part_num from *xyz.partition_table pm where type_code='cp' and pm.frequency='M'*);
after update:
select c1,c2 from T1
leftjoin t2 on t2.c1=t1.c1
and t2.part_num=(select part_num from **abc.partition_table pt where and type_code='cp'**)
where t1.c3='xyz'
and t1.part_num=(select part_num from **xyz.partition_table pm where type_code='cp'**);
How to handle this kind of updates.
Can anyone help on this scenario.
Thanks in advance.
I'd think of replacing those conditions with 1=1 as
it is always true
you don't have to worry what's in front of strings to be replaced (is it where or and and what will happen if you just "remove" those conditions, e.g. you might get
where and type_code='cp'
where type_code='cp' and);
which are both invalid.
Here's an example:
SQL> with test (col) as
2 (select q'[
3 select c1,c2 from T1
4 left join t2 on t2.c1=t1.c1
5 and t2.part_num=(select part_num from abc.partition_table pt where pt.frequency='M' and type_code='cp' )
6 where t1.c3='xyz'
7 and t1.part_num=(select part_num from xyz.partition_table pm where type_code='cp' and pm.frequency='M');]'
8 from dual
9 )
10 select regexp_replace(col, q'[p.\.frequency='M'|'D']', '1=1') result
11 from test
12 where instr(col, '.frequency') > 0;
RESULT
----------------------------------------------------------------------------------------------------
select c1,c2 from T1
left join t2 on t2.c1=t1.c1
and t2.part_num=(select part_num from abc.partition_table pt where 1=1 and type_code='cp' )
where t1.c3='xyz'
and t1.part_num=(select part_num from xyz.partition_table pm where type_code='cp' and 1=1);
SQL>

Join two tables and concatenate same column names in same statement

I have a large raw data table. It's long and I'm trying to sort of transpose it. I'm joining two select statements from within it. As such I get multiple columns with the same name. It's a full outer join and I would like to have the two separate columns of the same name as one column. Since it is an outer join I don't want to pick just one tables column for it either like select t1.c1
Thanks!
SELECT *
FROM (SELECT * FROM [LabData].[dbo].[FermHourlyDCSData] where Attribute='Urea') P
full outer JOIN
(SELECT * FROM [LabData].[dbo].[FermHourlyDCSData] where Attribute='Water to Mash Total Water') FPD ON
P.[TimeStamp] = FPD.[TimeStamp]
and P.Site = FPD.Site
and P.Element = FPD.Element
Actual:
Site Attribute Timestamp Value Site Attribute Timestamp Value
AD Urea 1/1/2019 127 Null Null Null Null
Null Null Null Null AD Water 1/1/2019 7.5
Expected/Desired:
Site Attribute Timestamp Value Value
AD Urea 1/1/2019 127 Null
AD Water 1/1/2019 Null 7.5
Try this, it's not very pretty, but it does work:
SELECT
[Site] = ISNULL(P.[Site], FPD.[Site]),
[Attribute] = ISNULL(P.[Attribute], FPD.[Attribute]),
[Timestamp] = ISNULL(P.[Timestamp], FPD.[Timestamp]),
[Value] = ISNULL(P.[Value], FPD.[Value]),
[Element] =ISNULL(P.[Element], FPD.[Element])
FROM (SELECT * FROM [dbo].[FermHourlyDCSData] where Attribute='Urea') P
full outer JOIN
(SELECT * FROM [dbo].[FermHourlyDCSData] where Attribute='Water to Mash Total Water') FPD ON
P.[TimeStamp] = FPD.[TimeStamp]
and P.Site = FPD.Site
and P.Element = FPD.Element
ISNULL is what you should use for this
ISNULL(p.Site,fpd.Site) as [Site]
Maybe I'm missing something, but you seem to want a much simpler query:
select Site, Attribute, Timestamp,
(case when Attribute = 'Urea' then Value end) as value_u,
(case when Attribute = 'Water to Mash Total Water' then Value end) as value_2
from [LabData].[dbo].[FermHourlyDCSData]
where Attribute in ('Urea', 'Water to Mash Total Water')

SQL execution time

I face execution time problem here ,really upset
UPDATE [OT]
SET AAA = 8
WHERE ID in
(
select UID from [IP] B
inner join [OT] A
on B.ADDR = A.ADDR and A.ID=B.ID
)
AND AAA= 6 ;
OT table have duplicate ID, count(*) about 900000 rows
IP table have unique ID, count(*) about 800000 rows
AAA is a column of OT table type: tinyint
select count(*) from OT where AAA=6 about 150000
I do not know why this query will take more than 1 hour ??
My other similar query only take 10 seconds
I would just write this more simply as:
UPDATE OT
SET AAA = 8
FROM OT
WHERE EXISTS (SELECT 1 FROM IP WHERE IP.ADDR = OT.ADDR AND IP.ID = OT.ID) AND
OT.AAA = 6 ;
For performance, you want indexes on OT(AAA) and IP(ADDR, ID).

Update query for a specfic columns in all rows

I'm searching for check a columns value in all the rows and update with a where clause as a condition. My case is as follows:
SubscriptionID ChannelURI StudentID
1 XXXX 4
2 yyyy 4
3 XXXX 3
4 XXXX 4
5 XXXX 2
I want to check the column channel uri value for a specfic student and for all matched results to set it to null.
So in this case row 3 and 5 should be set to null.
I've tried this, but it set all channeluri of other rows than studnetid = 4 to null
UPDATE SubscriptionCourse
Set ChannelURI = 1
, DeviceId = null
FROM SubscriptionCourse as t1
INNER JOIN SubscriptionCourse as t2
on t1.ChannelURI = t2.ChannelURI
WHERE StudentId! = 4
Reference the table to be updated by it's alias given in the FROM clause, rather than by name (since the same table name is referenced twice. Also qualify the reference on StudentId in the WHERE clause with the table alias as well.
UPDATE t1
SET t1.ChannelURI = 1
, t1.DeviceId = NULL
FROM SubscriptionCourse t1
JOIN SubscriptionCourse t2
ON t1.ChannelURI = t2.ChannelURI
WHERE t1.StudentId != 4
You say you want to set ChannelURI to NULL, but your statement is setting to a literal value of 1. I've left the assignment as you specified in your statement, but qualified the columns with the table alias.
I don't think this is your problem, but I never include a space in the "not equals" comparison operator symbol (!=). I've just never seen that before. I prefer to use the <> symbol for the "not equals" comparison operator.
From your description of the problem and your example, it's not at all clear why you need to join the table to itself.
I recommend you FIRST write a SELECT statement that returns the rows you want to update, by replacing the UPDATE and SET clauses with a SELECT <expression_list> clause, with the expression_list including the value of the primary key column(s), the column you want to update, and any other columns you want to check. Once that SELECT is returning the rows you want to update, then convert it into an UPDATE statement.
update [table_name] set channelURI = "null" where SubscriptionID = 3 or SubscriptionID =5
in this case,SubscriptionID must be a primary key.
Update table set channeluri = null where studentid <> 4
That what you want? Or you want to find all the ones that have the same uri as student 4 and null those?
Update table set channeluri = null from table inner join table t2 on table.channeluri =t2.channeluri where table.studentid <> 4 and t2.studentid =4
Something like that, im on my phone, the wife has stolen the pc

Tricky SQLite query, could use some assistance

I have a rather confusing SQLite query that I can't seem to quite wrap my brain around.
I have the following four tables:
Table "S"
sID (string/guid) | sNum (integer)
-----------------------------------
aaa-aaa 1
bbb-bbb 2
ccc-ccc 3
ddd-ddd 4
eee-eee 5
fff-fff 6
ggg-ggg 7
Table "T"
tID (string/guid) | ... other stuff
-----------------------------------
000
www
xxx
yyy
zzz
Table "S2TMap"
sID | tID
-------------------
aaa-aaa 000
bbb-bbb 000
ccc-ccc xxx
ddd-ddd yyy
eee-eee www
fff-fff 000
ggg-ggg 000
Table "temp"
oldID (string/guid) | newID (string/guid)
------------------------------------------
dont care fff-fff
dont care ggg-ggg
dont care zzz
What I need is to be able to get the MAX() sNum that exists in a specified "t" if the sID doesn't exist in the temp.NewID table.
For example, given the T '000', '000' has S 'aaa-aaa', 'bbb-bbb', 'fff-fff', and 'ggg-ggg' mapped to it. However, both 'fff-fff' and 'ggg-ggg' exist in the TEMP table, which means I need to only look at 'aaa-aaa' and 'bbb-bbb'. Thus, the statement would return "2".
How would I go about doing this?
I was thinking something along the lines of the following for selecting s that don't exist in the "temp" table, but I'm not sure how to get the max of the seat and only do it based on a specific 't'
SELECT s.sID, s.sNum FROM s WHERE NOT EXISTS ( SELECT newID from temp where tmp.newID = s.sID)
Thanks!
Give this a try:
select max(s.sNum) result from s2tmap st
join s on st.sId = s.sId
where st.tId = '000' and not exists (
select * from temp
where temp.newId = st.sId)
Here is the fiddle to play with.
Another option, probably less efficient would be:
select max(s.sNum) result from s2tmap st
join s on st.sId = s.sId
where st.tId = '000' and st.sId not in (
select newId from temp)
The following query should give you a list of Ts and their max sNums (as long as all exist in S and S2TMap):
SELECT t.tID, MAX(sNum)
FROM S s
JOIN S2TMap map on s.sID=map.sID
JOIN T t on map.tId=t.tID
LEFT JOIN temp tmp on s.sID=tmp.newID
WHERE tmp.newID IS NULL
You were close, you just had to join on S2TMap and then to T in order to restrict the result set to a given T.
SELECT MAX(s.sNum)
FROM s
INNER JOIN S2TMap m on m.sID = s.sID
INNER JOIN t on t.tID = m.tID
WHERE t.tID = '000'
AND NOT EXISTS (
SELECT newID FROM temp WHERE temp.newID = s.sID
)