need result of two table sql query - sql-server

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.

Related

substrr Comma Separated column value to rows in netezza

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?

Compare two data sets in TABLE A and insert into TABLE B

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

SQL query / CTE for Association table having many to many relationships

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

Convert a row with multiple quantity to multiple rows of single quantity while joining another table with partial data

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

SQL Query to get data based on multiple filters

I have following Product table and ProductTag tables -
ID | Product
--------------
1 | Product_A
2 | Product_B
3 | Product_C
TagID | ProductID
----------------------
1 | 2
1 | 3
2 | 1
2 | 2
2 | 3
3 | 1
3 | 2
Now I need a SQL query that return all products list which are having both Tag 1 and 2. Result should be as given below -
ProductID | Product
------------------------
2 | Product_B
3 | Product_C
Please suggest how can i write a MS SQL query for this.
SELECT p.ID, p.Product
FROM Product p
INNER JOIN ProductTag pt
ON p.ID = pt.ProductID
WHERE pt.TagID IN (1, 2) -- <== Tags you want to find
GROUP BY p.ID, o.Product
HAVING COUNT(*) = 2 -- <== tag count on WHERE clause
however, if TagID is not unique on every Product, you need to count only the distinct product.
HAVING COUNT(DISTINCT pt.TagID) = 2
More on: SQL of Relational Division

Resources