0% found this document useful (0 votes)
12 views10 pages

SQL Commands

Common SQL Command

Uploaded by

hariom_jadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views10 pages

SQL Commands

Common SQL Command

Uploaded by

hariom_jadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

SELECT * from INFORMATION_SCHEMA.

PROCESSLIST where state = 'Waiting for table


metadata lock';

alter table `dev_qa_db`.`dashboard_product_store` modify created_on timestamp NOT


NULL DEFAULT CURRENT_TIMESTAMP;

alter table `dev_qa_db`.`dashboard_product_store` modify last_modified_on timestamp NOT


NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

SELECT count(store_id), store_id FROM qa_db.dashboard_transactions where


transaction_date = '2021-11-15' group by store_id;

SELECT count(*) FROM qa_db.dashboard_predictions where tsnow = '2021-11-16';

**************************** duplicate qa_bd data insertion commands ********************

INSERT INTO demo_qa.dashboard_clients SELECT * from qa_db.dashboard_clients;

INSERT INTO demo_qa.dashboard_products SELECT * from qa_db.dashboard_products;

===============================================================

update my_demo_new.auth_user t1 join my_demo.auth_user t2


set
t1.`password` = t2.`password`,
t1.`last_login` = t2.`last_login`
where
t1.`id` = t2.`id` and
t1.`last_login` != t2.`last_login`;

=====================================================

select my_demo.dashboard_products.* from my_demo.dashboard_products


join my_demo_new.dashboard_products on my_demo.dashboard_products.id =
my_demo_new.dashboard_products.id
where my_demo.dashboard_products.last_modified_on !=
my_demo_new.dashboard_products.last_modified_on;
=================================================

SHOW COLUMNS FROM my_demo.auth_user;

=============================================================
INSERT IGNORE INTO """+DBNAME2+""".auth_user select """+DBNAME1+""".auth_user.*
from """+DBNAME1+""".auth_user left join """+DBNAME2+""".auth_user on
"""+DBNAME1+""".auth_user.id = """+DBNAME2+""".auth_user.id where
"""+DBNAME2+""".auth_user.id is null;

=============================================================
alter table `dev_qa_db`.`dashboard_compositekey` add created_on timestamp NOT NULL
DEFAULT CURRENT_TIMESTAMP;

alter table `dev_qa_db`.`dashboard_product_store` add last_modified_on timestamp NOT


NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
================================================================

SELECT group_concat(column_name)
FROM information_schema.columns
WHERE table_name = 'dashboard_transactions'
AND table_schema = 'lowinfood_dev';

=========================================================

******To truncate a table when foreign key constraints comes***********

SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE table dev_qa_db_1.dashboard_compositekey;
SET FOREIGN_KEY_CHECKS = 1;

=================================================
**********************
SET FOREIGN_KEY_CHECKS = 0;
insert into dev_qa_db_1.dashboard_compositekey(id ,customer_id, product_id, store_id,
client_id)
select * from qa_db.dashboard_compositekey;
SET FOREIGN_KEY_CHECKS = 1;

**********************

SET FOREIGN_KEY_CHECKS = 0;

SET FOREIGN_KEY_CHECKS = 1;

===============================================

SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE table lowinfood_dev.dashboard_products;
TRUNCATE table lowinfood_dev.dashboard_transactions;
TRUNCATE table lowinfood_dev.dashboard_promotion;
TRUNCATE table lowinfood_dev.dashboard_promotion_product_store_info;
SET FOREIGN_KEY_CHECKS = 1;

ALTER TABLE lowinfood_demo.dashboard_transactions ADD INDEX (product_id,store_id,


date_of_sale);

===========================================
CREATE TABLE `dev_qa_db`.`prediction_staging_temp` (
`id` INT NULL,
`prod` INT NULL,
`tsnow` DATETIME NULL,
`tsforecast` DATETIME NULL,
`forecast` DOUBLE NULL,
`model` VARCHAR(255) NULL,
`experiment` VARCHAR(255) NULL,
`prediction_date` DATETIME NULL);

