Concatenate multiple records to one record with postgresql - postgresql

I have a query like this:
Select * from
(Select a, b, c from table1 where some condition) as Result1,
(Select d, e from table2 where some another condition) as Result2
everything is OK until one of the nested selects returns nothing, then another select returns nothing too in finally select.
please tell me what is wrong with me?

As per my comment above, the following should work how you expect:
select
*
from
(select a, b, c from table1 where predicate1) Result1
full outer join
(select d, e from table2 where predicate2) Result2 on
1 = 1

Try:
Select (
(Select a, b, c from table1 where some condition) as Result1,
(Select d, e from table2 where some another condition) as Result
)
Try an inner or self join
Post some result samples to get a better understanding of the issue.

Related

inner query without FROM

I made a mistake when writing the query, I wrote a subquery without a FROM clause:
select * from foo where id in (select id where type 'mm')
I lost half a day to find a mistake because it worked!
I checked the documentation but I did not find any information why ...
It is more interesting:
select * from abc a
inner join abc b on b.id = a.id
where a.id in (select id from (select a.id, row_number () over () lp where b.name = 'abc') x where lp = 1);
http://sqlfiddle.com/#!15/4bb29/12
Can someone explain how it works?
Your first query will certainly not work as it is.
To understand your second query, keep in mind that
A SELECT without a FROM clause is like as if there is a FROM clause with a table that has no column, but one row.
You can reference columns from the outer query in the subquery.
Here is an attempt to explain your query:
SELECT * FROM abc a
/* ok, now we have a table with alias "a" */
INNER JOIN abc b ON b.id = a.id
/* now we also have a table with alias "b" */
WHERE a.id IN
(SELECT id
FROM (SELECT a.id,
/* this will count the one "artificial" row */
row_number() OVER () lp
/* Here you reference the table with alias "b" above.
This is constant as far as the subquery is concerned,
so if it is not TRUE, the subquery will return an
empty result */
WHERE b.name = 'abc') x
/* this is always true, since there is only one line in
the above subquery */
WHERE lp = 1);

Product of regular field and computed field in an ORDER BY clause

The following query works fine:
SELECT a, b, c,
(SELECT COUNT(*) AS COUNT FROM table_b WHERE field_a = p.a) AS d
FROM table_a AS p
ORDER BY b DESC
And this also works:
SELECT a, b, c,
(SELECT COUNT(*) AS COUNT FROM table_b WHERE field_a = p.a) AS d
FROM table_a AS p
ORDER BY d DESC
But the following produces a ERROR; column 'd' does not exist error:
SELECT a, b, c,
(SELECT COUNT(*) AS COUNT FROM table_b WHERE field_a = p.a) AS d
FROM table_a AS p
ORDER BY (b * d) DESC
Only difference between the three queries above is the ORDER BY clause. In the first two queries, results are ordered by either the b field or by the dynamic d field. In the last query, results are (should be) ordered by the product of b times d.
How comes, in the last query, PostgreSQL says that d does not exist while it can find it without issue in the second query?
Arbitrary expressions in the order by clause can only be formed from input columns:
Each expression can be the name or ordinal number of an output column (SELECT list item), or it can be an arbitrary expression formed from input-column values.
You will need to subquery it:
select *
from (select 1 as a, 2 as b) s
order by a * b

Common records for 2 fields in a table?

I have a Table which has 2 fields say A,B. Suppose A has values a1,a2.
Corresponding records for a1 in B are 1,2,3,x,y,z.
Corresponding records for a2 in B are 1,2,3,4,d,e,f
I need a a query to be written in DB2, so that it will fetch the common records in B for each record in A (a1 and a2).
So here the output would be :
A B
a1 1
a1 2
a1 3
a2 1
a2 2
a2 3
Can someone please help on this?
Try something like:
SELECT A, B
FROM Table t1
WHERE (SELECT COUNT(*) FROM Table t2 WHERE t2.B = t1.B)
= (SELECT COUNT(DISTINCT t3.A) FROM Table t3)
ORDER BY A, B
This might not be 100% accurate as I can't test it out in DB2 so you might have to tweak the query a little bit to make it work.
with t(num) as (select count(distinct A) from table)
select t1.A, t1.B
from table t1, table t2, t
where t1.B = t2.B
group by t1.A, t1.B, num
having count(*) = num
Basically, the idea is to join the same table with column B and filter out just the ones that match exactly the same number of times as the number of elements in column A, which indicates that it is a common record out of all the A values.

Browse two tables using a cursor

In my procedure I have two tables with the same data. I go through my first table through a cursor. Which compares with the second table that I find much the same data. What if, for example in my table_1 I have ten in my data and I have 12 data table2 how to detect missing data in my two table_1 which is traversed by the cursor?
Thx.
Sounds very much like you'd be better off using the MINUS operator.
SELECT a, b, c
FROM table1
MINUS
SELECT a, b, c
FROM table2
This will show you all results that exist in table1 which are not present in table2. In order to show discrepancies both ways, you could do something like this:
SELECT z.*, 'In table1, not in table2' problem_description
FROM (
SELECT a, b, c
FROM table1
MINUS
SELECT a, b, c
FROM table2
) z
UNION ALL
SELECT z.*, 'In table2, not in table1' problem_description
FROM (
SELECT a, b, c
FROM table2
MINUS
SELECT a, b, c
FROM table1
) z
SQL Fiddle for this answer

How to select multiple columns values same rows from single table

I have a SQL Server table. Now this table has columns like primary key Id, A, B, C, D, E, F, G
Now I want to select rows from this table like this
A=A, B=B, C=C, D=D and G > 132
So I am trying to select rows from this table which rows A,B,C,D columns has same data and G column data > 132.
So how can I do that ? Thank you.
I tried this query but returning same Id rows
SELECT TableA.Id,TableA.UserId,TableA.MaximumHp,TableA.Attack,TableA.Defense,TableA.SpAttack,TableA.SpDefense,TableA.Speed
FROM myTable as TableA
Inner Join myTable as TableB on
TableA.MaximumHp = TableB.MaximumHp
AND TableA.Attack = TableB.Attack
AND TableA.Defense = TableB.Defense
AND TableA.SpAttack = TableB.SpAttack
AND TableA.SpDefense = TableB.SpDefense
AND TableA.Speed = TableB.Speed
AND TableA.Id != TableB.Id
SQL Server 2008 R2
Sounds like you want to join the table to itself
SELECT *
FROM Table t1
Inner Join Table t2 on t1.A = t2.A
AND t1.B = t2.B
AND t1.C = t2.C
AND t1.D = t2.D
AND t1.G > 132
AND t1.ID <> t2.ID
I THINK what you mean is duplicates. Tell me if this is what you are looking for.
SELECT [Table].A, [Table].B, [Table].C, [Table].D, [Table].E, [Table].F, [Table].G
FROM [Table] LEFT JOIN (SELECT A, B, C, D FROM [Table]
GROUP BY A, B, C, D
HAVING count(*) > 1)
AS sub ON ([Table].A=sub.A) AND ([Table].B=sub.B) AND ([Table].C=sub.C) AND ([Table].D=sub.D)
WHERE G>132 and sub.A is not null;
This will give you all the rows where a,b,c, and D are equal to another row in the table...and G > 132