Change value into int - sql

So my question is: I run a SQL Query and my result is:
week sale sale override sell through(%)
Week-1 42 29 3.94804504619449
Week-2 46 36 3.39242402149418
Week-3 53 44 3.91839149445099
Week-4 44 33 3.53663152826439
Week-5 45 20 4.12465416879239
Week-6 45 24 3.9861284151902
Week-7 47 10 3.93148317015786
Week-8 27 14 4.96932263953541
Week-9 49 18 3.6518835739424
Week-10 56 35 3.54296186103223
Week-11 44 23 3.42675072960917
Week-12 42 28 3.73042394308072
So I want now change sell through(%) into this:
week sale sale override sell through(%)
Week-1 42 29 39
Week-2 46 36 33
Week-3 53 44 39
Week-4 44 33 35
Week-5 45 20 41
Week-6 45 24 39
Week-7 47 10 39
Week-8 27 14 49
Week-9 49 18 36
Week-10 56 35 35
Week-11 44 23 34
Week-12 42 28 37
Is there a way to change value?

Based on the data you have shown, it looks like you need to wrap the generation of sell through(%) in FLOOR(10.0 *...) e.g. if your existing query was used as a subquery you could write:
SELECT [week], [sale], [sale override],
FLOOR(10.0 * [sell through(%)]) AS [sell through(%)]
FROM (
-- your existing query
) d

