« first day (2033 days earlier)      last day (625 days later) » 

11:02 AM
@TehShrike Hi @TehShrike I'm using PostgreSQL 9.4
 
SELECT table1.*, table2.state
FROM table1
LEFT JOIN table2 ON table1.column1 = table2.column2
GROUP BY table1.id
ORDER BY COUNT(table1.column1) DESC


I'm retriving order by count descending with group by of another column that conflicting my result order can anyone help me in this kind of issue?
 
 
4 hours later…
3:23 PM
@SagarNaliyapara can you articulate the issue a bit further?
 
hi @TehShrike
he solved the problem
 
Is table1.id the primary key/unique? If so, COUNT(table1.somecolumn) will always just return 0 or 1 depending on if it's null
@Julo0sS cool, what was it?
 
it was related to his "count"
as you said, count(table1.%) was wrong
but I didn't solve mine yet :P
I managed to make the query work fine, and now I'd like to turn it into a function but ... no success
tried the example above, also tried this code :
 
@Julo0sS how exactly do you want to turn it into a function?
 
CREATE OR REPLACE FUNCTION getentitycontacts(
    entity_type_identifier integer,
    entity_identifier integer)
  RETURNS SETOF record AS
$BODY$
	DECLARE
		searchsql text := '';
		var_match record;
	BEGIN
		searchsql := 'SELECT
			entity_contact.entity AS ent,
			entity_contact.entity_type AS type,
			contact.value AS contact,
			contact.type AS type,
			CASE
				WHEN entity_contact.entity_type = 1 THEN concat_ws('' '',person.name,person.first_name)
				WHEN entity_contact.entity_type = 2 THEN (SELECT concat_ws('' '',person.name,person.first_name) WHERE person.id = student.person)
 
3:27 PM
In what context do you want to call it, and what do you want to happen when you call it?
 
Well, the "basic" query is supposed to retrieve the contact information for entities
 
Do you want to call it like SELECT myFunction(table1.column1) FROM table1 and have it return a single value in your SELECT?
Do you want to call it like CALL myFunction('someInput', 13, 'someOtherInput') and have it spit a full result set back at you?
 
well, I'd like to call it : SELECT getentitiescontact(entity_id, entity_type) and get a set of results
 
that doesn't make sense
 
it does
 
3:28 PM
in that context, the function is a single value
 
i don't get it... :/
okay, basically I do have few tables concerned
 
you can't SELECT someFunction() AS my_alias_name and have the query not know how many columns are going to be in the result set
there can only be one value, which would show up in the column my_alias_name
 
maybe I don't explain correctly what I'm tryin to do
 
You can run a stored procedure, NOT in the context of a SELECT query, and it can run arbitrary queries and return an arbitrary result set
 
(excuse my english^^)
I have several tables. One is "contact", holding "id, contact_value, contact_type"
Contact type is a foreign key, pointing to a record holding text value like "phone" or "skype_id", or whatever and value holds the value (555-564541321)
then, i have a "join table" called entity_contact. This holds the relationship between the "entity" and the "contact"
 
3:32 PM
So multiple entities can have the same contact information
 
it has many columns, but main ones are : "entity" (id of the entity), "entity_type" (holding a value that gives information on which table we have to look in to find the entity itself), and contact (the contact's id)
my objective is to retrieve the contact informations for entities. So, I did a query that does the job, for ALL entities
and that's this query I'd like to turn into a function I could call like this (maybe a procedure, I don't know the exact term)
but I'd like it to return the contact informationS for an entity from its Id and Type
 
Why put this on the database server? Why not make it a re-usable function in your application code?
 
so, in my brain, I was thinkin about something like :
getcontactinformation(entity_id, entity_type) returning all rows for this entity
I can do it in my application, but was wondering about the functions in the db server
why not use the db server for queries?
 
Because it takes a schema change to update the business logic, and it's more difficult to test
It's more difficult to build dynamic queries on the server
What language are you using in your application?
 
java / spring / angularjs
and postgresql db
 
3:38 PM
There aren't any great public Java SQL query builders, sadly
I wrote a pretty good one at my last job
 
hehe so YOU have one :P
well, I know a bit postGIS, which has lots (like 1000) of functions written in it
 
You can take a pretty good guess at the API by seeing this JavaScript library I wrote github.com/TehShrike/sql-concat
 
so, is that a bad idea to make functions in the db server?
 
@Julo0sS Not necessarily. But it's usually better to make your queries re-usable and composable in your application code.
 
good job @ github ;)
I get it, but, for learning purposes, what's the problem with my "function" definition?
The query I use is working fine, returning exactly what I need, from "any" table
I found interesting docs on "return setof record" that looks like it's the kind of things I need to return, but no way, everything fails
 
4:03 PM
@Julo0sS I can't speak for Postgres, but at least in MySQL, a function can't do what you want
That's why I asked about the context in which you'd like to call this functions
In MySQL, the only thing that can return arbitrary result sets like that is a "stored procedure"
 
okay, but it's "like" a function (like I call function in other languages such as php, js, & so on...)
 
@Julo0sS what, a stored procedure?
 
yes
 
Sure, they have similarities
But they are quite different in what they can do (at least in MySQL)
which is why I asked how you expected to call this code
 
this is where I got the "idea" to make this query as a function
see a pgsql function can take arguments, and you can manipulate them in the "function body"
 
4:07 PM
@Julo0sS yeah, that looks pretty similar to MySQL functions
 
and, reading further, these functions can return "table", which has to be defined strictly (number of columns, and types!)
or
setof record, which looks more "lazy"
create or replace function GetRows(text) returns setof record as
'
declare
r record;
begin
for r in EXECUTE ''select * from '' || $1 loop
return next r;
end loop;
return;
end
'
language 'plpgsql';
its awful :/
 
:-x
 
but
even this, does NOT work at all
same sh*t : 0A000 - feature_not_supported
guess the problem is not in my code...
 
Are you looking at the docs for the same version of Postgres that you have installed?
 
not "up to date" but this should work :/
CREATE TABLE tab (y int, z int);
INSERT INTO tab VALUES (1, 2), (3, 4), (5, 6), (7, 8);

CREATE FUNCTION sum_n_product_with_tab (x int, OUT sum int, OUT product int)
RETURNS SETOF record
AS $$
    SELECT $1 + tab.y, $1 * tab.y FROM tab;
$$ LANGUAGE SQL;

SELECT * FROM sum_n_product_with_tab(10);
 sum | product
-----+---------
  11 |      10
  13 |      30
  15 |      50
  17 |      70
(4 rows)
this is strange
it does not act like it should, I just made a "similar" function in my server like this :
CREATE FUNCTION strange_res(x int)
RETURNS SETOF record
AS $$
	SELECT * FROM person WHERE id < $1;
$$ LANGUAGE SQL;

SELECT strange_res(10);
it returns rows without columns
like :
1 | (1, name%1, firstname%1)
2 | (2, name%2, firstname%2)
3 | ....
 
4:41 PM
Guess this is "sql" :
CREATE FUNCTION strange_res_2(x int)
RETURNS TABLE (id integer, name text, first_name text, birth_date date, sex integer) AS
$$
	SELECT * FROM person WHERE id < $1;
$$
language 'sql' VOLATILE;
can you explain me why it returns "rows" with only 1 column (holding all the data in "()")
 

« first day (2033 days earlier)      last day (625 days later) »