Seperate Files
This commit is contained in:
30
CFY.sql
30
CFY.sql
@@ -1,3 +1,27 @@
|
|||||||
-- SQLite
|
WITH const as (select "%ROBOSHOT%" as target)
|
||||||
SELECT SUM(quantity) AS qty, SUM(revenue) AS rev
|
SELECT
|
||||||
FROM data WHERE description LIKE "%RoboSHOT 30E USB%" AND date >= '2021-01-01';
|
description,
|
||||||
|
-- year,
|
||||||
|
-- month,
|
||||||
|
sum(revenue),
|
||||||
|
sum(quantity)
|
||||||
|
from
|
||||||
|
data
|
||||||
|
where
|
||||||
|
description like const.target
|
||||||
|
and year = 2021
|
||||||
|
and month BETWEEN 7
|
||||||
|
and 9
|
||||||
|
group by
|
||||||
|
description;
|
||||||
|
select
|
||||||
|
description,
|
||||||
|
sum(revenue),
|
||||||
|
sum(quantity)
|
||||||
|
from
|
||||||
|
data
|
||||||
|
where
|
||||||
|
description like const.target
|
||||||
|
and year = 2021
|
||||||
|
and month BETWEEN 7
|
||||||
|
and 9;
|
||||||
98
main.go
98
main.go
@@ -1,44 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"database/sql"
|
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
|
||||||
"github.com/xuri/excelize/v2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var schema = `
|
|
||||||
CREATE TABLE IF NOT EXISTS data (
|
|
||||||
description text,
|
|
||||||
sku text,
|
|
||||||
level2 text,
|
|
||||||
level3 text,
|
|
||||||
revenue float,
|
|
||||||
cost float,
|
|
||||||
quantity integer,
|
|
||||||
year integer,
|
|
||||||
month integer
|
|
||||||
);
|
|
||||||
`
|
|
||||||
|
|
||||||
type Data struct {
|
|
||||||
Description string
|
|
||||||
SKU string
|
|
||||||
Level2 string
|
|
||||||
Level3 string
|
|
||||||
Revenue float64
|
|
||||||
Cost float64
|
|
||||||
Quantity int64
|
|
||||||
Year int64
|
|
||||||
Month int64
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkErr(err error) {
|
func checkErr(err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@@ -46,62 +12,14 @@ func checkErr(err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("Processing excel file...")
|
var (
|
||||||
start := time.Now()
|
excel_file string
|
||||||
|
)
|
||||||
|
|
||||||
f, err := excelize.OpenFile("data.xlsx")
|
flag.StringVar(&excel_file, "file", "", "The path to the excel file for processing")
|
||||||
checkErr(err)
|
flag.Parse()
|
||||||
|
|
||||||
rows, err := f.GetRows("Sheet1")
|
if excel_file != "" {
|
||||||
checkErr(err)
|
ProcessExcel(excel_file)
|
||||||
|
|
||||||
const offset = 3
|
|
||||||
data := []Data{}
|
|
||||||
for i, row := range rows {
|
|
||||||
if i >= offset {
|
|
||||||
new_data := Data{SKU: row[33], Level2: row[29], Level3: row[30], Description: row[44]}
|
|
||||||
|
|
||||||
//Parse Rev
|
|
||||||
rev, err := strconv.ParseFloat(row[72], 64)
|
|
||||||
checkErr(err)
|
|
||||||
new_data.Revenue = rev
|
|
||||||
|
|
||||||
//Parse Cost
|
|
||||||
cost, err := strconv.ParseFloat(row[16], 64)
|
|
||||||
checkErr(err)
|
|
||||||
new_data.Cost = cost
|
|
||||||
|
|
||||||
//Parse Quantity
|
|
||||||
qty, err := strconv.ParseInt(row[71], 10, 64)
|
|
||||||
checkErr(err)
|
|
||||||
new_data.Quantity = qty
|
|
||||||
|
|
||||||
//Parse Time
|
|
||||||
day, err := time.Parse("2006-01", row[26])
|
|
||||||
checkErr(err)
|
|
||||||
new_data.Year = int64(day.Year())
|
|
||||||
new_data.Month = int64(day.Month())
|
|
||||||
|
|
||||||
data = append(data, new_data)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := sql.Open("sqlite3", "data.db")
|
|
||||||
checkErr(err)
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
db.Exec("DROP TABLE IF EXISTS data")
|
|
||||||
db.Exec(schema)
|
|
||||||
|
|
||||||
values := []string{}
|
|
||||||
for _, elm := range data {
|
|
||||||
values = append(values, fmt.Sprintf(`("%s","%s","%s","%s",%f,%f,%d,"%d","%d")`, strings.ReplaceAll(elm.Description, "\"", ""), elm.SKU, elm.Level2, elm.Level3, elm.Revenue, elm.Cost, elm.Quantity, elm.Year, elm.Month))
|
|
||||||
}
|
|
||||||
|
|
||||||
stmt := "INSERT INTO data (description,sku,level2,level3,revenue,cost,quantity,year,month) VALUES " + strings.Join(values, ",")
|
|
||||||
_, err = db.Exec(stmt)
|
|
||||||
checkErr(err)
|
|
||||||
|
|
||||||
elapsed := time.Since(start)
|
|
||||||
fmt.Printf("Operation complete in %s\n", elapsed)
|
|
||||||
}
|
}
|
||||||
|
|||||||
100
process_excel.go
Normal file
100
process_excel.go
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"database/sql"
|
||||||
|
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
"github.com/xuri/excelize/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
var schema = `
|
||||||
|
CREATE TABLE IF NOT EXISTS data (
|
||||||
|
description text,
|
||||||
|
sku text,
|
||||||
|
level2 text,
|
||||||
|
level3 text,
|
||||||
|
revenue float,
|
||||||
|
cost float,
|
||||||
|
quantity integer,
|
||||||
|
year integer,
|
||||||
|
month integer
|
||||||
|
);
|
||||||
|
`
|
||||||
|
|
||||||
|
type Data struct {
|
||||||
|
Description string
|
||||||
|
SKU string
|
||||||
|
Level2 string
|
||||||
|
Level3 string
|
||||||
|
Revenue float64
|
||||||
|
Cost float64
|
||||||
|
Quantity int64
|
||||||
|
Year int64
|
||||||
|
Month int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func ProcessExcel(path string) {
|
||||||
|
fmt.Println("Processing excel file...")
|
||||||
|
start := time.Now()
|
||||||
|
|
||||||
|
f, err := excelize.OpenFile(path)
|
||||||
|
checkErr(err)
|
||||||
|
|
||||||
|
rows, err := f.GetRows("Sheet1")
|
||||||
|
checkErr(err)
|
||||||
|
|
||||||
|
const offset = 3
|
||||||
|
data := []Data{}
|
||||||
|
for i, row := range rows {
|
||||||
|
if i >= offset {
|
||||||
|
new_data := Data{SKU: row[33], Level2: row[29], Level3: row[30], Description: row[44]}
|
||||||
|
|
||||||
|
//Parse Rev
|
||||||
|
rev, err := strconv.ParseFloat(row[72], 64)
|
||||||
|
checkErr(err)
|
||||||
|
new_data.Revenue = rev
|
||||||
|
|
||||||
|
//Parse Cost
|
||||||
|
cost, err := strconv.ParseFloat(row[16], 64)
|
||||||
|
checkErr(err)
|
||||||
|
new_data.Cost = cost
|
||||||
|
|
||||||
|
//Parse Quantity
|
||||||
|
qty, err := strconv.ParseInt(row[71], 10, 64)
|
||||||
|
checkErr(err)
|
||||||
|
new_data.Quantity = qty
|
||||||
|
|
||||||
|
//Parse Time
|
||||||
|
day, err := time.Parse("2006-01", row[26])
|
||||||
|
checkErr(err)
|
||||||
|
new_data.Year = int64(day.Year())
|
||||||
|
new_data.Month = int64(day.Month())
|
||||||
|
|
||||||
|
data = append(data, new_data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := sql.Open("sqlite3", "data.db")
|
||||||
|
checkErr(err)
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
db.Exec("DROP TABLE IF EXISTS data")
|
||||||
|
db.Exec(schema)
|
||||||
|
|
||||||
|
values := []string{}
|
||||||
|
for _, elm := range data {
|
||||||
|
values = append(values, fmt.Sprintf(`("%s","%s","%s","%s",%f,%f,%d,"%d","%d")`, strings.ReplaceAll(elm.Description, "\"", ""), elm.SKU, elm.Level2, elm.Level3, elm.Revenue, elm.Cost, elm.Quantity, elm.Year, elm.Month))
|
||||||
|
}
|
||||||
|
|
||||||
|
stmt := "INSERT INTO data (description,sku,level2,level3,revenue,cost,quantity,year,month) VALUES " + strings.Join(values, ",")
|
||||||
|
_, err = db.Exec(stmt)
|
||||||
|
checkErr(err)
|
||||||
|
|
||||||
|
elapsed := time.Since(start)
|
||||||
|
fmt.Printf("Operation complete in %s\n", elapsed)
|
||||||
|
}
|
||||||
5
top.sql
5
top.sql
@@ -12,16 +12,13 @@ SELECT
|
|||||||
from
|
from
|
||||||
(
|
(
|
||||||
SELECT
|
SELECT
|
||||||
|
*,
|
||||||
ROW_NUMBER () OVER (
|
ROW_NUMBER () OVER (
|
||||||
PARTITION BY level2
|
PARTITION BY level2
|
||||||
ORDER BY
|
ORDER BY
|
||||||
SUM(revenue) DESC
|
SUM(revenue) DESC
|
||||||
) rownum,
|
) rownum,
|
||||||
SUM(revenue) OVER (PARTITION BY level2) total,
|
SUM(revenue) OVER (PARTITION BY level2) total,
|
||||||
description,
|
|
||||||
sku,
|
|
||||||
level2,
|
|
||||||
level3,
|
|
||||||
ROUND(SUM(revenue), 2) as rev,
|
ROUND(SUM(revenue), 2) as rev,
|
||||||
SUM(quantity) as qty,
|
SUM(quantity) as qty,
|
||||||
trend
|
trend
|
||||||
|
|||||||
Reference in New Issue
Block a user