I'm trying to update the data in table column "Supplier_Base.CreditBalance" with the sum of the colum "Purch" in table "Suppliers_Account" - sql

This was what i'm trying but it does not work.
UPDATE
dbo.Supplier_Base SET dbo.Supplier_Base.CreditBalance (
SELECT
SUM(dbo.Suppliers_Account.Purch ) AS BAL,
FROM
dbo.Suppliers_Account
INNER JOIN
dbo.Supplier_Base ON dbo.Suppliers_Account.Code = dbo.Supplier_Base.Code
GROUP BY dbo.Suppliers_Account.Code, dbo.Supplier_Base.CreditBalance
HAVING (dbo.Suppliers_Account.Code = N'C003'))

Sub-queries aren't allowed in a SET statement. You can work around this by aliasing the tables and doing a JOIN instead.
Do you have a test environment where you can run queries with damaging production data? I think this is what you're looking for, but DO test it first:
UPDATE
b
SET
b.CreditBalance = SUM(a.Purch)
FROM
dbo.Suppliers_Account a
INNER JOIN
dbo.Supplier_Base b
ON
a.Code = b.Code
WHERE
a.Code = N'C003'
GROUP BY
a.Code

This is the correct syntax:
UPDATE SB
SET CreditBalance = SA.Purch
FROM dbo.Supplier_Base SB
INNER JOIN (SELECT Code, SUM(Purch) Purch
FROM dbo.Suppliers_Account)
GROUP BY Code) SA
ON SB.Code = SA.Code
WHERE SB.Code = N'C003'

Related

Passing different column values to where clause

SELECT pims.icicimedicalexaminerreport.id,
pims.icicimerfemaleapplicant.adversemenstrualid,
pims.icicimerfemaleapplicant.pregnantid,
pims.icicimerfemaleapplicant.miscarriageabortionid,
pims.icicimerfemaleapplicant.breastdiseaseid,
pims.pimscase.tiannumber
FROM pims.pimscase
INNER JOIN pims.digitization
ON pims.pimscase.digitizationid = pims.digitization.id
INNER JOIN pims.medicalexaminerreport
ON pims.digitization.medicalexaminerreportid =
pims.medicalexaminerreport.id
INNER JOIN pims.icicimedicalexaminerreport
ON pims.medicalexaminerreport.id =
pims.icicimedicalexaminerreport.id
INNER JOIN pims.icicimerfemaleapplicant
ON pims.icicimedicalexaminerreport.id =
pims.icicimerfemaleapplicant.id
WHERE pims.pimscase.tiannumber = 'ICICI1234567890'
which gives me the following output
Now I want to use the above output values to select the rows from the table "YesNoAnswerWithObservation"
I imagine it should look something like this Select * from YesNoAnswerWithObservation Where Id in (22,27,26,...23)
Only instead of typing the values inside IN clause I want to use the values in each column resulting from above-mentioned query.
I tried the below code but it returns all the rows in the table rather than rows mentioned inside the In
SELECT pims.yesnoanswerwithobservation.observation,
graphitegtccore.yesnoquestion.description,
pims.yesnoanswerwithobservation.id ObservationId
FROM pims.yesnoanswerwithobservation
INNER JOIN graphitegtccore.yesnoquestion
ON pims.yesnoanswerwithobservation.yesnoanswerid =
graphitegtccore.yesnoquestion.id
WHERE EXISTS (SELECT pims.icicimedicalexaminerreport.id,
pims.icicimerfemaleapplicant.adversemenstrualid,
pims.icicimerfemaleapplicant.pregnantid,
pims.icicimerfemaleapplicant.pelvicorgandiseaseid,
pims.icicimerfemaleapplicant.miscarriageabortionid,
pims.icicimerfemaleapplicant.gynocologicalscanid,
pims.icicimerfemaleapplicant.breastdiseaseid,
pims.pimscase.tiannumber
FROM pims.pimscase
INNER JOIN pims.digitization
ON pims.pimscase.digitizationid =
pims.digitization.id
INNER JOIN pims.medicalexaminerreport
ON pims.digitization.medicalexaminerreportid =
pims.medicalexaminerreport.id
INNER JOIN pims.icicimedicalexaminerreport
ON pims.medicalexaminerreport.id =
pims.icicimedicalexaminerreport.id
INNER JOIN pims.icicimerfemaleapplicant
ON pims.icicimedicalexaminerreport.id =
pims.icicimerfemaleapplicant.id
WHERE pims.pimscase.tiannumber = 'ICICI1234567890')
Any help or a nudge in the right direction would be greatly appreciated
Presumably you want the ids from the first query:
SELECT awo.observation, ynq.description, ynq.id as ObservationId
FROM pims.yesnoanswerwithobservation awo JOIN
graphitegtccore.yesnoquestion ynq
ON awo.yesnoanswerid = ynq.id
WHERE ynq.id = (SELECT mer.id
FROM pims.pimscase c JOIN
pims.digitization d
ON c.digitizationid = d.id JOIN
pims.medicalexaminerreport mer
ON d.medicalexaminerreportid = mer.id JOIN
pims.icicimedicalexaminerreport imer
ON mer.id = imer.id JOIN
pims.icicimerfemaleapplicant ifa
ON imer.id = ifa.id
WHERE c.tiannumber = 'ICICI1234567890'
) ;
Notice that table aliases make the query much easier to write and to read.

