Schema UNION ALL HIVE should match condition - hive

so I have been trying to make union on 2 tables like this.
All those subquerys, qwz and qwx run successfully in separated ways.
Are they any ways to proceed this ?
I mean like using other join ? Or what kind of fixing I should make?
SELECT CURR_CODE,
EF_DATE,
EF_HOUR,
EF_MINUTE,
EXPI_DATE,
MRR,
CURR_MARKET
FROM
(
SELECT CURR_CODE,
EF_DATE,
EF_HOUR,
EF_MINUTE,
NVL((DA.EFFECTIVE_DATER),(DB.EFFECTIVE_DATER)) AS EXPI_DATE,
M.MRR,
M.CURR_MARKET
FROM
TEMP.DM_QUERY_1_M_FICANT M,
TEMP.DM_QUERY_1_4_AB_FICANT A,
TEMP.DM_1_ZEXPIDA_FICANT DA,
TEMP.DM_1_ZEXPIDB_FICANT DB,
TEMP.DM_2_ZEXPIDC_FICANT DC
WHERE M.CURRENCY_HIS_CODE = A.CURR_CODE
||';'
||A.CURR_NO
AND M.MRR IS NOT NULL
ORDER BY CURR_CODE ASC,
EF_DATE ASC
)
qwz
UNION ALL
SELECT CURR_CODE,
EF_DATE,
EF_HOUR,
EF_MINUTE,
EXPI_DATE,
MRR,
CURR_MARKET
FROM
(
SELECT M.CURRENCY_CODE AS CURR_CODE,
EF_DATE,
EF_HOUR,
EF_MINUTE,
DC.EFFECTIVE_DATER AS EXPI_DATE,
M.MRR,
M.CURR_MARKET
FROM
TEMP.DM_QUERY_2_M_FICANT M ,
TEMP.DM_QUERY_2_4_AB_FICANT A,
TEMP.DM_1_ZEXPIDA_FICANT DA,
TEMP.DM_1_ZEXPIDB_FICANT DB,
TEMP.DM_2_ZEXPIDC_FICANT DC
WHERE M.CURRENCY_CODE =A.CURR_CODE
AND M.MRR IS NOT NULL
ORDER BY CURR_CODE ASC,
EF_DATE ASC
)
qwx
Also, I already testing on some ways : hide the || part, (bar part), still it cannot run well.
Thank you for your time!

Related

Use merge with certain conditions and with ROW_NUMBER function

