简体   繁体   English

如何将查询结果显示为字符串的一部分?

[英]How do I display my query results as part of a string?

My database is for a school project and handles various jobs/contracts which are ran by consultants. 我的数据库用于学校项目,并处理顾问执行的各种工作/合同。 Apologies if the context is vague, we haven't really been given much either. 如果上下文含糊,我们深表歉意,我们也没有得到太多帮助。

My current code does what it is suppose to, it selects the forename and surname of the consultant which has led the most contracts/jobs. 我当前的代码执行了预期的工作,它选择了负责最多合同/工作的顾问的姓和名。 However I need to display the result in a specific format. 但是,我需要以特定格式显示结果。

SELECT DISTINCT CONSULTANT_FNAME, CONSULTANT_SNAME
FROM CONSULTANT LEFT JOIN CONTRACT ON CONSULTANT_ID = CONTRACT_LEAD
WHERE CONTRACT_LEAD IN(
SELECT CONTRACT_LEAD
FROM CONTRACT
GROUP BY CONTRACT_LEAD
ORDER BY COUNT(CONTRACT_LEAD) DESC
FETCH NEXT 1 ROWS ONLY);

Currently the result I am getting is: 目前,我得到的结果是:

CONSULTANT_FNAME     CONSULTANT_SNAME
-------------------- --------------------
Cloud                Strife

However I need to display the result as "The consultant who has led the most contracts is: Cloud Strife". 但是,我需要将结果显示为“领导合同最多的顾问是:Cloud Strife”。

You shouldn't need DISTINCT - you only put it in there because the LEFT JOIN is causing the consultant rows to repeat (multiple contracts per one consultant) but you don't need the left join either 您不需要DISTINCT-您只需将其放在那里,因为LEFT JOIN会导致顾问行重复(每位顾问有多个合同),但是您也不需要左联接

SELECT 
  'The consultant who has led the most contracts is: ' || CONSULTANT_FNAME || ' ' || CONSULTANT_SNAME
FROM CONSULTANT 
WHERE CONSULTANT_ID IN(
  SELECT CONTRACT_LEAD
  FROM CONTRACT
  GROUP BY CONTRACT_LEAD
  ORDER BY COUNT(CONTRACT_LEAD) DESC
  FETCH NEXT 1 ROWS ONLY
)

The subquery inside the IN returns the most active contract lead id, but this is the same thing as the consultant id - you don't need to join. IN中的子查询返回最活跃的合同潜在客户ID,但这与顾问ID相同-您无需加入。

I believe you could also do this: 我相信您也可以这样做:

SELECT 
  'The consultant who has led the most contracts is: ' || CONSULTANT_FNAME || ' ' || CONSULTANT_SNAME
FROM 
  CONSULTANT 
  INNER JOIN CONTRACT ON CONSULTANT_ID = CONTRACT_LEAD
GROUP BY CONSULTANT_FNAME, CONSULTANT_SNAME
ORDER BY COUNT(*) DESC
FETCH NEXT 1 ROWS ONLY

The reason why this works is that grouping on name is effectively the same as grouping on id : it gives a count of how many times the contract table has caused a repeat. 之所以可行,是因为按名称分组与按id分组实际上是相同的:它提供了合同表导致重复的次数的计数。 There is a slight bug risk with this though that if there really were two different people of identical names they would have their contracts added together. 但是,如果确实存在两个同名的人,他们的合同会加在一起,因此存在一个小错误风险。 To counter this you can add consultant_id to the group by - if you're doing a group by then you cannot use columns in the SELECT area that are not mentioned in the group by (unless you put them inside an aggregate function like min, max etc)but you ARE allowed to put columns in the group by that you then don't use in the select. 为了解决这个问题,您可以在group by中添加consultant_id-如果您要按group进行分组,则不能使用SELECT区域中未在group by中提及的列(除非您将它们放在诸如min,max之类的聚合函数中等等),但允许您将列放在该组中,然后在选择中不使用它们。 By adding the ID We provide a way to tell two identically names consultants from having their scores added together: 通过添加ID,我们提供了一种方法,可以告诉两个姓名相同的顾问,将他们的分数加在一起:

SELECT 
  'The consultant who has led the most contracts is: ' || s.CONSULTANT_FNAME || ' ' || s.CONSULTANT_SNAME
FROM 
  CONSULTANT s
  INNER JOIN CONTRACT t ON s.CONSULTANT_ID = t.CONTRACT_LEAD
GROUP BY s.CONSULTANT_FNAME, s.CONSULTANT_SNAME, s.CONSULTANT_ID
ORDER BY COUNT(*) DESC
FETCH NEXT 1 ROWS ONLY

The final learning point here; 这里的最后学习点; always alias your tables (I used s and t) and use the aliases. 总是为您的表加上别名(我使用s和t)并使用别名。 This prevents your queries from stopping working if someone adds a new column to eg contracts also called CONSULTANT_FNAME - without the alias your query will only work if column names are unambiguous, and db schema do change over time 如果有人将新列添加到合同(例如也称为CON​​SULTANT_FNAME的合同)中,这将防止您的查询停止工作-如果没有别名,则您的查询仅在列名明确且数据库架构随时间变化时才起作用

Adding table aliases also lets you join one table in twice, which can be vital if eg a consultant has a home_address_id and also a work_address_id that map to two different rows in the addresses table. 添加表别名还可以使一个表两次连接,这对于例如顾问的home_address_id和work_address_id映射到地址表中的两个不同行非常重要。 If you joined addresses in only once using home_address_id = address.id OR work_address_id = address.id you'd end up with two rows when the data would be more usable as one row. 如果使用home_address_id = address.id OR work_address_id = address.id仅将地址加入一次,那么当数据将更有用时,您将得到两行。 Here ends sql 101 :) but if you have more questions about this nugget of info see something like When to use SQL Table Alias 到这里结束sql 101 :),但是如果您对此信息有更多疑问,请参阅何时使用SQL表别名

You can use below query - 您可以使用以下查询-

SELECT DISTINCT 'The consultant who has led the most contracts is: ' || CONSULTANT_FNAME || ' ' || CONSULTANT_SNAME
FROM CONSULTANT LEFT JOIN CONTRACT ON CONSULTANT_ID = CONTRACT_LEAD
WHERE CONTRACT_LEAD IN(SELECT CONTRACT_LEAD
                       FROM CONTRACT
                       GROUP BY CONTRACT_LEAD
                       ORDER BY COUNT(CONTRACT_LEAD) DESC
                       FETCH NEXT 1 ROWS ONLY);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM