我有大约10个不同的查询。 使用UNION和UNION ALL会导致信息丢失,即别名消失
Q1。有没有办法在尝试使用UNION作为单个查询运行多个单独的查询时保留别名?
Q2。我打算导出到CSV文件并导入到excel。是否可以以特定格式生成查询结果?
SELECT 'dummy', count(*) as Total_Active_P
FROM prop
WHERE lifecycle_step = 2
UNION ALL
SELECT 'dummy', SUM(activities.compensation) as Daily_Exp
FROM activities
where DATE(due_at) = current_date - INTERVAL '1 day'
UNION ALL
select type, sum(compensation) as All_Activities_Comp
from activities
where DATE(due_at) = current_date - INTERVAL '1 day'
group by type
UNION ALL
select activities.type, sum(activity_fees.amount) as All_Activity_Fees
from activity_fees inner join activities
on activity_fees.activity_id = activities.id
where DATE(activities.due_at) = current_date - INTERVAL '1 day'
group by activities.type
UNION ALL
SELECT 'dummy', avg(activities.rating) as Avg_Vendor_Rating
FROM fellows
inner join authentications on authentications.authenticatable_id = fellows.id
inner join activities on activities.fellow_id = fellows.id
WHERE fellows.type in ('X', 'Y', 'Z')
and authentications.deactivated = false
and DATE(activities.due_at) = current_date - INTERVAL '1 day'
and activities.rating is not null
UNION ALL
SELECT 'dummy', COUNT(*) as Total_Active_Vendors
FROM fellows inner join authentications
on authentications.authenticatable_id = fellows.id
WHERE fellows.type in ('X', 'Y', 'Z')
and authentications.deactivated = false
UNION ALL
select 'dummy', (sum(total_cost / DATE_PART('day', checkout - checkin)) / count(*)) as Rev_Booked_Per_Prop
from reservations
where managed_by_owner = false
and canceled = false
and checkin <= current_date - INTERVAL '1 day'
and checkout >= current_timestamp - INTERVAL '1 day'
UNION ALL
select 'dummy', sum(total_cost / DATE_PART('day', checkout - checkin)) as Daily_Reservation_Rev
from reservations
where managed_by_owner = false
and canceled = false
and checkin <= current_date - INTERVAL '1 day'
and checkout >= current_timestamp - INTERVAL '1 day'
UNION ALL
SELECT 'dummy', count(activities) as Daily_Activities
FROM activities
where status = 3
and DATE(due_at) = current_date - INTERVAL '1 day'
UNION ALL
SELECT 'dummy', SUM(amount) AS Daily_Fee_Income
FROM reservations
INNER JOIN activities ON activities.reservation_id = reservations.id
INNER JOIN activity_fees ON activity_fees.activity_id = activities.id
WHERE (checkin <= current_timestamp - INTERVAL '1 day' AND checkout >= current_timestamp - INTERVAL '1 day')
UNION ALL
SELECT 'dummy', COUNT(*) as Total_Users FROM users
UNION ALL
SELECT 'dummy', avg(activities.compensation) as Avg_Cost_Per_Activity
FROM activities
WHERE (activities.status = 3)
AND (DATE(due_at) = (current_date - interval '1 day'))
UNION ALL
SELECT 'dummy', COUNT(*) as Daily_Booking
FROM reservations
where (checkin <= current_date + INTERVAL '-1 day' and checkout >= current_date + INTERVAL '-1 DAY')
UNION ALL
SELECT 'dummy', COUNT(DISTINCT users.id) as Total_Active_Users
FROM users INNER JOIN properties ON properties.user_id = users.id
INNER JOIN postal_addresses ON postal_addresses.postally_addressable_id = properties.id
AND postal_addresses.postally_addressable_type = 'Property'
and properties.calendar_availability > 0
WHERE (postal_addresses.service_area_id is not null)
谢谢。
答案 0 :(得分:1)
关于第一个问题,在联合之后,联合表的列名将丢失。如果没有为结果的列名设置别名,则将使用第一个表的列名。如果您需要区分哪一行来自哪个表,我建议您添加另一列,即(第一个表为1,第二个表为2,...)但是,您也可以考虑单独获得此结果。
关于第二个问题,在pgadmin查询窗口中有一个按钮(绿色播放三角形和保存图标的按钮),它将结果保存到文件中。它为您提供了选择使用分隔列等字符的选项。
答案 1 :(得分:1)
如果您想要一个表作为所有联盟的结果,您可以将别名给出的信息作为字符串放在第一个字段中,以类似于此的方式更改每个查询:
SELECT 'Total_Active_P' as Info , count(*) as Value
FROM prop
WHERE lifecycle_step = 2
UNION ALL
SELECT 'Daily_Exp' as Info, SUM(activities.compensation) as Value
FROM activities
where DATE(due_at) = current_date - INTERVAL '1 day'
UNION ALL
...
通过这种方式,您将拥有一个包含两列的大表,第一列标记为“信息”,提供了解第二列中标记为“值”所需的信息。