Inserting 2 insert query records in SQL server - sql-server

need your expert
when inserting query like this :
use tabelmantap
delete from T_CekUser
insert into T_CekUser(T_21SI)
select tbla.MASA
from
(
select * from T_21SI
union
select * from T_21SI
) as tbla
where tbla.THN_DATA = '2014' and tbla.KEY_NPWP = '01.576.555.5-123.000'
insert into T_CekUser(T_23SI)
select tblb.MASA from
(
select * from T_23SI
union
select * from T_23SI
) as tblb
where tblb.THN_DATA = '2014' and tblb.KEY_NPWP = '01.576.555.5-123.000'
all that query is in one line/page query in sql server
and i get results like this :
T_21SI | T_23SI
1 null
2 null
3 null
4 null
null 2
null 3
been tired, looking around for answer or solution as i wanna be like this :
T_21SI | T_23SI
1 2
2 3
3 null
4 null
null null
null null
all file type are int
i have made insert using select from two tables (using inner join) but result is not being expected
any help would be really appreciated..thanks

Try using the PIVOT statement details are here. https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx

Related

SQL: Match two columns containing NULL values and strings and extract only the matching rows without NULL

I have a temp table FileTable as follows:
ID FileNameAct FileNameString
1 NULL SalesOrderTarget
2 NULL SalesTarget
3 InventoryMaterialTarget_20220414.xlsx NULL
4 InventoryTarget_20220414.xlsx NULL
5 SalesOrderTarget_20220412.xlsx NULL
6 SalesTarget_20220412.xlsx NULL
Objective: To match the string between FileNameAct and FileNameString and take out the rows that has very close match.
So the resultant table should look like below:
ID FileNameAct FileNameString
1 SalesOrderTarget_20220412.xlsx SalesOrderTarget
2 SalesTarget_20220412.xlsx SalesTarget
I am thinking in below line:
SELECT X.* FROM (SELECT FileNameAct, FileNameString,
CASE WHEN ISNULL(FileNameAct,'') LIKE '%'+ ISNULL(FileNameString,'') + '%' THEN 1 ELSE 0
END AS Flag
FROM #FileTable) X
WHERE X.Flag=1
Clearly, this would not give the correct result.
Can anybody share any thoughts?
You can use a self-join with conditions
Select
b.id,
a.FileNameAct,
b.FileNameString
From #FileTable a
Join #FileTable b
On a.FileNameAct
like concat(b.FileNameString,'%')
Where b.FileNameString is not null;

what to use instead of union to join same results based on two where clauses

I have two queries that work as expected for example
Query 1
select Name,ID,Product,Question
from table 1
where Id= 9 and ProductID=30628
table output
Name | ID | Product | QUestion
0659e103-b33d-4603 |12356|Apple | is it picked up?
0659e103-b33d-4603 |12456|Apple |Available in store?
0659e103-b33d-4603 |12458|Apple |confirm order?
query 2
select Name,ID,Product,Question
from table 1
where Id= 9 and TypeID=2
table output
Name | ID | Product | QUestion
0659e103-b33d-4603 |12347|Apple | Problem at store?
as you can see in query 1 i use a ProductID and in query 2 i use a TypeID these two values gives me different out puts
so i used a union to join both as follows
select Name,ID,Product,Question
from table 1
where Id= 9 and ProductID=30628
union
select Name,ID,Product,Question
from table 1
where Id= 9 and TypeID=2
which i get the desired output
Name | ID | Product | QUestion
0659e103-b33d-4603 |12356|Apple | is it picked up?
0659e103-b33d-4603 |12456|Apple |Available in store?
0659e103-b33d-4603 |12458|Apple |confirm order?
0659e103-b33d-4603 |12347|Apple | Problem at store?
is their a better way to do this because my query will grow and i would not like to repeat the same thing over again. is their a better way to optimize the query?
NOte i can not use ProductID and TypeID on the same line because they do not result in accurate results
You could use OR since you are querying the same table.
SELECT Name
,ID
,Product
,Question
FROM TABLE1
WHERE (
Id = 9
AND ProductID = 30628
)
OR (
Id = 9
AND TypeID = 2
)
If you have a growing number of OR conditions you could use a temp table/variable and inner join to profit from a set based operation.
The inner join will only return matching rows.
CREATE TABLE #SomeTable(Id INT NOT NULL, ProductID INT NULL, TypeID INT NULL)
-- Insert all conditions you want to match.
INSERT INTO #SomeTable(Id, ProductID, TypeId)
VALUES (9, 30628, NULL)
, (9, NULL, 2)
SELECT Name
,ID
,Product
,Question
FROM TABLE1 x
INNER JOIN #SomeTable y ON
x.ID = y.ID -- Since ID is Not null in the temp table
AND (y.ProductID IS NULL OR y.ProductID = x.ProductID)
AND (y.TypeID IS NULL OR y.TypeID = x.TypeID)
You can use cas-when clause with a self join.
Case-when something like this:
SELECT t1_2.Name,
t1_2.ID,
t1_2.Product,
t1_2.Question,
(CASE WHEN (t1.Id= 9 and t1.ProductID=30628) THEN ID
WHEN (t1.Id= 9 and t1.TypeID=2) THEN ID
ELSE NULL) AS IDcalc
FROM table_1 t1 LEFT JOIN table_1 t1_2
ON t1.ID = t1_2.ID
WHERE (CASE WHEN (t1.Id= 9 and t1.ProductID=30628) THEN ID
WHEN (t1.Id= 9 and t1.TypeID=2) THEN ID
ELSE NULL) IS NOT NULL
You can use any table in the join.
In comparison of query performance the OR is much better until you have only one table, if you have more tables, then you should use temp table or case-when in your query.

Whats the difference between UNION and CROSS JOIN?

I have been reading about this two possibilities but I'm not sure if I understood them properly.
So UNION makes new rows with the union of the two queries data:
Table1 Table2
------ ------
1 3
2 4
SELECT * FROM TABLE1
UNION
SELECT * FROM TABLE2
Column1
---------
1
2
3
4
...
And CROSS JOIN makes Cartesian product of both tables:
SELECT * FROM TABLE1
CROSS JOIN TABLE 2
Column1 | Column2
-----------------
1 | 3
2 | 3
1 | 4
2 | 4
Its that ok?
Thanks for all.
The UNION operator is used to combine the result-set of two or more SELECT statements.
The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables.
If there is no relationship between the tables then it leads to cross join.
Others have already answered this question, but I just wanted to highlight that your example of the CROSS JOIN should return 4 and not 2 records.
Cross joins return every combination of records from the left of the join against the right.
Example Data
/* Table variables are a great way of sharing
* test data.
*/
DECLARE #T1 TABLE
(
Column11 INT
)
;
DECLARE #T2 TABLE
(
Column21 INT
)
;
INSERT INTO #T1
(
Column11
)
VALUES
(1),
(2)
;
INSERT INTO #T2
(
Column21
)
VALUES
(3),
(4)
;
UNION Query
/* UNION appends one recordset to the end of another,
* and then deduplicates the result.
*/
SELECT
*
FROM
#T1
UNION
SELECT
*
FROM
#T2
;
Returns
Column11
1
2
3
4
CROSS JOIN Query
/* CROSS JOIN returns every combination of table 1
* and table 2.
* The second recordset is appended to the right of the first.
*/
SELECT
*
FROM
#T1
CROSS JOIN #T2
;
Returns
Column11 Column21
1 3
2 3
1 4
2 4
Also important to note that Union will need exact same number of columns for both table while cross will not.
It is worth mentioning that unlike joins, UNION and CROSS-JOIN return all information about two tables and do not eliminate anything. But joins, inner and left/right joins, eliminate some part of the data which is not shared.

buLK Update duplicate row value

Update duplicate column value with
Suppose I have a table with follow column
ID, Code, IsDuplicate, Description
I have n records inside and I would like to bulk update the IsDuplicate value if there is duplicate code inside.
Example
1 ABC null null
2 DEF null null
3 DEF null null
4 ABC null null
5 FGH null null
ID 1, 2, 3, 4 IsDuplicate will be updated to true.
How could it be done?
This will update all duplicate codes :
UPDATE T
SET ISDUPLICATE = 'TRUE'
FROM YOURTABLE T
WHERE EXISTS (SELECT 1
FROM (SELECT *
FROM (SELECT ROW_NUMBER()
OVER (
PARTITION BY CODE
ORDER BY ID)RN,
*
FROM YOURTABLE)A
WHERE RN > 1)B
WHERE B.CODE = T.CODE)
You should use group by in select query.
SELECT code, COUNT(*) c FROM table GROUP BY code HAVING c > 1;
Then you can update it based on your requirements.

How to arrange data in sql server table

I tried many query to achieve the expected result , I couldn't find any solution.
Actual:-
ID | EmpDailyFee | EmpMonthlyFee | CompDailyFee | CompMnthlyFee
1 NULL 12 NULL NULL
1 50 NULL NULL NULL
1 60 NULL NULL NULL
2 50 NULL NULL NULL
3 NULL 30 NULL NULL
Expected :-
ID | EmpDailyFee | EmpMonthlyFee | CompDailyFee | CompMnthlyFee
1 50 12 NULL NULL
1 60 12 NULL NULL
2 50 NULL NULL NULL
3 NULL 30 NULL NULL
This looks like a table smell to me since the rows are sharing the same ID value but with different EmpDailyFee/EmpMonthlyFee values. But in this particular case you can get your expected output like this:
SELECT t1.ID, t1.EmpDailyFee, t2.EmpMonthlyFee
FROM #Test t1
INNER JOIN #Test t2 ON t1.ID = t2.ID
WHERE t1.EmpDailyFee IS NOT NULL AND t2.EmpMonthlyFee IS NOT NULL
UNION ALL
SELECT t1.ID, t1.EmpDailyFee, t1.EmpMonthlyFee
FROM #Test t1
WHERE (t1.EmpDailyFee IS NOT NULL OR t1.EmpMonthlyFee IS NOT NULL) AND t1.ID NOT IN
(
SELECT t3.ID
FROM #Test t3
INNER JOIN #Test t2 ON t3.ID = t2.ID
WHERE t3.EmpDailyFee IS NOT NULL AND t2.EmpMonthlyFee IS NOT NULL
)
This has been tested on SQL Fiddle
Note: The reason why I did not include CompDailyFee and CompMnthlyFee is because the values of both the actual and expected results were NULL. I am trying to write a "simple-as-possible" query based on what OP has provided.
If you're trying to insert or update data this could be the solution, customizing it on your needings
IF EXISTS (SELECT 1 FROM Employee WHERE ID = 1)
UPDATE Employee SET EmpMonthlyFee= #Value WHERE ID = 1
ELSE
INSERT Employee(ID,EmpDailyFee,EmpMonthlyFee,CompDailyFee,CompMnthlyFee)
VALUES(1,NULL,#value,NULL,NULL)
declare #t table (ID int,Empfee int,monthlyfee int,compdailyfee int,Cmpnymonthlyfee int)
insert into #t (ID,Empfee,monthlyfee,compdailyfee,Cmpnymonthlyfee)
values (1,NULL,12,NULL,NULL),
(1,50,NULL,NULL,NULL),
(1,60,NULL,NULL,NULL)
;with cte as (
select t.ID,t.Empfee,
tt.monthlyfee,
t.compdailyfee,
t.Cmpnymonthlyfee,
ROW_NUMBER()OVER(PARTITION BY t.ID,tt.monthlyfee ORDER BY t.ID,tt.monthlyfee)RN
from #t t
CROSS APPLY #t tt
where t.Empfee IS NULL OR tt.monthlyfee IS NOT NULL)
select C.ID,
C.Empfee,
C.monthlyfee,
C.compdailyfee,
C.Cmpnymonthlyfee from cte c
where c.Empfee IS NOT NULL AND c.monthlyfee IS NOT NULL
Can be done using COALESCE as well.
SELECT T1.ID,
T2.EmpDailyFee,
COALESCE(T1.EmpMonthlyFee , T2.EmpMonthlyFee) EmpMonthlyFee,
COALESCE(T1.CompDailyFee , T2.CompDailyFee) CompDailyFee ,
COALESCE(T1.CompMnthlyFee , T2.CompMnthlyFee) CompMnthlyFee
FROM
(SELECT * FROM Tab WHERE EmpDailyFee IS NULL)T1
JOIN
(SELECT * FROM Tab WHERE EmpDailyFee IS NOT NULL)T2
ON T1.ID = T2.ID
I have gone one step ahead and assumed that values from the first row will take priority if not null(As for the 3rd column in OP). This part can be neglected if not required.

Resources