Interbase - combining queries - interbase

I have a table with to columns: Question and Answer. I want a query that results in one column containing interchangeably the Question and it's respective Answer.
Exemplifying:
Table "Question_Answer"
Q1, A1
Q2, A2
Q3, A3
Q4, A4
Query output, column name "Question_Answer_Result"
Q1
A1
Q2
A2
Q3
A3
Q4
A4
I tried the following command:
select "Question_Answer_Result"
from (select "Question_Answer"."Question"
from "Question_Answer"
union all
select "Question_Answer"."Answer"
from "Question_Answer"
)
but I receive a message "Unexpected end of command".
What would be the right SQL command?
Thanks.

What are you selecting from the subquery? The select is looking for column "Question_Answer_Result" from the subquery result; no such column exists. The only column is "Question".

You are missing a column reference in the initial select. As alluded to in my first response, the column 'question' is the result of union subquery. You need to select that column by name (or * will suffice) and then rename it.
select question 'question_answer'
from (
select question from question_answer
union all
select answer from question_answer
)
EDIT: this is SQLite to get your exact output
select question 'question_answer'
from (
select question from question_answer
union all
select answer from question_answer
) x
order by substr(question,length(question-1),1),substr(question,1) desc
note, the order of output will not be as you illustrated.

Related

How to return both an aggregate function result and a different column value from a query?

Is it possible to build a select query that sums up value of a column and also get the fields from the column of the same table, and an example please . I am new to DB2 and still learning.
I tried using the below,
SELECT SUM(Column containing numbers)
,Column 2
FROM Table
but this gives me a SQL return code of -122
It's possible with the SUM olap function.
SELECT
SUM(C1) OVER () AS C1_SUM
, C2
FROM
(
SELECT 1, 'A' FROM SYSIBM.SYSDUMMY1
UNION ALL SELECT 2, 'B' FROM SYSIBM.SYSDUMMY1
) T (C1, C2)
C1_SUM
C2
3
B
3
A
fiddle

Postgres: Include Subselect and Derived Calculation from Subselect in the Same Column String

In Postgres I have the following query. There is a column being returned from a sub-select called filled_out. I need to include in the same column string, preferably without repeating the whole sub-select, the % percentage of filled_out / recalls, the column just before it. Example output is below. Is there a quick way to get this?
select s.name, s.description, s.abbreviation, s.created_by, to_char(s.created_date, 'mm/dd/yyyy'),
count(r.id) as recalls,
(select count(r2.id) from recalls_t r2 where
r2.last_changed_by <> 'ACT24_JOB' and r2.activities_duration_min >= 720 and
r2.user_id in (select user_id from study_participants_t sp2 where sp2.study_id = s.id))
as filled_out
from study_t s, study_participants_t sp, recalls_t r
where ...

Find equal twin record postgresql

I have a table company with 60 columns. The goal is to create a tool to find, compare and eliminate duplicates in this table.
Example: I have a record with id 22 and I know it has a twin because I run this (simplified code):
SELECT min(co_id),co_name,count(*) FROM co
GROUP BY co_name
HAVING count(*) > 1
The result shows there are one twin (count 2) and I get the oldest id by min(co_id)
My question is how I search for the twin co_id? Just passing the oldest id?
Something like:
SELECT co_id FROM co
WHERE co_name EQUAL TO co_id='22'
LIMIT 2
Sample data:
id co_name
22 Volvo
23 Volvo
24 Ford
25 Ford
I know id 22 and I want to search for the twin 23 based on the content of 22.
The closest I found is this. Which is far from generic. And a nightmare for comparing 60 field:
SELECT id,
(SELECT max(b.id) from co b
WHERE a.co_name = b.co_name
LIMIT 1) as twin
FROM co a
WHERE id='22'
How do I do this in a more simple and generic way? I just want the twin record co_id.
Thank you in advance!
select max_co,co_name from (
select max(co_id) max_co,min(co_id) min_co,co_name from co
group by co_name having count(*)>1) where min_co=(your old co id as input);
You can join your table with itself:
SELECT c1.*
FROM
co_name c1 INNER JOIN co_name c2
ON c1.co_name=c2.co_name
AND c1.id>c2.id
this will return all duplicated records (but not the original record with the lowest id). Or since you're using Postgresql you can use a window function:
SELECT *
FROM (
SELECT
id,
co_name,
row_number() OVER (PARTITION by co_name ORDER BY id) as row
FROM
co_name
) s
WHERE
row>1;
Please see an example here.
If you want to compare multiple columns, the JOIN solution would be more flexible. I don't know exactly how you want to compare your columns and how you exactly define "twin" rows, but you a query like this should help:
SELECT c1.*
FROM
co_name c1 INNER JOIN co_name c2
ON (
c1.co_name=c2.co_name
OR c1.co_city=c2.co_city
OR c1.co_owner=c2.co_owner
OR ...
) AND c1.id>c2.id
if you just want duplicated records of id=22 then you can try with this:
SELECT c1.*
FROM
co_name c1 INNER JOIN co_name c2
ON c1.co_name=c2.co_name
AND c1.id>c2.id
WHERE
c2.id=22
or if you just want a single twin, comparing 60 columns, you can try with this query:
SELECT MIN(ID) as Twin /* or MAX(ID), depending what you're after */
FROM
co_name c1 INNER JOIN co_name c2
ON (
c1.co_name=c2.co_name
OR c1.co_city=c2.co_city
OR c1.co_owner=c2.co_owner
OR ...
) AND c1.id>c2.id
WHERE
c2.id=22
I found one solution that is working on 60 columns if I use variables in stead of hardcode in the query. Thanks everybody for all input. Some of them were about the same track.
SELECT id,
(SELECT max(b.id) from co b
WHERE concat(a.co_name,etc) = concat(b.co_name,etc)
LIMIT 1) as twin
FROM co a
WHERE id='22'
Not the best one, but fetch one twin at a time. And it is far from generic. Thanks for pointing me in the right direction. A generic solution would be nicer.

