简体   繁体   English

获取最早日期的SQL方法

[英]SQL method to fetch Earliest date

What would be the best method to fetch the Sale date for each client when the records are stored in columns?当记录存储在列中时,获取每个客户的销售日期的最佳方法是什么? Each record is flattened and store the sales data for each month for the last 15 years.每条记录都被扁平化并存储过去 15 年每个月的销售数据。 Hence it contains Client_ID and the sale month (Dec_08, Jan_09, Feb_09...) as the column header and the column values contain the Sale value (number of Boxes) as Integer.因此,它包含 Client_ID 和销售月份(Dec_08、Jan_09、Feb_09...)作为列标题,列值包含作为整数的销售值(盒数)。 I would need to do a calculation of datediff to get the number of years since the first sale, hence the Month Year needs to be some sort of date answer here.我需要计算 datediff 以获得自第一次销售以来的年数,因此这里的 Month Year 需要是某种日期答案。 I am doing this in SQL in Snowflake enter image description here我在雪花中的 SQL 中执行此操作在此处输入图像描述

Example of the Sale is in the picture below销售示例如下图所示

Any help would be appreciated.任何帮助,将不胜感激。

You could use the UNPIVOT construct to rotate the table by transforming columns into rows.您可以使用UNPIVOT构造通过将列转换为行来旋转表。

Then calculate min date (you need to cast you month_year value from string to date using to_date( yourcolumn,'Mon_YY')) , group by ClientID, where box# is not null.然后计算最小日期(您需要使用 to_date( yourcolumn,'Mon_YY') 将您的月_年值从字符串转换为日期),按 ClientID 分组,其中 box# 不为空。

You can also apply datediff function in order to retrieve time between min date and current date ( https://docs.snowflake.com/en/sql-reference/functions/datediff.html ).您还可以应用 datediff 函数来检索最小日期和当前日期之间的时间( https://docs.snowflake.com/en/sql-reference/functions/datediff.html )。

Below is an example from Snowflake documentation of UNPIVOT construct ( https://docs.snowflake.com/en/sql-reference/constructs/unpivot.html ).下面是来自 UNPIVOT 构造的 Snowflake 文档的示例 ( https://docs.snowflake.com/en/sql-reference/constructs/unpivot.html )。

Given a table, monthly_sales, with the following structure and data, unpivot the individual month columns to return a single sales value by month for each employee:给定一个表,monthly_sales,具有以下结构和数据,对各个月份的列进行反透视,以按月返回每个员工的单个销售值:

-- example setup
create or replace table monthly_sales(empid int, dept text, jan int, feb int, mar int, april int);

insert into monthly_sales values
    (1, 'electronics', 100, 200, 300, 100),
    (2, 'clothes', 100, 300, 150, 200),
    (3, 'cars', 200, 400, 100, 50);

-- UNPIVOT example
select * from monthly_sales
    unpivot(sales for month in (jan, feb, mar, april))
    order by empid;


+-------+-------------+-------+-------+
| EMPID | DEPT        | MONTH | SALES |
|-------+-------------+-------+-------|
|     1 | electronics | JAN   |   100 |
|     1 | electronics | FEB   |   200 |
|     1 | electronics | MAR   |   300 |
|     1 | electronics | APRIL |   100 |
|     2 | clothes     | JAN   |   100 |
|     2 | clothes     | FEB   |   300 |
|     2 | clothes     | MAR   |   150 |
|     2 | clothes     | APRIL |   200 |
|     3 | cars        | JAN   |   200 |
|     3 | cars        | FEB   |   400 |
|     3 | cars        | MAR   |   100 |
|     3 | cars        | APRIL |    50 |
+-------+-------------+-------+-------+

You can't work with that data structure directly, it's too unwieldy.您不能直接使用该数据结构,它太笨拙了。 You have to first get it into a different form.你必须先把它变成不同的形式。 The way to do that is to introduce a new table and write a query to populate it like this:这样做的方法是引入一个新表并编写一个查询来填充它,如下所示:

insert into YourNewTable
(YearNum, MonthNum, Client_ID, Sale_Value)
select
    2008 as Year_Num,
    12 as Month_Num,
    Client_ID,
    Dec_08 as Sale_Value
from
    Sale
where
    Dec_08 <> 0

union all

select
    2009 as Year_Num,
    1 as Month_Num,
    Client_ID,
    Jan_09 as Sale_Value
from
    Sale
where
    Jan_09 <> 0

union all

[... etc., all the way to the most recent month]

You can't write the query above manually, you have to code some sort of automated routine to write the query, in the language of your choice.您不能手动编写上面的查询,您必须编写某种自动例程以使用您选择的语言编写查询。 But once it's done, it's done.但是一旦完成,它就完成了。

After you've run this new query to populate YourNewTable, you can query YourNewTable in a sensible fashion.在运行此新查询以填充 YourNewTable 后,您可以以合理的方式查询 YourNewTable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM