« first day (1592 days earlier)      last day (2726 days later) » 

6:50 AM
morning
 
 
1 hour later…
7:54 AM
Hello DB World....
 
hey man
 
Morning all
wow..we're DB world?
 
8:30 AM
hey @Shaneis
 
hey @AndyK
 
how's tricks?
 
hi there
need some help for "functions" with postgresql
 
o/
 
actually I do have something like this, 3 tables : entity(id,name), contact_type(id,name), contact(id,value,type), entity_contact(id,entity,contact)
 
8:35 AM
@AndyK Tricks are good, what's the craic? :)
 
craic is good mate. went to help my gf's mum with her hauling. took 4 hours
 
first will take :
(1, 'Julo0sS'),(2,'Andy K'), ...
Second :
(1, 'Phone'),(2,'Email'), ...
Third :
(1, '052364987','1'),(2,'andykmail@mail.com',2),...
Last :
(1,1,1),(2,2,2),...
I'd like to create a function that gets the contact's for one (or all) entities like this :
entity_id | entity_name | contact_type | contact_value
I'm completely confused with the "joins" I have to make in this query...
 
ok
forget about the function right now
can you retrieve the rows just by doing the query?
(I suppose not, from what you've written but I'm still asking)
 
indeed, no... i'm stucked at "JOIN" statement...
SELECT entity.id AS entity_id, entity.name AS entity_name, contact_type.name AS contact_type, contact.value AS contact
FROM entity
now, left join? right join? inner, outer... so confusing when I have more than 2 tables...
 
@AndyK hauling? as in moving stuff?
@Julo0sS it is at the start
 
8:46 AM
@Julo0sS what are the definitions of your tables?
 
shouldn't take long to get the grasp of it though
 
@Shaneis yep moving stuff
 
@AndyK what do you mean? This is just "basic stuff" from pgsql, id's are serial, (fk's are integers) and the other fields are text...
 
those FK's are helpful sometimes, they normally hint to you on how you should join your tables
 
sorry doing other things in the same time
entity(id,name),
contact_type(id,name),
contact(id,value,type),
entity_contact(id,entity,contact)
 
8:49 AM
CREATE TABLE contact
(
  id serial NOT NULL,
  value text,
  type integer
  CONSTRAINT pk_contact PRIMARY KEY (id),
  CONSTRAINT contact_fk_type FOREIGN KEY (type)
      REFERENCES contact_type (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);
ALTER TABLE contact
  OWNER TO postgres;
you want this?
 
yes
 
ok
 
but you gave it earlier
so we are good
 
ok
 
    SELECT
    entity.id AS entity_id,
    entity.name AS entity_name,
    contact_type.name AS contact_type,
    contact.value AS contact
    FROM entity INNER JOIN contact ON contact.id = entity.id
   INNER JOIN contact_type ON contact_type.id = entity.id
 
8:54 AM
eeehhhh...
entity.id is you, or me. Contact.id is any contact's serial... this cannot match
SELECT
	entity.id AS entity_id,
	entity.name AS entity_name,
	contact_type.name AS contact_type,
	contact.value AS contact
FROM
	entity
INNER JOIN
	entity_contact
ON
	entity_contact.entity = entity.id
INNER JOIN
	contact_type
ON
	contact.type = contact_type.id
This is where I'm confused... This doesnot work because I did not declare "contact" table in my statement, but how should I fix this? add another join? these "join things" are complicated...
 
@Julo0sS did not declare the contact table?
tables are persistent man, you shouldn't need to declare them (unless they are table variables which I don't think Postgres has)
 
well I mean there's no "reference" to contact table in my current query
it returns a 42P01 error on my "contact.type" line
undefined_table
it's like doing :
SELECT name FROM entity INNER JOIN other_table ON mysterious_table.field = other_table.field
 
oh! you haven't joined your contact table in
 
thats it...
 
you've joined in your contact_type but not contact
 
9:05 AM
yes
 
@Julo0sS sorry I'm only 1 coffee deep this morning so I'm a bit slow
 
:P
 
sorry, client calling
 
@Julo0sS so are you saying that there is no link between the contact table and the entity table?
 
yes, they're not related
only through entity_contact join table
 
9:07 AM
@AndyK no worries, whenever I don't hear from you I figure you're calming down your co-worker :P
then add that in...something like this:
   ... join entity_contact on entity.entity_id = entity_contact.entity_id
    join contact on entity_contact.contact_id = contact.contact_id...
 
thx! now just want to put this up into functions like "retrieveOneEntityContactData(entity_id)" or "retrieveAllEntitiesContactData()"
 
