I have a table Sample with data stored like below
Id | String
--------------
1 abc,def,ghi
2 jkl,mno,pqr
I need the output like..
Id | processedrows
--------------
1 abc
1 def
1 ghi
2 jkl
2 mno
2 pqr
I run in SQL server successfully with XML table.
select a.Id,trim(COLUMN_VALUE)
FROM tableA a ,xmltable(('"'|| REPLACE(a.String, ',', '","')|| '"'))
How can I do the same with a select query in netezza?
Related
I will like to do the following in snowflake query.
Very new to snowflake , from what I have read it seems Snowflake doesn't support loops
I have 2 tables
Table A
Id col1 col2 col3
1 ABC 100 50
2 DEF 200 0
Table B
Date Percentage Name
2021-04-28 .5 XYZ
2021-04-28 .5 ZYX
In my output query I want to split each row in Table A
the integer columns are split by the Percentage from Table B
Add Name column from Table B (to represent the split integer value against that particular name
Output
Id col1 col2 col3 Name
1 ABC 50 25 XYZ
1 ABC 50 25 ZYX
2 DEF 100 0 XYZ
2 DEF 100 0 ZYX
This seems like a simple cartesian join:
create or replace table A (id number, col1 varchar, col2 number, col3 number);
insert into A values (1,'ABC',100,50),
(2,'DEF',200,0);
create or replace table B ("Date" date, Percentage number(3,2),Name varchar );
insert into B values ('2021-04-28',0.5,'XYZ'),
('2021-04-28',0.5,'ZYX');
select Id, col1,
round(col2 * percentage) as col2,
round(col3 * percentage) as col3,
Name
from A, B;
+----+------+------+------+------+
| ID | COL1 | COL2 | COL3 | NAME |
+----+------+------+------+------+
| 1 | ABC | 50 | 25 | XYZ |
| 1 | ABC | 50 | 25 | ZYX |
| 2 | DEF | 100 | 0 | XYZ |
| 2 | DEF | 100 | 0 | ZYX |
+----+------+------+------+------+
I have a scenario. There are two Tables TABLE-A and TABLE-B. The source is TABLE-A. The destination is TABLE-B. I want to compare the ID with self join. If ID is matching i want to ensure only NOT NULL value is picked. If both records has NULL value then Null value can be considered as ouput.
Below scenario,
TABLE-A has one duplicate ID i.e 1. In output i will be have one value for that duplicate record and merge data intelligently that NULL VALUES are excluded and if both records are having NULL for any column then NULL will be populated in TABLE-B.
TABLE A
ID NAME ADDRESS PHONE STATUS PROCESSFLAG
1 YOU XYZ NULL NULL 1
2 PQR ABC 123 Active 2
1 YOU NULL 322 NULL 2
OUTPUT TABLE B
ID NAME ADDRESS PHONE STATUS PROCESSFLAG
2 PQR ABC 123 Active 2
1 YOU XYZ 322 NULL 2
You can group by id and select max() for each column to exclude nulls:
insert into tableb(id, name, address, phone, status, processflag)
select id, max(name), max(address), max(phone), max(status), max(processflag)
from tablea
group by id
I assume that your problem is the nulls and the non null columns of the duplicates have the same value in different rows or you want the maximum of the 2 values like your sample data.
See the demo.
Results:
ID | NAME | ADDRESS | PHONE | STATUS | PROCESSFLAG
-: | :--- | :------ | :---- | :----- | ----------:
1 | YOU | XYZ | 322 | null | 2
2 | PQR | ABC | 123 | Active | 2
I have two tables A and B. B can have many records of A but B will not have any records of A
One-to-many -> one way
Now
Table A:
Id Name
----------
1 Rule1
2 Rule2
Table B:
Id TargetedTypeId TargetedId TargetingType TargetingId Status
TargetedType table:
Id Name
--------------
1 Users
2 Employee
TargetingType table:
Id Name
-----------------
1 Vegetable
2 Fruits
Users table:
Id Name
--------------
1 Abc
2 xyz
Vegetables table:
Id Name
-----------------
1 Onion
2 Potato
Fruits table:
Id Name
---------------
1 Apple
2 Orange
Table B will have rows like this:
Id | TargetedTypeId | TargetedId (Table A Ids) | TargetingType | TargetingId | Status
-----------------------------------------------------------------------
1 1 2 1 2 1
2 1 2 1 1 1
3 1 2 2 1 1
4 1 2 2 2 1
Please hep me to write a query which will fetch A tables entries present in B with status 1 and fetch all targeting types and get fruit details if its fruit and if vegetable if its vegetable type which is TargetingType and fetch its details with respective table fruit/vegetable with Pagination
I have 2 tables, one with the ID and its count and the other with the names belonging to the respective IDs. They need to be joined so that the end result is a table with count of 1 in each row and the respective names next to them. Note that the number of names in table 2 is less than the count in table 1 for the same ID in some cases.
Table 1
ID | Count
-----------
100 | 3
101 | 2
102 | 4
Table 2
ID | Name
----------
100 | abc
100 | def
101 | ghi
101 | jkl
102 | mno
102 | pqr
102 | stu
Result
ID | Count | Name
------------------
100 | 1 | abc
100 | 1 | def
100 | 1 |
101 | 1 | ghi
101 | 1 | jkl
102 | 1 | mno
102 | 1 | pqr
102 | 1 | stu
102 | 1 |
I'm using TSQL for this and my current query converts table 1 into multiple rows in the result table; then it inserts individual names from table 2 into the result table through a loop. I'm hoping there must be a simpler or more efficient way to do this as the current method takes considerable amount of time. If there is, please let me know.
The first thing that comes to mind for me involves using a Number table, which you could create (as a one-time task) like this:
CREATE TABLE numbers (
ID INT
)
DECLARE #CurrentNumber INT, #MaxNumber INT
SET #MaxNumber = 100 -- Choose a value here which you feel will always be greater than MAX(table1.Count)
SET #CurrentNumber = 1
WHILE #CurrentNumber <= #MaxNumber
BEGIN
INSERT INTO numbers VALUES (#CurrentNumber)
SET #CurrentNumber = #CurrentNumber + 1
END
Once you have a numbers table, you can solve this problem like this:
SELECT one.ID,
1 AS [Count],
ISNULL(two.Name,'') AS Name
FROM table1 one
JOIN numbers n ON n.ID <= CASE WHEN one.[Count] >= (SELECT COUNT(1) FROM table2 two WHERE one.ID = two.ID)
THEN one.[Count]
ELSE (SELECT COUNT(1) FROM table2 two WHERE one.ID = two.ID)
END
LEFT JOIN (SELECT ID,
Name,
ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) AS RecordNo
FROM table2) two ON one.ID = two.ID
AND two.RecordNo = n.ID
I have a two sql server tables that contains data like this
Table a
catId | catname | Isdeleted
-------------------------------------------------
1 ABC 0
2 DEF 0
3 GHI 0
and another table is
Table B
id | Name | Name1 | Catid
--------------------------------------------------
1 abc aaaa 1
2 def bbbb 1
3 ghi gggg 2
4 jkl jjjj 2
5 xyz xxxxx 3
Now I want result in this format
catname from table a and all the fields from table b according to
catid of table a and catname should be distinct.
Please help me
Write your query like this :
SELECT DISTINCT a.catname, b.* FROM a INNER JOIN b
ON a.catid = b.catid WHERE catid = [catid]
If you have multiple records in table b for each catid or catname, you will see multiple records with same catname in result. there is no other choice unless catname be unique in both tables a and b.