Multiple Join not working on two string attributes

I have the following issue:
I have several tables in my Database, in order to check for a specific criteria I have to join several tables, where I use the following statement:
SELECT *
FROM (SELECT * FROM (((((((((SELECT * FROM table1 WHERE tab1_id = 1) AS A
INNER JOIN (SELECT * FROM table2 WHERE tab2_variable IS NOT NULL) AS B
ON A.variable = B.variable)
INNER JOIN (SELECT table3.*, IIF(year='XXXX', 0, 1) AS flag FROM table3) AS C
ON A.h_name = C.h_name)
INNER JOIN (SELECT * FROM table4 WHERE s_flag = 0) AS D
ON C.v_type = D.v_type)
INNER JOIN table5
ON C.ng_type = table5.ng_type)
INNER JOIN table6
ON C.part = table6.part)
INNER JOIN table7
ON C.ifg = table7.ifg)
INNER JOIN table8
ON C.v_type = table8.v_type AND C.ng_typ = table8.ng_typ AND C.ntr_flag = table8.ntr_flag)
INNER JOIN table9
ON table8.ifg_sii_id = table9.ifg_sii_id)) AS F
LEFT JOIN table10
ON F.risk = table10.risk AND C.v_type = table10.v_type
AND F.series = table10.series
This statement fails. But when I remove one the following two join-conditions in the last Left JOIN, it works as intended:
F.risk = table10.risk and/or C.v_type = table10.v_type
They are both of type CHAR, whereas series is type TINYINT, I guess it has something to do with joining on multiple conditions with strings, but I'm not able to find a workaround, any ideas?
according to your current SQL, Table C is not visible as it's a sub query within F. instead of C.v_type you need to use F.c_v_type and the c_v_type field must come from C table like. select v_type as c_v_type from table3... as C
In the last part of your Query You could try to actually select the table and include the Where Clause Like so:
LEFT JOIN (Select * from table10 Where C.v_type = v_type AND F.series = series) as xx
ON F.risk = xx.risk
Hope this helps

How do I fix the syntax of a sub query with joins?