I am trying to do a merge (insert and update) with the row_number function so that the ID_TRANS field are unique values ​​in the remaining fields apply certain conditions. But when executing it I get a right parenthesis error, it is worth mentioning that I have modified and added parentheses and it remains unresolved.
MERGE INTO TBL_TRANSAC trans
USING (
SELECT ID_TRANS,
TIT,
BEN,
BAN,
CTA_EMI,
CTA_REC,
INST,
TYPE_TRANS,
TYPE_MOV,
CONC,
DATE_OPER,
MONT,
DIV,
ID_CONT
FROM (
SELECT T1.*
, ROW_NUMBER() OVER (PARTITION BY T1.ID_TRANS ORDER BY T1.ID_TRANS DESC)ENUMERADO
FROM (
SELECT
'speibco1_'||UPPER(REPLACE( AREA,' ',''))||
UPPER(REPLACE( FVALOR,' ',''))||
UPPER(REPLACE( CLAVE_RASTREO,' ','')) ,
TIT ,
BEN,
BAN_EM_DES,
REPLACE(UPPER(NO_TP_CTA_EMISOR),' ',''),
LTRIM(CTA_REC,'0'),
'BAN ACTINVER',
'CTA_EXTER',
'SPEI ENTRADA BCO',
CONC_2 ,
TO_DATE(TO_CHAR(FVALOR),'YYYY-MM-DD'),
REPLACE(REPLACE(IMPORTE,'-',''),' ',''),
'MXN' ,
LTRIM(CTA_REC,'0')||'0999'
FROM (SELECT *
FROM IBM_I2.I2_SPEI WHERE REPLACE(NO_TP_CTA_EMISOR,' ','') IS NOT NULL
AND ID_OPERACION='0007'
AND ESTATUS='06'
AND CTA_REC NOT IN ('70000997', '7909567'))
)
WHERE ENUMERADO=1
AND ID_TRANS IS NOT NULL
)SPEI
ON (
trans.ID_TRANS = SPEI.ID_TRANS
)
WHEN MATCHED THEN
UPDATE SET
ID_TRANS = SPEI.ID_TRANS,
TIT = SPEI.TIT ,
BEN= SPEI.BEN,
BAN=SPEI.BAN_EMISOR,
CTA_EMI=SPEI.CTA_EMI,
CTA_REC =SPEI.CTA_REC,
INST= SPEI.INST,
TYPE_TRANS=SPEI.TYPE_TRANS,
TYPE_MOV=SPEI.TYPE_MOV,
CONC=SPEI.CONC,
DATE_OPER=SPEI.DATE_OPER,
MONT=SPEI.MONT,
DIV= SPEI.DIV,
ID_CONT= SPEI.ID_CONT
WHEN NOT MATCHED THEN
INSERT (
ID_TRANS,
TIT,
BEN,
BAN,
CTA_EMI,
CTA_REC,
INST,
TYPE_TRANS,
TYPE_MOV,
CONC,
DATE_OPER,
MONT,DIV,
ID_CONT
)
VALUES
(
SPEI.ID_TRANS,
SPEI.TIT ,
SPEI.BEN,
SPEI.BAN_EMISOR,
SPEI.CTA_EMI,
SPEI.CTA_REC,
SPEI.INST,
SPEI.TYPE_TRANS,
SPEI.TYPE_MOV,
SPEI.CONC ,
SPEI.DATE_OPER,
SPEI.MONT,
SPEI.DIV ,
SPEI.ID_CONT
);
MARK ERROR THAT MISSING RIGHT PARENTESIS AFTER AND ID_TRANS IS NOT NULL
STILL PLACING THE PARENTESIS.
Looks like a closing parenthesis is missing, here:
)) --> here; should be 2, not only 1
WHERE ENUMERADO=1
AND ID_TRANS IS NOT NULL
Put parentheses and aliases to each of them, as well as the fields that are called in USING put aliases, and that worked.
FROM (SELECT *
FROM IBM_I2.I2_SPEI WHERE REPLACE(NO_TP_CTA_EMISOR,' ','') IS NOT NULL
AND ID_OPERACION='0007'
AND ESTATUS='06'
AND CTA_REC NOT IN ('70000997', '7909567')
)
)T1
)T2
WHERE ENUMERADO=1
AND ID_TRANS IS NOT NULL
)SPEI
ON (
trans.ID_TRANS = SPEI.C1_ID_TRANS
)

query based on temp table hive

I am trying to query this in hive. My problem is how to query in hive with TEMP. table.
When I query this, I have got :
Invalid table alias or column reference
SELECT CURR_CODE,
EF_DATE,
EF_HOUR,
EF_MINUTE,
EXPI_DATE,
MRR,
CURR_MARKET
FROM
(SELECT CURR_CODE,
EF_DATE,
EF_HOUR,
EF_MINUTE,
CASE WHEN TEMP.DM_1_ZEXPIDA_FICANT IS NULL THEN TEMP.DM_1_ZEXPIDB_FICANT
ELSE TEMP.DM_1_ZEXPIDA_FICANT
END AS EXPI_DATE,
M.MRR,
M.CURR_MARKET
FROM
TEMP.DM_QUERY_1_M_FICANT M,
TEMP.DM_QUERY_1_4_AB_FICANT A
WHERE M.CHC = A.CURR_CODE
||';'
||A.CURR_NO
AND M.MRR IS NOT NULL
ORDER BY CURR_CODE ASC,
EF_DATE ASC
)qwx
UNION ALL
SELECT CURR_CODE,EF_DATE,EF_HOUR,EF_MINUTE,EXPI_DATE,MRR,CURR_MARKET
FROM
(SELECT M.CURRENCY_CODE AS CURR_CODE,
EF_DATE,
EF_HOUR,
EF_MINUTE,
TEMP.DM_2_ZEXPIDC_FICANT AS EXPI_DATE,
M.MRR,
M.CURR_MARKET
FROM
TEMP.DM_QUERY_2_M_FICANT M ,
TEMP.DM_QUERY_2_4_AB_FICANT A
WHERE M.CURRENCY_CODE =A.CURR_CODE
AND M.MRR IS NOT NULL
ORDER BY CURR_CODE ASC,
EF_DATE ASC
)qwz
The TEMP tables are shaped from
CREATE TABLE IF NOT EXISTS TEMP.DM_1_ZEXPIDA_FICANT AS SELECT..
What went wrong here?
Am I wrong when I try to make temp table?

