I apologize that my SQL Kung Fu isn't up to par, but this seems like a basic task that I can't seem to complete.
I've got two tables in Sql Server 2008
tblBoxAddress
--Address
--Zip
--Latitude
--Longitude
tblUpdatedDated
--Address
--latitude
--longitude
I have to update the latitude and longitude of tblBoxAddress with the lat. and long. from tblUpdatedData by matching on the address. The problem is that tblUpdatedData.Address contains the zip code. In tblBoxAddress, this is broken into two separate columns.
I've been able to make a select statement work correctly
select * from tblUpdatedData t
inner join (
Select Address + ' ' + zip As 'full_address', Latitude, Longitude from tblBoxAddress) d
on d.full_address = t.Address
However, I can't figure out how to combine the address and zip of tblBoxAddress on an update statement. This was as far as I've gotten:
update d
set d.Latitude = t.latitude, d.Longitude = t.longitude
FROM tblBoxAddress d inner join
tblUpdatedData t on t.Address = d.Address + ' ' + d.zip as 'full_address'
Any help is appreciated.
You don't need to alias in the update clause(s). e.g. try:
update d
set d.Latitude = t.latitude, d.Longitude = t.longitude
FROM tblBoxAddress d inner join
tblUpdatedData t on t.Address = d.Address + ' ' + d.zip;
Related
I have a join which shares a one to many relationship to one table. I would to in stead of returning this value:
125|PROGRAM1|OEM2|1
125|PROGRAM1|OEM2|2
125|PROGRAM1|OEM2|3
I want to return one line like this:
125|PROGRAM1|OEM2|1,2,3
Here is the sql i am running now:
select d.layout_id,d.pgm_nm,d.corp_nm from io_layout_output d
join (select f.program_nm, c.corp_nm from file_config f
join corp_config c
on f.output_id = c.output_id
where f.output_id = 112) b
on d.pgm_nm = b.program_nm and d.corp_nm = b.corp_nm
How would I end up with the correct output?
You need to use a function for that. See LISTAGG
select d.layout_id,
d.pgm_nm,
LISTAGG(d.corp_nm, ', ') as corp_nm
from io_layout_output d
join (select f.program_nm,
c.corp_nm
from file_config f
join corp_config c
on f.output_id = c.output_id
where f.output_id = 112) b
on d.pgm_nm = b.program_nm
and d.corp_nm = b.corp_nm
group by d.layout_id,
d.pgm_nm
EDIT:
Last time I used Oracle you could use LISTAGG using group by. I just looked at the docs and it doesn't mention it anymore. Here is the way if above does not work:
select d.layout_id,
d.pgm_nm,
LISTAGG(d.corp_nm, ', ')
WITHIN GROUP (ORDER BY d.layout_id, d.pgm_nm) as corp_nm
from io_layout_output d
join (select f.program_nm,
c.corp_nm
from file_config f
join corp_config c
on f.output_id = c.output_id
where f.output_id = 112) b
on d.pgm_nm = b.program_nm
and d.corp_nm = b.corp_nm
Note: I'm just showing how to use the function. Did not look at your query at all. Adding this note because your result data does not match the number of columns on your SQL
I have added a new column to a table and want to prepopulate it.
At the moment I have this:
update Table_A
set packages = 'a:1:{i:0;a:2:{s:4:"name"; s:' + ltrim(str(len(p.name))) + ':"' + p.name + '";'+
's:11:"category_id"; a:1:{i:0;s:1:"0";}}}'
from broker_product as Table_A
join scheme s on Table_A.scheme_id = s.id
join product p on p.id = s.product_ids
where Table_A.packages is null
What I want to do is populate category_id with a value based on the values of a column in product, as opposed to the fixed string that it is now.
An example of what is in product.risks is:
a:2:{i:0;a:5:{s:10:"field_name";s:24:"Gadget Accidental Damage";s:19:"risk_category_value";s:4:"1.00";s:13:"variable_name";s:24:"gadget-accidental-damage";s:11:"description";s:0:"";s:10:"hover_text";s:0:"";}i:1;a:5:{s:10:"field_name";s:32:"Home Emergency Accidental Damage";s:19:"risk_category_value";s:4:"2.00";s:13:"variable_name";s:0:"";s:11:"description";s:0:"";s:10:"hover_text";s:0:"";}}
In this example I would like category_id to be:
a:2:{i:0;s:1:"0";i:1;s:1:"1";}
which is an array of the same length as products.risk, with the values i:0;s:1:"0"; for the size of the array, counting the number of elements in product.risks.
As an example, I am looking for something like this (with correct regex, not the broken stuff I've supplied):
update Table_A
set packages = 'a:1:{i:0;a:2:{s:4:"name"; s:' + ltrim(str(len(p.name))) + ':"' + p.name + '";'
's:11:"category_id"; ' + preg_replace('/a:(\d+):{(i:\d+;)/', 'a:$1:{$2s:1:"0";}', p.risks) + '}}'
from broker_product as Table_A
join scheme s on Table_A.scheme_id = s.id
join product p on p.id = s.product_ids
where Table_A.packages is null
I cant get this SQL query to update my database activties table. This is my query:-
UPDATE Activities
SET CJSID = CJSEmpNum.StaffNumber
FROM CJSEmpNum
Where Activites.Name = CJSEmpNum.Surname + " " + CJSEmpNum.Forename
I think my problem is the where clause. In activities number Name is stored as Smith John and in CJSEmpNum it is stored in two seperate columns Surname and Forename.
Try this :
UPDATE A
SET CJSID = C.StaffNumber
FROM Activities A
INNER JOIN CJSEmpNum C ON A.Name = RTRIM(LTRIM(C.Surname)) +' '+ RTRIM(LTRIM(C.Forename))
Have you tried making this an inner join? Something like...
UPDATE
A
SET
A.CJSID = CJSEmpNum.StaffNumber
FROM
CJSEmpNum
INNER JOIN
Activities A ON A.Name = CJSEmpNum.Surname + ' ' + CJSEmpNum.Forename
I have tried to search for a solution to this but I have not been able to find one that fits this situation.
First I must say that my SQL is a lot rusty. The following query is the most complex one I have ever done to date.
Here is the query:
Declare #root varchar(Max)
set #root = ''
Select
ib.irrnum, ib.status, pu.probtype,
ib.submitby, ta.[Task Action], ib.area, co.cost,
rc.rootcause, ib.jobnum
From
tbl_irrbase ib
Left Join
tbl_cost co On ib.irrnum = co.irrnum
Left join
(Select Distinct probtype, irrnum
From tbl_probtype) pu On ib.irrnum = pu.irrnum
Left Join
(Select Distinct rootcause, irrnum
From tbl_rtcause) rc On ib.irrnum = rc.irrnum
Left Join
(Select TOP 1
(owner + Space(1) + Convert(varchar(10), senddate, 101) + Space(1) + taskitem) As 'Task Action',
irrnum
From
(select * From tbl_taskaction) ta
Order by
senddate Desc, sendtime Desc) ta On ib.irrnum = ta.irrnum
left Join
(Select [#root] = #root + rs.rootsource + Space(3), irrnum
From tbl_rtsource rs
Where rs.entrydate Between '10/04/2016' And '10/06/2016'
Select #root As 'Root Source') sr On ib.irrnum = sr.irrnum
Where
ib.submitedate between '09/28/2016' And '10/05/2016'
My problem is with the last Left Join line. If I take the entire Select statement out and run it in SSMS it runs fine, no errors. But when I try and run it in this query I get an error, red squiggly line under 'Select #root As' telling me the following:
Incorrect Syntax near 'Select'.
Expecting ')', EXCEPT, or UNION
I do not know how to fix this. If I remove this last 'Left Join' line the query runs fine.
Any ideas?
Instead of the last LEFT JOIN, try something like this:
CROSS APPLY (
SELECT (
SELECT rs.rootsource + Space(3)
From tbl_rtsource rs
Where rs.entrydate Between '10/04/2016' And '10/06/2016'
AND rs.irrnum=ib.irrnum
FOR XML PATH('')
) AS rootsource
) sr
Then include sr.rootsource in the columns of the first SELECT.
I have a SQL Query :
SELECT * FROM Customer c
LEFT JOIN Invoice I ON I.InvcNum = C.Cust_InvcNum
some thing changed and the join does not work because there is no consistency in the data in the 'Cust_InvcNum'. So now when it does not find the record for the join or if it is null it needs to check for another condition.
LEFT JOIN Invoice I ON I.InvcNum = (SELECT p.InvcPrefix FROM Prefix WHERE p.DealerID = I.DealrID ) + '-' + I.InvcNum
second join I do works but it is taking too long for it get me data. Is there any other way to do this.
Earlier it used to be
Join on I.InvcNum = C.Cust_InvcNum
both the columns has the same data like DlrCd-InvcNum i.e both the columns 1234-A789
but not it could match on the above data or now the column 'InvcNum' in invoice table
can be populated like Dlrd-InvcNum or InvcPrefix-InvcNum
So InvcNum = 1234-A789 but CustNum = I94-A789
so we need to check if the for InvoicePrefix-InvcNum
SELECT *
FROM Customer c
LEFT JOIN (
SELECT i.InvcNum, p.InvcPrefix + '-' + I.InvcNum AS pInvcNum
FROM Invoice i LEFT JOIN Prefix p ON i.DealrID = p.DealrID
) ip ON c.Cust_InvcNum = CASE WHEN c.Cust_InvcNum = ip.InvcNum
THEN ip.InvcNum
ELSE ip.pInvcNum END