============================================================

INSERT INTO {DBNAME}.dashboard_predictions(tsnow, tsforecast, prediction_value,


prediction_date,
model, experiment, compositeKey_id, customer_id, product_id, store_id)
SELECT DATE({DBNAME}.prediction_staging_temp.tsnow),
DATE({DBNAME}.prediction_staging_temp.tsforecast),
{DBNAME}.prediction_staging_temp.forecast AS prediction_value,
DATE({DBNAME}.prediction_staging_temp.prediction_date),
{DBNAME}.prediction_staging_temp.model,
{DBNAME}.prediction_staging_temp.experiment,
prod,
{DBNAME}.dashboard_compositekey.customer_id,
{DBNAME}.dashboard_compositekey.product_id,
{DBNAME}.dashboard_compositekey.store_id
FROM {DBNAME}.prediction_staging_temp
JOIN {DBNAME}.dashboard_compositekey ON PROD =
{DBNAME}.dashboard_compositekey.ID
LEFT JOIN {DBNAME}.dashboard_predictions
on ({DBNAME}.prediction_staging_temp.tsnow =
{DBNAME}.dashboard_predictions.tsnow and
{DBNAME}.prediction_staging_temp.tsforecast =
{DBNAME}.dashboard_predictions.tsforecast and
{DBNAME}.prediction_staging_temp.prod =
{DBNAME}.dashboard_predictions.compositeKey_id)
where {DBNAME}.dashboard_predictions.id is null;
=====================================================
UPDATE dev_qa_db_1.dashboard_predictions dp
join dev_qa_db_1.prediction_staging_temp pst
set dp.prediction_value = pst.forecast,
dp.model = pst.model,
dp.experiment = pst.experiment
where compositeKey_id = pst.prod
AND dp.tsnow = DATE(pst.tsnow)
AND dp.tsforecast =DATE( pst.tsforecast)
AND dp.prediction_date = DATE(pst.prediction_date)
AND (dp.prediction_value != pst.forecast
OR dp.model != pst.model
OR dp.experiment != pst.experiment);

=======================================================

show create table dev_qa_db_1.dashboard_compositekey;

=====================================================================

truncate table dev_qa_db_1.dashboard_predictions;


INSERT INTO dev_qa_db_1.dashboard_predictions(tsnow, tsforecast, prediction_value,
prediction_date,
model, experiment, compositeKey_id, customer_id, store_id, product_id) SELECT
DATE(dev_qa_db_1.prediction_staging_temp.tsnow),
DATE(dev_qa_db_1.prediction_staging_temp.tsforecast),
dev_qa_db_1.prediction_staging_temp.forecast AS prediction_value,
DATE(dev_qa_db_1.prediction_staging_temp.prediction_date),
dev_qa_db_1.prediction_staging_temp.model,
dev_qa_db_1.prediction_staging_temp.experiment,
prod,
dashboard_compositekey.customer_id, dashboard_compositekey.store_id,
dashboard_compositekey.product_id
FROM dev_qa_db_1.prediction_staging_temp
JOIN dev_qa_db_1.dashboard_compositekey ON PROD = dashboard_compositekey.ID
LEFT JOIN dev_qa_db_1.dashboard_predictions
on (dev_qa_db_1.prediction_staging_temp.tsnow =
dev_qa_db_1.dashboard_predictions.tsnow and
dev_qa_db_1.prediction_staging_temp.tsforecast =
dev_qa_db_1.dashboard_predictions.tsforecast and
dev_qa_db_1.prediction_staging_temp.prod =
dev_qa_db_1.dashboard_predictions.compositeKey_id)
where dev_qa_db_1.dashboard_predictions.id is null;

