Oracle SELECT Subquery - oracle

I have two tables. One for transaction list and one for reference
Transaction:
ID | Agency ID | Advertiser ID | Code
1 | 123 | 440 | samplecode
Reference:
ID | LongName | Type
123 | Agency1 | Agency
440 | Advertiser1 | Advertiser
How can I write the SQL Subquery in Oracle such that I can include the LongName and the Type in the SELECT statement in the transaction table so that it will look like this:
ID | Agency ID | LongName | Type | Advertiser ID | LongName | Type | Code
1 | 123 | Agency1 | Agency | 440 | Advertiser1 | Advertiser | samplecode

You may join Transaction to Reference twice:
SELECT
t.ID,
t."Agency ID",
r1.LongName AS ln1,
r1.Type AS type1,
t."Advertiser ID",
r2.LongName AS ln2,
r2.Type AS type2,
t.Code
FROM Transaction t
INNER JOIN Reference r1
ON t."Agency ID" = r1.ID
INNER JOIN Reference r2
ON t."Advertiser ID" = r2.ID

Related

Delete query inoracle db is fast but select query running too long

I have below table with 160,000 rows. When I use SELECT ID FROM mytable WHERE id NOT IN ( SELECT max(id) FROM mytable GROUP BY user_id); query is running very long and not finishing (I wait for 1 Hr) but when I use delete FROM mytable WHERE id NOT IN (SELECT max(id) FROM mytable GROUP BY user_id); query is running in 0.5 seconds. Why??
---------------------------------------------------------------------------------------------------
| id | MyTimestamp | Name | user_id ...
----------------------------------------------------------------------------------------------------
| 0 | 1657640396 | John | 123581 ...
| 1 | 1657638832 | Tom | 168525 ...
| 2 | 1657640265 | Tom | 168525 ...
| 3 | 1657640292 | John | 123581 ...
| 4 | 1657640005 | Jack | 896545 ...
-----------------------------------------------------------------------------------------

how to select only external tranzactions(column values) in oracle?

i have a first table with id_client,id_card (info table)
and second with id_debet,id_credit,value,vale_info (tranzaction table)
first table second table
______________________ ________________________________________________________
|id_client | id_card | |id_debet |id_credit | value | value info |
|50102411 | 5166 | | 5166 | 5167 | 300$ | transfer to client card |
|50102411 | 5167 | | 5167 | 4931239 | 100$ | UBER taxi NY |
|50102412 | 5168 | | 5168 | 5169 | 200$ | transfer to client card |
|50102412 | 5169 | | 5169 | 3582934 | 120$ | rent hotel room |
______________________ ________________________________________________________
id_debet -> spend from your card
id_Debet -> replenishment to your card
and i want to select only external tranzaction not between card clients
for ex
if 5166 send money to 5167 i skip this tranzaction
if 5166 send to another card i select it etc
You can use a NOT EXISTS query to find all transactions in the second table that have an id_debet or id_credit which is not in the first table:
SELECT *
FROM table2 t2
WHERE NOT EXISTS (SELECT *
FROM table1 t1
WHERE t1.id_card = t2.id_debet)
OR NOT EXISTS (SELECT *
FROM table1 t1
WHERE t1.id_card = t2.id_credit)
Output:
id_debet id_credit value value info
5167 4931239 100$ UBER taxi NY
5169 3582934 120$ rent hotel room
Demo on dbfiddle
You can use GROUP BY and HAVING as follows:
SELECT T.id_debet, T.id_credit, T.value, T.value info FROM tranzaction T
JOIN info I ON I.id_card IN (T.id_debet, T.id_credit)
GROUP BY T.id_debet, T.id_credit, T.value, T.value
HAVING COUNT(1) = 1

How to merge two tables with same schema in Talend avoiding duplicates?

