-3

我有不同的 sql,它返回不同的结果。我需要将它们全部按列获取。但我不知道该怎么做。有人可以帮我吗?下面是我的sql >>>

    SELECT  "one" AS value_1,
    "two" AS value_2

SELECT  "three" AS value_3,
    "four" AS value_4

SELECT  "five" AS value_5,
    "six" AS value_6

主要查询 1>>>

    SELECT -- lat.`group_info_id`, gi.`group_name`, 
    SUM(lat.`principal_outstanding`) AS principalOutstanding -- , SUM(lat.`accrued_interest`) AS interestReceivable, SUM(lat.`realizable_interest`) AS interestRealizable
    -- , SUM(lat.`principal_outstanding`)+SUM(lat.`accrued_interest`)+SUM(lat.`realizable_interest`) AS loanDue, SUM(lat.`outstanding_balance`) AS totalLoanDue
FROM loan_account_transaction AS lat
INNER JOIN loan_account AS la ON (la.id = lat.`loan_account_id`)
LEFT JOIN group_info AS gi ON (gi.id = lat.`group_info_id`)
INNER JOIN
(
SELECT MAX(ilat.id) AS max_lat_id, ilat.loan_account_id 
FROM loan_account_transaction AS ilat
WHERE ilat.`domain_status_id` = 1 AND ilat.`office_info_id` = (SELECT id FROM office_info WHERE office_code = 'UG500002')
AND ilat.`project_info_id` IN ('BI0000000000000000000004') AND ilat.transaction_date <= '2013-04-30'
GROUP BY ilat.loan_account_id
) AS tlat ON (tlat.loan_account_id = lat.`loan_account_id` AND tlat.max_lat_id = lat.id)
WHERE lat.`office_info_id` = (SELECT id FROM office_info WHERE office_code = 'UG500002') 
AND lat.`project_info_id` IN ('BI0000000000000000000004') 
AND lat.`loan_status_id` != 2 AND lat.`loan_status_id` != 7 AND la.`domain_status_id` = 1 AND lat.`domain_status_id` = 1

主查询 2 >>>

 SELECT     SUM(CASE WHEN la.loan_duration = 12 THEN 1 ELSE 0 END) AS total_count_40,
    SUM(CASE WHEN la.loan_duration = 12 THEN IFNULL(la.disbursed_amount,0) ELSE 0 END) AS disbursed_amount_40,
    SUM(CASE WHEN la.loan_duration = 6 THEN 1 ELSE 0 END) AS total_count_20,
    SUM(CASE WHEN la.loan_duration = 6 THEN IFNULL(la.disbursed_amount,0) ELSE 0 END) AS disbursed_amount_20
FROM    loan_account AS la
INNER JOIN member_info AS mi ON (la.disbursement_date <='2013-04-30'
    AND la.project_info_id IN ('BI0000000000000000000004')
    AND la.domain_status_id = 1 AND la.member_id=mi.id )
INNER JOIN office_info AS oi
 ON( oi.id = 'BI0000000000000000000363' AND mi.branch_info_id = oi.id )

** 新的 >>>

    SELECT  COUNT(1), SUM(principal_outstanding) 
FROM
(

/* Current Loan */
        SELECT  tempData.country_id,
         tempData.project_info_id,
        tempData.prodId,
        tempData.loan_name,
       tempData.office_info_id,
        office_info.office_code,
        office_info.office_name,
        tempData.group_info_id,
        group_info.group_code,
        group_info.group_name,
        missed_days,
        SUM(tempData.principal_outstanding) AS principal_outstanding,
        COUNT(DISTINCT member_id) AS borrower_count

        FROM (
                SELECT lat.country_id,lat.office_info_id,lat.project_info_id, lp.id AS prodId, lp.`loan_name`,
                lat.group_info_id,
                lat.principal_outstanding,la.member_id,

                (CEIL(lat.overdue_amount/la.installment_amount) *
                        CASE WHEN lpp.policy_repayments_plan_id = 1 THEN
                            1
                        WHEN lpp.policy_repayments_plan_id = 2 THEN
                            7
                        WHEN lpp.policy_repayments_plan_id = 3 THEN
                            30
                        WHEN lpp.policy_repayments_plan_id = 4 THEN
                            365
                        END) AS missed_days
                FROM loan_account_transaction AS lat
                INNER JOIN loan_account AS la ON (la.id = lat.loan_account_id)
                INNER JOIN loan_product AS lp ON (la.`product_info_id` = lp.`id`)
                INNER JOIN loan_product_policy AS lpp ON(lpp.id = la.policy_info_id)
                INNER JOIN (SELECT MAX(lat2.id) AS max_lat2_id,lat2.loan_account_id FROM loan_account_transaction AS lat2
                WHERE lat2.transaction_date <=   '2013-03-31'   AND
                lat2.domain_status_id = 1 AND
                 lat2.country_id IN (1) AND 
                 lat2.office_info_id IN ('BI0000000000000000000363') AND 
                 lat2.project_info_id IN ('BI0000000000000000000004') 
                GROUP BY lat2.loan_account_id
            ) AS temp ON lat.id = temp.max_lat2_id
            WHERE (lat.loan_status_id= 1  OR lat.loan_status_id IS NULL)
        ) AS tempData
        INNER JOIN office_info ON (office_info.id = tempData.office_info_id)
        LEFT JOIN group_info ON (group_info.id = tempData.group_info_id)
        WHERE missed_days > 30
        GROUP BY
        tempData.country_id,
        tempData.office_info_id,
        tempData.project_info_id,
        tempData.prodId,
        tempData.group_info_id,
        missed_days

UNION

/* Late and NIBL Loan */
        SELECT  tempData.country_id,tempData.project_info_id,
                tempData.prodId,
        tempData.loan_name,
                tempData.office_info_id,office_info.office_code,office_info.office_name,
                tempData.group_info_id,group_info.group_code,group_info.group_name,
                tempData.missed_days,SUM(tempData.principal_outstanding) AS principal_outstanding,
                COUNT(DISTINCT tempData.member_id) AS borrower_count
        FROM
        (SELECT lat.country_id,lat.office_info_id,lat.project_info_id, lp.id AS prodId, lp.`loan_name`,
                lat.group_info_id,
                    (CASE WHEN lat.loan_status_id = 3 THEN
                        (la.loan_duration*30) -- +(la.loan_duration*30/2)
                    WHEN lat.loan_status_id = 4 THEN
                        (la.loan_duration*30) -- *2
                    WHEN lat.loan_status_id = 5 THEN
                        (la.loan_duration*30) -- *2+7
                    WHEN lat.loan_status_id = 6 THEN
                        (la.loan_duration*30) -- *2+7
                    END) AS missed_days,
                lat.principal_outstanding,la.member_id
                FROM loan_account_transaction AS lat
                INNER JOIN loan_account AS la ON (la.id = lat.loan_account_id)
                INNER JOIN loan_product AS lp ON (la.`product_info_id` = lp.`id`)
                INNER JOIN (SELECT MAX(lat2.id) AS max_lat2_id,lat2.loan_account_id FROM
                loan_account_transaction AS lat2
                WHERE lat2.transaction_date <=   '2013-03-31'   AND
                lat2.domain_status_id = 1 AND
                 lat2.country_id IN (1) AND 
                 lat2.office_info_id IN ('BI0000000000000000000363') AND 
                 lat2.project_info_id IN ('BI0000000000000000000004') 
                GROUP BY lat2.loan_account_id
            ) AS temp ON lat.id = temp.max_lat2_id
                WHERE (lat.loan_status_id != 2 AND lat.loan_status_id != 7 AND lat.loan_status_id = 1)
        ) AS tempData
        INNER JOIN office_info ON (office_info.id = tempData.office_info_id)
        LEFT JOIN group_info ON (group_info.id = tempData.group_info_id)
        GROUP BY tempData.country_id,
        tempData.office_info_id,
        tempData.project_info_id,
        tempData.prodId,
        tempData.group_info_id,
        missed_days

        ) AS t
        GROUP BY t.prodId
4

3 回答 3

3

Yu可以使用以下查询,

select * from
    (
        (SELECT  'one' AS value_1, 'two' AS value_2) A

    JOIN (SELECT  'three' AS value_3, 'four' AS value_4) B  ON 1=1

    JOIN (SELECT  'five' AS value_5, 'six' AS value_6) C  ON 1=1
    )

SQL 小提琴演示

新问题的答案:

我不确定这是否可行。但只是尝试一下。

SELECT * from
 (SELECT  COUNT(1) COUNT1, SUM(principal_outstanding) SUM1
FROM
(

/* Current Loan */
        SELECT  tempData.country_id,
         tempData.project_info_id,
        tempData.prodId,
        tempData.loan_name,
       tempData.office_info_id,
        office_info.office_code,
        office_info.office_name,
        tempData.group_info_id,
        group_info.group_code,
        group_info.group_name,
        missed_days,
        SUM(tempData.principal_outstanding) AS principal_outstanding,
        COUNT(DISTINCT member_id) AS borrower_count

        FROM (
                SELECT lat.country_id,lat.office_info_id,lat.project_info_id, lp.id AS prodId, lp.`loan_name`,
                lat.group_info_id,
                lat.principal_outstanding,la.member_id,

                (CEIL(lat.overdue_amount/la.installment_amount) *
                        CASE WHEN lpp.policy_repayments_plan_id = 1 THEN
                            1
                        WHEN lpp.policy_repayments_plan_id = 2 THEN
                            7
                        WHEN lpp.policy_repayments_plan_id = 3 THEN
                            30
                        WHEN lpp.policy_repayments_plan_id = 4 THEN
                            365
                        END) AS missed_days
                FROM loan_account_transaction AS lat
                INNER JOIN loan_account AS la ON (la.id = lat.loan_account_id)
                INNER JOIN loan_product AS lp ON (la.`product_info_id` = lp.`id`)
                INNER JOIN loan_product_policy AS lpp ON(lpp.id = la.policy_info_id)
                INNER JOIN (SELECT MAX(lat2.id) AS max_lat2_id,lat2.loan_account_id FROM loan_account_transaction AS lat2
                WHERE lat2.transaction_date <=   '2013-03-31'   AND
                lat2.domain_status_id = 1 AND
                 lat2.country_id IN (1) AND 
                 lat2.office_info_id IN ('BI0000000000000000000363') AND 
                 lat2.project_info_id IN ('BI0000000000000000000004') 
                GROUP BY lat2.loan_account_id
            ) AS temp ON lat.id = temp.max_lat2_id
            WHERE (lat.loan_status_id= 1  OR lat.loan_status_id IS NULL)
        ) AS tempData
        INNER JOIN office_info ON (office_info.id = tempData.office_info_id)
        LEFT JOIN group_info ON (group_info.id = tempData.group_info_id)
        WHERE missed_days > 30
        GROUP BY
        tempData.country_id,
        tempData.office_info_id,
        tempData.project_info_id,
        tempData.prodId,
        tempData.group_info_id,
        missed_days ) g1
        GROUP BY g1.prodId )t1



/* Late and NIBL Loan */
       JOIN (SELECT  COUNT(1) COUNT2, SUM(principal_outstanding) SUM2
FROM(SELECT  tempData.country_id,tempData.project_info_id,
                tempData.prodId,
        tempData.loan_name,
                tempData.office_info_id,office_info.office_code,office_info.office_name,
                tempData.group_info_id,group_info.group_code,group_info.group_name,
                tempData.missed_days,SUM(tempData.principal_outstanding) AS principal_outstanding,
                COUNT(DISTINCT tempData.member_id) AS borrower_count
        FROM
        (SELECT lat.country_id,lat.office_info_id,lat.project_info_id, lp.id AS prodId, lp.`loan_name`,
                lat.group_info_id,
                    (CASE WHEN lat.loan_status_id = 3 THEN
                        (la.loan_duration*30) -- +(la.loan_duration*30/2)
                    WHEN lat.loan_status_id = 4 THEN
                        (la.loan_duration*30) -- *2
                    WHEN lat.loan_status_id = 5 THEN
                        (la.loan_duration*30) -- *2+7
                    WHEN lat.loan_status_id = 6 THEN
                        (la.loan_duration*30) -- *2+7
                    END) AS missed_days,
                lat.principal_outstanding,la.member_id
                FROM loan_account_transaction AS lat
                INNER JOIN loan_account AS la ON (la.id = lat.loan_account_id)
                INNER JOIN loan_product AS lp ON (la.`product_info_id` = lp.`id`)
                INNER JOIN (SELECT MAX(lat2.id) AS max_lat2_id,lat2.loan_account_id FROM
                loan_account_transaction AS lat2
                WHERE lat2.transaction_date <=   '2013-03-31'   AND
                lat2.domain_status_id = 1 AND
                 lat2.country_id IN (1) AND 
                 lat2.office_info_id IN ('BI0000000000000000000363') AND 
                 lat2.project_info_id IN ('BI0000000000000000000004') 
                GROUP BY lat2.loan_account_id
            ) AS temp ON lat.id = temp.max_lat2_id
                WHERE (lat.loan_status_id != 2 AND lat.loan_status_id != 7 AND lat.loan_status_id = 1)
        ) AS tempData
        INNER JOIN office_info ON (office_info.id = tempData.office_info_id)
        LEFT JOIN group_info ON (group_info.id = tempData.group_info_id)
        GROUP BY tempData.country_id,
        tempData.office_info_id,
        tempData.project_info_id,
        tempData.prodId,
        tempData.group_info_id,
        missed_days

        ) AS g2
        GROUP BY g2.prodId) t2 ON 1=1
于 2013-05-27T06:53:03.000 回答
0

这可能是你的意思吗?

SELECT id, column1 AS data
FROM table1
UNION
SELECT id, column2 AS data
FROM table2
UNION
SELECT id, column3 AS data
FROM table3;

column1, column2,column3应该都具有相同的数据类型,并且将它们别名为具有相同的名称是让UNIONs 工作的原因。

于 2013-05-27T04:46:33.430 回答
0
      
 与 cte(值 1,值 2)
                      作为(选择“一个”作为值_1,
                                    '两个' 作为 value_2
                         ),
                    cte2
                      作为(选择“三个”作为 value_3,
                                    '四' 作为 value_4
                         ),
                    cte3
                      作为(选择“五”作为值_5,
                                    '六' AS value_6
                         )
                选择 *
                从 cte ,
                        cte2 ,
                        cte3

于 2013-05-27T05:38:26.543 回答