查询以将行值与列匹配

huangapple go评论104阅读模式
英文:

Query to tick rows values against colums

问题

对于网页内容,我想编写PL SQL查询。在这个网页上,有产品的列,如CAR、BIKE、VAN、LORRY、JEEP等。在同一页上有客户的行。

一旦客户拥有一辆CAR,它应该被打勾。同一个客户可以拥有多个产品。因此,他可以在他的名字下打勾多个产品。

如何编写这样的查询?

视图:

  1. cars bike van jeep lorry cab bus
  2. john * * *
  3. smith *
  4. shane * *
  5. mark * * * * *

(请注意,视图的格式在文本中可能不太清晰,实际查询需要根据数据库架构来编写。)

英文:

For a web page content I want to write PL SQL Query. In this web page have columns of products such as CAR,BIKE,VAN,LORRY,JEEP etc. In the same page have rows of customers.

Once the customer have a CAR, It should be ticked. Same customer can have multiple products. Therefore he can be ticked more products against his name.

How to write a query for this?

View:

  1. cars bike van jeep lorry cab bus
  2. john * * *
  3. smith *
  4. shane * *
  5. mark * * * * *

答案1

得分: 1

一个查询可以像这样创建:

  1. select c.name as "name",
  2. max(case when s.product_id = 1 then '*' end) as "cars",
  3. max(case when s.product_id = 2 then '*' end) as "bike",
  4. max(case when s.product_id = 3 then '*' end) as "van",
  5. max(case when s.product_id = 4 then '*' end) as "jeep",
  6. max(case when s.product_id = 5 then '*' end) as "lorry",
  7. max(case when s.product_id = 6 then '*' end) as "cab",
  8. max(case when s.product_id = 7 then '*' end) as "bus"
  9. from sells s
  10. left join customers c on c.id = s.customer_id
  11. group by c.id, c.name
  12. order by c.id

使用 illustration 进行演示。

英文:

A query might be created like

  1. select c.name as "name",
  2. max(case when s.product_id = 1 then '*' end) as "cars",
  3. max(case when s.product_id = 2 then '*' end) as "bike",
  4. max(case when s.product_id = 3 then '*' end) as "van",
  5. max(case when s.product_id = 4 then '*' end) as "jeep",
  6. max(case when s.product_id = 5 then '*' end) as "lorry",
  7. max(case when s.product_id = 6 then '*' end) as "cab",
  8. max(case when s.product_id = 7 then '*' end) as "bus"
  9. from sells s
  10. left join customers c on c.id = s.customer_id
  11. group by c.id, c.name
  12. order by c.id

with the <kbd>illustration</kbd>

huangapple
  • 本文由 发表于 2020年1月3日 14:42:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定