Postgres Case update syntax error - postgresql

I've done this update script:
UPDATE etude
SET id_enseigne
(CASE WHEN id_enseigne= 1 THEN 6
ELSE CASE WHEN id_enseigne= 1 THEN 6
ELSE CASE WHEN id_enseigne= 2 THEN 26
ELSE CASE WHEN id_enseigne= 3 THEN 2122
ELSE CASE WHEN id_enseigne= 4 THEN 1960
ELSE CASE WHEN id_enseigne= 5 THEN 84
ELSE CASE WHEN id_enseigne= 6 THEN 103
ELSE CASE WHEN id_enseigne= 7 THEN 56
ELSE CASE WHEN id_enseigne= 8 THEN 108
ELSE CASE WHEN id_enseigne= 9 THEN 68
ELSE CASE WHEN id_enseigne= 10 THEN 489
ELSE CASE WHEN id_enseigne= 11 THEN 1124
ELSE CASE WHEN id_enseigne= 13 THEN 502
ELSE CASE WHEN id_enseigne= 14 THEN 1298
ELSE 0 END)
But I get this error, I don't understand why, because it is supposed to be simple:
ERROR: syntax error at or near "("
LINE 3: (CASE WHEN id_enseigne= 1 THEN 6
^
********** Erreur **********
ERROR: syntax error at or near "("
État SQL :42601
Caractère : 30

This other case syntax is more convenient:
update etude
set id_enseigne = case id_enseigne
when 1 then 6
when 2 then 26
when 3 then 2122
when 4 then 1960
when 5 then 84
when 6 then 103
when 7 then 56
when 8 then 108
when 9 then 68
when 10 then 489
when 11 then 1124
when 13 then 502
when 14 then 1298
else 0
end

You're missing the assignment operator. You could keep or lose the parenthesis though.
UPDATE etude
SET id_enseigne = CASE ...
-- Here -----------^

Related

kdb: How to display entire list in console?

How may I display an entire list, or control the number of elements displayed, on the console? I tried show and searched a bit but am unsure where to look next.
q)til 100
0 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 6..
The console height & width is controlled by this: https://code.kx.com/q/basics/syscmds/#c-console-size
However, if you need to escape those boundaries without modifying the settings, you can do something like
q)\c 10 30
q)til 20
0 1 2 3 4 5 6 7 8 9 10 11 1..
q)
q)-1" "sv string til 20;
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
q)
q)// or
q)
q)-1 .Q.s2 enlist til 20;
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
.Q.s2 can be used for other structures but bear in mind, like other 'deeper' .Q functions, .Q.s2 is undocumented and could be subject to change
q)t:flip (20#`abcd)!2?/:20#10
q)t
abcd abcd abcd abcd abcd ab..
---------------------------..
6 3 8 8 8 4 ..
1 2 2 5 7 1 ..
q)
q)-1 .Q.s2 t;
abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd
---------------------------------------------------------------------------------------------------
6 3 8 8 8 4 9 9 3 9 5 0 9 6 5 9 7 3 9 8
1 2 2 5 7 1 6 4 0 2 8 1 5 5 4 1 2 4 4 3
You can adjust the console size like so
q)til 100
0 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..
q)\c 20 50
q)til 100
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ..
q)\c 20 10
q)til 100
0 1 2 3..
More detail on adjusting the console size can be found here https://code.kx.com/q4m3/13_Commands_and_System_Variables/#1322-console-size-c

Split row according to last column values and save them into different files

I have a txt file which have 9 column and the last column is either 0 or 1. I want to save them into different txt file based on the value of last column.How do I do this using matlab?
txt file
43 34 6 8 2 6 2 3 0
23 54 2 5 8 2 61 6 1
46 4 6 8 23 6 2 3 0
3 5 2 75 48 23 1 6 0
3 54 23 5 58 2 1 6 1
46 4 67 8 3 6 24 23 0
32 5 2 75 98 23 1 6 1
I assume you know how to save matrix to files but not the way to split the matrix.
Here I just provide a solution that can help you split the matrix according to the last column:
% ending with 0 in each row
m0 = m(find(m(:,end)==0),:);
>> m0
m0 =
43 34 6 8 2 6 2 3 0
46 4 6 8 23 6 2 3 0
3 5 2 75 48 23 1 6 0
46 4 67 8 3 6 24 23 0
% ending with 1 in each row
m1 = m(find(m(:,end)),:);
>> m1
m1 =
23 54 2 5 8 2 61 6 1
3 54 23 5 58 2 1 6 1
32 5 2 75 98 23 1 6 1
then you can save the matrices m0 and m1 in different .txt files, e.g., dlmwrite('m0.txt',m0) and dlmwrite('m1.txt',m1)
DATA
m = [43 34 6 8 2 6 2 3 0
23 54 2 5 8 2 61 6 1
46 4 6 8 23 6 2 3 0
3 5 2 75 48 23 1 6 0
3 54 23 5 58 2 1 6 1
46 4 67 8 3 6 24 23 0
32 5 2 75 98 23 1 6 1];