I have two tables TableA and TableB
TableA looks similar to following:
customerId | name | email |telephone
-------------------------------------------------
00001 | Anne | anne#gmail.com | 123456
00002 | Ben | ben#gmail.com |
00003 | Ryan | ryan#yahoo.com |
TableB looks similar to following:
customerId | name | email | telephone
---------------------------------------------------
76105 | Anne | anne#gmail.com |
89102 | Ben | ben#gmail.com | 567890
23390 | Ryan | ryan#yahoo.com | 756541
43769 | Abby | abby#yahoo.com | 890437
I'm trying to achieve the following 2 tables.
TableC
customerId | name | email |telephone
-------------------------------------------------
00001 | Anne | anne#gmail.com | 123456
00002 | Ben | ben#gmail.com | 567890
00003 | Ryan | ryan#yahoo.com | 756541
TableD
customerId | name | email |telephone
-------------------------------------------------
43769 | Abby | abby#gmail.com | 890437
I was using a tmap with TableA as the main and the TableB as the look up. In the tmap I created an inner join between TableA and TableB using email as the foreign key. I wrote innerJoin outputs to one table and innerJoin rejects to another. However I find some of the records missing in TableC.
What is the correct way to achieve this in Talend DI?
I think the choice of the main and the lookup impact the reject catching, here is what you need :
tmap :
tFixedFlowInput : to simulate your data
tLogRow: to display output data

List customer ID, name and all of his/her accounts

customers:
+------------+--------------+
| cid | Name |
+------------+--------------+
| 1 | Bob |
| 2 | John |
| 3 | Jane |
+------------+--------------+
accounts:
+------------+--------------+
| aid | type |
+------------+--------------+
| 1 | Checking |
| 2 | Saving |
| 3 | CD |
+------------+--------------+
transactions:
+------------+--------------+--------------+
| tid | cid | aid |
+------------+--------------+--------------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 1 | 2 |
| 4 | 2 | 3 |
| 5 | 3 | 1 |
+------------+--------------+--------------+
I am trying to write a plsql procedure that, given the customer id as a parameter, will display his/her id, name and all accounts. Displaying the id and name is simple enough. What I'm not sure about is how to get all the accounts that are linked to the customer id and how to retrieve more than a single account.
An ideea can be:
select c.cid, c.name, a.type
from customers c
left join transactions t on (t.cid = c.cid)
left join accounts a on (a.aid = t.aid)
where c.cid = :customer_id
group by c.cid, c.name, a.type;
the group by is needed because can be more transactions.
Further, if you want to see one line:
select cid, name, LISTAGG(type, ',') WITHIN GROUP (ORDER BY type) as account_types
from(
select distinct c.cid, c.name, a.type
from customers c
left join transactions t on (t.cid = c.cid)
left join accounts a on (a.aid = t.aid)
where c.cid = :customer_id
)
group by cid, name;
Putting this into a stored procedure/function is too simple, so I let it to you.

How are product attributes and attribute options stored in Magento database?

