我想知道如何创建一个SQL select语句,该语句可能在几列中可能为null。以下是一个可能的结果集:
Volume | Ticket | Hours | Rate | Amount
1234 10.5 190 1995
90 90 100 9000
1235 11 190 2090
90 90 110 9900
该卷与上述票证相关。因此,第一次显示故障单我们不想显示音量,但我们想要显示故障单#。如果有卷,我们希望下一行显示它的值,但不显示票证。
我提取的表格是Charges
。这些是列:
ChargeID, TicketNum, ProductID, Hours, Rate, Amount
其中ChargeID是主键。
如果有人有任何想法,他们将不胜感激。
答案 0 :(得分:1)
你可以做这样的事情(虽然这是在Oracle中) 不知道你为什么把这个问题标记为vba ..
with w_data as (
select 33 volume, 1234 ticket, 10.5 hours, 190 rate, 1995 amount from dual union all
select 90 volume, 1234 ticket, 90 hours, 100 rate, 9000 amount from dual union all
select 33 volume, 1235 ticket, 11 hours, 190 rate, 2090 amount from dual union all
select 90 volume, 1235 ticket, 90 hours, 110 rate, 9900 amount from dual
)
Select case when ticket = lag(ticket) over (partition by ticket order by ticket, volume) then volume else null end new_volume,
case when ticket = lag(ticket) over (partition by ticket order by ticket, volume) then null else ticket end new_ticket,
hours, rate, amount
from w_data
order by ticket, volume
/
NEW_VOLUME NEW_TICKET HOURS RATE AMOUNT
---------- ---------- ---------- ---------- ----------
1234 10.5 190 1995
90 90 100 9000
1235 11 190 2090
90 90 110 9900
4 rows selected.