【问题标题】:Issue in combining two queries组合两个查询的问题
【发布时间】:2013-07-17 12:25:29
【问题描述】:

我在获取一些数据时遇到问题。我有 4 个表
user
user_profile
user_income
tax_category

我需要选择适用于每个用户的税种。为此我有两个查询

第一个选择用户的总收入和年龄,第二个选择适用于用户的税种。但我无法将其转换为一个查询

SELECT userid,user_profile_gender,date_part('years',age(user_profile_dob))+1 AS userage,incomeresult.totalincome FROM user
LEFT JOIN user_profile ON user_profile_userid = user.userid 
INNER JOIN 
        (SELECT SUM(income_amount) totalincome, income_userid FROM user_income
        WHERE income_date BETWEEN '2012-03-30' AND '2014-03-30' 
        GROUP BY income_userid) AS incomeresult ON user.userid = incomeresult.income_userid     
WHERE user.status = 1

SELECT * FROM tax_category
WHERE 70 BETWEEN taxcategory_agefrom AND taxcategory_ageto AND taxcategory_gender=1 AND 12000 BETWEEN taxcategory_incomefrom AND taxcategory_incometo

在第二个查询中,70 应该是来自userage1 来自user_profile_gender12000 来自incomeresult.totalincome 的值。
是否可以创建这样的查询?我正在使用 PostgreSQL 8.4

【问题讨论】:

  • 确保 BETWEEN 运算符被赋予正确的端点。如果您的日期值没有时间组件,这可能符合您的要求。但是,如果存在时间组件,则将不包括在午夜(12:00:00 AM)之后发生的任何日期为 2014-03-30 的记录。您可能希望使用 2014-03-31 减去一毫秒作为上限。

标签: php sql postgresql


【解决方案1】:

我猜你可以使用 CTE 语法:

WITH A AS
(  
SELECT 
  userid,
  user_profile_gender,
  date_part('years',age(user_profile_dob))+1 AS userage,
  incomeresult.totalincome 
FROM 
  user
  LEFT JOIN user_profile ON user_profile_userid = user.userid 
  INNER JOIN 
        (SELECT SUM(income_amount) totalincome, income_userid FROM user_income
        WHERE income_date BETWEEN '2012-03-30' AND '2014-03-30' 
        GROUP BY income_userid) AS incomeresult ON user.userid = incomeresult.income_userid     
WHERE 
  user.status = 1
)
SELECT * 
FROM 
  tax_category
  INNER JOIN A 
WHERE A.userage BETWEEN taxcategory_agefrom AND taxcategory_ageto AND taxcategory_gender=1 AND A.totalincome BETWEEN taxcategory_incomefrom AND taxcategory_incometo

【讨论】:

    【解决方案2】:
    select
        userid,
        user_profile_gender,
        date_part('years', age(user_profile_dob)) + 1 as userage,
        incomeresult.totalincome,
        tax_category.*
    from
        user
        left join
        user_profile on user_profile_userid = user.userid 
        inner join 
            (
                select sum(income_amount) totalincome, income_userid
                from user_income
                where income_date between '2012-03-30' and '2014-03-30' 
                group by income_userid
            ) as incomeresult on user.userid = incomeresult.income_userid
        left join
        tax_category on 
            date_part('years', age(user_profile_dob)) + 1 between taxcategory_agefrom and taxcategory_ageto
            and taxcategory_gender = 1
            and totalincome between taxcategory_incomefrom and taxcategory_incometo
    where user.status = 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-06
      • 1970-01-01
      • 1970-01-01
      • 2016-09-28
      • 2018-08-07
      • 1970-01-01
      相关资源
      最近更新 更多