SQL:如何使用仅代表一周中几天的数据显示一周中的完整7天?

时间:2012-07-30 01:16:50

标签: sql sql-server select join group-by

我有一个表格,可以存储一周中的数据。我想在一周中的每一天返回一行,即使我表中的那一天没有任何行。这是我当前的sql select语句。

SELECT StoreID,
   CASE 
       WHEN S.[DayOfWeek] = 1 THEN 'Sunday' 
       WHEN S.[DayOfWeek] = 2 THEN 'Monday'
       WHEN S.[DayOfWeek] = 3 THEN 'Tuesday'
       WHEN S.[DayOfWeek] = 4 THEN 'Wednesday'
       WHEN S.[DayOfWeek] = 5 THEN 'Thursday'
       WHEN S.[DayOfWeek] = 6 THEN 'Friday'
       WHEN S.[DayOfWeek] = 7 THEN 'Saturday'
       ELSE 'BAD' 
   END AS [DayOfWeek], 
       isOpen
  FROM MyTable S
 WHERE StoreID = @I_StoreID
 ORDER BY S.[DayOfWeek]  

现在它只返回一个星期一和星期二的记录,因为这就是表中存在的全部内容,但是我希望它也返回其他行,即使当前没有它们的记录。谢谢!

编辑:

这是我的......

  StoreID | DayOfWeek | isOpen

      22     Sunday      0
      22     Monday      1
      29     Sunday      0

这是我希望得到的......

  StoreID | DayOfWeek | isOpen

      22     Sunday      0
      22     Monday      1
      22     Tuesday     NULL
       ....
      22     Saturday    NULL

      29     Sunday      1
      29     Monday      NULL 
      29     Tuesday     NULL
       ....
      29     Saturday    NULL

3 个答案:

答案 0 :(得分:3)

您可以使用此解决方案:

SELECT
    a.StoreID,
    a.weekdayname,
    b.isOpen
FROM
(
    SELECT *
    FROM
    (
        SELECT DISTINCT StoreID
        FROM MyTable
    ) aa
    CROSS JOIN
    (
        SELECT 1 AS weekdaynum, 'Sunday' AS weekdayname UNION ALL
        SELECT 2, 'Monday'                UNION ALL
        SELECT 3, 'Tuesday'               UNION ALL
        SELECT 4, 'Wednesday'             UNION ALL
        SELECT 5, 'Thursday'              UNION ALL
        SELECT 6, 'Friday'                UNION ALL
        SELECT 7, 'Saturday'
    ) bb
) a
LEFT JOIN
    MyTable b ON a.StoreID = b.StoreID AND 
                 a.weekdaynum = b.[DayOfWeek]
WHERE
    a.StoreID = @I_StoreID
ORDER BY
    a.StoreID, a.weekdaynum

在这里,我们手动选择所有工作日名称,并使用每个不同的CROSS JOIN选择StoreID个名称。然后,我们将该选择的结果包装在FROM子句中,并在LEFT JOIN以及工作日编号匹配的条件下将StoreID重新执行回主表。如果没有,则isOpen字段将为NULL,但StoreID和相应的工作日仍会显示。


SQLFiddle Demo

答案 1 :(得分:2)

如果isOpen为1表示开放,0表示关闭,则可能

select StoreID,
   max(case when s.[DayOfWeek] = 1 then isOpen else 0 end) as Sunday,
   max(case when s.[DayOfWeek] = 2 then isOpen else 0 end) as Monday,
   max(case when s.[DayOfWeek] = 3 then isOpen else 0 end) as Tuesday,
   max(case when s.[DayOfWeek] = 4 then isOpen else 0 end) as Wednesday,
   max(case when s.[DayOfWeek] = 5 then isOpen else 0 end) as Thursday,
   max(case when s.[DayOfWeek] = 6 then isOpen else 0 end) as Friday,
   max(case when s.[DayOfWeek] = 7 then isOpen else 0 end) as Saturday
from MyTable S
where StoreID = @I_StoreID
group by StoreID

这将更改查询的格式,但是应该为一周中的每一天提供1列,如果商店当天打开。

答案 2 :(得分:1)

编辑:我在看到您的更新回复之前更改了此内容。鉴于您想要查看的数据,其他答案应该更好。

你有一张独立于MyTable的桌子,有DayOfWeek吗?如果整个表中没有DayOfWeek = 1的单个记录,那么在开始使用MyTable之前,您必须引入一个单独的DayOfWeek表来列出这些值。

这是一个假设的DayOfWeek表和将导致的SQL:

DayOfWeek表
DayOfWeek:姓名
1:周日
2:星期一 3:星期二 4:星期三 5:星期四 6:星期五 7:星期六

<强>查询

SELECT D.Name,
S.isOpen,
COUNT(S.StoreID) as StoreID_Count
FROM DayOfWeek D
LEFT OUTER JOIN MyTable S ON S.DayOfWeek = D.DayOfWeek
WHERE S.StoreID = @I_StoreID
GROUP BY D.[DayOfWeek], S.isOpen
ORDER BY D.[DayOfWeek]

我相信这应该让你至少接近。