You can try something like this in your query:
declare #float float = '3.94804504619449'
select cast(#float * 10 as int)

Related

How can I translate this nested query into R dplyr?

I'm a newbie in R and I'm trying to translate the following nested query using dplyr:
SELECT * FROM DAT
where concat(code, datcomp) IN
(SELECT concat(code, max(datcomp)) from DAT group by code)
DAT is a data frame containing several hundreds columns.
code is a not-unique numeric field
datcomp is a string like 'YYYY-MM-DDTHH24:MI:SS'
What I'm trying to do is extracting from data frame the most recent timestamp for each code.
Eg: given
code datcomp
1 1005 2019-06-12T09:13:47
2 1005 2019-06-19T16:15:46
3 1005 2019-06-17T21:46:02
4 1005 2019-06-17T17:52:01
5 1005 2019-06-24T13:10:05
6 1015 2019-05-02T10:33:13
7 1030 2019-06-11T14:58:16
8 1030 2019-06-20T09:50:20
9 2008 2019-05-17T18:43:34
10 2008 2019-05-28T15:16:50
11 3030 2019-05-24T09:51:30
12 3032 2019-05-30T16:40:03
13 3032 2019-05-21T09:34:27
14 3062 2019-05-17T16:10:53
15 3062 2019-06-20T16:45:51
16 3069 2019-07-01T17:54:59
17 3069 2019-07-09T12:39:56
18 3069 2019-07-09T17:45:09
19 3069 2019-07-17T14:31:01
20 3069 2019-06-24T13:42:27
21 3104 2019-06-05T14:47:38
22 3104 2019-05-17T15:18:47
23 3111 2019-06-06T15:52:51
24 3111 2019-07-01T09:50:33
25 3127 2019-04-16T16:04:59
26 3127 2019-05-15T11:49:29
27 3249 2019-06-21T18:24:14
28 3296 2019-07-01T17:44:54
29 3311 2019-06-10T11:05:20
30 3311 2019-06-21T12:11:05
31 3311 2019-06-19T11:36:47
32 3332 2019-05-13T09:38:21
33 3440 2019-06-11T12:53:07
34 3440 2019-05-17T17:40:19
35 3493 2019-04-18T11:18:37
36 5034 2019-06-06T15:24:04
37 5034 2019-05-31T11:39:17
38 5216 2019-05-20T17:16:07
39 5216 2019-05-14T15:08:15
40 5385 2019-05-17T13:19:54
41 5387 2019-05-13T09:33:31
42 5387 2019-05-07T10:49:14
43 5387 2019-05-15T10:38:25
44 5696 2019-06-10T16:16:49
45 5696 2019-06-11T14:47:00
46 5696 2019-06-13T17:10:36
47 6085 2019-05-21T10:15:58
48 6085 2019-06-03T11:22:34
49 6085 2019-05-29T11:25:37
50 6085 2019-05-31T12:52:42
51 6175 2019-05-13T17:17:48
52 6175 2019-05-27T09:58:04
53 6175 2019-05-23T10:32:21
54 6230 2019-06-21T14:28:11
55 6230 2019-06-11T16:00:48
56 6270 2019-05-28T08:57:38
57 6270 2019-05-17T16:17:04
58 10631 2019-05-22T09:46:51
59 10631 2019-07-03T10:41:41
60 10631 2019-06-06T11:52:42
What I need is
code datcomp
1 1005 2019-06-24T13:10:05
2 1015 2019-05-02T10:33:13
3 1030 2019-06-20T09:50:20
4 2008 2019-05-28T15:16:50
5 3030 2019-05-24T09:51:30
6 3032 2019-05-30T16:40:03
7 3062 2019-06-20T16:45:51
8 3069 2019-07-17T14:31:01
9 3104 2019-06-05T14:47:38
10 3111 2019-07-01T09:50:33
11 3127 2019-05-15T11:49:29
12 3249 2019-06-21T18:24:14
13 3296 2019-07-01T17:44:54
14 3311 2019-06-21T12:11:05
15 3332 2019-05-13T09:38:21
16 3440 2019-06-11T12:53:07
17 3493 2019-04-18T11:18:37
18 5034 2019-06-06T15:24:04
19 5216 2019-05-20T17:16:07
20 5385 2019-05-17T13:19:54
21 5387 2019-05-15T10:38:25
22 5696 2019-06-13T17:10:36
23 6085 2019-06-03T11:22:34
24 6175 2019-05-27T09:58:04
25 6230 2019-06-21T14:28:11
26 6270 2019-05-28T08:57:38
27 10631 2019-07-03T10:41:41
Thank you in advance.
a more generalized version - group, then sort so that you get whatever you want first, then slice (which would allow you to take the nth value from each group as sorted):
dati %>%
group_by(code) %>%
arrange(desc(datcomp)) %>%
slice(1) %>%
ungroup()

How to divide a result set into equal parts?

I have a table new_table
ID PROC_ID DEP_ID OLD_STAFF NEW_STAFF
1 15 43 58 ?
2 19 43 58 ?
3 29 43 58 ?
4 31 43 58 ?
5 35 43 58 ?
6 37 43 58 ?
7 38 43 58 ?
8 39 43 58 ?
9 58 43 58 ?
10 79 43 58 ?
How I can select all proc_ids and update new_staff, for example
ID PROC_ID DEP_ID OLD_STAFF NEW_STAFF
1 15 43 58 15
2 19 43 58 15
3 29 43 58 15
4 31 43 58 15
5 35 43 58 23
6 37 43 58 23
7 38 43 58 23
8 39 43 58 28
9 58 43 58 28
10 79 43 58 28
15 - 4(proc_id)
23 - 3(proc_id)
28 - 3(proc_id)
58 - is busi
where 15, 23, 28 and 58 staffs in one dep
"how to divide equal parts"
Oracle has a function, ntile() which splits a result set into equal buckets. For instance this query puts your posted data into four buckets:
SQL> select id
2 , proc_id
3 , ntile(4) over (order by id asc) as gen_staff
4 from new_table;
ID PROC_ID GEN_STAFF
---------- ---------- ----------
1 15 1
2 19 1
3 29 1
4 31 2
5 35 2
6 37 2
7 38 3
8 39 3
9 58 4
10 79 4
10 rows selected.
SQL>
This isn't quite the solution you want but you need to clarify your requirements before it's possible to provide a complete answer.
update new_table
set new_staff='15'
where ID in('1','2','3','4')
update new_table
set new_staff='28'
where ID in('8','9','10')
update new_table
set new_staff='23'
where ID in('5','6','7')
Not sure if this is what you mean.

Remove all instances of a string 7 characters long in a textbox in VB

I have two text boxes. The first contains this text just like shown.
I need to remove the first 7 characters of each row then show the edited text in the second box.
The first number is different every time so I can't use this
RawText.Text = Replace(RawText.Text, "1757792", " ")
TextFilter.Text = RawText.Text
because the number changes every row.
Is there a way to have a button remove ALL instances of ANY text 7 characters long?
1757792 02 08 09 10 15 21 22 29 34 40 44 46 47 48 53 56 58 68 69 71
1757793 01 07 16 20 22 25 30 36 38 39 42 48 49 51 58 66 70 72 79 80
1757794 01 02 07 09 10 18 29 32 35 36 48 53 54 56 62 65 68 69 71 73
1757795 01 02 06 09 12 18 23 27 30 35 43 52 57 59 60 61 62 73 74 76
1757796 01 11 13 14 18 19 22 31 34 41 45 46 54 57 61 70 71 72 79 80
1757797 01 08 10 18 19 21 32 41 43 44 45 54 61 62 64 66 68 73 74 80
1757798 02 03 06 09 10 23 27 28 33 36 38 41 49 53 60 61 64 73 74 80
1757799 02 12 16 34 36 44 51 52 55 57 58 59 64 71 73 75 76 78 79 80
1757800 05 11 13 17 18 19 23 24 27 31 34 38 39 45 48 61 67 73 79 80
1757801 17 23 29 31 35 38 43 45 48 51 56 57 60 64 65 66 67 73 77 78
1757802 05 06 11 14 17 20 21 27 28 29 33 41 45 49 58 66 67 73 79 80
1757803 06 07 10 11 12 19 20 21 25 30 33 35 38 42 46 51 65 66 75 80
1757804 06 14 16 19 20 23 32 42 43 44 48 52 62 67 68 69 71 72 74 78
You can use string methods like Substring. If you really want to remove the first 7 you can use String.Substring:
Dim txt2Lines = From l In RawText.Lines
Let index = Math.Min(l.Length, 7)
Select l.Substring(index)
txt2.Lines = txt2Lines.ToArray()
This handles also the case that there are also shorter lines.
Note that it doesn't remove the leading space since that is not part of the first seven characters. You could use l.Substring(index).TrimStart().
Another approach is to search the first space and remove everything before that:
Dim txt2Lines = From l In RawText.Lines
Let index = Math.Max(l.IndexOf(" "), 0)
Select l.Substring(index)
txt2.Lines = txt2Lines.ToArray()
String.IndexOf returns -1 if the substring wasn't found, that's why i've used Math.Max(l.IndexOf(" "), 0). In that case the full line should be taken.
You could use String.Split to split the text at the vbCrLf (line break), then use String.SubString to select the string parter starting at index 8, and there you are.
And as GSerg pointed out, if you would like to replace all 7 digit occurences try this:
Dim ResultString As String
Try
ResultString = Regex.Replace(SubjectString, "\d{7}", "", RegexOptions.Singleline)
Catch ex As ArgumentException
'Syntax error in the regular expression
End Try

Repeating the format specifiers in awk

I am trying to format the output of the AWK's printf() function. More precisely, I am trying to print a matrix with very long rows and I would like to wrap them and continue on the next line. What I am trying to do is best illustrated using Fortran. Consider the following Fortran statement:
write(*,'(10I5)')(i,i=1,100)
The output would be the integers in the range 1:100 printed in rows of 10 elements.
Is it possible to do the same in AWK. I could do that by offsetting the index and printing to new line with "\n". The question is whether that can be done in an elegant manner as in Fortran.
Thanks,
As suggested in the comments I would like to explain my Fortran code, given as an example above.
(i,i=1,100) ! => is a do loop going from 1 to 100
write(*,'(10I5)') ! => is a formatted write statement
10I5 says print 10 integers and for each integer allocate 5 character slot
The trick is, that when one exceeds the 10 x 5 character slots given by the formatted write, one jumps on the next line. So one doesn't need the trailing "\n".
This may help you
[akshay#localhost tmp]$ cat test.for
implicit none
integer i
write(*,'(10I5)')(i,i=1,100)
end
[akshay#localhost tmp]$ gfortran test.for
[akshay#localhost tmp]$ ./a.out
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
[akshay#localhost tmp]$ awk 'BEGIN{for(i=1;i<=100;i++)printf("%5d%s",i,i%10?"":"\n")}'
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100

Top % by Group in Access

I want to preface this by saying I use the front end of access and not sql.
I have a set of data that I'm trying to do a scorecard for. I have Region, Store, SKU, SKU Count. I need to give a score of 6 to SKU's within the to 15% of their region, a score of 3 for 50-85%, and zero for anything below 50% of the region.
This is my data....I cannot figure out how to do this...am I missing a piece of data to do it?
R Store SKU SKU Cnt
24 4969 14260 -942
24 7660 14162 -54
24 78 12486 -174
24 4881 14160 -376
24 78 14161 -162
24 4905 14161 -1179
24 4901 14162 -114
24 4941 14160 -312
24 4815 17912 544
24 4926 18974 112
24 4845 14160 -160
24 4828 14162 -142
28 3004 14261 -3064
28 4813 70339 -229
28 4941 14161 -382
28 4905 12486 -161
28 4907 17912 -1150
28 4907 12486 152
28 4884 14160 -62
28 7217 14261 420
28 4845 14161 -1123
28 4853 20692 -50
28 4899 20692 40
28 4872 14261 -225
32 78 17470 993
32 4956 13978 -374
32 4948 70330 -2454
32 4815 14162 167
32 4972 14161 -242
32 4372 14261 -3613
32 5413 20692 50
32 7155 64673 60
32 78 17471 484
32 4870 20713 47
32 4926 17472 -310
32 4844 12486 -66
32 4863 17471 517