I have the following query:
SELECT tours_atp.NAME_T, today_atp.TOUR, today_atp.ID1, odds_atp.K1, today_atp.ID2, odds_atp.K2
FROM (players_atp INNER JOIN (players_atp AS players_atp_1 INNER JOIN (today_atp INNER JOIN odds_atp ON (today_atp.TOUR = odds_atp.ID_T_O) AND (today_atp.ID1 = odds_atp.ID1_O) AND (today_atp.ID2 = odds_atp.ID2_O) AND (today_atp.ROUND = odds_atp.ID_R_O)) ON players_atp_1.ID_P = today_atp.ID2) ON players_atp.ID_P = today_atp.ID1) INNER JOIN tours_atp ON today_atp.TOUR = tours_atp.ID_T
WHERE (((tours_atp.RANK_T) Between 1 And 4) AND ((today_atp.RESULT)="") AND ((players_atp.NAME_P) Not Like "*/*") AND ((players_atp_1.NAME_P) Not Like "*/*") AND ((odds_atp.ID_B_O)=2))
ORDER BY tours_atp.NAME_T;
I'd like to add a field to this query that provides me with the sum of a field in another table (FS) with a few criteria applied.
I've been able to build a stand alone query to get the sum of FS by ID_T as follows:
SELECT tbl_Ts_base_atp.ID_T, Sum(tbl_Ts_mkv_atp.FS) AS SumOfFS
FROM tbl_Ts_base_atp INNER JOIN tbl_Ts_mkv_atp ON tbl_Ts_base_atp.ID_Ts = tbl_Ts_mkv_atp.ID_Ts
WHERE (((tbl_Ts_base_atp.DATE_T)>Date()-2000 And (tbl_Ts_base_atp.DATE_T)<Date()))
GROUP BY tbl_Ts_base_atp.ID_T, tbl_Ts_mkv_atp.ID_Ts;
I now want to match up the sum of FS from the second query to the records of the first query by ID_T. I realise I need to do this using a sub query. I'm confident using these when there's only one table but I consistently get 'syntax errors' when there are joins.
I simplified the first query down to remove all the WHERE conditions so it was easier for me to try and error check but no luck. I guess the resulting SQL will also be easier for you guys to follow:
SELECT today_atp.TOUR, (SELECT Sum(tbl_Ts_mkv_atp.FS)
FROM tbl_Ts_mkv_atp INNER JOIN (tbl_Ts_base_atp INNER JOIN today_atp ON tbl_Ts_base_atp.ID_T = today_atp.TOUR) ON tbl_Ts_mkv_atp.ID_Ts = tbl_Ts_base_atp.ID_Ts AS tt
WHERE tt.DATE_T>Date()-2000 And tt.DATE_T<Date() AND tt.TOUR=today_atp.TOUR
ORDER BY tt.DATE_T) AS SumOfFS
FROM today_atp
Can you spot where I'm going wrong? My hunch is that the issue is in the FROM line of the sub query but I'm not sure. Thanks in advance.
It's difficult to advise an appropriate solution without knowledge of how the database tables relate to one another, but assuming that I've correctly understood what you are looking to achieve, you might wish to try the following solution:
select
tours_atp.name_t,
today_atp.tour,
today_atp.id1,
odds_atp.k1,
today_atp.id2,
odds_atp.k2,
subq.sumoffs
from
(
(
(
(
today_atp inner join odds_atp on
today_atp.tour = odds_atp.id_t_o and
today_atp.id1 = odds_atp.id1_o and
today_atp.id2 = odds_atp.id2_o and
today_atp.round = odds_atp.id_r_o
)
inner join players_atp as players_atp_1 on
players_atp_1.id_p = today_atp.id2
)
inner join players_atp on
players_atp.id_p = today_atp.id1
)
inner join tours_atp on
today_atp.tour = tours_atp.id_t
)
inner join
(
select
tbl_ts_base_atp.id_t,
sum(tbl_ts_mkv_atp.fs) as sumoffs
from
tbl_ts_base_atp inner join tbl_ts_mkv_atp on
tbl_ts_base_atp.id_ts = tbl_ts_mkv_atp.id_ts
where
tbl_ts_base_atp.date_t > date()-2000 and tbl_ts_base_atp.date_t < date()
group by
tbl_ts_base_atp.id_t
) subq on
tours_atp.tour = subq.id_t
where
(tours_atp.rank_t between 1 and 4) and
today_atp.result = "" and
players_atp.name_p not like "*/*" and
players_atp_1.name_p not like "*/*" and
odds_atp.id_b_o = 2
order by
tours_atp.name_t;

SQL UPDATE 'The Column 'ID' was specified multiple times for 'a'

