create a new column contains ISO week postgresql - sql

I have table like this:
CustomerID
Trans_date
C001
01-sep-22
C001
04-sep-22
C001
14-sep-22
C002
03-sep-22
C002
01-sep-22
C002
18-sep-22
C002
20-sep-22
C003
02-sep-22
C003
28-sep-22
C004
08-sep-22
C004
18-sep-22
I would make a new column consist ISO week
CustomerID
Trans_date
Week_ISO
C001
01-sep-22
35
C001
04-sep-22
35
C001
14-sep-22
35
C002
03-sep-22
35
C002
01-sep-22
35
C002
18-sep-22
35
C002
20-sep-22
35
C003
02-sep-22
35
C003
28-sep-22
35
C004
08-sep-22
36
C004
18-sep-22
36
But i can't make because there's not have datepart

You can define a view instead of altering the original table. Use extract or date_part.
create or replace view the_view as
select customerid, trans_date,
extract('week' from trans_date) week_iso
from the_table;
DB-fiddle

Related

calling many table in one query based on week

if there are multiple tables with the same contents like this:
table one:
CustomerID
CustTrans
Weeks
C001
2022-09-03
36
C002
2022-09-02
36
C003
2022-09-03
36
C004
2022-09-02
36
C002
2022-09-08
37
C001
2022-09-05
37
C002
2022-09-11
38
C002
2022-09-23
39
C004
2022-09-19
39
C001
2022-09-18
39
C003
2022-09-26
40
C005
2022-09-17
38
C006
2022-09-25
40
C001
2022-09-25
40
table 2:
CustomerID
CustTrans
Weeks
C001
2022-09-03
36
C002
2022-09-02
36
C003
2022-09-03
36
C004
2022-09-02
36
C002
2022-09-08
37
C001
2022-09-05
37
C002
2022-09-11
38
C002
2022-09-23
39
C004
2022-09-19
39
C001
2022-09-18
39
C003
2022-09-26
40
C005
2022-09-17
38
C006
2022-09-25
40
C001
2022-09-25
40
table 3
CustomerID
CustTrans
Weeks
C001
2022-09-03
36
C002
2022-09-02
36
C003
2022-09-03
36
C004
2022-09-02
36
C002
2022-09-08
37
C001
2022-09-05
37
C002
2022-09-11
38
C002
2022-09-23
39
C004
2022-09-19
39
C001
2022-09-18
39
C003
2022-09-26
40
C005
2022-09-17
38
C006
2022-09-25
40
C001
2022-09-25
40
it's possible to make many table in just single table?
this is my query
CREATE DATABASE manydata;
CREATE TABLE trydata1
(
CustomerID CHAR(7) not null,
CustTrans date,
CustSales int,
)
insert into trydata1(CustomerID,CustSales,CustTrans)
values('C001',34,'2022-09-03'),('C002',23,'2022-09-02'),('C003',132,'2022-09-03'),
('C004',95,'2022-09-02'),('C002',68,'2022-09-08'),('C001',54,'2022-09-05'),
('C002',34,'2022-09-11'),('C002',98,'2022-09-23'),('C004',34,'2022-09-19'),
('C001',30,'2022-09-18'),('C003',34,'2022-09-26'),('C005',34,'2022-09-17'),
('C006',34,'2022-09-25'),('C001',34,'2022-09-25');
CREATE TABLE trydata2
(
CustomerID CHAR(7) not null,
CustTrans date,
CustSales int,
)
insert into trydata2(CustomerID,CustSales,CustTrans)
values('C001',34,'2022-09-03'),('C002',23,'2022-09-02'),('C003',132,'2022-09-03'),
('C004',95,'2022-09-02'),('C002',68,'2022-09-08'),('C001',54,'2022-09-05'),
('C002',34,'2022-09-11'),('C002',98,'2022-09-23'),('C004',34,'2022-09-19'),
('C001',30,'2022-09-18'),('C003',34,'2022-09-26'),('C005',34,'2022-09-17'),
('C006',34,'2022-09-25'),('C001',34,'2022-09-25');
CREATE TABLE trydata3
(
CustomerID CHAR(7) not null,
CustTrans date,
CustSales int,
)
insert into trydata3(CustomerID,CustSales,CustTrans)
values('C001',34,'2022-09-03'),('C002',23,'2022-09-02'),('C003',132,'2022-09-03'),
('C004',95,'2022-09-02'),('C002',68,'2022-09-08'),('C001',54,'2022-09-05'),
('C002',34,'2022-09-11'),('C002',98,'2022-09-23'),('C004',34,'2022-09-19'),
('C001',30,'2022-09-18'),('C003',34,'2022-09-26'),('C005',34,'2022-09-17'),
('C006',34,'2022-09-25'),('C001',34,'2022-09-25');
You seem to be looking for UNION or UNION ALL.
select *
from
(
select * from table1
union all
select * from table2
union all
select * from table3
) t
where ...;
According to your sample data, this gets you duplicates, as some entries exist in more than one table. If you want to remove the duplicates, use UNION instead of UNION ALL.

