从SQLite获取当前周数据和当前年数据的总和

nqwrtyyt  于 2022-12-13  发布在  SQLite
关注(0)|答案(3)|浏览(131)

我有一个SQLite数据库和sales表,如下所示:

| Id | quantity |     dateTime     |
------------------------------------
| 1  | 10       | 2019-12-25 12:55 |
| 2  | 05       | 2019-12-30 12:55 |
| 3  | 25       | 2020-08-23 12:55 |
| 4  | 25       | 2020-08-24 12:55 |
| 5  | 56       | 2020-08-25 12:55 |
| 6  | 25       | 2020-08-26 12:55 |
| 7  | 12       | 2020-08-27 12:55 |
| 8  | 30       | 2020-08-28 12:55 |
| 9  | 40       | 2020-08-29 12:55 |

我需要获取本周数据(周一到周日)和本年数据(一月到十二月)。因此,如果我传递了今天的日期,我只需要获取按天分组的本周销售数据,如下所示:
如果我传递今天的日期和时间(2020-08-28 13:55),查询应给予如下所示的当前周数据,

Day        Sold Items (SUM(quantity))
Monday     20
Tuesday    25
Wednesday  10
Thursday   50
Friday     60
Saturday   0 (If the date hasn't come yet I need to get 0)
Sunday     0

与传递Current Date时的Current Year数据相同,

Month      Sold Items (SUM(quantity))
JAN        20
FEB        25
MAR        10
APR        50
MAY        60
JUN        0 (If the month hasn't come yet I need to get 0)
JUL        0
...        ...

我尝试在SQLite中使用多个查询,但无法获得所需的结果。以下是我尝试的查询。

Weekly Data (This one gave me past week data also)
SELECT SUM(quantity) as  quantity, strftime('%w', dateTime) as Day
From sales
Group by strftime('%w', dateTime)

Monthly Data
SELECT SUM(quantity) as  quantity, strftime('%m', dateTime) as Month
From sales
Group by strftime('%m', dateTime)

所以有谁能帮我实现这个目标吗?提前感谢。

k3bvogb1

k3bvogb11#

对于当前周的总计,您需要一个返回日期名称的CTE和另一个返回当前周的星期一的CTE。
您必须交叉连接这些CTE并左连接表以进行聚合:

with 
  days as (
    select 1 nr, 'Monday' day union all
    select 2, 'Tuesday' union all
    select 3, 'Wednesday' union all
    select 4, 'Thursday' union all
    select 5, 'Friday' union all
    select 6, 'Saturday' union all
    select 7, 'Sunday'
  ),
  weekMonday as (
    select date(
        'now', 
        case when strftime('%w', 'now') <> '1' then '-7 day' else '0 day' end, 
        'weekday 1'
      ) monday
  )
select d.day, 
       coalesce(sum(t.quantity), 0) [Sold Items]
from days d cross join weekMonday wm
left join tablename t
on strftime('%w', t.dateTime) + 0 = d.nr % 7
and date(t.dateTime) between wm.monday and date(wm.monday, '6 day')
group by d.nr, d.day
order by d.nr

对于当前年份的总计,您需要一个CTE,它返回月份名称,然后左联接表以进行聚合:

with 
  months as (
    select 1 nr, 'JAN' month union all
    select 2 nr, 'FEB' union all
    select 3 nr, 'MAR' union all
    select 4 nr, 'APR' union all
    select 5 nr, 'MAY' union all
    select 6 nr, 'JUN' union all
    select 7 nr, 'JUL' union all
    select 8 nr, 'AUG' union all
    select 9 nr, 'SEP' union all
    select 10 nr, 'OCT' union all
    select 11 nr, 'NOV' union all
    select 12 nr, 'DEC'
  )
select m.month, 
       coalesce(sum(t.quantity), 0) [Sold Items]
from months m
left join tablename t
on strftime('%m', t.dateTime) + 0 = m.nr
and date(t.dateTime) between date('now','start of year') and date('now','start of year', '1 year', '-1 day')
group by m.nr, m.month
order by m.nr
1wnzp6jl

1wnzp6jl2#

您可以使用下面的查询来获取每周日期,我假设everydate只有一个条目,因此没有分组,否则您可以添加group by。
首先,我们将得到基于输入日期的周历(我已经采取了当前日期),然后离开加入日历,以获得所需的出售物品信息。

WITH seq(n) AS 
(
  SELECT 0 UNION ALL SELECT n + 1 FROM seq
  WHERE n < DATEDIFF(DAY, (SELECT DATEADD(DAY, 2 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)) [Week_Start_Date]), (Select DATEADD(DAY, 8 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)) [Week_End_Date]))
),
CALENDAR(d) AS 
(
  SELECT DATEADD(DAY, n, (SELECT DATEADD(DAY, 2 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)) [Week_Start_Date])) FROM seq
)
SELECT coalesce(QUANTITY, 0) sold_items ,DATENAME(WEEKDAY, d) week_day FROM CALENDAR a left outer join Table_WEEKDAY b
on (a.d = convert(date, b.dateTime))
ORDER BY d
OPTION (MAXRECURSION 0);
rur96b6h

rur96b6h3#

您可以尝试下面的-DEMO

select day,coalesce(sum(quantity),0) as quantity
from 
(select 0 as day union all select 1 union all select 2 union all select 3 union all select 4
union all select 5 union all select 6) as d
left join sales on cast(strftime('%w', dateTime) as int)=day 
group by strftime('%w', dateTime),day
order by day

相关问题