I am fairly new to SQL and I'm having trouble finding out how to fix this error. I understand that I'll get the error because I'm pulling the same column name twice from the same table, so I've created different aliases for the tables.
What I am trying to do is update a table in my database using a query to pull data from a linked server.
Here is a sample:
UPDATE [Database].dbo.T1
SET
T1.Status = item.Status,
T1.CategoryA = c.DESC_TEXT,
T1.CategoryB = d.DESC_TEXT
FROM
(SELECT c.DESC_TEXT, d.DESC_TEXT
inner join CSM_CODE c ON c.DESC_CD = item.ParCat and c.DESC_TYPE = 'PARCAT'
inner join CSM_CODE d ON d.DESC_CD = item.ChCat and d.DESC_TYPE = 'CHCAT'
WHERE
T1.Status = 'NEW')) A
WHERE [Database].dbo.T1.ID = A.ID
Here is my exact error:
The column 'DESC_TEXT' was specified multiple times for 'A'
So I don't know what to do about the aliases in my subquery for this update. Any help is appreciated!
Thank you all for helping figure this out. I now know why I was still receiving errors. Once I created the alias within my sub query, I had failed to update that alias in the SET.
UPDATE [database].dbo.T1
SET
[STATUS] = A.[STATUS],
[Scrum Team] = A.team_name,
[Parent Category] = A.prodparcat,
[Child Category] = A.prodcat
FROM
(SELECT
item.SEQ_ID,
item.STATUS,
c.DESC_TEXT prodparcat,
d.DESC_TEXT prodcat
FROM item
inner join csm_code c ON c.DESC_CD = item.parent_cat_cd and c.DESC_TYPE = 'PRODPARCAT'
inner join CSM_CODE d ON d.DESC_CD = item.prod_cat and d.DESC_TYPE = 'PRODCAT'
WHERE
item.STATUS = 'NEW' ) A
WHERE
[database.dbo.T1.[external ID] = A.SEQ_ID
It's also important to note that I was querying a linked server which required some creativity with my alias. Overall a great learning experience.
Thanks again!
Instead of FROM it should be a update-join construct like below. Also use column alias name for the duplicated columns.
UPDATE [Database].dbo.T1
SET
T1.Status = item.Status,
T1.CategoryA = A.cdesk,
T1.CategoryB = A.ddesk
JOIN
(SELECT c.DESC_TEXT as cdesk, d.DESC_TEXT as ddesk
FROM item
inner join CSM_CODE c ON c.DESC_CD = item.ParCat and c.DESC_TYPE = 'PARCAT'
inner join CSM_CODE d ON d.DESC_CD = item.ChCat and d.DESC_TYPE = 'CHCAT'
WHERE T1.Status = 'NEW')) A
ON [Database].dbo.T1.ID = A.ID
You are combining a column named DESC_TEXT from two different tables into a single table, namely A in your case. In that case, you have to give these two columns different names, like DESC_TEXT_c and DESC_TEXT_d and update T1 accordingly.
I suspect you want:
UPDATE T1
SET Status = item.Status,
CategoryA = c.DESC_TEXT,
CategoryB = d.DESC_TEXT
FROM [Database].dbo.T1 T1 JOIN
CSM_CODE c
ON c.DESC_CD = T1.ParCat and c.DESC_TYPE = 'PARCAT' JOIN
CSM_CODE d
ON d.DESC_CD = item.ChCat and d.DESC_TYPE = 'CHCAT'
WHERE T1.Status = 'NEW';
As written, your code has multiple syntax errors. The two references to DESC_TEXT in the subquery are the tip of the iceberg. I believe the above is the logic you want.
You have specified DESC_TEST twice with
c.DESC_TEXT, d.DESC_TEXT
Try renaming to.
c.DESC_TEXT category_a , d.DESC_TEXT category_b
You'll also need an id in A to join on.
An example to give you an idea.
UPDATE [Database].dbo.T1
SET
T1.Status = A.Status,
T1.CategoryA = A.CategoryA,
T1.CategoryB = A.CategoryB
FROM
(SELECT item.status, c.id, c.DESC_TEXT CategoryA, d.DESC_TEXT CategoryB
from item -- added after seeing the answer.
inner join CSM_CODE c ON c.DESC_CD = item.ParCat and c.DESC_TYPE = 'PARCAT'
inner join CSM_CODE d ON d.DESC_CD = item.ChCat and d.DESC_TYPE = 'CHCAT'
WHERE
T1.Status = 'NEW')) A
WHERE [Database].dbo.T1.ID = A.ID
Untested

SQL update statement based unique value in another table

I am trying to do an SQL UPDATE query to set a value for b.[Disposition] WHERE the i.uid field is unique
The following select statement returns the correct rows.
Select distinct i.*
FROM [dbo].[Imported] i
inner join [DaisyCompare].[dbo].[Baseline] b
on b.[CLI] = i.[CLI]
and b.[Quantity] = i.[Quantity]
and b.[UnitCost] = i.[UnitCost]
and b.[TotalCost] = i.[TotalCost]
and b.[Description] = i.[Description]
However I am unsure how to incorporate that into an SQL UPDATE statement.
Any help greatly appreciated.
Try this
UPDATE upb
SET b.Disposition = "YOUR VALUE"
FROM [DaisyCompare].[dbo].[Baseline] ubp
INNER JOIN (Select distinct i.* FROM [dbo].[Imported] i
inner join [DaisyCompare].[dbo].[Baseline] b on
b.[CLI]=i.[CLI] AND
b.[Quantity]=i.[Quantity] AND
b.[UnitCost]=i.[UnitCost] AND
b.[TotalCost]=i.[TotalCost] AND
b.[Description]=i.[Description] )tmp ON Tmp.UID = ubp.UID
Your question is not very clear but you can do something like this
update b
set -- your fields here
FROM [DaisyCompare].[dbo].[Baseline] b
inner join [dbo].[Imported] i on
b.[CLI]=i.[CLI] AND
b.[Quantity]=i.[Quantity] AND
b.[UnitCost]=i.[UnitCost] AND
b.[TotalCost]=i.[TotalCost] AND
b.[Description]=i.[Description]
I think this works..Mention the column name equal to
update b
set b.disposition = i.xxxx from [dbo].[Imported] i
inner join [DaisyCompare].[dbo].[Baseline] b on
b.[CLI]=i.[CLI] AND
b.[Quantity]=i.[Quantity] AND
b.[UnitCost]=i.[UnitCost] AND
b.[TotalCost]=i.[TotalCost] AND
b.[Description]=i.[Description]
update [b].[Disposition] set x(Desired value)
from [dbo].[Imported] i inner join [DaisyCompare].[dbo].[Baseline] b on
b.[CLI]=i.[CLI] AND
b.[Quantity]=i.[Quantity] and
b.[UnitCost]=i.[UnitCost] and
b.[TotalCost]=i.[TotalCost] and
b.[Description]=i.[Description] where i.uid = y(Desired value)