如何 运行 多个 postgresql 语句而不丢失别名
How to run multiple postgresql statements without losing alias
我有大约 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)
谢谢。
关于你的第一个问题,联合后,联合后的table的列名将丢失。如果您没有为结果的列名设置别名,将使用第一个 table 的列名。如果您需要区分哪一行来自哪一行 table,我建议您添加另一列,即(第一个 table 为 1,第二个 table 为 2,...)但是,恕我直言,您也可以考虑单独获得此结果。
关于你的第二个问题,在 pgadmin 查询 window 上有一个按钮(带有绿色播放三角形和保存图标的按钮)可以将结果保存到文件中。它为您提供 select 使用哪个字符分隔列等选项。
如果您想要单个 table 作为所有联合的结果,您可以将别名给出的信息作为字符串放在第一个字段中,以类似于此的方式更改每个查询:
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
...
通过这种方式,您将拥有一个包含两列的大 table,第一列标记为 Info,提供理解第二列中值所需的信息,标记为 Value。
我有大约 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)
谢谢。
关于你的第一个问题,联合后,联合后的table的列名将丢失。如果您没有为结果的列名设置别名,将使用第一个 table 的列名。如果您需要区分哪一行来自哪一行 table,我建议您添加另一列,即(第一个 table 为 1,第二个 table 为 2,...)但是,恕我直言,您也可以考虑单独获得此结果。
关于你的第二个问题,在 pgadmin 查询 window 上有一个按钮(带有绿色播放三角形和保存图标的按钮)可以将结果保存到文件中。它为您提供 select 使用哪个字符分隔列等选项。
如果您想要单个 table 作为所有联合的结果,您可以将别名给出的信息作为字符串放在第一个字段中,以类似于此的方式更改每个查询:
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
...
通过这种方式,您将拥有一个包含两列的大 table,第一列标记为 Info,提供理解第二列中值所需的信息,标记为 Value。