@Julo0sS why?
 
why not?
call to "getContacts(1)" is shorter isn't it?
 
functions like those in databases don't scale well, the more data that you get the worse they start to perform
it's like trying to bring programming language to the database, doesn't work all the time
 
(maybe I'm wrong and there's no reason I should use it as a function)
 
9:12 AM
there are hidden gotcha's that you just have to learn
 
So you think this is not useful to "register" that query as a function?
 
depends on what you consider more useful
do you want useful = "shorter call"
or
do you want useful = "perform better"?
 
both possible? ^^
 
if it's the first one, just make a note of it so when performance dips, you can go back and re-write it
@Julo0sS hahaha
@Julo0sS sure, I guess
 
what about more "specific" databases like the postgis template functions
 
9:15 AM
that's where you need more expertise
I have a postgres db but I only mess about with it
 
postgis is geographical/geometric objects management. There are something like 1000 functions in the template
 
you'd have to ask someone more knowledgable
@Julo0sS really? wow...
@Julo0sS those are system built though, right?
 
like "st_area" that calculates area of a polygon and so on...
 
yeah system ones, those more often than not have been tuned to an n-th degree
 
well these functions are using algorythms and strange calculation stuff, but they're function... so just wondering about the real function utility then ... :/
I don't get it when you talk about "system ones"
let's think about another example : I have several tables like this :
entity(id,name)
society(id,name,members)
public_service(id,name,comment)
contact table is just the same
a new column is added to contact_entity table, called "entity_type", text, holding "entity" or "society" or "public_service"
 
9:23 AM
@Shaneis you have no idea ...
 
So, I know that if the entity_type column holds 1, I have to get the entity data in the "entity" table. If it's 2, then I have to go to "society" table, and so on
 
hold on, have to restart my pc, new release of SSMS to fix the breaks in the old release
 
then, I think that a function is the good solution for this example isn't it? otherwise I'll have to make 3 different queries, one for each "entity_type" value... am I wrong?
ok @Shaneis
 
@Julo0sS sorry I could not help you more on this one
was dealing with my colleague :)
 
np, first example solved with query, but now more "advanced" (and maybe simply wrong) example to go
 
9:39 AM
@Julo0sS so depending on data in a column, you may have to join 1 table or the other?
 
yes this is it
functions can manage "if statements", so, in my opinion this would fit, right?
 
functions can manage IF statements, yes. You can also use LEFT JOINs...
just don't think that's the best design schema @Julo0sS
 
why?
 
because the logical data (the data in the entity_type) dictates what the physical join will be (either entity or society)...
I'm just saying it though, I know it's not a perfect world so you work with what you have
 
I understand what you mean, you mean that it would be easier to have a "entity_type" column in the "entity" table, and manage "societies" as "entities" and so on
 
9:48 AM
@Julo0sS yessum
 
but what I point here is a more specific example, think about the "user" table. User can be linked to a "entity", for example, Shaneis is the user, and you have a name (which is not your login)
you can also tell, as an entity, my contact data is : ... whatever. And as a user, my contact data (newsletter, whatever) is : ... whatever2.
 
ahuh
 
the contact data is registered in the contact table anyway but is related to table "user" or "entity", this cannot be managed in the same table
 
technically I think it can if it has a "qualifier" but we're getting into database design here @Julo0sS :)
so unless you want a spirited debate you better bring your 'A' game xD
 
lol, well it's still a concrete question/example, but, once again, maybe I'm wrong (in design as you said)...
 
10:01 AM
@Julo0sS nah in these things it gets more opinionated so you're never wrong as long as you can argue
@Julo0sS anyway you can do composite joins in Postgres right?
like:
LEFT JOIN society ON table_name.id =other_table.id AND entity_type = 2
 
this looks like a decent solution
 
@Julo0sS give it a test run?
@Julo0sS seriously though, don't assume you're wrong here. like I said it's a personal preference, I could be wrong
 
I will, but, if I understand, it means that it'll execute (read) all left joins even if two of them are not needed
take my example with societies, entities and public_service, i need 3 left join's, and only one of them will be used
 
@Julo0sS v.true but if you have IF functions, every single IF statement will be compiled even if only 1 of them is ran
 
you're right with it
I'll test it, need to eat something, my brain is hungry :P
 
10:06 AM
@Julo0sS I also suggest coffee :)
@Julo0sS and let me know, like I said, I mess about with Postgres, I'd be interested in what you find
 
