【问题标题】:How to add dummy month (SQL query)如何添加虚拟月份(SQL 查询)
【发布时间】:2016-07-07 12:41:25
【问题描述】:

我在table1中有这些专栏:

现在,我需要添加虚拟月份。如何快速编写SQL查询,将Q1(一行)除以3个月?

结果应该是这样的:

这可能吗?

编辑 1

这里是生成第一个表的 SQL 语句

select year, time, q, th, l1, l2, par, par2, sum(value)
from table1
group by year, time, q, th, l1, l2, par, par2

【问题讨论】:

  • 你有产生第一张表的SQL语句吗?
  • select year, time, q, th, l1, l2, par, par2, sum(value) from table1 group by year, time, q, th, l1, l2, par, par2跨度>
  • 你也可以共享表架构吗?
  • 每一列都是varchar,value是数字
  • 我的意思是完整的列列表

标签: sql sql-server sql-server-2008


【解决方案1】:

使用CROSS JOINROW_NUMBER() 尝试这种方法。

CROSS JOIN 带有一个有 3 条记录的动态表 (table2) 将在主表中为每个季度输出 3 行,然后使用ROW_NUMBER() 函数按季度获取每年订单的月份数。

以下是一个例子。

--Add some sample data to represent your table1
DECLARE @table1 TABLE ([Year] int, Q varchar(10))

INSERT INTO @table1 ([Year], Q)
VALUES (2016, 'Q1'),
       (2016, 'Q2'),
       (2016, 'Q3'),
       (2016, 'Q4')

--Query
SELECT [year], Q,  
       ROW_NUMBER() OVER (PARTITION BY [Year] ORDER BY qtr) [Month]
FROM @Table1 t1
        CROSS JOIN (SELECT qtr FROM (VALUES(1),(2),(3)) Table2 (qtr)) t2

【讨论】:

    【解决方案2】:

    使用convert将Q1,Q2,Q3,Q4变成起始月份数;然后使用union 将该行复制三次(总和除以三)。

    select year, time, q, 
        month = convert(int, right(q,1))*3 - 2,
        th, l1, l2, par, par2, sum(value)/3 
    from table1 group by year, time, q, th, l1, l2, par, par2
    union all
    select year, time, q, 
        month = convert(int, right(q,1))*3 - 1,
        th, l1, l2, par, par2, sum(value)/3 
    from table1 group by year, time, q, th, l1, l2, par, par2
    union all
    select year, time, q, 
        month = convert(int, right(q,1))*3,
        th, l1, l2, par, par2, sum(value)/3 
    from table1 group by year, time, q, th, l1, l2, par, par2
    

    【讨论】:

      【解决方案3】:

      这并不是您要问的,但您可以了解如何在您的情况下采用它。我使用了表变量,你必须用你的表名替换它。我做了一些插入只是为了测试目的:

      declare @table table (Q varchar(10),amount decimal(18,2))
      insert into @table values ('Q1',80000),('Q2',500000),('Q3',457000),('Q4',75000)
      declare @table2 table (Q varchar(10),muaji int)
      insert into @table2 values ('Q1',1),('Q1',2),('Q1',3),('Q2',4),('Q2',5),('Q2',6),('Q3',7),('Q3',8),('Q3',9),('Q4',10),('Q4',11),('Q4',12)
      select t1.Q,t2.muaji,t1.amount/3 from @table t1 inner join @table2 t2 on t1.Q=t2.Q
      

      【讨论】:

        【解决方案4】:

        这应该可行:

        declare @t table (id int identity(1,1),q char(2))
        insert into @t 
        select 'q1'
        union all select 'q1'
        union all select 'q2'
        union all select 'q2'
        union all select 'q2'
        union all select 'q1'
        union all select 'q4'
        union all select 'q3'
        union all select 'q3'
        union all select 'q4'
        union all select 'q4'
        union all select 'q3'
        
        --select * from @t
        select mn =  ROW_NUMBER() OVER(ORDER BY q) ,q
        from @t
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-12
          • 2022-07-07
          • 2021-11-14
          • 1970-01-01
          相关资源
          最近更新 更多