Where should i put the AS Clause for tax rate (VAT_RATE)

I want to put a AS VAT_RATE in this SELECT statement but i don't know where.
SELECT ROW_NUMBER() OVER(ORDER BY QD.DETAIL_ID) AS No,
QD.PRODUCT_ID AS PROD_ID,PM.'+#ProdCode+' AS PROD_CODE,pm.DESCRIPTION AS SHORT_DESC,
QD.CORPORATE_PRICE AS Corpo_Price,CONVERT(DECIMAL(18,2),QD.RETAIL_PRICE) AS UNIT_SP,QD.COST_PRICE AS COST_SP,
QD.GM,QD.DETAIL_ID,QD.DISC AS Discount,QD.NOTE,
VAT_RATE=(SELECT VAT_RATE/100 FROM dbo.vat
WHERE VAT_ID=(SELECT TOP 1 VAT_ID FROM dbo.product_detail(NOLOCK) WHERE PRODUCT_ID=PM.PROD_ID))
,
Img=(SELECT TOP 1 IMAGE_DATA FROM dbo.PRODUCT_IMAGE WHERE PRODUCT_ID=PM.PROD_ID), QD.CostPrice_Percentage
FROM dbo.CUSTOMER_QUOTATION_DETAIL(NOLOCK) QD
JOIN dbo.product_master(NOLOCK) PM ON PM.PROD_ID=QD.PRODUCT_ID
In TSQL you there is 3 way to name your columns
1) With the AS (optional in tsql)
SELECT QD.PRODUCT_ID AS PROD_ID
FROM dbo.CUSTOMER_QUOTATION_DETAIL(NOLOCK) QD
2) Without the AS (since it is optional)
SELECT QD.PRODUCT_ID PROD_ID
FROM dbo.CUSTOMER_QUOTATION_DETAIL(NOLOCK) QD
3) with an equal sign as if it is a formula
SELECT PROD_ID = QD.PRODUCT_ID
FROM dbo.CUSTOMER_QUOTATION_DETAIL(NOLOCK) QD
Specifically for your query this is where the AS should go.
You would have to remove the equal and put the AS at the end of the sub-query.
Please do note that you have various other issues with the queries that is beyond the scope your original question. If you run into preformance issue, do investigated on the subject of CROSS APPLY / CROSS OUTER JOIN and/or CTE : Common Table Expression.
SELECT ROW_NUMBER() OVER (
ORDER BY QD.DETAIL_ID
) AS No
, QD.PRODUCT_ID AS PROD_ID
--, PM.'+#ProdCode+' AS PROD_CODE
, #ProdCode AS PROD_CODE
, pm.DESCRIPTION AS SHORT_DESC
, QD.CORPORATE_PRICE AS Corpo_Price
, CONVERT(DECIMAL(18, 2), QD.RETAIL_PRICE) AS UNIT_SP
, QD.COST_PRICE AS COST_SP
, QD.GM
, QD.DETAIL_ID
, QD.DISC AS Discount
, QD.NOTE
, (
SELECT TOP 1 (VAT_RATE / 100)
FROM dbo.vat
WHERE VAT_ID = (
SELECT TOP 1 VAT_ID
FROM dbo.product_detail(NOLOCK)
WHERE PRODUCT_ID = PM.PROD_ID
)
) AS VAT_RATE
, (
SELECT TOP 1 IMAGE_DATA
FROM dbo.PRODUCT_IMAGE
WHERE PRODUCT_ID = PM.PROD_ID
) AS Img
, QD.CostPrice_Percentage
FROM dbo.CUSTOMER_QUOTATION_DETAIL(NOLOCK) QD
JOIN dbo.product_master(NOLOCK) PM
ON PM.PROD_ID = QD.PRODUCT_ID

How to optimize mapping over several tables

