SQL Indicate Completion once 2 matching result queries are matched - sql

I have 2 queries. I basically need to work out from the 2 queries when the all threads have completed processing and alert in a form of Print Complete. To do this - If the current Processing query matches total put through query output number, All threads are completed in that batch. I need to create a stored proc that would PRINT Complete. What would be the best way of doing this?
--How Many To Process - Which is put through first (but not processed)
SELECT COUNT(IsProcessed) as 'Current Put through'
FROM [dbo].[threads]
-- Current Processed -- This incrementally goes up by the Isprocessed flag changing 1 by 1 to the value of 1
SELECT COUNT(IsProcessed) 'Current_Processing'
FROM [dbo].[threads] as count
where IsProcessed=1

Can you do something like the following?
IF NOT EXISTS (SELECT 1 FROM dbo.Threads WHERE IsProcessed != 1)
BEGIN
PRINT 'Complete'
END
ELSE
BEGIN
PRINT 'Ongoing'
END

select *,
case when cntAll = cntProcessed then 'Complete' else 'Ongoing' end as Status
from (
select count(case when IsProcessed = 1 then 1 else NULL end) Current_Processing,
count(1) 'Current Put through'
from [dbo].[threads])A

Related

Max match same numbers from each row

To generate 1mln rows of report with the below mentioned script is taking almost 2 days so, really appreciate if somebody could help me with different script which the report can be generated within 10-15mins please.
The requirement of the report is as following;
Table “cover” contains 5mln rows & 6 columns of data and likewise table “data” contains 500,000 rows and 6 columns.
So, each numbers of the rows in table cover has to go through table date and provide the maximum matches.
For instance, as mentioned on the below tables, there could be 3 matches in row #1, 2 matches in row #2 and 5 matches in row #3 so the script has to select the max selection which is 5 in row #3.
Sample table
UPDATE public.cover_sheet AS fc
SET maxmatch = (SELECT MAX(tmp.mtch)
FROM (
SELECT (SELECT CASE WHEN fc.a=drwo.a THEN 1 ELSE 0 END) +
(SELECT CASE WHEN fc.b=drwo.b THEN 1 ELSE 0 END) +
(SELECT CASE WHEN fc.c=drwo.c THEN 1 ELSE 0 END) +
(SELECT CASE WHEN fc.d=drwo.d THEN 1 ELSE 0 END) +
(SELECT CASE WHEN fc.e=drwo.e THEN 1 ELSE 0 END) +
(SELECT CASE WHEN fc.f=drwo.f THEN 1 ELSE 0 END) AS mtch
FROM public.data AS drwo
) AS tmp)
WHERE fc.code>0;
SELECT *
FROM public.cover_sheet AS fc
WHERE fc.maxmatch>0;
As #a_horse_with_no_name mentioned in the comment to the question, your question is not clear...
Seems, you want to get the number of records which 6 fields from both tables are equal.
I'd suggest to:
reduce the number of select statements, then the speed of query execution will increase,
split your query into few smaller ones (good practice), to check your logic,
use join to get equal data, see: Visual Representation of SQL Joins
use subquery or cte to get result on which you'll be able to update table.
I think you want to get result as follow:
SELECT COUNT(*) mtch
FROM public.cover_sheet AS fc INNER JOIN public.data AS drwo ON
fc.a=drwo.a AND fc.b=drwo.b AND fc.c=drwo.c AND fc.d=drwo.d AND fc.e=drwo.e AND fc.f=drwo.f
If i'm not wrong and above query is correct, the time of execution of above query will reduce to about 1-2 minutes.
Finally, update query may look like:
WITH qry AS
(
-- proper select statement here
)
UPDATE public.cover_sheet AS fc
SET maxmatch = qry.<fieldname>
FROM qry
WHERE fc.code>0 AND fc.<key> = qry.<key>;
Note:
I do not see your data and i know nothing about its structure, relationships, etc. So, you have to change above query to your needs.

SET with case when SQL