10:57 AM
the things you do to get the write data
:smh: :facepalm:
full_object_name	= SUBSTRING(up.[message], 18, (CHARINDEX(' WITH', up.[message]) - 18)),
	time_duration	= CAST(SUBSTRING(tt.[message], 29, (CHARINDEX('[', tt.[message], 29) - 29)) AS time(0)),
	schema_part		= PARSENAME(LTRIM(SUBSTRING(up.[message], 18, (LEN(up.[message]) - (CHARINDEX('[', REVERSE(up.[message]), 17) + 18)))), 2),
	table_part		= PARSENAME(LTRIM(SUBSTRING(up.[message], 18, (LEN(up.[message]) - (CHARINDEX('[', REVERSE(up.[message]), 17) + 18)))), 1),
	stats_part		= SUBSTRING(up.[message], (LEN(up.[message]) - CHARINDEX('[', REVERSE(up.[message]), 17)), (CHARINDEX('[', REVERSE(u
I am ashamed of this :'(
 
11:18 AM
@Shaneis Well,... find something "interesting"
mixing your solution (with AND clause)
and "CASE" clauses
w8
SELECT
    entity_contact.entity AS ent,
    entity_type.name AS type,
    contact.value AS contact,
    contact_type.name AS type,
    CASE
        WHEN entity_contact.entity_type = 1 THEN entity.name
        WHEN entity_contact.entity_type = 2 THEN society.name
        WHEN entity_contact.entity_type = 3 THEN public_service.name
    END AS name,
    CASE
        WHEN entity_contact.entity_type = 1 THEN entity.id
        WHEN entity_contact.entity_type = 2 THEN society.id
        WHEN entity_contact.entity_type = 3 THEN public_service.id
this is some kind of maze... but it fixes the problem
any suggestion? advice? comment?
(example considering "entity" is type 1, "society" is type 2 and pservice is type 3)
 
12:03 PM
looks fine with the problem you have, does it work for you?
sorry btw, I was having lunch
 
yep exactly what i needed, with this, you can define (in the first block with cases) the fields you want to get, depending on the column value, and then, in the left outer join is your solution with the conditionnal join
didn't find anything better yet...
now, this returns the whole contacts data's for any entity of any type
turning it into a function with "entity_id" and/or "entity_type" would make an easy search tool... in database
 
@Julo0sS hahaha again you can create a function if you want as long as you are okay with the performance hit
end of the day, your database mate
 
x) sounds like you do not believe in functions :P
belieeeveeee
 
back
 
@Julo0sS There's no FUN in functions.
apart from, you know, the start
@AndyK functions, yay or nay?
 
12:18 PM
functions: yay
 
you're both strange :|
 
if you go to python, it's even more pronounced
but in sql, functions are not that much used
 
@AndyK they should be used
but are not
 
actually, in term of comparison, proc is equivalent to function in sql
 
I need @War...
although he may also agree with you
 
12:19 PM
he's buried in meetings
 
@AndyK okay, I'm interested...how? :)
@AndyK poor guy
 
let's say
 
@AndyK What do you think about my previous "huge" query above?
 
@Julo0sS it is very clean
actually you can write in a stored procedure
it will be cleaner
I'm not highly familiar with postgres yet
but in sqlserver report, it is advised to use stored procedure (also known as proc) to not overburden the report itself
 
how would you do it (if not too hard to make an example) ?
 
12:30 PM
@AndyK don't think Postgres has Sprocs , just functions :(
 
well, it's the same isn't it?
 
@Julo0sS not in SQL Server I would say, although I'm still waiting for @AndyK's saying that they are equivalent
 
"proc" makes me think "ruby"... and proc is "like" a function...
(in ruby)
("like" != "===")
 
@Julo0sS "===" is LIKE in Ruby?
weird
 
12:51 PM
xD
 
with a client
 
1:03 PM
back
 
@AndyK you're getting busy man
 
1:19 PM
yeah kind of
 
2:03 PM
if any "function comfortable pro" comes around, some help to turn my "maze" query into a function would be appreciated...
^^
 
War
maze query?
you procedurally generating a game world? (lol)
 
afternoon @War
 
War
sup @Shaneis
 
2:27 PM
@War same old same old, same sh1t, different day
 
War
2:42 PM
no 2 days are ever the same for me mate
always something new
I have a habit of solving problems in a way that means I don't have to keep revisiting them
 
@War admirable
 
War
it's harder some days but generally speaking I'm reasonably successful at it
 
3:28 PM
going back home
see you
 
see ya @AndyK
 
War
cya @AndyK
 
4:09 PM
cya guys
 

« first day (1592 days earlier)      last day (2726 days later) »