2

我有一个表存储一周中的几天的数据。我想为一周中的每一天返回一行,即使我的表中一周中的那一天没有任何行。这是我当前的 sql 选择语句。

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
4

3 回答 3

4

您可以使用此解决方案:

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 演示

于 2012-07-30T01:45:05.897 回答
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 列。

于 2012-07-30T01:39:23.050 回答
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]

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

于 2012-07-30T01:26:52.433 回答