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
Related
I ran into this problem because I'm using grouping sets in a subquery that gives me this results
name | objective | vehicle | count_of_installed
------------------------------------------------------
Antonio | | | 1
Antonio | 40 | 2nd vehicle | 19
Antonio | NULL | NULL | 20
George | | | 10
George | 50 | 2nd vehicle | 20
George | NULL | NULL | 30
And I'm then only searching only for rows where there are nulls, as they are the correct count_of_installed, now I want to have the correct objective and vehicle replacing the NULL values with the correct values, so I thought of something like coalesce(objective, objective), but I'm not sure this works.
For reference, my grouping sets are:
grouping sets (name, (name, objective, vehicle))
Edit 1
In my example above is the result of a query with joining my seller and sales tables, it shouldn't show neither blank nor null values, so when I say coalesce(objective, objective), I mean replace the null results from the result table with the correct values from my seller table.
I have a data that should be presented as a flexible table.
For example, this is how I save the data on my mysql server:
Column 1 | Column 2| Column 3| Column 4|Column 6|
------------|---------|---------|---------|--------|
UniqueValue1| value | value 1 | aaa | 1 |
UniqueValue2| value | value 1 | bbb | 2 |
UniqueValue3| value | value 3 | ccc | 3 |
UniqueValue4| value | value 2 | ddd | 4 |
Now, I want to present it on a web site with the ability to query this database.
For example
Querying all the data where you find the "value 1" on column 3 and group it by column 3. The result will be
Querying all the data where column 5 is bigger than 2 and group it by column 3 The result will be:
Please find below 2 queries for your questions.
1. SELECT column3,column1,column2,column4,column5 FROM <table_name> WHERE column3 = "value 1" GROUP BY column3,column1;
2. SELECT column3,column1,column2,column4,column5 FROM <table_name> WHERE column5 > 2 GROUP BY column3,column1;
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 3 tables. ID and Name is the primary key for the first table.
First Table
ID | Name | Date
----------------
1 | AA | 11/02
2 | BB | 04/10
ID, Name and Option are the primary key for second table:
Second Table
ID | Name | Option | SeqNo
---------------------------
3 | DD | LOVE | 1
4 | EE | SINGLE | 1
Option is the primary key for the third table:
Third Table
Option | Status
---------------
LOVE | Y
MARRIED| Y
SINGLE | N
After I join these tables, I will get like this.
ID | Name | Option | SeqNo | Status
------------------------------------
1 | AA | NULL | NULL | NULL
2 | BB | NULL | NULL | NULL
3 | CC | LOVE | 1 | Y
4 | DD | SINGLE | 1 | N
My question is, how to change the NULL value to a value contain in another table?
As an example, The Option column must be filled in with the value inside the third table. I'm using SQL Server 2005
This link describes how to replace null values in different sql engines, including sql server -
http://www.sqlines.com/oracle/functions/nvl
Basically, the syntax you are looking for is -
ISNULL(SeqNo, 'N/A')
So I have a sql table that will hold an ID like so
---------------------------------------------------
| RecordID | Date | Price
--------------------------------------------------
| 1 | 8/31/2016 | 49
--------------------------------------------------
| 2 | 8/31/2016 | 101
--------------------------------------------------
And I have 3 other tables that will hold information about this ID and can have different amount of columns
Table 1
---------------------------------------------------
| RecordID | Date | Price | Name
--------------------------------------------------
| 1 | 8/31/2016 | 50 | System
--------------------------------------------------
Table 2
---------------------------------------------------
| RecordID | Date | Price | Coupon
--------------------------------------------------
| 2 | 8/31/2016 | 100 | 7
--------------------------------------------------
but the IDs are split between them. Meaning table 1 can have ID 1 then table 2 can have ID 2 and so on. An ID can only exist in one of the three tables.
So my desire is to create a view where i can get the ID and price from the original table and find which table this ID exists in and put it all together nicely like this
---------------------------------------------------
| RecordID | Date | Price1 | Price2
--------------------------------------------------
| 1 | 8/31/2016 | 49 | 50
--------------------------------------------------
| 2 | 8/31/2016 | 101 | 100
--------------------------------------------------
You can do it using LEFT OUTER JOINS and coalesce.
Assuming you have a main table called tRecords and 3 other tables called tInfo1, tInfo2 and tInfo3, all of which have a RecordID and Price columns:
SELECT
A.RecordID
,A.[Date]
,A.Price AS Price1
,COALESCE(B.Price,C.Price,D.Price) AS Price2
FROM
tRecords A
LEFT OUTER JOIN tInfo1 B
ON A.RecordID = B.RecordID
LEFT OUTER JOIN tInfo2 C
ON A.RecordID = C.RecordID
LEFT OUTER JOIN tInfo3 D
ON A.RecordID = D.RecordID
Depending on the type of data you have in your tables, consider creating a View to combine the results of your 3 other tables, to make a virtual combination table:
create view vRecordCombo
select RecordId, Date, Price, Name as 'InfoString', NULL as 'InfoValue'
from table1
UNION ALL
select RecordId, Date, Price, NULL as 'InfoString', Coupon as 'InfoValue'
from table2
Then you can do joins on the combo as if it's a single table containing all your records.