正确使用STUFF命令

时间:2018-03-02 16:04:03

标签: sql sql-server

我尝试使用以下代码获取stuff命令,但我似乎无法正确使用 - 抱歉,我对所有这些都是一个菜鸟。

以下代码返回21行,因为countries.countryname具有针对SpecSummary.specnumber保存的多个记录 - 我希望数据仅在1行中输出。请帮助!!

select SpecSummary.specnumber, countries.countryname 
from specsummary, gsmapprovedusage, gsmApprovedUsageCountryJoin, countries where 
 gsmapprovedusage.fkspecid=specsummary.specid 
 and gsmApprovedUsageCountryJoin.fkapprovedusage=gsmapprovedusage.pkid 
 and specsummary.SpecNumber = '5366443-002'
 and countries.langid=0 and countries.pkid=gsmApprovedUsageCountryJoin.fkcountry

2 个答案:

答案 0 :(得分:1)

由于不知道表格中存储了哪些数据,因此很难提供答案。哪些表包含唯一值,哪些表没有。要确定21行的来源,需要知道数据的存储方式。这些JOIN是一对一还是一对多。与此同时,我使用ANSI Join语法清理了您的查询。请尝试不要在WHERE子句中添加所有JOIN和过滤器。为了更好的可读性,请将JOIN与表保持一致,并将过滤器保留在WHERE子句中。

/* Alias your tables for easier readability as well as using the ANSI JOIN 
Syntax */
SELECT ss.specnumber, c.countryname
FROM specsummary ss /* Not sure what data is stored here without making assumptions */
  JOIN gsmapprovedusage u ON u.fkspecid = ss.specid /* Not sure what data is stored here */
  JOIN gsmapprovedusagecountryjoin gc ON gc.fkapprovedusage = u.pkid /* Not sure what data is stored here */
  JOIN countries c ON c.pkid = gc.fkcountry /* Lookup Table for Country Names */
WHERE /* Keep your JOINs out of the WHERE clause if you can. */
  /* The WHERE clause should mainly be reserved for your Filters */
  c.langid = 0
  AND ss.specnumber = '5366443-002'
;

一个很棒的阅读,最初让我转向使用ANSI JOIN语法: Please use ANSI join syntax

答案 1 :(得分:0)

请参阅我对您选择加入的列的评论。使用您的查询并对其进行调整以使JOIN有意义,请参阅下面的查询的工作版本,使用虚假数据,这可以通过大多数数据库中称为公用表表达式(CTE)或以其他方式称为WITH Block或WITH的功能实现。条款。我创建的查询产生了9个记录。我在Oracle数据库中工作。只知道我没有改变你的查询,除了我提到的BAD Join。只需选择ALL并运行查询。