I'm trying to optimize my code. The solution described below works fine, but I'm pretty sure there are better ways to do it. Do you have any recommendations?
I have one table with business contracts and some characteristic attributes:
table_contracts
contract_number attribute_1 attribute_2 attribute_3
123 a e t
456 a f s
789 b g s
And a second table that maps each contract into a specific group. These groups have different priorities (higher number => higher priority). If the attribute column is empty it means that it is not required (=> m3 is the catch all mapping)
table_mappings
map_number priority attribute_1 attribute_2 attribute_3
m1 5 a e t
m2 4 a
m3 3
As a result I need the the contract_number and the corresponding map_number with the highest priority.
This is how I did it, it works but does anyone knows how to optimize that?
with
first_selection as
(
select
table_contracts.contract_number
,table_mappings.priority
,row_number() over(partition by table_contracts.contract_number order by table_mappings.priority desc)
from table_contracts
left join table_mappings
on (table_contracts.attribute_1 = table_mappings.attribute_1 or table_mappings.attribute_1 is null)
and (table_contracts.attribute_2 = table_mappings.attribute_2 or table_mappings.attribute_2 is null)
and (table_contracts.attribute_3 = table_mappings.attribute_3 or table_mappings.attribute_3 is null)
),
second_selection as
(
select
table_contracts.contract_number
,table_mappings.priority
,table_mappings.map_number
from table_contracts
left join table_mappings
on (table_contracts.attribute_1 = table_mappings.attribute_1 or table_mappings.attribute_1 is null)
and (table_contracts.attribute_2 = table_mappings.attribute_2 or table_mappings.attribute_2 is null)
and (table_contracts.attribute_3 = table_mappings.attribute_3 or table_mappings.attribute_3 is null)
)
select
first_selection.contract_number
,second_selection.map_number
from first_selection
join second_selection
on first_selection.contract_number = second_selection.contract_number and first_selection.priority = second_selection.priority
where first_selection.rn = 1
The output of this code would be:
Results
contract_number map_number
123 m1
456 m2
789 m3
I think you only need one of the selections :
with prioritized as(
select c.contract_number, c.attribute_1, c.attribute_2, c.attribute_3, m.map_number
,row_number() over(
partition by c.contract_number
order by m.priority desc
) as rn
from table_contracts c
left join table_mappings m on(
(c.attribute_1 = m.attribute_1 or m.attribute_1 is null)
and (c.attribute_2 = m.attribute_2 or m.attribute_2 is null)
and (c.attribute_3 = m.attribute_3 or m.attribute_3 is null)
)
)
select *
from prioritized
where rn = 1
Try out the below logic using CTE version similar to yours. Hope it helps!
Demo
WITH contracts AS
(SELECT 123 AS contract_number, 'a' AS attribute_1, 'e' AS attribute_2, 't' AS attribute_3 FROM dual
UNION
SELECT 456, 'a', 'f', 's' FROM dual
UNION SELECT 789, 'b', 'g', 's' FROM dual
),
mappings AS
(SELECT 'm1' AS map_number, 5 AS priority, 'a' AS attribute_1, 'e' AS attribute_2, 't' AS attribute_3 FROM dual
UNION
SELECT 'm2', 4, 'a', NULL, NULL FROM dual
UNION
SELECT 'm3', 3, NULL, NULL, NULL FROM dual
),
prioritymap AS
(SELECT contract_number,
map_number,
Rank() over(PARTITION BY contracts.contract_number ORDER BY mappings.priority DESC) AS rank
FROM contracts
JOIN mappings
ON ( contracts.attribute_1 = mappings.attribute_1 OR mappings.attribute_1 IS NULL )
AND ( contracts.attribute_2 = mappings.attribute_2 OR mappings.attribute_2 IS NULL )
AND ( contracts.attribute_3 = mappings.attribute_3 OR mappings.attribute_3 IS NULL )
)
SELECT contract_number, map_number
FROM prioritymap
WHERE prioritymap.rank = 1
You can simply join the tables on the condition given (an attribute is null in the maping table or must match the attribute in the contracts table). Then aggregate per contract number to get the best map_number.
select
c.contract_number,
max(m.map_number) keep (dense_rank last order by m.priority) as map_number
from table_contracts c
join table_mappings m
on (m.attribute_1 is null or m.attribute_1 = c.attribute_1)
and (m.attribute_2 is null or m.attribute_2 = c.attribute_2)
and (m.attribute_3 is null or m.attribute_3 = c.attribute_3)
group by c.contract_number
order by c.contract_number;
Anyway, you are doing this for all contracts and a mapping may match on any combination of attributes, so this will lead to full table scans. The only way I can see to get this quicker is parallel excecution. Maybe the DBMS is set to do this automatically, otherwise you can use a hint:
select /*+parallel(4)*/
...

How can I get percentage of each answer in sql?

I have a poll system. I use this query to get count of every answer of question. How can I get percentage of every answered option like this:
question 1 ---> option1=20.13 % ---> option2=79.87 %
question 2 ---> option3=100 %
question 3 ---> option4=30 % ---> option5=70 %
....
I tried this query But is not my desire:
[getPollResult]
#poll_form_id int
AS
BEGIN
SELECT a.question_id,a.title,COUNT(*) vote
FROM tbl_poll_option a
JOIN tbl_poll_answer b ON a.Id=b.option_id
JOIN tbl_poll_question c ON a.question_id=c.Id
WHERE poll_form_id=#poll_form_id
GROUP BY a.title,a.question_id
END
I agree with the advice of doing calculations (like percentages) on the front end.
Having said that, this might be helpful for you.
Design limitations: this is suitable for relatively small data sets, and I find the "with()" syntax easier to work with than making temp tables and hoping I remember to clean them up. (for more on the "with...as.." sytnax see here ).
The following sql (untested) attempts to generate two temporary result sets.
counts_by_option - # of votes for each question+option pair
counts_by_question - total # of votes for each question
Let's ease into it:
counts_by_option
This is more or less what you started with (just a little more actually, I think we'll likely want to know both question & option later, so we grab both of them now):
with counts_by_option( question_id, question_title, option_id, option_title, vote_cnt )
as (
SELECT opt.question_id
, quest.title as question_title
, opt.id as option_id
, opt.title as option_title
, COUNT(*) vote_cnt
FROM tbl_poll_option opt
JOIN tbl_poll_answer ans ON ans.option_id = opt.Id
JOIN tbl_poll_question quest ON quest.Id = opt.question_id
WHERE poll_form_id=#poll_form_id
GROUP BY opt.question_id, quest.title, opt.id, opt.title
)
select * from counts_by_option
order by question_id, option_id
If that works, let's extend the sql and add our next temporary result set...
counts_by_question
Now we can use our option-totals to generate the grand total of votes for each question; we'll need that in a bit to compute the actual %ge.
with counts_by_option( question_id, question_title, option_id, option_title, vote_cnt )
as (
SELECT opt.question_id
, quest.title as question_title
, opt.id as option_id
, opt.title as option_title
, COUNT(*) vote_cnt
FROM tbl_poll_option opt
JOIN tbl_poll_answer ans ON ans.option_id = opt.Id
JOIN tbl_poll_question quest ON quest.Id = opt.question_id
WHERE poll_form_id=#poll_form_id
GROUP BY opt.question_id, quest.title, opt.id, opt.title
)
-- select * from counts_by_option order by question_id, option_id
-- (I like to comment out select but leave in place for future testing...)
, counts_by_question( question_id, question_total_votes )
as (
select question_id, sum(vote_cnt) as question_total_votes
from counts_by_option group by question_id
)
select * from counts_by_question order by question_id
If that worked we are (finally) in a position to answer the original question about percentages:
percentages example
with counts_by_option( question_id, question_title, option_id, option_title, vote_cnt )
as (
SELECT opt.question_id
, quest.title as question_title
, opt.id as option_id
, opt.title as option_title
, COUNT(*) vote_cnt
FROM tbl_poll_option opt
JOIN tbl_poll_answer ans ON ans.option_id = opt.Id
JOIN tbl_poll_question quest ON quest.Id = opt.question_id
WHERE poll_form_id=#poll_form_id
GROUP BY opt.question_id, quest.title, opt.id, opt.title
)
-- select * from counts_by_option order by question_id, option_id
-- (I like to comment out select but leave in place for future testing...)
, counts_by_question( question_id, question_total_votes )
as (
select question_id, sum(vote_cnt) as question_total_votes
from counts_by_option group by question_id
)
-- select * from counts_by_question order by question_id
select question_id
, question_title
, option_id
, option_title
, vote_cnt
, (100.0 * vote_cnt)
/ (select b.question_total_votes
from counts_by_question b
where b.question_id = a.question_id
) as option_percentage
from counts_by_option a
order by question_id, vote_cnt desc
The result set is driven by counts_by_option a.
The percentages expression asks counts_by_question b for help.
Let's focus on the expression for option_percentage:
(100.0 * vote_cnt)
/ (select b.question_total_votes
from counts_by_question b
where b.question_id = a.question_id
)
as option_percentage
This is kind of complex (which is why I prefer to do things like aggregations in the front end) but not too bad...
We start by multiplying vote_count by 100.0.
Then we use the current OPTION to drive a sub-query that hits question_total_values to find our current question's total number of votes.
Note the qualifiers a and b which are important to focus our subquery against counts_by_question on only a's current question using this where clause: where b.question_id = a.question_id (important since a subquery like that can only return a single value, otherwise it errors out at execution time).
This would blow up if any questions could have zero total votes (e.g. division by zero error).
edit: The way counts_by_option is constructed, only questions with answers (table tbl_poll_answer) would be used, so all of the values in counts_by_question will be non-zero.
The schema for the above case
CREATE TABLE #POLL_QUESTION (QUESTION_ID INT, QUESTION VARCHAR(50))
INSERT INTO #POLL_QUESTION
SELECT 1,'WHAT?'
UNION ALL
SELECT 2,'WHEN?'
UNION ALL
SELECT 3,'WHY?'
UNION ALL
SELECT 4,'WHERE?'
CREATE TABLE #POLL_OPTION(OPTION_ID INT , QUESTION_ID INT, OPTION_NME VARCHAR(50))
INSERT INTO #POLL_OPTION
SELECT 1,1,'A'
UNION ALL
SELECT 2,1,'B'
UNION ALL
SELECT 3,1,'C'
UNION ALL
SELECT 4,2,'A'
UNION ALL
SELECT 5,2,'B'
UNION ALL
SELECT 6,2,'C'
UNION ALL
SELECT 7,3,'A'
UNION ALL
SELECT 8,3,'B'
UNION ALL
SELECT 9,3,'C'
UNION ALL
SELECT 10,4,'A'
UNION ALL
SELECT 11,4,'B'
UNION ALL
SELECT 12,4,'C'
CREATE TABLE #POLL_ANSWER(ANSWER_ID INT, OPTION_ID INT)
INSERT INTO #POLL_ANSWER
SELECT 1,2
UNION ALL
SELECT 2,2
UNION ALL
SELECT 3,3
UNION ALL
SELECT 4,4
UNION ALL
SELECT 5,4
UNION ALL
SELECT 6,4
UNION ALL
SELECT 7,5
UNION ALL
SELECT 8,7
UNION ALL
SELECT 9,8
UNION ALL
SELECT 10,9
UNION ALL
SELECT 11,10
UNION ALL
SELECT 12,10
The Statment for the data
SELECT Q.QUESTION_ID,Q.QUESTION,O.OPTION_NME,COUNT(O.OPTION_NME)COUNT_OPTION,LEFTQUERY.COUNT_QUESTION, (COUNT(O.OPTION_NME)*100)/LEFTQUERY.COUNT_QUESTION AS PER FROM #POLL_QUESTION Q
INNER JOIN #POLL_OPTION O ON Q.QUESTION_ID=O.QUESTION_ID
INNER JOIN #POLL_ANSWER A ON O.OPTION_ID= A.OPTION_ID
LEFT JOIN
(
SELECT Q.QUESTION_ID,Q.QUESTION, COUNt(O.OPTION_NME)COUNT_QUESTION FROM #POLL_QUESTION Q
INNER JOIN #POLL_OPTION O ON Q.QUESTION_ID=O.QUESTION_ID
INNER JOIN #POLL_ANSWER A ON O.OPTION_ID= A.OPTION_ID
GROUP BY Q.QUESTION_ID,Q.QUESTION
)AS LEFTQUERY ON Q.QUESTION_ID= LEFTQUERY.QUESTION_ID
GROUP BY Q.QUESTION_ID,Q.QUESTION,O.OPTION_NME,
LEFTQUERY.COUNT_QUESTION
select
q.title,
o.title,
CAST(count(distinct o.id) as float)
/ (SELECT count(allo.id)
FROM option allo
WHERE q.option_FK = allo.id)
* 100.0,
FROM question q
inner join option o on q.option_FK = o.id
GROUP BY q.id, q.title, q.option_FK, o.id, o.title
(probably needs some cleanup around the cast).
Would return this list:
question 1, option1, 20.13
question 1, option2, 79.87
question 2, option3, 100
question 3, option4, 30
question 3, option5, 70
It could be further processed ideally in a higher programming language.