Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何在联合查询中对月份名称进行排序_Sql_Sorting - Fatal编程技术网

Sql 如何在联合查询中对月份名称进行排序

Sql 如何在联合查询中对月份名称进行排序,sql,sorting,Sql,Sorting,我有两个问题。查询1按月份名称显示表。按工作周查询两个显示表。我合并了这两个表,但问题是表1的月份名称显示顺序不正确 我想这样显示: October November WW52 SELECT Month, InputQuantity, ShippingQuantity FROM ( SELECT DATENAME(MONTH, dbo.SBR.SBR_DateCreated) AS Month, SUM(dbo.SBR.SBR_Quantity) AS Inpu

我有两个问题。查询1按月份名称显示表。按工作周查询两个显示表。我合并了这两个表,但问题是表1的月份名称显示顺序不正确

我想这样显示:

October    
November    
WW52


SELECT Month, InputQuantity, ShippingQuantity FROM
(
   SELECT DATENAME(MONTH, dbo.SBR.SBR_DateCreated) AS Month, 
   SUM(dbo.SBR.SBR_Quantity) AS InputQuantity, 
   SUM(dbo.SBR.SBR_ShippingQuantity) AS ShippingQuantity 
   FROM dbo.SBR WHERE DATENAME(MONTH, SBR_DateCreated) <> DATENAME(MONTH, GETDATE()) 
   GROUP BY DATENAME(MONTH, dbo.SBR.SBR_DateCreated)
UNION
   SELECT dbo.SBR.SBR_WorkingWeek AS Month, 
   SUM(dbo.SBR.SBR_Quantity) AS InputQuantity, 
   SUM(dbo.SBR.SBR_ShippingQuantity) AS ShippingQuantity
   FROM dbo.SBR
  INNER JOIN dbo.WorkingWeek 
    ON dbo.SBR.SBR_WorkingWeek=dbo.WorkingWeek.WorkingWeek
  WHERE (dbo.WorkingWeek.DateFrom < GETDATE() 
  AND dbo.WorkingWeek.DateTo > GETDATE())
  GROUP BY dbo.SBR.SBR_WorkingWeek
) AS UWAIS

And Current OUTPUT:

Month       InputQuantity   ShippingQuantity    
November         12                12    
October          12                12    
WW52             13                10
按在第一个子查询中创建的SBR_日期下单

SELECT Month, InputQuantity, ShippingQuantity FROM
(
SELECT DATENAME(MONTH, dbo.SBR.SBR_DateCreated) AS Month, SUM(dbo.SBR.SBR_Quantity) AS InputQuantity, SUM(dbo.SBR.SBR_ShippingQuantity) AS ShippingQuantity FROM dbo.SBR WHERE DATENAME(MONTH, SBR_DateCreated) <> DATENAME(MONTH, GETDATE()) 
GROUP BY DATENAME(MONTH, dbo.SBR.SBR_DateCreated)
ORDER BY dbo.SBR.SBR_DateCreated
UNION
SELECT dbo.SBR.SBR_WorkingWeek AS Month, SUM(dbo.SBR.SBR_Quantity) AS InputQuantity, SUM(dbo.SBR.SBR_ShippingQuantity) AS ShippingQuantity
FROM dbo.SBR
INNER JOIN dbo.WorkingWeek ON dbo.SBR.SBR_WorkingWeek=dbo.WorkingWeek.WorkingWeek
WHERE (dbo.WorkingWeek.DateFrom < GETDATE() AND dbo.WorkingWeek.DateTo > GETDATE())
GROUP BY dbo.SBR.SBR_WorkingWeek

) AS UWAIS

我会在每个子查询中添加一个额外的列,以表示该月的一个日期。然后可将其用于订购

不幸的是,我不知道第二个子查询使用哪一列。但其中一张表中可能有一个合适的日期

此外:

我把工会改成了全体工会。没有理由产生删除重复项的开销。 我添加了可编辑的别名,因此查询更容易阅读。
SELECT Month, InputQuantity, ShippingQuantity
FROM (SELECT DATENAME(MONTH, s.SBR_DateCreated) AS Month,
             SUM(s.SBR_Quantity) AS InputQuantity, SUM(s.SBR_ShippingQuantity) AS ShippingQuantity,
             MIN(s.SBR_DateCreated) as mindc
      FROM dbo.SBR s
      WHERE DATENAME(MONTH, SBR_DateCreated) <> DATENAME(MONTH, GETDATE()) 
      GROUP BY DATENAME(MONTH,s.SBR_DateCreated)
      UNION ALL
      SELECT s.SBR_WorkingWeek AS Month, SUM(s.SBR_Quantity) AS InputQuantity,
             SUM(s.SBR_ShippingQuantity) AS ShippingQuantity,
             MIN(ww.<datecol>)
      FROM dbo.SBR s INNER JOIN
           dbo.WorkingWeek ww
           ON s.SBR_WorkingWeek = ww.WorkingWeek
      WHERE (ww.DateFrom < GETDATE() AND ww.DateTo > GETDATE())
      GROUP BY s.SBR_WorkingWeek
     ) UWAIS
ORDER BY mindc;