Nesting a Case statement inside a sum - SQL Server - sql-server

Having trouble with this :
I have the following query which gives me exactly the view I need.
SELECT t.Week,
SUM(t.Hours) AS AH,
SUM(c.Input) AS input,
SUM(c.[recruitedPos]) AS recruitedPos,
(SUM(t.Hours) - SUM(c.Input)) AS Possible
FROM tempPOC AS t
LEFT JOIN (SELECT Week,
SUM(input) AS Input,
SUM([Normal Weekly Hours]) AS recruitedPos,
Shop
FROM colData
GROUP BY week,
Shop) AS c ON t.Week = c.Week
AND c.Shop = t.Store
GROUP BY t.Week,
t.Store
ORDER BY t.Week;
What I'm having trouble with is writing a case statement to set any values from possible alias to 0 if they are lower than 0.
I thought this would be as simple as
(CASE(SUM(t.Hours - c.Hours)) < 0 then 0 else Possible end as Possible)
but this is giving me error
Msg 156, Level 15, State 1, Line 9
Incorrect syntax near the keyword 'as'.
Input :
Week, AH, Input, RecruitedPos, Possible
1, 15, 25, 13, -10
1, 30, 15, 15, 15
expected output :
Week, AH, Input, RecruitedPos, Possible
1, 15, 25, 13, 0
1, 30, 15, 15, 15

CASE
WHEN SUM(t.Hours) - SUM(c.Input) < 0 THEN 0
ELSE SUM(t.Hours) - SUM(c.Input)
END AS Possible

Related

How to query lines between two point in Postgis

I have 2 table: Point and Line
I want to query all line between 2 point A and B. (Line 112, 113, 114)

Last WHEN expression overrides result of first WHEN expression in CASE WHEN in SQL SELECT statement

This looks so familiar to most of in this community as it is all about SQL CASE expression. But I am going now in triage mode rather doing actual implementation. I appreciate if there is optimal way to work around this.
SCENARIO:
I have one select statement where in I retrieve multiple columns from a table. The table has columns mostly with numeric(10, 3) datatype. I chose this datatype as I thought if I need to display int value the conversion would be easier rather vice versa. Here is the table structure.
Name: FleetRange
Columns:
CaptionID INT NOT NULL
Caption NVARCHAR(50)
FleetRange_1 numeric(10,3)
FleetRange_2_4 numeric(10,3)
.......
Total numeric(10,3)
Critera:
Current result:
My SQL query:
SELECT
CaptionId, Caption,
CASE
WHEN CaptionId IN (1, 2, 4, 5, 6, 7, 8, 9, 14, 15, 17, 18, 20, 21, 22, 23)
THEN CONVERT(INT, FleetRange_1)
WHEN CaptionId IN (11, 12, 13)
THEN CONVERT(NUMERIC(10, 2), FleetRange_1)
WHEN CaptionId IN (3, 10, 16, 19)
THEN CONVERT(NUMERIC(10, 3), FleetRange_1)
END AS 'FleetRange_1'
FROM
FleetRange
NOTE:
What currently happening is, the last WHEN is overriding previous evaluation and hence every row display values with 3 decimal places even if there is an integer value.
I have applied same case structure for other numeric(10,3), hence I have shortened the query.
instead of case written within above query, I tried below syntax too - but no difference.
WHEN
CaptionId = 11 OR
CaptionId = 12 OR
CaptionId = 13
THEN...
My expectation (desired actual result): my objective is - particular row value should be converted to int numeric given precision if the particular when expression with specific CaptionID is evaluated.
Something like below:
CaptionID | Caption | FleetRange_1 | FleetRange_2_4 | .....
1 | SafetyFirst | 0 | 1 |
11 | DriveSafe | 2.15 | null|
3 | Caution | 1.025 | 2.174|
Every expression in a query has a single data type. The data type of a CASE expression is:
the highest precedence type from the set of types in
result_expressions and the optional else_result_expression.
CASE (TSQL)
And see Data Type Precedence
If you want different display formatting for different rows in a single resultset, you'll have to convert them to strings and Format them yourself.

Run duration sysjobs - sysjobshistory

I have a question regarding the run_duration of sysjobs. Official docs seem to contradict itself:
https://learn.microsoft.com/en-us/sql/relational-databases/system-tables/dbo-sysjobhistory-transact-sql?view=sql-server-2017
First it specifies:
run_duration int Elapsed time in the execution of the job or step in HHMMSS format.
But then it also mentiones a query for a more friendly time format:
SELECT sj.name,
sh.run_date,
sh.step_name,
STUFF(STUFF(RIGHT(REPLICATE('0', 6) + CAST(sh.run_time as varchar(6)), 6), 3, 0, ':'), 6, 0, ':') 'run_time',
STUFF(STUFF(STUFF(RIGHT(REPLICATE('0', 8) + CAST(sh.run_duration as varchar(8)), 8), 3, 0, ':'), 6, 0, ':'), 9, 0, ':') 'run_duration (DD:HH:MM:SS) '
FROM msdb.dbo.sysjobs sj
JOIN msdb.dbo.sysjobhistory sh
ON sj.job_id = sh.job_id
Notice the difference, days are 'suddenly' brought into the picture.
In my real life example, I came across a job that ran really long. The results are as follows:
run_duration (DD:HH:MM:SS) run_duration
01:49:39:39 1493939
So how do I read this? Is this actually 149 hours, 39 minutes and 39 secs?
One day and 49 makes no sense.
Thanks a lot for the feedback!
Normally, if you're using a notation with times, the largest denominator isn't limited; as in that you stop at 24 for hours because that's how many there are in a day. So, in the format HHMMSS, HH can be any value from 0+. the HH isn't limited to 24, as you aren't using a days denominator. Just like if you were counting months you wouldn't stop as 12, even if the difference between 2 dates is 16 months. You're counting in months so why would crossing a year gap stop you.
Like you commented as well 1 day 49 hours literally makes no sense. 1493939 should be read as 149 hours, 39 minutes and 39 seconds.