Density plot of a matrix

I have a 100x200 matrix and I would like to show this matrix as a density plot. Here is a 8x10 sample.
X = [104 122 138 159 149 167 184 164 190 158; ...
54 42 55 55 63 75 72 73 66 76; ...
15 22 28 21 23 28 32 47 32 40; ...
18 12 20 22 28 17 30 17 22 18; ...
10 7 14 10 14 11 14 20 16 10; ...
5 6 3 3 6 12 6 2 8 9; ...
4 8 9 2 5 3 3 12 7 7; ...
6 6 2 3 10 1 9 8 11 8]
I have tried to use functions like bar3, surf, hist and so on but they don't have the end result I am after.
I would also like to represent the y axis on the new successful plot to be on a log axis. So similar to having semilogy(x,y,'rx') for example.
Are there any other methods I could use?
How about "surf" it like a spectrogram?
XX = log([104 122 138 159 149 167 184 164 190 158;
54 42 55 55 63 75 72 73 66 76;
15 22 28 21 23 28 32 47 32 40;
18 12 20 22 28 17 30 17 22 18;
10 7 14 10 14 11 14 20 16 10;
5 6 3 3 6 12 6 2 8 9;
4 8 9 2 5 3 3 12 7 7;
6 6 2 3 10 1 9 8 11 8]
figure
surf(XX, 'edgecolor', 'none'); view(0,90); axis tight;
xlabel ('x')
ylabel ('y')
NOTE:The first row represent the first row (104,122,138...)
and row 8 represent row 8 (6,7,2....)
Dark red = high value
light blue = low value
Matlab also provides a heatmap function.
>> X = [104 122 138 159 149 167 184 164 190 158; ...
54 42 55 55 63 75 72 73 66 76; ...
15 22 28 21 23 28 32 47 32 40; ...
18 12 20 22 28 17 30 17 22 18; ...
10 7 14 10 14 11 14 20 16 10; ...
5 6 3 3 6 12 6 2 8 9; ...
4 8 9 2 5 3 3 12 7 7; ...
6 6 2 3 10 1 9 8 11 8];
>> heatmap(X)
ans =
HeatmapChart with properties:
ColorData: [8×10 double]
Show all properties
The following plot appears:

Stacked-area with date format at x-axis on Gnuplot

I have created graphs using filledcurves. Now, the graphs looks bad because long range of data.
This is my data:
a b c d e f g h i
201312 49 26 34 30 14 25 9 4 1
201311 38 22 47 30 9 9 4 3 1
201310 44 24 43 38 9 14 5 7 0
201309 65 18 33 39 15 12 4 5 1
201308 42 31 44 30 5 11 0 2 2
201307 58 27 35 29 8 4 2 4 2
201306 30 22 15 17 2 6 3 4 0
201305 61 52 20 16 11 12 2 3 0
201304 62 60 33 18 13 9 5 6 0
201303 43 53 49 27 9 11 7 0 0
201302 31 30 42 27 10 8 4 2 0
201301 42 30 20 47 9 13 3 2 1
201212 26 19 39 24 9 11 0 0 0
201211 26 26 30 28 1 2 0 2 1
201210 55 46 34 30 13 5 0 2 1
201209 56 31 27 28 27 13 2 4 1
201208 48 75 38 46 22 10 0 1 0
201207 60 56 37 47 19 11 2 1 0
201206 60 41 37 28 17 12 5 1 0
201205 49 43 38 46 15 16 2 2 0
201204 43 50 36 33 4 7 3 0 2
201203 49 63 35 43 16 7 1 2 0
201202 43 59 59 52 16 13 3 4 1
201201 51 44 30 37 20 9 4 1 0
201112 50 38 36 36 8 2 3 1 1
201111 75 35 30 36 16 7 3 3 1
201110 68 53 41 27 11 15 1 2 1
201109 68 46 48 47 16 19 4 0 1
201108 45 41 20 36 17 10 1 0 0
201107 48 34 30 24 13 7 3 3 1
201106 49 29 24 25 5 6 0 3 0
201105 45 35 21 37 1 7 2 1 0
201104 53 35 23 18 4 6 1 5 1
201103 58 42 20 18 6 4 1 0 4
201102 54 32 19 20 4 10 0 2 0
201101 42 41 21 28 3 6 1 2 1
and this is my gnuplot file:
set terminal postscript eps color font 20
set xtics 1 out
set tics front
#set style fill transparent solid 0.5 noborder
set key below autotitle columnheader
set ylabel "Count"
set xlabel "across time"
set output 't1.eps'
set title "t1-Across time of Aspects"
set xtics 1
plot for [i=10:2:-1] \
"< awk 'NR==1 {print \"year\",$".(i-1)."} NR>=2 {for (i=2; i<=".i."; i++) \
{sum+= $i} {print $1, sum; sum=0} }' data.dat" \
using (column(2)):xtic(1) with filledcurves x1 t column(2)
When I add time in xdata:
set xdata time
set timefmt "%Y%m"
set xtics format "%b"
Erros message:
Need full using spec for x time data
Is the Errors because of my date format? I have googling this and do not have any answer about it. Please give me suggestion about this.
In the script you show, you specify only a single column in the using statement (besides the xtic). That means, that this value is taken as y-value and the row number is implicitely used as x-value.
When using time data, you must explicitely specify all columns which are needed for the plotting style, there is no assumption about what might be the first column. Use:
set key below autotitle columnheader
set ylabel "Count"
set xlabel "across time"
set tics front
set xdata time
set timefmt "%Y%m"
set xtics format "%b'%y"
set autoscale xfix
plot for [i=10:2:-1] \
"< awk 'NR==1 {print \"year\",$".(i-1)."} NR>=2 {for (i=2; i<=".i."; i++) \
{sum+= $i} {print $1, sum; sum=0} }' data.dat" \
using 1:2 with filledcurves x1 t column(2)
Result with 4.6.4:
I guess you don't want xtic(1) if you have time data and specify the x format.

column Calculation if column > 1in T-SQL

hi i am trying to calculate a value of certain columns together and depending if a column is a certain value for instance
if lens.qty > 1 then (CASE LENS.LNS_PROGTYPE --DESIGN pOINTS
WHEN 762
THEN 70
when 767
THEN 70
when 768
THEN 70
WHEN 841
THEN 35
WHEN 842
then 35
else 0
end +
case LTRIM(RTRIM(LENS.COATTYP)) --ARC POINTS
when 'HVLL'
then 50
when 'HVLLBLUE'
then 100
else 0
end +
CASE LENS.LNS_IDX --MATERIAL POINTS
when 53
THEN 35
WHEN 56
THEN 35
WHEN 58
then 35
when 61
then 35
else 0
END +
CASE LENS.LNS_MATCLR --COLOR POINTS
WHEN 00
THEN 0
WHEN 46
THEN 35
WHEN 47
THEN 35
WHEN 48
then 35
else 0
end as TOTAL_POINTS)*lens.qty / 2
else
CASE LENS.LNS_PROGTYPE --DESIGN pOINTS
WHEN 762
THEN 70
when 767
THEN 70
when 768
THEN 70
WHEN 841
THEN 35
WHEN 842
then 35
else 0
end +
case LTRIM(RTRIM(LENS.COATTYP)) --ARC POINTS
when 'HVLL'
then 50
when 'HVLLBLUE'
then 100
else 0
end +
CASE LENS.LNS_IDX --MATERIAL POINTS
when 53
THEN 35
WHEN 56
THEN 35
WHEN 58
then 35
when 61
then 35
else 0
END +
CASE LENS.LNS_MATCLR --COLOR POINTS
WHEN 00
THEN 0
WHEN 46
THEN 35
WHEN 47
THEN 35
WHEN 48
then 35
else 0
end as TOTAL_POINTS)
i keep getting syntax error and i am not sure where i am going wrong
i am not sure how to do it and to be honest i don't completely understand the examples i have viewed your help would be greatly appreciated
I would do something like:
(CASE
WHEN LENS.LNS_PROGTYPE IN (762,767,768) THEN 70
WHEN LENS.LNS_PROGTYPE IN (841,842) THEN 35
else 0
end +
case LTRIM(RTRIM(LENS.COATTYP)) --ARC POINTS
when 'HVLL' then 50
when 'HVLLBLUE' then 100
else 0
end +
CASE
WHEN LENS.LNS_IDX IN (53,56,58,61) THEN 35
else 0
END +
CASE
WHEN LENS.LNS_MATCLR IN (46,47,48) THEN 35
else 0
end) * CASE WHEN lens.qty > 1 THEN lens.qty / 2 ELSE 1 END
For the entire expression. But, as I said, I'd also introduce some mapping tables rather than having all of these magic constants in the CASE expressions.
you must make sure, that all elements of that string/sum are of the same datatype. cast/convert them appropriately.