MSSQL server SQL query how to utilize "group by"

Table Data
My_table_data.
testdate plant value
2020-05-14 02:00:00.000 1 68
2020-05-14 01:00:00.000 3 115
2020-05-14 02:00:00.000 2 107
2020-05-14 03:00:00.000 2 102
2020-05-14 05:00:00.000 2 104
2020-05-14 06:00:00.000 2 111
2020-05-14 08:00:00.000 1 74
2020-05-14 08:00:00.000 2 114
2020-05-14 09:00:00.000 1 70
2020-05-14 09:00:00.000 2 114
2020-05-14 03:00:00.000 3 106
2020-05-14 03:00:00.000 3 102
2020-05-15 02:00:00.000 2 108
2020-05-14 05:00:00.000 1 74
2020-05-14 04:00:00.000 3 96
2020-05-14 04:00:00.000 3 97
2020-05-14 06:00:00.000 1 80
2020-05-14 03:00:00.000 1 77
2020-05-14 06:00:00.000 3 102
I am novice at complex SQL query phrase. How to achieve the result like below.
Conditions
1. Latest data on timestamp(Hour)
2. single row for each plant.
WITH data AS(
SELECT testdate, plant, value, (DATEPART(HOUR,testdate))AS dH FROM My_table_data
WHERE testdate >= (SELECT CONVERT(DATETIME, CONVERT(DATE, GETDATE()), 120))
)
SELECT data.testdate, data.plant, data.value from data
WHERE data.dH= (SELECT MAX(data.dH) FROM data)
GROUP BY data.plant
ORDER BY data.plant DESC;
It gives error!!
testdate Plant value
2020-05-14 09:00:00.000 1 70
2020-05-14 09:00:00.000 2 114
2020-05-14 06:00:00.000 3 102
I think this should be as simple as
with maxData
AS
(
SELECT MAX(testDate) as testDate, plantId
FROM My_table_data
GROUP BY plantId
)
select
t.testDate,
t.plantId,
t.value
from My_table_data t
INNER JOIN maxData m
on t.testDate = m.testDate
and t.plantId = m.plantId
order by plantId
WHat this does is uses a CTE to find the max date for every plantId and then joins that info back to the original table to find the value corresponding to that plantId and date.

grouping a table with different dates

I have a table like below,
SalesId ItemId DateSale USDVal
ABC 01A 2018-04-01 52
ABC 01B 2018-04-01 300
ABC 01C 2018-04-01 12
ABC 01D 2018-04-01 62
ABC 01A 2018-03-23 66
MNB 01A 2018-01-01 584
MNB 01A 2018-02-20 320
MNB 01F 2018-02-20 5
I want to write a query that selects the last date for each SalesId and shows those records so the result would look something like below.
Result
SalesId ItemId DateSale USDVal
ABC 01A 2018-04-01 52
ABC 01B 2018-04-01 300
ABC 01C 2018-04-01 12
ABC 01D 2018-04-01 62
MNB 01A 2018-02-20 320
MNB 01F 2018-02-20 5
In SQL Server, the fastest way is often a correlated subquery:
select t.*
from t
where t.datesale = (select max(t2.datesale) from t t2 where t2.salesid = t.salesid);

MSSQL MAX returns all results?