I'm trying to update the status column of this table and I used the following:
UPDATE table_a
SET status = CASE
WHEN test_group LIKE 'Control' THEN 0
WHEN test_group NOT LIKE 'Control' THEN 1
END;
But it doesn't appear to have worked, it actually just gave every observation a 1 for status. I just want the observations that have the word "control" in them to take a 0 for status and observations that don't have the word "control" in them to take a 1. The default was not a 0 or 1 for this column.
You don't need the second condition. But you do need wildcards for the LIKE. I would suggest:
UPDATE table_a
SET status = (CASE WHEN test_group LIKE '%Control%' THEN 0 ELSE 1 END);
In many databases, you can simplify this to:
UPDATE table_a
SET status = (test_group NOT LIKE '%Control%');
Try this (T-SQL):
UPDATE table_a
SET status = CASE
WHEN charindex('Control', test_group)>0 THEN 0
ELSE 1
END;
UPDATE table_a
SET status = CASE
WHEN test_group LIKE '%Control%' THEN 0
WHEN test_group NOT LIKE '%Control%' THEN 1
END;

MS SQL UPDATE-SET 'Case' different results to SELECT 'Case'

I have an update statement which is designed to manage "jobs". I process one job first whilst the others wait in line until the first one is complete. After which they will be sent to process (far faster as I can leverage calcs from the first run).
To quote my own comment:
-- Set job status to 2 for where there is saved results file
-- Where there is no saved results file:
-- --Set job status to 2 for one instance of each unique param combination
-- --Set job status to 8 for all others
PseudoCode:
UPDATE #Jobs
SET [JobStatusId] = CASE
WHEN ISNULL([PreCalculated].[FileCount], 0) > 0 THEN 2
WHEN ISNULL([PreCalculated].[FileCount], 0) = 0 AND [GroupedOrder] = 1 THEN 2
ELSE 8 END
FROM #NewJobsGrouped [NewJobsGrouped]
LEFT JOIN (
SELECT COUNT([Id]) [FileCount],
[ResultsFile].[ParamsId]
FROM #ResultsFile [ResultsFile]
WHERE [ResultsFile].[IsActive] = 1
GROUP BY [ResultsFile].[ParamsId]
) [PreCalculated]
ON [PreCalculated].[ParamsId] = [NewJobsGrouped].[ParamsId]
where #NewJobsGrouped looks like:
Job ID || GroupedOrder || ParamsId
1460 1 807
1461 2 807
1462 3 807
This does not work. Every job is being set to status 2. However:
SELECT CASE
WHEN ISNULL([PreCalculated].[FileCount], 0) > 0 THEN 2
WHEN ISNULL([PreCalculated].[FileCount], 0) = 0 AND [GroupedOrder] = 1 THEN 2
ELSE 8 END [JobStatusId]
etc
Works exactly as I am expecting.
Why would these two case statements give different results? Is there something obvious I am missing? I honestly can't explain what I'm seeing and whilst I can probably use another temp table to hold the output from the select and have a simpler update - but I'd like to understand what's going on?

Check Multiple Rows for a Value

I need to check the column IsSubmitted in multiple rows in a table for a a certain user id.
If any of the rows has a 1 for is submitted I return 0 else I return another value.
How do I check the results of this query to see if any of the rows has a 1 for IsSubmitted if it returns multiple rows?
SELECT IsSubmitted FROM [Application] WHERE ID = #id
EXAMPLE this query may return
IsSubmitted
0
0
0
1
0
or
0
0
0
0
it could be any number of rows or only one row. I need to know if any of them contain a 1. We have an application where they only have to pay one time and they can submit as many apps as they want, so I need to check and see if they have already paid(submitted).
If they have submitted then i need to take one action, if they haven't then i need to take another action.
SELECT CASE
WHEN EXISTS(SELECT * FROM Application WHERE ID=#id AND IsSubmitted=1) THEN 0
ELSE 1 --or some other value
END
This can be inserted into the context of a larger query if needed.

CASE statement for checking a field value

Hopefully you guy's can help.
I am writing a query from a table which has variable data specifically a completed column which can have a value of 1 or 3 the table also has two qty_ fields QTYORDERED and QTYSHIPPED
If the value is 3 in COMPLETED then QTYSHIPPED will contain a value and if the value is 1 the QTYORDERED will have a value.
What I need to do is within my query create one column which just has a QTY can someone show me how to achieve this in SQL Server
This is a simple case statement:
select (case when completed = 1 then QTYORDERED
when completed = 3 then QTYSHIPPED
end) as QTY
Note this will return NULL when completed has any other value.
You can also write this as:
select (case completed when 1 then QTYORDERED
when 3 then QTYSHIPPED
end)
However, the first form is more general, giving you more flexibility for complicated logic.
SELECT
CASE completed
WHEN 1 THEN QTYORDERED
WHEN 3 THEN QTYSHIPPED
ELSE 0 --Or add any logic when something goes wrong with "completed" value
END as Quantity
FROM ....