我在 SQL Server 中有一个表,用户需要在其中的两列中输入数据。也就是说,两者之一必须输入数据,但同时我不想让用户在两列中输入。它要么是要么,但一个是必须的。
我在我的数据库上运行这个选择查询:
Select Item.ItemId, Item.ItemDescription, Bid.BidPrice, bid.BidDate,
Sum(bid.bidPrice) over () as TotalBids
from Bid
inner join Item on bid.ItemId=Item.ItemId
where BidDate between '2016-08-24' and '2017-11-15'
Run Code Online (Sandbox Code Playgroud)
我得到以下结果:
ItemId ItemDescription BidPrice BidDate TotalBids
1 Frame 35 2016-08-24 3624
4 Wooden chair 40 2016-10-25 3624
2 Car 3000 2017-10-26 3624
3 Stand Fan 29 2017-10-30 3624
5 Black Sofa 400 2017-11-11 3624
6 Cabinet 120 2017-11-15 3624
Run Code Online (Sandbox Code Playgroud)
我的问题是:是否有可能Total Bids
在每行的列中得到一个总数,而不是在列的底部得到一个总数BidPrice
?
我在我的 sql server 2012 数据库上运行以下查询。
select Appointment.AppointmentDate, Appointment.AppointmentTime, Auctioneer.AuctioneerName, Auctioneer.AuctioneerSurname, Buyer.BuyerName, Buyer.BuyerSurname
from Appointment
inner join Auctioneer on Appointment.AuctioneerId=Auctioneer.AuctioneerId
inner join Buyer on Appointment.BuyerId=Buyer.BuyerId
where AppointmentDate between '2017-01-01' and '2017-12-31'
Run Code Online (Sandbox Code Playgroud)
结果如下:
+-----------------+-----------------+----------------+-------------------+-----------+----------------+
| AppointmentDate | AppointmentTime | AuctioneerName | AuctioneerSurname | BuyerName | BuyerSurname |
+-----------------+-----------------+----------------+-------------------+-----------+----------------+
| 2017-10-23 | 13:00:00 | Mary | Borg | David | Borg |
| 2017-10-24 | 15:30:00 | Mary | Borg | Joseph | Sammut |
| 2017-11-03 | 09:30:00 | Joseph | Smith …
Run Code Online (Sandbox Code Playgroud)