Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql General

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-09-2008, 11:10 PM
Tom Hart
 
Posts: n/a
Default convert access sql to postgresql

Hey guys. I have a long piece of sql that I'm trying to take out of an
existing Access db and modify to work with Postgresql. I've started
trying to convert it, but I've come across a problem that I don't even
know how to describe, let alone google. Here's the function

INSERT INTO MemberMailingAddress (
mb_cn_num,
mb_acct_num,
isactive,
ismember,
ismail,
ispromomail,
.... <trimmed for your convenience> ...

)
SELECT
mb_cn_num,
mb_acct_num,
mb_stat_cd<>1 as isactive,
mb_stat_cd=0 as ismember,
isactive and (mb_mail_cd=0 or mb_mail_cd=1) as ismail,
ismail and (mb_stat_cd=0 or mb_stat_cd=2) as ispromomail,
.... <trimmed for your convenience> ...

FROM member
ORDER BY mb_cn_num, mb_acct_num
;

Specifically I'm looking at these two lines

isactive and (mb_mail_cd=0 or mb_mail_cd=1) as ismail,
ismail and (mb_stat_cd=0 or mb_stat_cd=2) as ispromomail,

which appear to use other fields it's preparing to insert as variables
in the determination of the values of other fields (I told you I
couldn't figure out how to explain it).

Does anybody have any idea what I'm talking about? I sure don't.

Thomas R. Hart II
tomhart@coopfed.org

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-09-2008, 11:11 PM
Peter Eisentraut
 
Posts: n/a
Default Re: convert access sql to postgresql

Tom Hart wrote:
> Specifically I'm looking at these two lines
>
> isactive and (mb_mail_cd=0 or mb_mail_cd=1) as ismail,
> ismail and (mb_stat_cd=0 or mb_stat_cd=2) as ispromomail,
>
> which appear to use other fields it's preparing to insert as variables
> in the determination of the values of other fields (I told you I
> couldn't figure out how to explain it).


I can't tell more without the exact table definitions, but this should work
just fine.

--
Peter Eisentraut
http://developer.postgresql.org/~petere/

---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-09-2008, 11:11 PM
Tom Hart
 
Posts: n/a
Default Re: convert access sql to postgresql

Peter Eisentraut wrote:
> Tom Hart wrote:
>
>> Specifically I'm looking at these two lines
>>
>> isactive and (mb_mail_cd=0 or mb_mail_cd=1) as ismail,
>> ismail and (mb_stat_cd=0 or mb_stat_cd=2) as ispromomail,
>>
>> which appear to use other fields it's preparing to insert as variables
>> in the determination of the values of other fields (I told you I
>> couldn't figure out how to explain it).
>>

>
> I can't tell more without the exact table definitions, but this should work
> just fine.
>

The error I'm receiving is

ERROR: column "isactive" does not exist
LINE 26: isactive and (mb_mail_cd=0 or mb_mail_cd=1) as ismail,
^

Here are the table definitions for member (from) and
membermailingaddress (to)

current=# \d member
Table "public.member"
Column | Type | Modifiers
-----------------------+---------------+-----------
mb_acct_closed_dt | date |
mb_acct_num | numeric(6,2) | not null
mb_alt_num | text |
mb_backwithhold_perc | integer |
mb_birth_dt | date |
mb_cell_phone | text |
mb_census_tract | numeric(6,2) |
mb_city | text |
mb_cn_num | integer | not null
mb_credit_score | text |
mb_credit_score_date | date |
mb_ctr_cd | text |
mb_drivers_license | text |
mb_email_acct_num | numeric(6,2) |
mb_email_address | text |
mb_email_cn_num | integer |
mb_email_rec | integer |
mb_employ_phone | text |
mb_employer | text |
mb_first_name | text |
mb_flag4 | text |
mb_frst_99r_rec | integer |
mb_frst_at_rec | integer |
mb_frst_cl_rec | integer |
mb_frst_ct_rec | integer |
mb_frst_df_rec | integer |
mb_frst_ira_rec | integer |
mb_frst_ln_rec | integer |
mb_frst_sh_rec | integer |
mb_frst_tran_rec | integer |
mb_head_of_household | text |
mb_hire_dt | date |
mb_home_phone | text |
mb_homebank_status | text |
mb_income | numeric(10,2) |
mb_jand/or_cd | text |
mb_jfirst_name | text |
mb_jlast_name | text |
mb_jmid_init | text |
mb_join_dt | date |
mb_joint_birth_dt | date |
mb_joint_relationship | text |
mb_jssn_num | text |
mb_jsuff | text |
mb_last_name | text |
mb_life_ins_amt | numeric(10,2) |
mb_life_save_amt | numeric(10,2) |
mb_loc_cd | text |
mb_lst_act_dt | date |
mb_lst_tran_cd | text |
mb_lst_tran_rec | text |
mb_m/f_cd | text |
mb_mail_addr1 | text |
mb_mail_addr2 | text |
mb_mail_cd | integer |
mb_mail_city | text |
mb_mail_state | text |
mb_mail_zip_cd | text |
mb_master_rec | integer |
mb_mid_init | text |
mb_misc_cd | text |
mb_ofac_verified | text |
mb_payroll_num | integer |
mb_privacy_code | integer |
mb_prn_state | text |
mb_send_estatement | text |
mb_soc_emp_cd | integer |
mb_soc_sec_num | text |
mb_stat_cd | integer |
mb_state_cd | text |
mb_statement_pages | text |
mb_str_addr1 | text |
mb_str_addr2 | text |
mb_suff | text |
mb_tot_payroll | numeric(10,2) |
mb_vru_status | integer |
mb_w9_ver_cd | integer |
mb_withold | text |
mb_zip_cd | text |
Indexes:
"member_pkey" PRIMARY KEY, btree (mb_cn_num, mb_acct_num)

current=# \d membermailingaddress
Table "public.membermailingaddress"
Column | Type | Modifiers
-----------------+--------------+-----------
mb_cn_num | integer |
mb_acct_num | numeric(6,1) |
isactive | boolean |
ismember | boolean |
ismail | boolean |
ispromomail | boolean |
householdid | numeric |
householdsize | integer |
name1 | text |
name2 | text |
addr1 | text |
addr2 | text |
addr3 | text |
city | text |
state | text |
zip | text |
zip5 | text |
xxprimaryname | text |
xxjointname | text |
xxhouseholdhash | text |

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-09-2008, 11:11 PM
Tom Hart
 
Posts: n/a
Default Re: convert access sql to postgresql

Tom Hart wrote:
> Peter Eisentraut wrote:
>> Tom Hart wrote:
>>
>>> Specifically I'm looking at these two lines
>>>
>>> isactive and (mb_mail_cd=0 or mb_mail_cd=1) as ismail,
>>> ismail and (mb_stat_cd=0 or mb_stat_cd=2) as ispromomail,
>>>
>>> which appear to use other fields it's preparing to insert as variables
>>> in the determination of the values of other fields (I told you I
>>> couldn't figure out how to explain it).
>>>

>>
>> I can't tell more without the exact table definitions, but this
>> should work just fine.
>>

> The error I'm receiving is
>
> ERROR: column "isactive" does not exist
> LINE 26: isactive and (mb_mail_cd=0 or mb_mail_cd=1) as ismail,
> ^

I've been doing some googling on sql aliases (my sql knowledge is far
from impressive) and it appears that column aliases can be great for
displaying different column names in your output. However I was unable
to find any information regarding using column aliases as variables,
like in the code above. I'm not sure why this works in access, but does
postgreSQL support using a column alias like a variable within the query
that the alias was defined in?

I suppose the lines

mb_stat_cd<>1 as isactive,
mb_stat_cd=0 as ismember,
(isactive and (mb_mail_cd=0 or mb_mail_cd=1)) as ismail,
(ismail and (mb_stat_cd=0 or mb_stat_cd=2)) as ispromomail

Could be written

mb_stat_cd<>1 as isactive,
mb_stat_cd=0 as ismember,
((mb_stat_cd<>1) and (mb_mail_cd=0 or mb_mail_cd=1)) as ismail,
(((mb_stat_cd<>1) and (mb_mail_cd=0 or mb_mail_cd=1)) and (mb_stat_cd=0
or mb_stat_cd=2)) as ispromomail

But if postgreSQL does indeed support this, and I can get it to work,
I'd like to be able to use the syntax in the first set.

TIA

Thomas R. Hart II
tomhart@coopfed.org

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-09-2008, 11:11 PM
Martijn van Oosterhout
 
Posts: n/a
Default Re: convert access sql to postgresql

On Fri, Nov 16, 2007 at 05:43:44PM -0500, Tom Hart wrote:
> I've been doing some googling on sql aliases (my sql knowledge is far
> from impressive) and it appears that column aliases can be great for
> displaying different column names in your output. However I was unable
> to find any information regarding using column aliases as variables,
> like in the code above. I'm not sure why this works in access, but does
> postgreSQL support using a column alias like a variable within the query
> that the alias was defined in?


IIRC this is not mentioned in the SQL standard. Not sure why, maybe the
theory is that all output columns are calculated simultenously which
can't be true if they refer to eachother.

Have a nice day,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/
> Those who make peaceful revolution impossible will make violent revolution inevitable.
> -- John F Kennedy


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFHPiP5IB7bNG8LQkwRApsJAJ9MVYDqmWt4crhkeha8ze FhcmIongCgj3Os
rjAhbjhVq+48z+1XS6Mny+A=
=zyL2
-----END PGP SIGNATURE-----

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-09-2008, 11:11 PM
A.M.
 
Posts: n/a
Default Re: convert access sql to postgresql


On Nov 16, 2007, at 5:43 PM, Tom Hart wrote:

> Tom Hart wrote:
>> Peter Eisentraut wrote:
>>> Tom Hart wrote:
>>>
>>>> Specifically I'm looking at these two lines
>>>>
>>>> isactive and (mb_mail_cd=0 or mb_mail_cd=1) as ismail,
>>>> ismail and (mb_stat_cd=0 or mb_stat_cd=2) as ispromomail,
>>>>
>>>> which appear to use other fields it's preparing to insert as
>>>> variables
>>>> in the determination of the values of other fields (I told you I
>>>> couldn't figure out how to explain it).
>>>>
>>>
>>> I can't tell more without the exact table definitions, but this
>>> should work just fine.
>>>

>> The error I'm receiving is
>>
>> ERROR: column "isactive" does not exist
>> LINE 26: isactive and (mb_mail_cd=0 or mb_mail_cd=1) as ismail,
>> ^

> I've been doing some googling on sql aliases (my sql knowledge is
> far from impressive) and it appears that column aliases can be
> great for displaying different column names in your output. However
> I was unable to find any information regarding using column aliases
> as variables, like in the code above. I'm not sure why this works
> in access, but does postgreSQL support using a column alias like a
> variable within the query that the alias was defined in?


Isn't the issue that you are not matching the case of the column? I'm
guessing you used some kind of Access dump and loaded it into
postgresql. If you must have capitalization in the column, you need
to quote all references to the column.

Here is the relevant FAQ:
http://www.postgresql.org/docs/faqs.FAQ.html#item4.21

Cheers,
M

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org/

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-09-2008, 11:11 PM
Shane Ambler
 
Posts: n/a
Default Re: convert access sql to postgresql

Tom Hart wrote:
> Hey guys. I have a long piece of sql that I'm trying to take out of an
> existing Access db and modify to work with Postgresql. I've started
> trying to convert it, but I've come across a problem that I don't even
> know how to describe, let alone google. Here's the function
>
> INSERT INTO MemberMailingAddress (
> mb_cn_num,
> mb_acct_num,
> isactive,
> ismember,
> ismail,
> ispromomail,
> ... <trimmed for your convenience> ...
>
> )
> SELECT
> mb_cn_num,
> mb_acct_num,
> mb_stat_cd<>1 as isactive,
> mb_stat_cd=0 as ismember,
> isactive and (mb_mail_cd=0 or mb_mail_cd=1) as ismail,
> ismail and (mb_stat_cd=0 or mb_stat_cd=2) as ispromomail,
> ... <trimmed for your convenience> ...
>
> FROM member
> ORDER BY mb_cn_num, mb_acct_num
> ;
>
> Specifically I'm looking at these two lines
>
> isactive and (mb_mail_cd=0 or mb_mail_cd=1) as ismail,
> ismail and (mb_stat_cd=0 or mb_stat_cd=2) as ispromomail,
>


Lets's clarify something
- the insert puts data into the MemberMailingAddress table which
includes the column isactive

The select retrieves and calculates data from the member table.

The select calculates a value for the third column that you alias to be
named isactive.

Are you expecting the 5th and 6th column (as ismail - as ispromomail) to
calculate from the 3rd and 5th column of the select or from the columns
of MemberMailingAddress?

If you expect the later you need to add a join to the
MemberMailingAddress table to get those columns. (or use a sub-select to
get the data)

If you are only calculating from the member table then you will need to
repeat the calculations instead of referring to the alias. And if this
is the case how does the insert fit with the issue of the select?



--

Shane Ambler
pgSQL@Sheeky.Biz

Get Sheeky @ http://Sheeky.Biz

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-09-2008, 11:11 PM
Tom Hart
 
Posts: n/a
Default Re: convert access sql to postgresql

Shane Ambler wrote:
> Tom Hart wrote:
>> Hey guys. I have a long piece of sql that I'm trying to take out of
>> an existing Access db and modify to work with Postgresql. I've
>> started trying to convert it, but I've come across a problem that I
>> don't even know how to describe, let alone google. Here's the function
>>
>> INSERT INTO MemberMailingAddress (
>> mb_cn_num,
>> mb_acct_num,
>> isactive,
>> ismember,
>> ismail,
>> ispromomail,
>> ... <trimmed for your convenience> ...
>>
>> )
>> SELECT
>> mb_cn_num,
>> mb_acct_num,
>> mb_stat_cd<>1 as isactive,
>> mb_stat_cd=0 as ismember,
>> isactive and (mb_mail_cd=0 or mb_mail_cd=1) as ismail,
>> ismail and (mb_stat_cd=0 or mb_stat_cd=2) as ispromomail,
>> ... <trimmed for your convenience> ...
>>
>> FROM member
>> ORDER BY mb_cn_num, mb_acct_num
>> ;
>>
>> Specifically I'm looking at these two lines
>>
>> isactive and (mb_mail_cd=0 or mb_mail_cd=1) as ismail,
>> ismail and (mb_stat_cd=0 or mb_stat_cd=2) as ispromomail,
>>

> Lets's clarify something
> - the insert puts data into the MemberMailingAddress table which
> includes the column isactive
> The select retrieves and calculates data from the member table.
> The select calculates a value for the third column that you alias to
> be named isactive.

That's correct.
> Are you expecting the 5th and 6th column (as ismail - as ispromomail)
> to calculate from the 3rd and 5th column of the select or from the
> columns of MemberMailingAddress?

It's not that I expect pgSQL to do this, as much as I've seen access
behave this way, in which column aliases were able to be used within the
same select query in a "variable" type fashion.
> If you expect the later you need to add a join to the
> MemberMailingAddress table to get those columns. (or use a sub-select
> to get the data)
> If you are only calculating from the member table then you will need
> to repeat the calculations instead of referring to the alias. And if
> this is the case how does the insert fit with the issue of the select?

I figured I would have to repeat the calculations (I've tried this with
a small chunk of the sql with success). What I was really looking for
was an answer on whether postgreSQL behaved this way, or if this was
standard SQL. The answer I've been hearing is no on both counts, just
wanted to be sure. Thank you for your reply.

Thomas R. Hart II
tomhart@coopfed.org

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:10 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
UnixAdminTalk.com

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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639