【发布时间】:2019-04-02 15:32:55
【问题描述】:
我写了一个返回数据的查询,这是我的代码
SELECT o.Id, p.ProductName, po.Quantity, p.Price, DeliveryMethod,
OrderDate, TotalSum, u.UserName FROM [Order] o
JOIN ProductsOrders po ON o.Id = po.OrderId
JOIN Product p ON po.ProductId = p.Id
JOIN Users u ON o.UserId = u.Id
ORDER BY o.Id
然后它返回下一个:
OrderId ProductName Quantity Price DeliveryMethod OrderDate TotalSum UserName
98 Bed3 1 3000 Express 2018-08-04 7015 Ivan
98 Bed4 1 4000 Express 2018-08-04 7015 Ivan
100 Bed2 1 2000 2018-08-05 2000 Ivan
101 Bed2 1 2000 2018-08-05 5000 Ivan
101 Bed3 1 3000 2018-08-05 5000 Ivan
102 Bed0 1 0 Standard 2018-09-04 1005 Ivan
我们可以注意到,对于每个产品 - 一条记录
但我想按一个订单对所有产品进行分组并得到类似的结果:
- orderId
- deliveryMethod
- userName
- product1 quantity price
- ...
- productN quantity price
- totalAmount
101
Courier
John Doe
Black bag 1 1500
Fat Bike 1 5000
Thermos bottle 5 200
Total amount 7500
如何修改查询?
【问题讨论】:
-
您想要做的工作看起来像是 SQL Server Reporting Services (SSRS)、Microsoft Excel 或其他实用程序的工作。以这种格式显示查询结果的任何尝试都类似于使用牙刷粉刷墙壁。
-
这个数据库有 ORM(即 Django)吗?如果您有与表对应的数据模型,那么您可以轻松地做到这一点。
标签: sql sql-server linq