==============================================================
SELECT distinct ArticleId, NameLongArticle FROM dev_qa_db_1.staging_16_12_21 where
ArticleId in
(25893, 31331, 31376, 31374, 31379, 31377, 31375, 31369, 31378, 31335, 31370, 31360,
31338, 31336, 31334, 31332, 31339, 31337, 31333, 31340, 31269, 31346, 31342, 31329,
31280, 31343, 31310, 31279, 31278, 31277, 31276, 29936, 31264, 31268, 31295, 31266,
31293, 31298, 31291, 31296, 31267, 31294, 31299, 31265, 31292, 31297, 31316, 31283,
31282, 31252, 31249, 31229, 31230, 31227, 31225, 31226, 31219, 31215, 31171, 31169,
31167, 31172, 31170, 31168, 31189, 31187, 31178, 19959, 20135, 19958)
order by ArticleId;

SELECT * FROM dev_qa_db_1.dashboard_products where id in


(25893, 31331, 31376, 31374, 31379, 31377, 31375, 31369, 31378, 31335, 31370, 31360,
31338, 31336, 31334, 31332, 31339, 31337, 31333, 31340, 31269, 31346, 31342, 31329,
31280, 31343, 31310, 31279, 31278, 31277, 31276, 29936, 31264, 31268, 31295, 31266,
31293, 31298, 31291, 31296, 31267, 31294, 31299, 31265, 31292, 31297, 31316, 31283,
31282, 31252, 31249, 31229, 31230, 31227, 31225, 31226, 31219, 31215, 31171, 31169,
31167, 31172, 31170, 31168, 31189, 31187, 31178, 19959, 20135, 19958)
order by id;

update qa_db.dashboard_products prod join dev_qa_db_1.staging_16_12_21 staging


set prod.name = staging.NameLongArticle
where prod.id = staging.ArticleId and
staging.ArticleId in (25893, 31331, 31376, 31374, 31379, 31377, 31375, 31369, 31378,
31335, 31370, 31360, 31338, 31336, 31334, 31332, 31339, 31337, 31333, 31340, 31269,
31346, 31342, 31329, 31280, 31343, 31310, 31279, 31278, 31277, 31276, 29936, 31264,
31268, 31295, 31266, 31293, 31298, 31291, 31296, 31267, 31294, 31299, 31265, 31292,
31297, 31316, 31283, 31282, 31252, 31249, 31229, 31230, 31227, 31225, 31226, 31219,
31215, 31171, 31169, 31167, 31172, 31170, 31168, 31189, 31187, 31178, 19959, 20135,
19958)
;

================================================
Made by me
select count(*)
from qa_db.dashboard_predictions a
left join qa_db.dashboard_predictions b on
a.compositeKey_id = b.compositeKey_id and
a.tsnow = b.tsnow and
a.tsforecast = b.tsforecast and
a.prediction_value = b.prediction_value and
a.product_id = b.product_id and
a.store_id = b.store_id
where b.id is not null;
==================================================

SELECT * FROM dev_qa_db_1.staging_16_12_21;


SELECT distinct ArticleId, NameLongArticle FROM dev_qa_db_1.staging_16_12_21 where
ArticleId in
(25893, 31331, 31376, 31374, 31379, 31377, 31375, 31369, 31378, 31335, 31370, 31360,
31338, 31336, 31334, 31332, 31339, 31337, 31333, 31340, 31269, 31346, 31342, 31329,
31280, 31343, 31310, 31279, 31278, 31277, 31276, 29936, 31264, 31268, 31295, 31266,
31293, 31298, 31291, 31296, 31267, 31294, 31299, 31265, 31292, 31297, 31316, 31283,
31282, 31252, 31249, 31229, 31230, 31227, 31225, 31226, 31219, 31215, 31171, 31169,
31167, 31172, 31170, 31168, 31189, 31187, 31178, 19959, 20135, 19958)
order by ArticleId;

select count(name) as cnt from qa_db.dashboard_products where name is not null; -- 15674

======================================================

DELETE FROM `dev_qa_db_1`.`dashboard_products` WHERE (`id` = '31331');


