Query now extremely slow after server change - sql-server

After changing servers, we are now experiencing extremely slow execution times for just one query. When we analyze the sessions during the runtime, we see hundreds (sometimes over 1000) open sessions all with the same session id and they are blocking themselves. Here is an extract:
+----------------------+------------+-----------------+------------------+-----------+--------------------+-----------------------+---------------------+--------------------------+--------------------------------------------------------------------+
| waiting_task_address | session_id | exec_context_id | wait_duration_ms | wait_type | resource_address | blocking_task_address | blocking_session_id | blocking_exec_context_id | resource_description |
+----------------------+------------+-----------------+------------------+-----------+--------------------+-----------------------+---------------------+--------------------------+--------------------------------------------------------------------+
| 0x00000005A3B83468 | 161 | 19 | 121058 | CXPACKET | 0x0000000EB9B9C830 | 0x00000010BF029C28 | 161 | 3 | exchangeEvent id=Pipe8a2d88200 WaitType=e_waitPipeNewRow nodeId=12 |
| 0x00000010BE003C28 | 161 | 10 | 121079 | CXPACKET | 0x00000008A1A0E9C0 | 0x00000005734964E8 | 161 | 93 | exchangeEvent id=Pipe79fcc6200 WaitType=e_waitPipeNewRow nodeId=8 |
| 0x000000050BFA1088 | 161 | 42 | 121092 | CXPACKET | 0x000000058C7C12D0 | 0x00000010B484A8C8 | 161 | 27 | exchangeEvent id=Pipe5647e2110 WaitType=e_waitPipeNewRow nodeId=15 |
| 0x0000000DFB199088 | 161 | 20 | 121094 | CXPACKET | 0x0000000E77A4DCC0 | 0x00000005A3B82CA8 | 161 | 44 | exchangeEvent id=Pipe915578ed0 WaitType=e_waitPipeGetRow nodeId=15 |
| 0x0000000E501A64E8 | 161 | 66 | 121094 | CXPACKET | 0x000000088DB9DCB0 | 0x0000000E44591C28 | 161 | 81 | exchangeEvent id=Pipe79fcc8d00 WaitType=e_waitPipeGetRow nodeId=5 |
| 0x0000000E501A64E8 | 161 | 66 | 121094 | CXPACKET | 0x000000088DB9DCB0 | 0x00000003714868C8 | 161 | 82 | exchangeEvent id=Pipe79fcc8d00 WaitType=e_waitPipeGetRow nodeId=5 |
| 0x0000000E501A64E8 | 161 | 66 | 121094 | CXPACKET | 0x000000088DB9DCB0 | 0x0000000DC854B848 | 161 | 83 | exchangeEvent id=Pipe79fcc8d00 WaitType=e_waitPipeGetRow nodeId=5 |
| 0x0000000E501A64E8 | 161 | 66 | 121094 | CXPACKET | 0x000000088DB9DCB0 | 0x00000010B3A25848 | 161 | 84 | exchangeEvent id=Pipe79fcc8d00 WaitType=e_waitPipeGetRow nodeId=5 |
| 0x0000000E501A64E8 | 161 | 66 | 121094 | CXPACKET | 0x000000088DB9DCB0 | 0x0000000F39DFA4E8 | 161 | 85 | exchangeEvent id=Pipe79fcc8d00 WaitType=e_waitPipeGetRow nodeId=5 |
+----------------------+------------+-----------------+------------------+-----------+--------------------+-----------------------+---------------------+--------------------------+--------------------------------------------------------------------+
I am not sure what causes this. The problem didn't exist before moving the server. We had a short period of time during which the query executed fast on the current server and now have the problem of slow execution times again.

Have you compared execution plans? If you have upgraded SQL to a newer version have you updated statistics? If these are different servers with the same database is the data the same? Is the indexing the same? I used to run a replication topology and would delete unused indexes on subscribers.
The self blocking isn't really blocking. I think they introduced it in SQL 2005 and it's just slowness.

Related

Building index for specific value

I have a table that keeps inventory information for products in stores on daily basis. It is like:
|------------|-----------|---------|-----------------|
| Date | ProductId | StoreId | InventoryOnHand |
|------------|-----------|---------|-----------------|
| 2017-10-11 | 348 | 121 | 2 |
| 2017-10-11 | 110 | 200 | 0 |
| 2017-10-11 | 254 | 587 | -2 |
| 2017-10-12 | 311 | 875 | 26 |
| 2017-10-12 | 954 | 364 | 15 |
| 2017-10-12 | 348 | 121 | 0 |
| 2017-10-12 | 441 | 121 | 7 |
| . | . | . | . |
| . | . | . | . |
| . | . | . | . |
|------------|-----------|---------|-----------------|
In most queries I used have condition like WHERE InventoryOnHand > 0. I need to speed up these queries.
Therefore, I want to build and index that separates values on column InventoryOnHand whether they are greater than 0 or not.
Filtered Index does not solve my problem because if I use filtered index all values greater than 0 will be indexed and this increases index size. I only need to know if a value greater than 0 or not.
i.e. I want to build an index that only works when condition is InventoryOnHand > 0. Is there any way to do this on SQL-Server?

Powerpivot 2016 measure using DAX to sum an array

I want to sum the 7 preceding values of a row as a measure like so:
| Wk_number | Value A | Measure | Array |
-------------------------------------------
| 01 | 1 | N/A# | N/A# |
| 02 | 1 | 1 | {01} |
| 03 | 10 | 2 | {01-02} |
| 04 | 3 | 12 | {01-03} |
| 05 | 5 | 15 | {01-04} |
| 06 | 10 | 20 | {01-05} |
| 07 | 1 | 30 | {01-06} |
| 08 | 4 | 31 | {01-07} |
| 09 | -10 | 34 | {02-08} |
| 10 | 3 | 26 | {03-09} |
| 11 | 2 | 18 | {04-10} |
etc...
I added the array column just to clarify the example how of the sum is comprised, notice that from wk09 it's not simply a running total.
How to do this using DAX statements?
Two ways to do this: you can either create a calculated column, or a measure.
For the column:
=CALCULATE(SUM([Value A]),FILTER(Table,Table[Wk_number]<EARLIER(Table[Wk_number]) && Table[Wk_number] >= (EARLIER(Table[Wk_number])-7)))
For the measure, it's a very similar formula but instead of using EARLIER(), we use MAX():
=CALCULATE(SUM([Value A]),FILTER(ALL(Table3),Table3[Wk_number]<MAX(Table3[Wk_number]) && Table3[Wk_number] >= (MAX(Table3[Wk_number])-7)))
Below is a screenshot of the results. A few of the numbers in your example table seem to be off based on the math:

Windows 10 all icon resolutions on all DPI settings? Format? Pixel art as icon? Large size icon in start menu medium tile?