I am trying to figure out how the linkage between attribute and attribute options, and product and attributes are made in Magento. Is there any reference to how this is working? or anyone give me a hint on this.
Thanks,
Balan
As Alan Storm says: "you do not have to know about how your db works. You have to learn how the models work ". (This is not an exact quote. I gave you the meaning).
But I created own scheme to understand the DB structure. So this screen shows how it works:
Hope, it helps.
Also I recommend you to look through these links:
http://www.magentocommerce.com/wiki/2_-_magento_concepts_and_architecture/magento_database_diagram
http://alanstorm.com/magento_advanced_orm_entity_attribute_value_part_1
1) The attributes are stored in eav_attribute. There you get the attribute_id.
2) The options are stored in eav_attribute_option_value. There yout get the option_id.
3) The options are assigned to the product in catalog_product_entity_varchar. There you need the entity_id of the product, the attribute_id from 1) and the value which are the comma separated option_ids from 2)
I've found these queries to be very helpful for hunting down things like - where does it say the product color is black?, for example.
-- show_product_attr.sql
select
p.entity_id,
p.entity_type_id,
p.attribute_set_id,
p.type_id,
p.sku,
a.attribute_id,
a.frontend_label as attribute,
av.value
from
catalog_product_entity p
left join catalog_product_entity_{datatype} av on
p.entity_id = av.entity_id
left join eav_attribute a on
av.attribute_id = a.attribute_id
where
-- p.entity_id = 28683
-- p.sku = '0452MR'
p.entity_id = {eid}
;
And for attr_options
-- show_product_attr_options.sql
select
p.entity_id,
-- p.entity_type_id,
-- p.attribute_set_id,
p.type_id,
p.sku,
a.attribute_id,
a.frontend_label as attribute,
-- a.attribute_code,
av.value,
ao.*
from
catalog_product_entity p
left join catalog_product_entity_int av on
p.entity_id = av.entity_id
left join eav_attribute a on
av.attribute_id = a.attribute_id
left join eav_attribute_option_value ao on
av.value = ao.option_id
where
-- p.entity_id = 28683
p.entity_id = {eid}
;
You need to replace {datatype} with text, varchar, int, decimal, etc, for the first query, and {eid} with entity_id for both queries. Which you can do on the command like like this:
$ cat show_product_attr_options.sql | sed -e "s/{eid}/30445/" | mysql -uUSER -pPASS DATABASE -t
+-----------+---------+--------------+--------------+---------------------------+-------+----------+-----------+----------+--------------------+-------------+
| entity_id | type_id | sku | attribute_id | attribute | value | value_id | option_id | store_id | value | colorswatch |
+-----------+---------+--------------+--------------+---------------------------+-------+----------+-----------+----------+--------------------+-------------+
| 30445 | simple | 840001179127 | 96 | Status | 1 | 5972 | 1 | 0 | Male | NULL |
| 30445 | simple | 840001179127 | 102 | Visibility | 1 | 5972 | 1 | 0 | Male | NULL |
| 30445 | simple | 840001179127 | 122 | Tax Class | 2 | 5973 | 2 | 0 | Female | NULL |
| 30445 | simple | 840001179127 | 217 | Size | 257 | 17655 | 257 | 0 | XS | NULL |
| 30445 | simple | 840001179127 | 217 | Size | 257 | 17657 | 257 | 1 | XS | NULL |
| 30445 | simple | 840001179127 | 224 | Color | 609 | 18717 | 609 | 0 | Arctic Ice Heather | NULL |
| 30445 | simple | 840001179127 | 260 | Featured | 0 | NULL | NULL | NULL | NULL | NULL |
| 30445 | simple | 840001179127 | 262 | Clearance Product | 0 | NULL | NULL | NULL | NULL | NULL |
| 30445 | simple | 840001179127 | 263 | Skip from Being Submitted | 0 | NULL | NULL | NULL | NULL | NULL |
| 30445 | simple | 840001179127 | 283 | Discontinued | 0 | NULL | NULL | NULL | NULL | NULL |
+-----------+---------+--------------+--------------+---------------------------+-------+----------+-----------+----------+--------------------+-------------+
A similar set of sql scripts can be created for catalog.
Product Attributes are extra values that you can assign to a product and is stored in the main EAV table, by name, and the data is then stored in a few different tables based on the data type, like varchar, decimal, text Integer, date, etc.
if you had multiple values for your Product Attribute, then that will be stored in the Attribute Options tables, again, different tables based on the data type.
the following link explains the relationships better:
http://www.magentocommerce.com/wiki/2_-_magento_concepts_and_architecture/magento_database_diagram
And deeper developer's detail:
http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-7-advanced-orm-entity-attribute-value
And Attribute sets will be the other thing you come across, like the name suggests, a set of attributes grouped together. http://www.magentocommerce.com/knowledge-base/entry/how-do-i-create-an-attribute-set
HTH
Shaun
SELECT pei.value
FROM `catalog_product_entity_int` pei
JOIN `eav_attribute` ea
ON pei.attribute_id = ea .attribute_id
WHERE pei.entity_id = {your product_id}
AND ea.attribute_code = '{your attribute_code}'
Note that there are a number of different tables like catalog_product_entity_int depending on the type of the attribute, so one of those other ones could be appropriate.
You can get all product properties by using this query:
SELECT CPEV.entity_id, CPE.sku, EA.attribute_id, EA.frontend_label, CPEV.value
FROM catalog_product_entity_varchar AS CPEV
INNER JOIN catalog_product_entity AS CPE ON CPE.entity_id = CPEV.entity_id
INNER JOIN eav_attribute AS EA ON(CPEV.attribute_id = EA.attribute_id AND EA.entity_type_id = 4)
INNER JOIN catalog_eav_attribute AS CEA ON(CEA.attribute_id = EA.attribute_id AND CEA.is_visible_on_front = 1 AND CEA.is_visible_in_grid = 1)

Resources