WITH countries AS /* This is a CTE */
(
  /* Using the WITH Clause we can populate fake data of 21 rows in this table.  */
  /* This is known as a Common Table Expresion aka CTE                          */
  SELECT 1001 AS pkid, 'Country 1' AS countryname, 0 AS langid FROM dual UNION ALL
  SELECT 1002 AS pkid, 'Country 2' AS countryname, 0 AS langid FROM dual UNION ALL
  SELECT 1003 AS pkid, 'Country 3' AS countryname, 0 AS langid FROM dual UNION ALL
  SELECT 1004 AS pkid, 'Country 4' AS countryname, 0 AS langid FROM dual UNION ALL
  SELECT 1005 AS pkid, 'Country 5' AS countryname, 0 AS langid FROM dual UNION ALL
  SELECT 1006 AS pkid, 'Country 6' AS countryname, 0 AS langid FROM dual UNION ALL
  SELECT 1007 AS pkid, 'Country 7' AS countryname, 0 AS langid FROM dual UNION ALL
  SELECT 1008 AS pkid, 'Country 8' AS countryname, 0 AS langid FROM dual UNION ALL
  SELECT 1009 AS pkid, 'Country 9' AS countryname, 0 AS langid FROM dual UNION ALL
  SELECT 1010 AS pkid, 'Country 10' AS countryname, 0 AS langid FROM dual UNION ALL
  SELECT 1011 AS pkid, 'Country 11' AS countryname, 0 AS langid FROM dual UNION ALL
  SELECT 1012 AS pkid, 'Country 12' AS countryname, 0 AS langid FROM dual UNION ALL
  SELECT 1013 AS pkid, 'Country 13' AS countryname, 0 AS langid FROM dual UNION ALL
  SELECT 1014 AS pkid, 'Country 14' AS countryname, 0 AS langid FROM dual UNION ALL
  SELECT 1015 AS pkid, 'Country 15' AS countryname, 0 AS langid FROM dual UNION ALL
  SELECT 1016 AS pkid, 'Country 16' AS countryname, 0 AS langid FROM dual UNION ALL
  SELECT 1017 AS pkid, 'Country 17' AS countryname, 0 AS langid FROM dual UNION ALL
  SELECT 1018 AS pkid, 'Country 18' AS countryname, 0 AS langid FROM dual UNION ALL
  SELECT 1019 AS pkid, 'Country 19' AS countryname, 0 AS langid FROM dual UNION ALL
  SELECT 1020 AS pkid, 'Country 20' AS countryname, 0 AS langid FROM dual UNION ALL
  SELECT 1021 AS pkid, 'Country 21' AS countryname, 0 AS langid FROM dual UNION ALL
  SELECT 1022 AS pkid, 'Country 22' AS countryname, 1 AS langid FROM dual
)
--SELECT * FROM countries; /* Uncomment this to select rows from the countries table */
, gsmapprovedusagecountryjoin AS /* This is a CTE */
( /* Rows commented out to simulate the fact that not every record in this table */
  /*   has a country in it that exists in the countries table.  This may or may  */
  /*   not be true in your case but the fact is the possibility still remains.   */
  /* Without any knowledge of how your Database is structured and what data is   */
  /*  being stored in your tables I can only make assumptions.                   */
  SELECT 5425 AS primary_key, 1001 AS fkapprovedusage  FROM dual UNION ALL
  SELECT 5424 AS primary_key, 1002 AS fkapprovedusage  FROM dual UNION ALL
  SELECT 5423 AS primary_key, 1003 AS fkapprovedusage  FROM dual UNION ALL
  --SELECT 5422 AS primary_key, 1004 AS fkapprovedusage  FROM dual UNION ALL
  SELECT 5421 AS primary_key, 1005 AS fkapprovedusage  FROM dual UNION ALL
  --SELECT 5420 AS primary_key, 1006 AS fkapprovedusage  FROM dual UNION ALL
  --SELECT 5419 AS primary_key, 1007 AS fkapprovedusage  FROM dual UNION ALL
  SELECT 5418 AS primary_key, 1008 AS fkapprovedusage  FROM dual UNION ALL
  SELECT 5438 AS primary_key, 1009 AS fkapprovedusage  FROM dual UNION ALL
  --SELECT 5437 AS primary_key, 1010 AS fkapprovedusage  FROM dual UNION ALL
  SELECT 5436 AS primary_key, 1011 AS fkapprovedusage  FROM dual UNION ALL
  --SELECT 5435 AS primary_key, 1012 AS fkapprovedusage  FROM dual UNION ALL
  SELECT 5434 AS primary_key, 1013 AS fkapprovedusage  FROM dual UNION ALL
  SELECT 5433 AS primary_key, 1014 AS fkapprovedusage  FROM dual UNION ALL
  SELECT 5432 AS primary_key, 1015 AS fkapprovedusage  FROM dual UNION ALL
  --SELECT 5430 AS primary_key, 1016 AS fkapprovedusage  FROM dual UNION ALL
  SELECT 5431 AS primary_key, 1017 AS fkapprovedusage  FROM dual UNION ALL
  SELECT 5429 AS primary_key, 1018 AS fkapprovedusage  FROM dual UNION ALL
  --SELECT 5428 AS primary_key, 1019 AS fkapprovedusage  FROM dual UNION ALL
  SELECT 5427 AS primary_key, 1020 AS fkapprovedusage  FROM dual UNION ALL
  SELECT 5426 AS primary_key, 1021 AS fkapprovedusage  FROM dual

)
--SELECT * FROM gsmapprovedusagecountryjoin;
, gsmapprovedusage AS
( /* Rows commented out to simulate the fact that not every record in this table */
  /*   has a country in it that exists in the countries table.                   */
  SELECT 7158 AS primary_key, 1001 AS pkid, 2001 AS fkspecid  FROM dual UNION ALL
  SELECT 7157 AS primary_key, 1002 AS pkid, 2002 AS fkspecid  FROM dual UNION ALL
  SELECT 7156 AS primary_key, 1003 AS pkid, 2003 AS fkspecid  FROM dual UNION ALL
  --SELECT 7155 AS primary_key, 1004 AS pkid, 2004 AS fkspecid  FROM dual UNION ALL
  SELECT 7163 AS primary_key, 1005 AS pkid, 2005 AS fkspecid  FROM dual UNION ALL
  SELECT 7162 AS primary_key, 1006 AS pkid, 2006 AS fkspecid  FROM dual UNION ALL
  SELECT 7161 AS primary_key, 1007 AS pkid, 2007 AS fkspecid  FROM dual UNION ALL
  SELECT 7160 AS primary_key, 1008 AS pkid, 2008 AS fkspecid  FROM dual UNION ALL
  SELECT 7159 AS primary_key, 1009 AS pkid, 2009 AS fkspecid  FROM dual UNION ALL
  SELECT 7170 AS primary_key, 1010 AS pkid, 2010 AS fkspecid  FROM dual UNION ALL
  SELECT 7169 AS primary_key, 1011 AS pkid, 2011 AS fkspecid  FROM dual UNION ALL
  SELECT 7168 AS primary_key, 1012 AS pkid, 2012 AS fkspecid  FROM dual UNION ALL
  SELECT 7167 AS primary_key, 1013 AS pkid, 2013 AS fkspecid  FROM dual UNION ALL
  --SELECT 7166 AS primary_key, 1014 AS pkid, 2014 AS fkspecid  FROM dual UNION ALL
  SELECT 7165 AS primary_key, 1015 AS pkid, 2015 AS fkspecid  FROM dual UNION ALL
  SELECT 7164 AS primary_key, 1016 AS pkid, 2016 AS fkspecid  FROM dual UNION ALL
  SELECT 7154 AS primary_key, 1017 AS pkid, 2017 AS fkspecid  FROM dual UNION ALL
  SELECT 7153 AS primary_key, 1018 AS pkid, 2018 AS fkspecid  FROM dual UNION ALL
  SELECT 7173 AS primary_key, 1019 AS pkid, 2019 AS fkspecid  FROM dual UNION ALL
  SELECT 7172 AS primary_key, 1020 AS pkid, 2020 AS fkspecid  FROM dual UNION ALL
  SELECT 7171 AS primary_key, 1021 AS pkid, 2021 AS fkspecid  FROM dual

)
--SELECT * FROM gsmapprovedusage;
, specsummary AS
(
  SELECT 9245 AS primary_key, 2001 AS specid, '5366443-002' AS specnumber  FROM dual UNION ALL
  --SELECT 9246 AS primary_key, 2002 AS specid, '5366443-002' AS specnumber  FROM dual UNION ALL
  SELECT 9252 AS primary_key, 2003 AS specid, '5366443-005' AS specnumber  FROM dual UNION ALL
  SELECT 9251 AS primary_key, 2004 AS specid, '5366443-002' AS specnumber  FROM dual UNION ALL
  SELECT 9250 AS primary_key, 2005 AS specid, '5366443-002' AS specnumber  FROM dual UNION ALL
  --SELECT 9249 AS primary_key, 2006 AS specid, '5366443-002' AS specnumber  FROM dual UNION ALL
  SELECT 9258 AS primary_key, 2007 AS specid, '5366443-002' AS specnumber  FROM dual UNION ALL
  SELECT 9257 AS primary_key, 2008 AS specid, '5366443-007' AS specnumber  FROM dual UNION ALL
  --SELECT 9256 AS primary_key, 2009 AS specid, '5366443-002' AS specnumber  FROM dual UNION ALL
  SELECT 9255 AS primary_key, 2010 AS specid, '5366443-002' AS specnumber  FROM dual UNION ALL
  SELECT 9254 AS primary_key, 2011 AS specid, '5366443-002' AS specnumber  FROM dual UNION ALL
  SELECT 9253 AS primary_key, 2012 AS specid, '5366443-010' AS specnumber  FROM dual UNION ALL
  SELECT 9263 AS primary_key, 2013 AS specid, '5366443-002' AS specnumber  FROM dual UNION ALL
  SELECT 9262 AS primary_key, 2014 AS specid, '5366443-002' AS specnumber  FROM dual UNION ALL
  SELECT 9261 AS primary_key, 2015 AS specid, '5366443-002' AS specnumber  FROM dual UNION ALL
  --SELECT 9260 AS primary_key, 2016 AS specid, '5366443-150' AS specnumber  FROM dual UNION ALL
  SELECT 9259 AS primary_key, 2017 AS specid, '5366443-002' AS specnumber  FROM dual UNION ALL
  SELECT 9265 AS primary_key, 2018 AS specid, '5366443-002' AS specnumber  FROM dual UNION ALL
  --SELECT 9264 AS primary_key, 2019 AS specid, '5366443-055' AS specnumber  FROM dual UNION ALL
  SELECT 9248 AS primary_key, 2020 AS specid, '5366443-002' AS specnumber  FROM dual UNION ALL
  SELECT 9247 AS primary_key, 2021 AS specid, '5366443-002' AS specnumber  FROM dual
)
--SELECT * FROM specsummary;


/* Alias your tables for easier readability as well as using the ANSI JOIN 
Syntax */
SELECT ss.specnumber, c.countryname
FROM specsummary ss /* Not sure what data is stored here without making assumptions */
  JOIN gsmapprovedusage u ON u.fkspecid = ss.specid /* Not sure what data is stored here */
  JOIN gsmapprovedusagecountryjoin gc ON gc.fkapprovedusage = u.pkid /* Not sure what data is stored here */
  JOIN countries c ON c.pkid = gc.fkapprovedusage /* Lookup Table for Country Names */
WHERE /* Keep your JOINs out of the WHERE clause if you can. */
  /* The WHERE clause should mainly be reserved for your Filters */
  c.langid = 0
  AND ss.specnumber = '5366443-002'
;

enter image description here

如果您需要/想要更好的答案,请提供足够的信息,以便个人不必猜测或对表格中的数据做出假设。我在创造假冒的过程中做了几个假设。我提供的查询中的数据产生了我在此处发布的图像中看到的9行。