Just skip to the answer at the answer section, the question part has speculations and mistakes. The answer is based on experiment and it is accurate.
For a long time I have used a single png packed 256px ico file for my Visual Studio projects, and it worked well, cause scaling works fine on those icons and they take almost no space, and I did not care that much before.
But now I have an icon that needs to "retain hard edges", it is "pixel art".
If I build with a 256px version downscales terribly or if I build with 16px upscales slightly less terribly but not good enough. So my questions are:
What are the sizes I have to generate to merge into the ico (I don't care about pre win7 icons)?
If I got that list, do I have to generate 1.25x, 1.5x and 2.0x versions for high dpi settings?
Finally, some apps like firefox has a large icon on start menu inside the medium square block, my apps have a smaller one in the center like Visual Studio does, how can I put a large icon in the start menu medium sized square?
I did found it: Unfortunately VS says: VisualElements is not supported in a Windows Presentation Foundation (WPF) project. Is there a way around this?
I looked around and collected possible sizes #1x(96DPI):
16, 20, 24, 30, 32, 40, 48, 50, 64, 128, 150, 256, 512, 768
Mostly I collected these from Which icon sizes should my Windows application's icon include?.
Not that bad, but if I add 1.25x, 1.5x, 2.0x then we get:
16, 20, 24, 25, 30, 32, 36, 38, 40, 45, 48, 50, 60, 62, 64, 72, 75, 80, 96, 100, 128, 150, 160, 188, 192, 225, 256, 300, 320, 384, 512, 640, 768, 960, 1024, 1152, 1536 In my case it makes a 500k ico file and as it seems the 1024 is the max resolution you can put into an ico file, my icon is pixelated so compresses really well with png and still ~500k.
I also made an ico has all of the above resolutions in rgba, and every one has its size on it, so you can see which windows loads on which dpi setting. you can download it from here and use it in a vs project to test.
As I understand everything under 256px can't be png compressed, is this right?
And do I need all these sizes to retain a pixel perfect icon? Are only 32bit(RGBA) pngs okay? I hope I don't need to include other depths.
After i wrote an ico writer from the spec I realized that pngs can have 0 for resolution cause 1 byte is available for x or y(but i have never seen an ico that is non square, maybe curs can be non square), in this case probably the first one in the file with zeros for resolution going to be used...This is not sure but I think its not far from the truth. See images. Rescaling issues are still confusing me, if I'll have some time tomorrow I'll test it. A pixel perfect icon seems to be imposible to do: having one image to rescale to anything above 256px.
The answers:
Are PNGs are acceptable under 256px inside ICO file?
Microsoft states that the sizes under 256px should be a BMP without the first 14 bytes.
But at least in the case of windows 10, the answer is YES.
Can you add larger than 1024px image into the ICO?
YES. As long as it is a PNG it can be as big as you want.
Note the "one icon over 255px" limitation:
You can only add one image larger than 255px ( you can add more but windows will only read the first image block in the ICO head where the resolution is 0,0). The format specifies 1 byte for each dimension. See table #2.
What sizes to include for all DPI setting?
Windows 10 uses the following icon sizes (see table below):
16, 20, 24, 28, 30, 31, 32, 40, 42, 47, 48, 56, 60, 63, 84 and one larger than 255px.
Note that Windows RT apps do not use ICO files, they use PNGs or Fonts, this is from Firefox's source:
<Application xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<VisualElements
ShowNameOnSquare150x150Logo='on'
Square150x150Logo='browser\VisualElements\VisualElements_150.png'
Square70x70Logo='browser\VisualElements\VisualElements_70.png'
ForegroundText='light'
BackgroundColor='#0996f8'/>
</Application>
So how Firefox has a large icon on start menu in the medium tile?
Well its include this above file in the exe directory before the shortcut added to start menu, this article explains it how. My app has an example.
If you make icons for Windows 10 you are better off with my tool and photoshop (or something like that) than anything else, I tried editors and they suck.
win10iconTools by me
You can create ICO files (as MS recommendation or not) or create icons with resolution printed on them, the latter is what I used to make the table.
Supports multiple resize modes including nearest neighbor.
If you want to you can use it on other windows versions, it works with .net2, test another windows, send me the results and I extend the table for the good of mankind.
As for pixel art in icons it is not possible to be perfect :(, unless we can change the scaling algorithm in windows 10.
So a wide range 84-256 gets the scaled to "0" (see above), so there is no real point adding something bigger that 256 as I see it now you should create a 256px image for "0".
(See table why)
If Windows gets all icons 16 to 255 + the 1 larger than 255 (0 in the table) chooses these sizes:
(so no markdown tables here?, its kind of wide, the tables are in the app readme also)
| | Windows 10 |
| | 96DPI | 120DPI | 144DPI | 168DPI |
| icon |disp.|load|disp.|load|disp.|load|disp.|load|
|-------------------------------------------|-----|----|-----|----|-----|----|-----|----|
| alt-tab | 24 | 32 | 30 | 32 | 36 | 32 | 42 | 32 |
| desktop large | 96 | 0 | 120 | 0 | 144 | 0 | 168 | 0 |
| desktop medium | 48 | 48 | 60 | 60 | 72 | 72 | 84 | 84 |
| desktop small | 32 | 32 | 40 | 40 | 48 | 48 | 56 | 56 |
| explorer content, inc0 | 32 | 32 | 40 | 40 | 48 | 48 | 56 | 56 |
| explorer extra large | 256 | 0 | 256 | 0 | 256 | 0 | 256 | 0 |
| explorer large | 96 | 0 | 120 | 0 | 144 | 0 | 168 | 0 |
| explorer medium | 48 | 48 | 60 | 60 | 72 | 72 | 84 | 84 |
| explorer small:inc4,list:inc3,details:inc2| 16 | 16 | 20 | 20 | 24 | 24 | 28 | 28 |
| explorer tiles, inc1 | 48 | 48 | 60 | 60 | 72 | 72 | 84 | 84 |
| startmenu medium | 32 | 32 | 40 | 40 | 48 | 48 | 56 | 63 |
| startmenu programs | 24 | 24 | 30 | 30 | 36 | 36 | 42 | 42 |
| startmenu search | 32 | 60 | 40 | 60 | 48 | 60 | 56 | 0 |
| startmenu tile small | 24 | 24 | 30 | 31 | 36 | 39 | 42 | 47 |
| taskbar normal | 24 | 32 | 30 | 40 | 36 | 48 | 42 | 56 |
| taskbar small | 16 | 16 | 20 | 20 | 24 | 24 | 28 | 28 |
| window icon | 16 | 16 | 20 | 16 | 24 | 16 | 28 | 16 |
| desktop inc0 | 16 | 16 | 20 | 20 | 24 | 24 | 28 | 28 |
| desktop inc1 | 18 | 32 | 23 | 40 | 27 | 48 | 32 | 56 |
| desktop inc2 | 20 | 30 | 25 | 40 | 30 | 48 | 35 | 56 |
| desktop inc3 | 22 | 32 | 28 | 40 | 33 | 48 | 39 | 56 |
| desktop inc4 | 24 | 32 | 30 | 40 | 36 | 48 | 42 | 56 |
| desktop inc5 | 27 | 32 | 34 | 40 | 41 | 48 | 47 | 56 |
| desktop inc6 | 30 | 32 | 38 | 40 | 45 | 48 | 53 | 56 |
| desktop inc7 | 33 | 48 | 41 | 60 | 50 | 72 | 58 | 84 |
| desktop inc8 | 37 | 48 | 46 | 60 | 56 | 72 | 65 | 84 |
| desktop inc9 | 41 | 48 | 51 | 60 | 62 | 72 | 72 | 84 |
| desktop inc10 | 46 | 48 | 58 | 60 | 69 | 72 | 82 | 84 |
| desktop inc11 | 51 | 0 | 64 | 0 | 77 | 0 | 89 | 0 |
| desktop inc12 | 57 | 0 | 71 | 0 | 86 | 0 | 100 | 0 |
| desktop inc13 | 63 | 0 | 79 | 0 | 95 | 0 | 110 | 0 |
| desktop inc14 | 70 | 0 | 88 | 0 | 105 | 0 | 123 | 0 |
| desktop inc15 | 78 | 0 | 98 | 0 | 117 | 0 | 137 | 0 |
| desktop inc16 | 87 | 0 | 109 | 0 | 131 | 0 | 152 | 0 |
| desktop inc17 | 97 | 0 | 121 | 0 | 146 | 0 | 170 | 0 |
| desktop inc18 | 108 | 0 | 135 | 0 | 162 | 0 | 189 | 0 |
| desktop inc19 | 120 | 0 | 150 | 0 | 180 | 0 | 210 | 0 |
| desktop inc20 | 133 | 0 | 166 | 0 | 200 | 0 | 233 | 0 |
| desktop inc21 | 148 | 0 | 185 | 0 | 222 | 0 | 256 | 0 |
| desktop inc22 | 164 | 0 | 205 | 0 | 246 | 0 | 256 | 0 |
| desktop inc23 | 182 | 0 | 228 | 0 | 256 | 0 | 256 | 0 |
| desktop inc24 | 202 | 0 | 253 | 0 | 256 | 0 | 256 | 0 |
| desktop inc25 | 224 | 0 | 256 | 0 | 256 | 0 | 256 | 0 |
| desktop inc26 | 249 | 0 | 256 | 0 | 256 | 0 | 256 | 0 |
| explorer inc5 | 18 | 32 | 23 | | | | | |
| explorer inc6 | 20 | 32 | 25 | | | | | |
| explorer inc7 | 22 | 32 | 28 | | | | | |
| explorer inc8 | 23 | 32 | 29 | | | | | |
| explorer inc9 | 25 | 32 | 31 | | | | | |
| explorer inc10 | 27 | 32 | 34 | | | | | |
| explorer inc11 | 29 | 32 | 36 | | | | | |
| explorer inc12 | 31 | 32 | 39 | | | | | |
| explorer inc13 | 33 | 48 | 41 | | | | | |
| explorer inc14 | 35 | 48 | 44 | | | | | |
| explorer inc15 | 38 | 48 | 48 | | | | | |
| explorer inc16 | 41 | 48 | 51 | | | | | |
| explorer inc17 | 44 | 48 | 55 | | | | | |
| explorer inc18 | 47 | 48 | 59 | | | | | |
| explorer inc19 | 50 | 0 | 63 | | | | | |
| explorer inc20 | 54 | 0 | 68 | | | | | |
| explorer inc44 | 239 | 0 | 256 | 0 | | | | |
| explorer inc45 | 256 | 0 | 256 | 0 | | | | |
There are 27 zoom increments on desktop
and 45 zoom increments on explorer (including the defaults from the menu on the "bottom level")
Icon format specification:
|**block** |**offset** |**offset** |**length** |**description** |
|-----------|-----------|-----------|-----------|-------------------------------|
|main header| 0 | | 2 |Reserved=0 |
| | 2 | | 2 |Image type: 1(.ICO) 2(.CUR) |
| | 4 | | 2 |Number of images in container |
|image head1| 6 | 0 | 1 |Pixel width |
| | 7 | 1 | 1 |Pixel height |
| | 8 | 2 | 1 |Color palette size or 0 |
| | 9 | 3 | 1 |Reserved=0 |
| | A | 4 | 2 |Color planes=0 or 1 |
| | C | 6 | 2 |Bits per Pixel |
| | E | 8 | 4 |Image raw size |
| | 12 | C | 4 |Offset of imageblock from BOF |
|image head2| 16 | 0 | 1 |Pixel width |
| ... | ... | ... | ... |... |
|imageblock1| ... | ... | ... |all image data goes here: |
| | ... | ... | ... | pngs included in whole |
| | ... | ... | ... | bmps missing first 14 bytes |
The icons you see in Windows 10 are packed into a icon font named Segoe MDL2 Assets & all built-in UWP apps like Groove Music are using this font for icons. Also some apps in the Windows store using it.

Multiple outcomes/scenarios

I got a problem that I have already created a solution for, but I'm wondering if there's a better way of solving the problem. Basically I have to create a flag for certain scenarios under a partition of ID and date. My solution involved mapping for all the possible scenarios, then creating "case when" statements for all these scenarios with the specific outcome. Basically, I was the one that created the outcomes. I am wondering if there's another way, something around letting SQL create the outcomes instead of myself.
Thanks a lot!
Background:
+----+-----------+--------+-------+------+-----------------+-----------------------------------------------------------------------------------+
| ID | Month | Status | Value | Flag | Scenario Number | Scenario Description |
+----+-----------+--------+-------+------+-----------------+-----------------------------------------------------------------------------------+
| 1 | 1/01/2016 | First | 123 | No | 1 | First, second and blank exists. Do not flag |
| 1 | 1/01/2016 | Second | 456 | No | 1 | First, second and blank exists. Do not flag |
| 1 | 1/01/2016 | | 789 | No | 1 | First, second and blank exists. Do not flag |
| 1 | 1/02/2016 | Second | 123 | Yes | 2 | First does not exist, two second but have different values. Flag these as Yes |
| 1 | 1/02/2016 | Second | 456 | Yes | 2 | First does not exist, two second but have different values. Flag these as Yes |
| 1 | 1/02/2016 | Second | 123 | No | 3 | First does not exist, two second have same values. Do not flag |
| 1 | 1/02/2016 | Second | 123 | No | 3 | First does not exist, two second have same values. Do not flag |
| 1 | 1/03/2016 | Second | 123 | No | 4 | Only one entry of Second exist. Do no flag |
| 1 | 1/04/2016 | | 123 | Yes | 5 | Two blanks for the partition. Flag these as Yes |
| 1 | 1/04/2016 | | 123 | Yes | 5 | Two blanks for the partition. Flag these as Yes |
| 1 | 1/05/2016 | | | No | 6 | Only one entry of blank exist. Do not flag these |
| 1 | 1/06/2016 | First | 123 | Yes | 7 | First exist for the partition. Do not flag |
| 1 | 1/06/2016 | | 456 | Yes | 7 | First exist for the partition. Do not flag |
| 1 | 1/07/2016 | Second | 123 | Yes | 8 | First does not exist and second and blank do not have the same value. Flag these. |
| 1 | 1/07/2016 | | 456 | Yes | 8 | First does not exist and second and blank do not have the same value. Flag these. |
| 1 | 1/07/2016 | Second | 123 | Yes | 8 | First does not exist and second and blank have the same value. Flag these. |
| 1 | 1/07/2016 | | 123 | Yes | 8 | First does not exist and second and blank have the same value. Flag these. |
+----+-----------+--------+-------+------+-----------------+-----------------------------------------------------------------------------------+
Data:
+----+-----------+-------+----------+---------------+
| ID | Month | Value | Priority | Expected_Flag |
+----+-----------+-------+----------+---------------+
| 1 | 1/01/2016 | 96.01 | | Yes |
| 1 | 1/01/2016 | 96.01 | | Yes |
| 1 | 1/02/2016 | 65.2 | First | No |
| 1 | 1/02/2016 | 3.47 | Second | No |
| 1 | 1/02/2016 | 45.99 | | No |
| 11 | 1/01/2016 | 25 | | No |
| 11 | 1/02/2016 | 74.25 | Second | No |
| 11 | 1/02/2016 | 74.25 | Second | No |
| 11 | 1/02/2016 | 23.25 | | No |
| 24 | 1/01/2016 | 1.25 | First | No |
| 24 | 1/01/2016 | 1.365 | | No |
| 24 | 1/04/2016 | 1.365 | First | No |
| 24 | 1/04/2016 | 1.365 | | No |
| 24 | 1/05/2016 | 1.365 | First | No |
| 24 | 1/05/2016 | 1.365 | First | No |
| 24 | 1/06/2016 | 1.365 | Second | No |
| 24 | 1/06/2016 | 1.365 | Second | No |
| 24 | 1/07/2016 | 1.365 | Second | Yes |
| 24 | 1/07/2016 | 1.365 | | Yes |
| 24 | 1/08/2016 | 1.365 | First | No |
| 24 | 1/08/2016 | 1.365 | | No |
| 24 | 1/09/2016 | 1.365 | Second | No |
| 24 | 1/09/2016 | 1.365 | | No |
| 27 | 1/01/2016 | 0 | Second | Yes |
| 27 | 1/01/2016 | 0 | Second | Yes |
| 27 | 1/02/2016 | 45.25 | Second | No |
| 3 | 1/01/2016 | 96.01 | First | No |
| 3 | 1/01/2016 | 96.01 | First | No |
| 3 | 1/03/2016 | 96.01 | First | No |
| 3 | 1/03/2016 | 96.01 | First | No |
| 35 | 1/01/2016 | | | Yes |
| 35 | 1/01/2016 | | | Yes |
| 35 | 1/02/2016 | | First | No |
| 35 | 1/02/2016 | | Second | No |
| 35 | 1/02/2016 | | | No |
| 35 | 1/02/2016 | | | No |
| 35 | 1/03/2016 | | Second | Yes |
| 35 | 1/03/2016 | | Second | Yes |
| 35 | 1/04/2016 | | Second | No |
| 35 | 1/04/2016 | | Second | No |
+----+-----------+-------+----------+---------------+

How to make a SQL "IF-THEN-ELSE" statement

I've seen other questions about SQL If-then-else stuff, but I'm not seeing how to relate it to what I'm trying to do. I've been using SQL for about a year now but only basic stuff and never this.
If I have a SQL table that looks like this
| Name | Version | Category | Value | Number |
|:-----:|:-------:|:--------:|:-----:|:------:|
| File1 | 1.0 | Time | 123 | 1 |
| File1 | 1.0 | Size | 456 | 1 |
| File1 | 1.0 | Final | 789 | 1 |
| File2 | 1.0 | Time | 312 | 1 |
| File2 | 1.0 | Size | 645 | 1 |
| File2 | 1.0 | Final | 978 | 1 |
| File3 | 1.0 | Time | 741 | 1 |
| File3 | 1.0 | Size | 852 | 1 |
| File3 | 1.0 | Final | 963 | 1 |
| File1 | 1.1 | Time | 369 | 2 |
| File1 | 1.1 | Size | 258 | 2 |
| File1 | 1.1 | Final | 147 | 2 |
| File2 | 1.1 | Time | 741 | 2 |
| File2 | 1.1 | Size | 734 | 2 |
| File2 | 1.1 | Final | 942 | 2 |
| File3 | 1.1 | Time | 997 | 2 |
| File3 | 1.1 | Size | 997 | 2 |
| File3 | 1.1 | Final | 985 | 2 |
How can I write a SQL IF, ELSE statement that creates a new column called "Replication" that follows this rule:
A = B + 1 when x = 1
else
A = B
where A = the number we will use for the next Number
B = Max(Number)
x = Replication count (this is the number of times that a loop is executed. x=i)
The results table will look like this:
| Name | Version | Category | Value | Number | Replication |
|:-----:|:-------:|:--------:|:-----:|:------:|:-----------:|
| File1 | 1.0 | Time | 123 | 1 | 1 |
| File1 | 1.0 | Size | 456 | 1 | 1 |
| File1 | 1.0 | Final | 789 | 1 | 1 |
| File2 | 1.0 | Time | 312 | 1 | 1 |
| File2 | 1.0 | Size | 645 | 1 | 1 |
| File2 | 1.0 | Final | 978 | 1 | 1 |
| File1 | 1.0 | Time | 369 | 1 | 2 |
| File1 | 1.0 | Size | 258 | 1 | 2 |
| File1 | 1.0 | Final | 147 | 1 | 2 |
| File2 | 1.0 | Time | 741 | 1 | 2 |
| File2 | 1.0 | Size | 734 | 1 | 2 |
| File2 | 1.0 | Final | 942 | 1 | 2 |
| File1 | 1.1 | Time | 997 | 2 | 1 |
| File1 | 1.1 | Size | 997 | 2 | 1 |
| File1 | 1.1 | Final | 985 | 2 | 1 |
| File2 | 1.1 | Time | 438 | 2 | 1 |
| File2 | 1.1 | Size | 735 | 2 | 1 |
| File2 | 1.1 | Final | 768 | 2 | 1 |
| File1 | 1.1 | Time | 786 | 2 | 2 |
| File1 | 1.1 | Size | 486 | 2 | 2 |
| File1 | 1.1 | Final | 135 | 2 | 2 |
| File2 | 1.1 | Time | 379 | 2 | 2 |
| File2 | 1.1 | Size | 943 | 2 | 2 |
| File2 | 1.1 | Final | 735 | 2 | 2 |
EDIT: Based on the answer by Sean Lange, this is my 2nd attempt at a solution:
SELECT COALESCE(MAX)(Number) + CASE WHEN Replication = 1 then 1 else 0, 1) FROM Table
The COALESCE is in there for when there is no value yet in the Number column.
The IF/Else construct is used to control flow of statements in t-sql. You want a case expression, which is used to conditionally return values in a column.
https://msdn.microsoft.com/en-us/library/ms181765.aspx
Yours would be something like:
case when x = 1 then A else B end as A
As SeanLange pointed out in this case it would be better to use an CASE/WHEN but to illustrate how to use If\ELSE the way to do it in sql is like this:
if x = 1
BEGIN
---Do something
END
ELSE
BEGIN
--Do something else
END
I would say the best way to know the difference and when to use which is if you are writing a query and want a different field to appear based on a certain condition, use case/when. If a certain condition will cause a series of steps to happen then use if/else

Resources