Seperate Files
This commit is contained in:
30
CFY.sql
30
CFY.sql
@@ -1,3 +1,27 @@
|
||||
-- SQLite
|
||||
SELECT SUM(quantity) AS qty, SUM(revenue) AS rev
|
||||
FROM data WHERE description LIKE "%RoboSHOT 30E USB%" AND date >= '2021-01-01';
|
||||
WITH const as (select "%ROBOSHOT%" as target)
|
||||
SELECT
|
||||
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
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"flag"
|
||||
"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) {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -46,62 +12,14 @@ func checkErr(err error) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("Processing excel file...")
|
||||
start := time.Now()
|
||||
var (
|
||||
excel_file string
|
||||
)
|
||||
|
||||
f, err := excelize.OpenFile("data.xlsx")
|
||||
checkErr(err)
|
||||
flag.StringVar(&excel_file, "file", "", "The path to the excel file for processing")
|
||||
flag.Parse()
|
||||
|
||||
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)
|
||||
if excel_file != "" {
|
||||
ProcessExcel(excel_file)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user