Identifying rows with multiple IDs linked to a unique value

Using ms-sql 2008 r2; am sure this is very straightforward. I am trying to identify where a unique value {ISIN} has been linked to more than 1 Identifier. An example output would be:
isin entity_id
XS0276697439 000BYT-E
XS0276697439 000BYV-E
This is actually an error and I want to look for other instances where there may be more than one entity_id linked to a unique ISIN.
This is my current working but it's obviously not correct:
select isin, entity_id from edm_security_entity_map
where isin is not null
--and isin = ('XS0276697439')
group by isin, entity_id
having COUNT(entity_id) > 1
order by isin asc
Thanks for your help.
Elliot,
I don't have a copy of SQL in front of me right now, so apologies if my syntax isn't spot on.
I'd start by finding the duplicates:
select
x.isin
,count(*)
from edm_security_entity_map as x
group by x.isin
having count(*) > 1
Then join that back to the full table to find where those duplicates come from:
;with DuplicateList as
(
select
x.isin
--,count(*) -- not used elsewhere
from edm_security_entity_map as x
group by x.isin
having count(*) > 1
)
select
map.isin
,map.entity_id
from edm_security_entity_map as map
inner join DuplicateList as dup
on dup.isin = map.isin;
HTH,
Michael
So you're saying that if isin-1 has a row for both entity-1 and entity-2 that's an error but isin-3, say, linked to entity-3 in two separe rows is OK? The ugly-but-readable solution to that is to pre-pend another CTE on the previous solution
;with UniqueValues as
(select distinct
y.isin
,y.entity_id
from edm_security_entity_map as y
)
,DuplicateList as
(
select
x.isin
--,count(*) -- not used elsewhere
from UniqueValues as x
group by x.isin
having count(*) > 1
)
select
map.isin
,map.entity_id
from edm_security_entity_map as map -- or from UniqueValues, depening on your objective.
inner join DuplicateList as dup
on dup.isin = map.isin;
There are better solutions with additional GROUP BY clauses in the final query. If this is going into production I'd be recommending that. Or if your table has a bajillion rows. If you just need to do some analysis the above should suffice, I hope.

How to compare two tables in postgres

I want compare two column values which come from two different queries. Can anyone suggest a query which compares two columns in Postgres?
Well, the easiest to understand--but not necessarily the fastest--is probably something like this. (But you might mean something else by "compare".)
-- Values in column1 that aren't in column2.
SELECT column1 FROM query1
WHERE column1 NOT IN (SELECT column2 FROM query2);
-- Values in column2 that aren't in column1.
SELECT column2 FROM query2
WHERE column2 NOT IN (SELECT column1 FROM query1);
-- Values common to both column1 and column2
SELECT q1.column1 FROM query1 q1
INNER JOIN query2 q2 ON (q1.column1 = q2.column2);
You can also do this in a single statement to give you a visual comparison. A FULL OUTER JOIN returns all the values in both columns, with matching values in the same row, and NULL where one column is missing a value that's in the other column.
SELECT q1.column1, q2.column2 FROM query1 q1
FULL OUTER JOIN query2 q2 ON (q1.column1 = q2.column2);