==================================================

update qa_db.dashboard_products prod join dev_qa_db_1.staging_20_12_21 staging


set prod.name = staging.NameLongArticle
where prod.id = staging.ArticleId and
staging.ArticleId in (3, 12946, 13137, 13183, 13192, 14650, 28649, 28711, 28790,
28799, 28802, 28803, 28804, 28806, 28807, 28815, 28823, 28825,
28826, 28829, 28830, 28834, 28840, 28843, 28848, 29006, 29015,
29017, 29019, 29068, 29165, 29170, 29174, 29175, 29176, 29177,
29178, 29179, 29180, 29183, 29184, 29193, 29519, 29647, 29997,
30552, 30955, 30956, 31260, 31261, 31286, 31344, 31386)

===========================================

SELECT DISTINCT id
FROM dev_qa_db_1.dashboard_products
left join dev_qa_db_1.staging_20_12_21
on dev_qa_db_1.dashboard_products.id = dev_qa_db_1.staging_20_12_21.ArticleId
where dev_qa_db_1.dashboard_products.name is null GROUP BY ArticleId;
======================================

SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE `foresightee_repository_prod`.`dashboard_transactiontemp`;
SET FOREIGN_KEY_CHECKS = 1;

============================================

SELECT id FROM qa_db.dashboard_products where id not in(select id from


dev_qa_db_1.dashboard_products) ;
==========================================================

update qa_db.dashboard_products prod join dev_qa_db_1.dashboard_products pd


set prod.name = pd.name
where prod.id = pd.id and
pd.id = 30910;
==============================================================
To check promotions

SELECT dp.name as product_name, product_id, ds.name as store_name, store_id FROM


qa_db.dashboard_promotion_product_store_info dpp
join qa_db.dashboard_products dp on dp.id= dpp.product_id
join qa_db.dashboard_stores ds on ds.id= dpp.store_id
where promotion_id in (
SELECT id FROM qa_db.dashboard_promotion where effective_date>= '2022-04-04' );
==================================================================

select transaction_date , count(*) from qa_db.dashboard_transactions


where transaction_date>'2021-10-01' group by 1 order by 1 desc ;
====================================================
SELECT * FROM qa_db.prediction_record;
DELETE FROM qa_db.prediction_record WHERE id = 1;

insert into qa_db.prediction_record (prediction_file) values('2021-12-14');


========================================================

SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE


TABLE_NAME = 'dashboard_promotion';

-- ALTER TABLE qa_db.dashboard_promotion DROP INDEX effective_date_UNIQUE;

ALTER TABLE qa_db.dashboard_promotion DROP INDEX end_date_UNIQUE;


================================================================
INSERT INTO foresightee_repository_prod.repository_file(
sftp_user_id, file_name, status, activity, file_received_at, is_active, created_on,
last_modified_on
) VALUES(4, 'prediction_file_2021-12-15.csv', 'DELIVERED_TO_SFTP', 'upload',
'2022-01-11 11:01:54', 1, '2022-01-11 11:01:54', '2022-01-11 11:01:54');
==================================================================

update qa_db.dashboard_stores set client_id = 4 where id in ( 1, 2, 3, 6, 7);


============================================================

update qa_db.dashboard_products prod join dev_qa_db_1.dashboard_products staging


set prod.name = staging.name
where prod.id = staging.id and
staging.id in ( 31413, 31414, 31368, 29262, 29746, 30499);
====================================================================
For Aldi store

SELECT name, ArticleCategory, ArticleFamily, ArticleSubfamily FROM


qa_db.dashboard_compositekey dc
join qa_db.dashboard_products dp
on dp.id = dc.product_id
where store_id in (292, 293, 294, 295, 340, 584, 700, 899);

For Makro store


where store_id in (304, 305, 306, 307, 308, 309);

For Metro store


where store_id in (299, 300, 301, 302, 303, 339, 437, 600, 601, 761, 784, 871, 911);