Understanding input and labels in word2vec (TensorFlow)

I am trying to properly understand the batch_input and batch_labels from the tensorflow "Vector Representations of Words" tutorial.
For instance, my data
1 1 1 1 1 1 1 1 5 251 371 371 1685 ...
... starts with
skip_window = 2 # How many words to consider left and right.
num_skips = 1 # How many times to reuse an input to generate a label.
Then the generated input array is:
bach_input = 1 1 1 1 1 1 5 251 371 ....
This makes sense, starts from after 2 (= window size) and then continuous. The labels:
batch_labels = 1 1 1 1 1 1 251 1 1685 371 589 ...
I don't understand these labels very well. There are supposed to be 4 labels for each input right (window size 2, on each side). But the batch_label variable is the same length.
From the tensorflow tutorial:
The skip-gram model takes two inputs. One is a batch full of integers
representing the source context words, the other is for the target
words.
As per the tutorial, I have declared the two variables as:
batch = np.ndarray(shape=(batch_size), dtype=np.int32)
labels = np.ndarray(shape=(batch_size, 1), dtype=np.int32)
How should I interpret the batch_labels?
There are supposed to be 4 labels for each input right (window size 2, on each side). But the batch_label variable is the same length.
The key setting is num_skips = 1. This value defines the number of (input, label) tuples each word generates. See the examples with different num_skips below (my data sequence seems to be different from yours, sorry about that).
Example #1 - num_skips=4
batch, labels = generate_batch(batch_size=8, num_skips=4, skip_window=2)
It generates 4 labels for each word, i.e. uses the whole context; since batch_size=8 only 2 words are processed in this batch (12 and 6), the rest will go into the next batch:
data = [5239, 3084, 12, 6, 195, 2, 3137, 46, 59, 156, 128, 742, 477, 10572, ...]
batch = [12 12 12 12 6 6 6 6]
labels = [[6 3084 5239 195 195 3084 12 2]]
Example #2 - num_skips=2
batch, labels = generate_batch(batch_size=8, num_skips=2, skip_window=2)
Here you would expect each word appear twice in the batch sequence; the 2 labels are randomly sampled from 4 possible words:
data = [5239, 3084, 12, 6, 195, 2, 3137, 46, 59, 156, 128, 742, 477, 10572, ...]
batch = [ 12 12 6 6 195 195 2 2]
labels = [[ 195 3084 12 195 3137 12 46 195]]
Example #3 - num_skips=1
batch, labels = generate_batch(batch_size=8, num_skips=1, skip_window=2)
Finally, this setting, same as yours, produces exactly one label per each word; each label is drawn randomly from the 4-word context:
data = [5239, 3084, 12, 6, 195, 2, 3137, 46, 59, 156, 128, 742, 477, 10572, ...]
batch = [ 12 6 195 2 3137 46 59 156]
labels = [[ 6 12 12 195 59 156 46 46]]
How should I interpret the batch_labels?
Each label is the center word to be predicted from the context. But the generated data may take not all (context, center) tuples, depending on the settings of the generator.
Also note that the train_labels tensor is 1-dimensional. Skip-Gram trains the model to predict any context word from the given center word, not all 4 context words at once. This explains why all training pairs (12, 6), (12, 3084), (12, 5239) and (12, 195) are valid.

How are 3-state cellular automata rules generated?

Let's limit the neighborhood to n=1 (which means we always need 3 cells to evaluate the next-gen cell).
Here's an example of a 2 state rule. Note that the upper row of the rules are generated in a particular order, whereas the lower row is the bit representation of the number 30.
I cannot find a single visualization of the equivalent for a 3 state CA. Following the logic of 2 state CA, it should contain 27 possible outcomes, but I have no clue in which order they should be generated. The lower row should be 30 in ternary (with leading zeroes to occupy a total of 27 positions).
Is there a general algorithm for generating these permutations in the conventional order of CAs (regardless of the number of states)?
Thank you very much in advance and sorry if the question is stupid. :(
What you are using is called Wolfram's code (from Stephen Wolfram) that is used for elementary CAs.
If you use more states or bigger neighborhoods then it is sufficient to extend it naturally.
Your question is not stupid.
For three states, this will give you ternary numbers. First write all the three digits number in ternary (descending order):
222, 221, 220, 212, 211, 210, 202, 201, 200, 122, 121, 120, 112, 111, 110, 102, 101, 100, 022, 021, 020, 012, 011, 010, 002, 001, 000
There are 27 of them 3^3, and 222_3 = 26, 221_3 = 25, 001_3 = 1, 000_3 = 0
Now decompose 30 onto base 3 27-digits number: 30 = 1*3^3+ 1*3^1, so there is only two digits equals to 1, the fourth and the second (from the right), here is rule 30 for radius-1 3-states CA:
000000000000000000000001010
This CA has a very different behavior than rule 30 radius-1 2-states CA.
Here is rule 33 for radius-1 3-states (33 = 1*3^3 + 2*3^1):
000000000000000000000001020
So for n,r, enumerate in descending order all 2r+1 digits numbers in base n and associate for each of them a value in [0,n[.

Resources