I have a dynamic sql, where I need to select two columns(say A & B) from the table. I need to generate the result set only if column B has at least one non zero value. If there is no non zero value in the column B, result set should be empty.
Should be that simple, just as you wrote the rule -
select a, b
from the_table
where exists (select from the_table where b <> 0);
Related
I'm using postgresql here
Here's a field in my table "num":
adsh: some numeric with dash eg.(1234-23456-456789)
tag: character eg.(ReturnOnAsset)
version: can be one of two format 1. exactly same as adsh 2. us-gaap/yyyy eg. (us-gaap/2009)
there are some other column but not important
primary key is the 3 column above and some other column combine.
for each adsh, there's several version value us-gaap/dei/invest + yyyy or
Also keep in mind that there's 10^8 tuples in this table
step one: select tag, version from num where version=adsh as result a
step two: select adsh, version from num where version!=adsh as result b
step three: select tag, b.version from a, b where a.version=b.adsh
I wonder if I can save the first two steps' result temporarily for in order to do the third step. Can I do that. What's the most efficient way to do this?
Thanks!
I have found the solution using with clause:
with a as (select tag, version from num where version=adsh), b as (select adsh, version from num where version!=adsh) select tag, b.version from a, b where a.version=b.adsh;
I want to retrieve all ids within a certain timespan. The timestamps however,
are stored in a different table:
Table A has column my_id
Table B has columns my_id, timestamp
I would want something like
SELECT
id,
time
FROM
(SELECT my_id AS id FROM A) q1,
(SELECT timestamp AS time FROM B WHERE my_id = id) q2
;
But how can I get the value of id within a different subquery? Is there
an elegant solution for this problem?
I see that in the second subquery you try to link the two tables:
(SELECT my_id AS id FROM A) q1,
(SELECT timestamp AS time FROM B WHERE my_id = id) q2
If both ids must be equals:
SELECT a.my_id as id, b.timestamp as time
FROM A a
JOIN B b ON (a.my_id = b.my_id);
I hope this helps.
Simpler:
SELECT my_id AS id, b.my_ts
FROM a
JOIN b USING (my_id);
FROM A a (like has been advised) is nonsense because, I quote the manual:
Quoting an identifier also makes it case-sensitive, whereas unquoted
names are always folded to lower case.
While being allowed in PostgreSQL, the type names time and timestamp are reserved words in every SQL standard and should not be used as identifiers.
USING is a syntactical shorthand, mostly.
I'm having a little problem with JPA. Consider this scenario:
Table A (id_a) | Table B (id_b, id_a)
What I need is a query like this:
Select a.*, c.quantity from A as a, (Select Count(*) as quantity
from B as b where b.id_a = a.id_a) as c;
The thing is that I want to use a jpa query and not a native query, something like this:
Select a, c FROM A a, (Select Count(b) FROM B b where a.idA = b.a.idA) c;
So then I could iterate from the result (a list of Object[] with a and c in each node) and then assign a.quantity = c;
I repeat, I don't want to use native query, but I found no other way than use redundant data, and add another column to A called Quantity and every time I insert and delete from B, update this column in A.
Please help, I read somewhere that JPA doesn't accept subqueries in Form clause, so, what can I do ?
Thanks a lot !
JPA does not support sub-selects in the FROM clause but EclipseLink 2.4 current milestones builds does have this support.
See,
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#Sub-selects_in_FROM_clause
You can probably rewrite the query with just normal joins though.
Maybe,
Select a, size(a.bs) from A a
or
Select a, count(b) from A a join a.bs b group by a
I want the count of the one column and I have 5 columns in FROM clause but it is giving wrong count as I have included all my columns that are in the from clause. I don't want that particular column in the GROUP BY clause.
If I remove that column from GROUP BY clause it throws the following error:
ERROR: column "pt.name" must appear in the GROUP BY clause or be used
in an aggregate function LINE 1: SELECT distinct on (pu.id) pu.id,
pt.name as package_name, c...
E.g.:
SELECT DISTINCT ON (a) a,b,c,count(d),e
FROM table GROUP BY a,b,c,d,e ORDER BY a
From this I want to remove e from the GROUP BY.
How can I remove that column from GROUP BY so that I can get correct count?
Updated after rereading the question.
You are mixing GROUP BY and DISTINCT ON. What you want (how I understand it) can be done with a window function combined with a DISTINCT ON:
SELECT DISTINCT ON (a)
a, b, c
, count(d) OVER (PARTITION BY a, b, c) AS d_ct
, e
FROM tbl
ORDER BY a, d_ct DESC;
Window functions require PostgreSQL 8.4 ore later.
What happens here?
Count in d_ct how many identical sets of (a,b,c) there are in the table with non-null values for d.
Pick exactly one row per a. If you don't ORDER BY more than just a, a random row will be picked.
In my example I ORDER BY d_ct DESC in addition, so a pseudo-random row out of the set with the highest d_ct will be picked.
Another, slightly different interpretation of what you might need, with GROUP BY:
SELECT DISTINCT ON (a)
a, b, c
, count(d) AS d_ct
, min(e) AS min_e -- aggregate e in some way
FROM t
GROUP BY a, b, c
ORDER BY a, d_ct DESC;
GROUP BY is applied before DISTINCT ON, so the result is very similar to the one above, only the value for e / min_e is different.
Is it possible to use the names of the actual columns for the order by clause?
I am using a view to let a client use a reporter writer (Pentaho) and this would make things easier on them.
To clarify, I want to put the results in alphabetical order of the column names themselves. I want to sort the data using the columns, not the data IN the columns.
In PostgreSQL you can try:
SELECT column_name, data_type FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME =
'my_table' ORDER BY column_name;
If what you mean is to change the order of the columns themselves according to their names (that would make sense only if you are using SELECT *, I guess), I'm afraid that's not possible, at least not straightforwardly. And that sounds very unSQL, I'd say...
Sure, you can order by column name, column alias, or column position:
select a, b from table order by b;
select a as x, b as y from table order by x, y;
select a, b from table order by 1;
You can create the view with the columns in any order you like. Then SELECT * FROM your_view queries will return the columns in the order specified by the view.