For FreshDirect store


where store_id in (1, 2, 3, 6, 7, 290, 772);

============================================================
SELECT distinct da.product_id from qa_db.dashboard_assortment_products da
join qa_db.dashboard_products dp
on dp.id = da.product_id
where store_description like '%Aldi%' ;
===========================================================
INSERT INTO `foresightee_repository_dev`.`rep_schema_config` (`name`, `hostname`,
`username`, `password`, `type`) VALUES ('wallmart', 'abcdef', 'admin', 'foresightee',
'staging');
========================================================
For checking multiple values in

SELECT
col1, col2,..., COUNT(*)
FROM
table_name
GROUP BY
col1,
col2,
...
HAVING
(COUNT(col1) > 1) AND
(COUNT(col2) > 1) AND
==================================================================

show tables from foresightee_repository_dev;

===================================
To check Promotion for ald onlyi

SELECT count(*) FROM qa_db.dashboard_promotion where id <=290;

SELECT count(*) FROM qa_db.dashboard_promotion_product_store_info


where store_id in (292,293,294,295,340,584,700,899);

SELECT count(*) FROM qa_db.dashboard_transactions where promotion = 1


and store_id in (292,293,294,295,340,584,700,899);

SELECT count(*) FROM qa_db.greenyard_input_new where promotion = 1;


===========================================================
To count data in transaction table for Aldi, Freshdirect and MecroMetro client

SELECT count(*) FROM qa_db.dashboard_transactions where transaction_date = '2022-02-


10' and store_id in (1,2,3,6,7,290,772);

SELECT count(*) FROM qa_db.dashboard_transactions where transaction_date = '2022-02-


10' and store_id in (292,293,294,295,340,584,700,899);

SELECT count(*) FROM qa_db.dashboard_transactions


where transaction_date = '2022-02-10' and
store_id in
(304,305,306,307,308,309,299,300,301,302,303,339,437,600,601,761,784,871,911);
===============================================================
To update client_id in compositkey table

update dev_qa_db_1.dashboard_compositekey set client_id = 1


where store_id in (292,293,294,295,340,584,700,899);

update dev_qa_db_1.dashboard_compositekey set client_id = 2


where store_id in (304,305,306,307,308,309);

update dev_qa_db_1.dashboard_compositekey set client_id = 3


where store_id in (299,300,301,302,303,339,437,600,601,761,784,871,911);

update dev_qa_db_1.dashboard_compositekey set client_id = 4


where store_id in (1,2,3,6,7,290,772);
=============================================================

update dev_qa_db_1.dashboard_transactions
set created_on = '2022-02-07 08:03:00',
last_modified_on = '2022-02-07 08:03:00'
where transaction_date = '2022-02-06' and store_id in (1,2,3,6,7,290,772);
================================================================
Create table in AWS Redshift with auto increment id and insert query
create table s3_file_log(
id bigint identity(1, 1),
file_name varchar(250),
created_on datetime)

INSERT INTO s3_file_log (file_name, created_on) VALUES( '{file}', '{created_on}');


====================================================

Redshift copy json file from s3 to redshift table

COPY public.s3_data_tmp FROM 's3://revealera-redshift-uploads/input_to_rds.json'


IAM_ROLE 'arn:aws:iam::713992711280:role/redshifts3' json 'auto' REGION 'us-east-2';

With json path file

copy s3_data_tmp
from 's3://revealera-redshift-uploads/input_to_rds.json'
credentials 'aws_iam_role=arn:aws:iam::713992711280:role/redshifts3'
region 'us-east-2'
json 's3://revealera-redshift-uploads/input_to_rds_jsonpath.json';

====================================================================

To view database size with free space

SELECT table_schema "database name", sum( data_length + index_length ) / 1024 / 1024


"database size in MB",
sum( data_free )/ 1024 / 1024 "free space in MB" FROM information_schema.TABLES
GROUP BY table_schema;

You might also like