ALTER Proc [dbo].[K_FS_GetBalanceforOrderconfirm]
#name varchar(50),
#date datetime
as
begin
select round(sum(DISTINCT AR.recamount) - sum(OD1.noofbags*MR.rateperkg*BG.bagtype),2) as balance from K_FS_AmountReceived AR inner join K_FS_OrderDetails1 OD1 on AR.orderno =OD1.ID
inner join K_FS_bagtype BG on BG.sno = OD1.bagtype
inner join k_FPS_FeedType FT on FT.sno = OD1.feedtype
inner join K_FS_FeedMrpDetails MR on FT.sno=MR.feedtype
where AR.Name = #name and MR.date = #date
end
Above query gives output based on date, but sum(.......) some values already have some date including that date sum(....) I want sum(......) based on whatever value been passed.
Your question in not clear to me. Are you trying to say that some tables are already have daily value summarized?
From my experience I would suggest to start with preparing data with intermediate subqueries. Something like
SELECT DISTINCT col1 FROM (SELECT * FROM RawData WHERE RowData.col1_date = #mydate) a
This way you can run subqueries independently and see what you get there.
Related
MyActualresultI am trying to get count of data which have particular status on different dates. Something like tracking shipment and getting today's report.
I have used Pivot with stored procedure. I get result as column and rows but return value null on executing the Sp.
Can any one please suggests how can I rectify the issue?
I tried to put the query in some variable and then execute but its not done correctly.
Create PROCEDURE [dbo].[GetCountOfShipmentWithStatus]
#DateToStart Date,
#DateToEnd Date,
#LabName nvarchar(30)
AS
BEGIN
SET NOCOUNT ON;
WITH TrackShipment AS
(
SELECT exData.ID, CAST(ExpectedDeliveryDt AS DATE) AS Deliverydate,A.AccountCode,L.Name, DATENAME(dw, ExpectedDeliveryDt) AS DayOfWeek,exData.MileStoneTypeId AS TrackingStatus,exData.AccountNo
FROM [Tracking].[TrackingExternalData] exData
Left Join [systemManagement].[SystemMetaData] sysmetadat on exdata.MileStoneTypeId=sysmetadat.systemMetaDataId
Left Join [Data_Replication_EZSHIP].[dbo].[AccountNumber] AS A on exData.AccountNo=A.AccountCode
Left JOIN [Data_Replication_EZSHIP].[dbo].[Location] AS L ON A.LocationId = L.Id
WHERE L.Name=#LabName
AND sysmetadat.systemMetaDataId IN ('E770CE7C-E0E6-40C6-AC51-5D2129F2DEB7','D6A011C8-C39C-45B5-9127-52D20C68E1C3','C7657AE1-9354-E911-BB4A-005056B00B08','559ECEC2-969A-4F8C-9A95-21C613D82F3A')
AND exData.ExpectedDeliveryDt <= #DateToStart AND exData.ExpectedDeliveryDt >= #DateToEnd
)
SELECT Deliverydate, DayOfWeek,
-- List of Pivoted Columns
[E770CE7C-E0E6-40C6-AC51-5D2129F2DEB7],[D6A011C8-C39C-45B5-9127-52D20C68E1C3], [C7657AE1-9354-E911-BB4A-005056B00B08], [559ECEC2-969A-4F8C-9A95-21C613D82F3A]
FROM TrackShipment
PIVOT
(
COUNT(Id)
-- List of Pivoted columns
FOR TrackingStatus IN([E770CE7C-E0E6-40C6-AC51-5D2129F2DEB7],[D6A011C8-C39C-45B5-9127-52D20C68E1C3], [C7657AE1-9354-E911-BB4A-005056B00B08], [559ECEC2-969A-4F8C-9A95-21C613D82F3A])
) as pvt
ORDER BY Deliverydate DESC
END
GO
I expect return value same as result of pivot.
I am guessing that this condition is incorrect:
exData.ExpectedDeliveryDt <= #DateToStart AND
exData.ExpectedDeliveryDt >= #DateToEnd
I don't know what you intend, because you haven't provided sample data, desired results, or an explanation of the logic you want.
But I am guessing that the comparison are backwards:
exData.ExpectedDeliveryDt >= #DateToStart AND
exData.ExpectedDeliveryDt <= #DateToEnd
I have below query in mysql where I want to check if branch id and year of finance type from branch_master are equal with branch id and year of manager then update status in manager table against branch id in manager
UPDATE manager as m1
SET m1.status = 'Y'
WHERE m1.branch_id IN (
SELECT m2.branch_id FROM manager as m2
WHERE (m2.branch_id,m2.year) IN (
(
SELECT DISTINCT branch_id,year
FROM `branch_master`
WHERE type = 'finance'
)
)
)
but getting error
Table 'm1' is specified twice, both as a target for 'UPDATE' and as a
separate source for data
This is a typical MySQL thing and can usually be circumvented by selecting from the table derived, i.e. instead of
FROM manager AS m2
use
FROM (select * from manager) AS m2
The complete statement:
UPDATE manager
SET status = 'Y'
WHERE branch_id IN
(
select branch_id
FROM (select * from manager) AS m2
WHERE (branch_id, year) IN
(
SELECT branch_id, year
FROM branch_master
WHERE type = 'finance'
)
);
The correct answer is in this SO post.
The problem with here accepted answer is - as was already mentioned multiple times - creating a full copy of the whole table. This is way far from optimal and the most space complex one. The idea is to materialize the subset of data used for update only, so in your case it would be like this:
UPDATE manager as m1
SET m1.status = 'Y'
WHERE m1.branch_id IN (
SELECT * FROM(
SELECT m2.branch_id FROM manager as m2
WHERE (m2.branch_id,m2.year) IN (
SELECT DISTINCT branch_id,year
FROM `branch_master`
WHERE type = 'finance')
) t
)
Basically you just encapsulate your previous source for data query inside of
SELECT * FROM (...) t
Try to use the EXISTS operator:
UPDATE manager as m1
SET m1.status = 'Y'
WHERE EXISTS (SELECT 1
FROM (SELECT m2.branch_id
FROM branch_master AS bm
JOIN manager AS m2
WHERE bm.type = 'finance' AND
bm.branch_id = m2.branch_id AND
bm.year = m2.year) AS t
WHERE t.branch_id = m1.branch_id);
Note: The query uses an additional nesting level, as proposed by #Thorsten, as a means to circumvent the Table is specified twice error.
Demo here
Try :::
UPDATE manager as m1
SET m1.status = 'Y'
WHERE m1.branch_id IN (
(SELECT DISTINCT branch_id
FROM branch_master
WHERE type = 'finance'))
AND m1.year IN ((SELECT DISTINCT year
FROM branch_master
WHERE type = 'finance'))
The problem I had with the accepted answer is that create a copy of the whole table, and for me wasn't an option, I tried to execute it but after several hours I had to cancel it.
A very fast way if you have a huge amount of data is create a temporary table:
Create TMP table
CREATE TEMPORARY TABLE tmp_manager
(branch_id bigint auto_increment primary key,
year datetime null);
Populate TMP table
insert into tmp_manager (branch_id, year)
select branch_id, year
from manager;
Update with join
UPDATE manager as m, tmp_manager as tmp_m
inner JOIN manager as man on tmp_m.branch_id = man.branch_id
SET status = 'Y'
WHERE m.branch_id = tmp_m.branch_id and m.year = tmp_m.year and m.type = 'finance';
This is by far the fastest way:
UPDATE manager m
INNER JOIN branch_master b on m.branch_id=b.branch_id AND m.year=b.year
SET m.status='Y'
WHERE b.type='finance'
Note that if it is a 1:n relationship the SET command will be run more than once. In this case that is no problem. But if you have something like "SET price=price+5" you cannot use this construction.
Maybe not a solution, but some thoughts about why it doesn't work in the first place:
Reading data from a table and also writing data into that same table is somewhat an ill-defined task. In what order should the data be read and written? Should newly written data be considered when reading it back from the same table? MySQL refusing to execute this isn't just because of a limitation, it's because it's not a well-defined task.
The solutions involving SELECT ... FROM (SELECT * FROM table) AS tmp just dump the entire content of a table into a temporary table, which can then be used in any further outer queries, like for example an update query. This forces the order of operations to be: Select everything first into a temporary table and then use that data (instead of the data from the original table) to do the updates.
However if the table involved is large, then this temporary copying is going to be incredibly slow. No indexes will ever speed up SELECT * FROM table.
I might have a slow day today... but isn't the original query identical to this one, which souldn't have any problems?
UPDATE manager as m1
SET m1.status = 'Y'
WHERE (m1.branch_id, m1.year) IN (
SELECT DISTINCT branch_id,year
FROM `branch_master`
WHERE type = 'finance'
)
Modified some stuff from my pic so you guys can understand it
I have this database. I am trying to update a value from a table based on another value from an another table.
I want to update the SUM from salary like this :
( sum = presence * 5 )
This is what I've been trying to use ( unsuccessful )
update table salary
set suma.salary = users.presence * 5
FROM salary INNER JOIN users1 INNER JOIN presence on id_salary = id_presence
I am not sure what to do, I'd appreciate some help, Thanks
In MySQL to UPDATE tables with a join you use this syntax:
UPDATE table1, table2
SET table1.column = some expression
WHERE table1.column = table2.column
That said, even with the updated picture, in your SQL you are mentioning columns that I cannot understand in which table are to be found. You also have an inner join between salariu and users1, with no join condition. Could you please clean up the question and make everything clear?
Assuming you are making the updates to the db structure you were talking about, then you can start working on this one maybe:
UPDATE salary, presence
SET salary.sum = SUM(presence.hours) * 5
WHERE presence.id = salary.id
AND <some filter on the month that depends on salary.date>
Another way, but I'm not sure it is supported in all RDBMS, would be something like this:
UPDATE salary
SET sum = (
SELECT SUM(presence.hours) * 5
FROM user, presence
WHERE presence.id = salary.id
AND <some filter on the month that depends on salary.date>
)
I am trying desperately to avoid a foreach situation in SQL Server 2008 (my background is in c#).
Basically, I have a list of SKUs. For each SKU in the list, I need to perform some calculations that determine if that particular SKU will be displayed on the web.
To get my list of SKUs, I use this:
SELECT Feed.StyleCode as SKU
FROM [eCommerce].[dbo].[BABW_ItemFeed] as Feed
WHERE Feed.ProductDefinition = 'Kit'
Returning this:
And to calculate each SKUs fields, I've been using this:
DECLARE #SKU AS varchar(50)
SET #SKU= '11993_16559_16227'
SELECT
#SKU as SKU,
0 AS Quantity,
MIN(ISNULL(Sending.IsActive, 'WEBNO')) AS IsActive,
MAX(ISNULL(Sending.IsDiscontinued, 1)) AS IsDiscontinued
FROM
(
SELECT * FROM [eCommerce].[dbo].[Split] (
#SKU
,'_')
) AS SplitSkus
LEFT JOIN #SkusToSend AS Sending
ON Sending.SKU = SplitSkus.items
Returning this:
Now I need to synch the two tables together, removing the #SKU declaration. I don't think I'm able to use a UNION to do this, because the second function requires fore-knowledge of the SKU it will be processing... and a JOIN would require something to join on, which I don't really have. Is there some function I'm not familiar with that I can use to create a complete table of SKUs in one go without a looping mechanism?
Try a CROSS APPLY... which will execute your UDF for each row in BABW_ItemFeed:
SELECT
Feed.StyleCode as SKU,
COUNT(*) AS Quantity,
MIN(ISNULL(Sending.IsActive, 'WEBNO')) AS IsActive,
MAX(ISNULL(Sending.IsDiscontinued, 1)) AS IsDiscontinued
FROM
[eCommerce].[dbo].[BABW_ItemFeed] as Feed
CROSS APPLY [eCommerce].[dbo].[Split] (Feed.StyleCode, '_') AS SplitSkus
LEFT JOIN #SkusToSend AS Sending
ON Sending.SKU = SplitSkus.items
WHERE
Feed.ProductDefinition = 'Kit'
GROUP BY
Feed.StyleCode
Stop using Min() and Max()...or else, pull SKU (don't use the parameter in the SELECT).
Try This:
SELECT
SKU,
0 AS Quantity,
MIN(ISNULL(Sending.IsActive, 'WEBNO')) AS IsActive,
MAX(ISNULL(Sending.IsDiscontinued, 1)) AS IsDiscontinued
FROM
(
SELECT [eCommerce].[dbo].[Split] (Feed.StyleCode,'_') as SKU
FROM [eCommerce].[dbo].[BABW_ItemFeed] as Feed
WHERE Feed.ProductDefinition = 'Kit'
) AS SplitSkus
LEFT JOIN #SkusToSend AS Sending
ON Sending.SKU = SplitSkus.items
The following query is constantly timing out, is there a less overhead way to achieve the same function ?
UPDATE Invoices SET ispaid = 0
WHERE Invoice_number IN (SELECT invoice_number
FROM payment_allocation
WHERE transactionID=305)
What I'm doing is unallocating invoices from a transaction, there can be up to 30+ records returned but it stops the database dead everytime I try to run it
USE JOIN instead of subquery it will improve the performance.
Create index on Invoice_number column in both table if you haven't created.
Try this:
UPDATE Invoices i
INNER JOIN payment_allocation pa ON i.Invoice_number = pa.invoice_number
SET i.ispaid = 0
WHERE pa.transactionID = 305;
I'd try EXISTS :
UPDATE Invoices a set ispaid=0
WHERE EXISTS
(
SELECT NULL FROM payment_allocation b
WHERE b.Invoice_number =a.Invoice_number AND b.transactionID=305
)
As of MySQL 5.5, Subquery Selects (another full select statement inside the query) cannot be optimized. This is probably why your query is so slow. Refactor you query to get rid of the inner select statement.
UPDATE Invoices, payment_allocation
SET ispaid=0
WHERE payment_allocation.transactionID=305 AND
Invoices.Invoice_number = payment_allocation.invoice_number
An interesting sidenote... But MariaDB (a branch of MySQL by the original creator) has implemented Subquery select optimization.
UPDATE invoices i
JOIN payment_allocation pa
ON pa.invoice_number = i.invoice_number
SET i.ispaid=0
WHERE pa.transactionID = 305;