I have tried the following query to return the highest P.Maxvalue for each ME.Name from the last day between 06:00 and 18:00:
SELECT MAX(P.MaxValue) AS Value,P.DateTime,ME.Name AS ID
FROM vManagedEntity AS ME INNER JOIN
Perf.vPerfHourly AS P ON ME.ManagedEntityRowId = P.ManagedEntityRowId INNER JOIN
vPerformanceRuleInstance AS PRI ON P.PerformanceRuleInstanceRowId = PRI.PerformanceRuleInstanceRowId INNER JOIN
vPerformanceRule AS PR ON PRI.RuleRowId = PR.RuleRowId
WHERE (ME.ManagedEntityTypeRowId = 2546) AND (pr.ObjectName = 'VMGuest-cpu') AND (pr.CounterName LIKE 'cpuUsageMHz') AND (CAST(p.DateTime as time) >= '06:00:00' AND CAST(p.DateTime as time) <='18:00:00') AND (p.DateTime > DATEADD(day, - 1, getutcdate()))
group by ME.Name,P.DateTime
ORDER by id
but it seems to return each MaxValue for each ID instead of the highest?
like:
Value DateTime ID
55 2018-02-19 12:00:00.000 bob:vm-100736
51 2018-02-19 13:00:00.000 bob:vm-100736
53 2018-02-19 14:00:00.000 bob:vm-100736
52 2018-02-19 15:00:00.000 bob:vm-100736
52 2018-02-19 16:00:00.000 bob:vm-100736
51 2018-02-19 17:00:00.000 bob:vm-100736
54 2018-02-19 18:00:00.000 bob:vm-100736
51 2018-02-20 06:00:00.000 bob:vm-100736
51 2018-02-20 07:00:00.000 bob:vm-100736
53 2018-02-20 08:00:00.000 bob:vm-100736
52 2018-02-20 09:00:00.000 bob:vm-100736
78 2018-02-19 12:00:00.000 bob:vm-101
82 2018-02-19 13:00:00.000 bob:vm-101
79 2018-02-19 14:00:00.000 bob:vm-101
78 2018-02-19 15:00:00.000 bob:vm-101
79 2018-02-19 16:00:00.000 bob:vm-101
77 2018-02-19 17:00:00.000 bob:vm-101
82 2018-02-19 18:00:00.000 bob:vm-101
82 2018-02-20 06:00:00.000 bob:vm-101
79 2018-02-20 07:00:00.000 bob:vm-101
81 2018-02-20 08:00:00.000 bob:vm-101
82 2018-02-20 09:00:00.000 bob:vm-101
155 2018-02-19 12:00:00.000 bob:vm-104432
there is one value per hour for each id hence twelve results for each id
does MAX not work in this way i want ?
Thanks
expected view like this :
Value DateTime ID
55 2018-02-19 12:00:00.000 bob:vm-100736
82 2018-02-19 13:00:00.000 bob:vm-101
etc
If you're using group by on datetime and id, you'll get all datetimes and all ids, it's that simple.
If you don't need exact time, you can group by date only:
SELECT MAX(P.MaxValue) AS Value, cast(P.DateTime as date) as dat, ME.Name AS ID
...
group by ME.Name, cast(P.DateTime as date)
Or if you do, you may use not exists clause instead of group by.

Trigger created with compilation error

I am trying to create a trigger that increases the discnt of a customer by .04 every time that customer places an order. Next I need to insert a new order in the orders table.
The following is the Customers table:
CID CNAME CITY DISCNT
c001 Tiptop Duluth 10
c002 Basics California 12
c003 7/11 California 8
c004 ACME Duluth 8
c006 ACME Kyoto 0
c007 Goldberg NYC 15
The following is the orders table:
ORDNO MON CID AID PID QTY DOLLARS
1011 jan c001 a01 p01 1000 450
1012 jan c001 a01 p01 1000 450
1019 feb c001 a02 p02 400 180
1017 feb c001 a06 p03 95959 540
1018 feb c001 a03 p04 600 540
1023 mar c001 a04 p05 500 450
1022 mar c001 a05 p06 400 720
1025 apr c001 a05 p07 800 720
1013 jan c002 a03 p03 1000 880
1026 may c002 a05 p03 800 704
1015 jan c003 a03 p05 1200 1104
1014 jan c003 a03 p05 1200 1104
1021 feb c004 a06 p01 1000 460
1016 jan c006 a01 p01 1000 500
1020 feb c006 a03 p07 600 600
1024 mar c006 a06 p01 800 400
The trigger I have created is:
create or replace trigger UpdateDiscnt
after insert or update on orders
for each row
begin
update customers set discnt = 0.4 + :old.discnt where
customers.cid=:new.cid;
end;
/
The error is an oracle error and there is no discnt in the order table so any version of old.discnt is incorrect.
try
create or replace trigger UpdateDiscnt
after insert or update on orders
for each row
begin
update customers set discnt = 0.4 + discnt
where customers.cid= :new.cid;
end;
/