Add date to SQL table - sql

Presto SQL
The idea is to get the historic data from the table by querying date. How to add current_date (YYYY-MM-DD) data I will set a refresh on daily. So when I want to look back of 5 days data I get the info.
**Data type **
member_id bigint
segemnt_id integer
DROP TABLE IF EXISTS ABC;
CREATE TABLE ABC AS ( SELECT member_id, Segment_id FROM XYZ)
This XYZ table has information of member_id and segment_id (Creating a separate table since XYZ contains many column just to reduce the load)
CREATE TABLE QQQ
(member_id bigint,
segment_id int)
INSERT INTO QQQ
(SELECT*, current_date as date, member_id, segment_id FROM ABC);

Related

Adding a Varchar auto-increment column to an select query

I'm trying to design a database and I have grouped a certain data as follows
select Name, prod_cat, prod_country, count(*)
from table1
group by Name, prod_cat, prod_country
union
select Name, prod_cat, prod_country, count(*)
from table2
group by Name, prod_cat, prod_country
Now for the resultant data from the query above, I would like to add an auto-increment ID as a VARCHAR column which should start as 'U000001' , 'U000002' , 'U000003' , 'U000004' ... all the way to the last column
For a better database, is it better to put the resultant query into a temporary table/view or is it better to use a stored procedure as there could be more data coming in
How can I add an auto-incrementing ID VARCHAR column to the above mentioned select query? Should I use Declare and increment?
Expected result :
Prod_ID
Name
Prod_cat
Prod_country
U000001
abc
12
USA
U000002
efg
1
IND
U000003
def
3
MEX
U000004
ijk
21
CHN

Oracle no result from comparing to date

I have run this simple query and return no result
enterselect * from record where recorddate = TO_DATE(2018, 'YYYY');
I have tested
Select to_date(recorddate,'YYYY') from record
It return ora01830:date format picture ends before converting entire input string
Here is my table structure :
create table record(
recordid varchar2(10),
singerid varchar2(10),
producedcountryid varchar2(10),
songid varchar2(10),
recorddate date,
constraint recordid_pk primary key (recordid),
constraint singerid2_fk foreign key (singerid) references singer(singerid),
constraint songid2_fk foreign key (songid) references song(songid)
);
DATEs in Oracle include hours, minutes and seconds.
So unless there are any RECORDDATEs that are at exactly 00:00:00 in the given month, the predicate where recorddate = TO_DATE(2018, 'YYYY') will not find anything to match.
In the second query, to_date(recorddate,'YYYY') is not a valid syntax for using to_date. Please see to_date for more information.
If you are trying to find all the RECORDs with RECORDDATEs in the year 2018, There are many ways to do so. Below are a couple examples.
CREATE TABLE RECORD (LOREM_IPSUM NUMBER, RECORDDATE DATE);
INSERT INTO RECORD VALUES (1,DATE '2017-05-05');
INSERT INTO RECORD VALUES (2,DATE '2018-05-05');
COMMIT;
SELECT * FROM RECORD;
LOREM_IPSUM RECORDDATE
1 05-MAY-17
2 05-MAY-18
Then:
SELECT * FROM RECORD WHERE EXTRACT(YEAR FROM RECORDDATE) = 2018;
Result:
LOREM_IPSUM RECORDDATE
2 05-MAY-18
-- Or:
SELECT * FROM RECORD WHERE TO_CHAR(RECORDDATE,'YYYY') = '2018';
Result:
LOREM_IPSUM RECORDDATE
2 05-MAY-18
If you want records from a specific year + month, you can:
SELECT * FROM RECORD WHERE TRUNC(RECORDDATE,'MM') = DATE '2017-05-01';
Result:
LOREM_IPSUM RECORDDATE
1 05-MAY-17
You will get the result set you want by querying the year-part of the date column recorddate.
select * from record
where extract (year from recorddate) = 2018

Oracle SQL Out put two table at a query

CREATE TABLE Products (
ID int,
name VARCHAR(70),
price NUMBER(8,2),
primary key (ID, name)
);
drop table Instock;
/* Delete the tables if they already exist */
create table Instock (
ID int,
quantity int
);
I have two table in my database. i want to have two table showing a output as a table that look like this
ID Name Price Quantity
SELECT A.ID,A.NAME,A.PRICE,B.QUANTITY FROM Products A,Instock B WHERE A.ID=B.ID
You can try this to create the new table(this creates the table in addition to putting the data in there)
CREATE TABLE MY_TABLE as SELECT A.ID,A.NAME,A.PRICE,B.QUANTITY FROM Products A,Instock B WHERE A.ID=B.ID

insert data from one table to another

I have 2 different tables but the columns are named slightly differently.
I want to take information from 1 table and put it into the other table. I need the info from table 1 put into table 2 only when the "info field" in table 1 is not null. Table 2 has a unique id anytime something is created, so anything inserted needs to get the next available id number.
Table 1
category
clientLastName
clientFirstName
incidentDescription
info field is not null then insert all fields into table 2
Table 2
*need a unique id assigned
client_last_name
client_first_name
taskDescription
category
This should work. You don't need to worry about the identify field in Table2.
INSERT INTO Table2
(client_last_name, client_first_name, taskDescription, category)
(SELECT clientLastName, clientFirstName, incidentDescription, category
FROM Table1
WHERE info_field IS NOT NULL)
Member_ID nvarchar(255) primary key,
Name nvarchar(255),
Address nvarchar(255)
)
insert into Member(Member_ID,Name,Address) (select m.Member_Id,m.Name,m.Address from library_Member m WHERE Member_Id IS NOT NULL)

Help With SQL - Combining Two Rows Into One Row

I have an interesting SQL problem that I need help with.
Here is the sample dataset:
Warehouse DateStamp TimeStamp ItemNumber ID
A 8/1/2009 10001 abc 1
B 8/1/2009 10002 abc 1
A 8/3/2009 12144 qrs 5
C 8/3/2009 12143 qrs 5
D 8/5/2009 6754 xyz 6
B 8/5/2009 6755 xyz 6
This dataset represents inventory transfers between two warehouses. There are two records that represent each transfer, and these two transfer records always have the same ItemNumber, DateStamp, and ID. The TimeStamp values for the two transfer records always have a difference of 1, where the smaller TimeStamp represents the source warehouse record and the larger TimeStamp represents the destination warehouse record.
Using the sample dataset above, here is the query result set that I need:
Warehouse_Source Warehouse_Destination ItemNumber DateStamp
A B abc 8/1/2009
C A qrs 8/3/2009
D B xyz 8/5/2009
I can write code to produce the desired result set, but I was wondering if this record combination was possible through SQL. I am using SQL Server 2005 as my underlying database. I also need to add a WHERE clause to the SQL, so that for example, I could search on Warehouse_Source = A. And no, I can't change the data model ;).
Any advice is greatly appreciated!
Regards,
Mark
SELECT source.Warehouse as Warehouse_Source
, dest.Warehouse as Warehouse_Destination
, source.ItemNumber
, source.DateStamp
FROM table source
JOIN table dest ON source.ID = dest.ID
AND source.ItemNumber = dest.ItemNumber
AND source.DateStamp = dest.DateStamp
AND source.TimeStamp = dest.TimeStamp + 1
Mark,
Here is how you can do this with row_number and PIVOT. With a clustered index or primary key on the columns as I suggest, it will use a straight-line query plan with no Sort operation, thus be particularly efficient.
create table T(
Warehouse char,
DateStamp datetime,
TimeStamp int,
ItemNumber varchar(10),
ID int,
primary key(ItemNumber,DateStamp,ID,TimeStamp)
);
insert into T values ('A','20090801','10001','abc','1');
insert into T values ('B','20090801','10002','abc','1');
insert into T values ('A','20090803','12144','qrs','5');
insert into T values ('C','20090803','12143','qrs','5');
insert into T values ('D','20090805','6754','xyz','6');
insert into T values ('B','20090805','6755','xyz','6');
with Tpaired(Warehouse,DateStamp,TimeStamp,ItemNumber,ID,rk) as (
select
Warehouse,DateStamp,TimeStamp,ItemNumber,ID,
row_number() over (
partition by ItemNumber,DateStamp,ID
order by TimeStamp
)
from T
)
select
max([1]) as Warehouse_Source,
max([2]) as Warehouse_Destination,
ItemNumber,
DateStamp
from Tpaired
pivot (
max(Warehouse) for rk in ([1],[2])
) as P
group by ItemNumber, DateStamp, ID;
go
drop table T;