Sample Data in Yellow the desired output
Just need your expert help on this.
I need to get the output based on T-SQL. Whenever the invoice_line_id_link is 0 put the product_id value if not look into the id column and get the product_id of that row.
Here's the script.
declare #t table
(id int, invoice_id int, product_id int, invoice_line_id_link int);
insert into #t values
(53,10,383,0),
(54,10,344,53),
(55,10,920,53),
(57,10,384,0),
(58,10,359,57),
(59,10,242,57),
(60,10,284,0);
select id, invoice_id, product_id,invoice_line_id_link, null desiredoutput from #t
based on the image if possible to populate using TSQL
Based on the image you have provided , here is the piece of code producing desired output.
Table Populate
declare #t table (id int, invoice_id int, product_id int, invoice_line_id_link int);
insert into #t values (53,10,383,0), (54,10,344,53), (55,10,920,53), (57,10,384,0), (58,10,359,57), (59,10,242,57), (60,10,284,0);
Code for Output
select *,CASE WHEN invoice_line_id_link = 0 THEN product_id
ELSE (select t1.product_id from #t t1 where t1.id = t2.invoice_line_id_link)
END from #t t2
Output
id invoice_id product_id invoice_line_id_link desiredoutput
53 10 383 0 383
54 10 344 53 383
55 10 920 53 383
57 10 384 0 384
58 10 359 57 384
59 10 242 57 384
60 10 284 0 284
*/
Related
I would like to know if two tables 'table1' and 'table2' are identical.
I know I could compare every column of both tables in the 'where'-clause
So this would basically show me every Row that is identical.
But what I want to know is which columns are identical. So I think it would be easy to just transpone the tables and compare the results as mentioned before. Column names and order in the tables are both identical as already given.
I made also an example Input and Output scenario:
-------Input---------------
table1
id
col1
col2
col3
1
14
23
45
2
12
21
43
3
12
22
43
4
10
12
41
5
11
23
44
6
13
25
43
table2
id
col1
col2
col3
1
14
20
45
2
12
0
43
3
12
22
43
4
10
30
41
5
11
23
44
6
13
43
----------Output----------------
result
col2
20
0
22
30
23
OR result
???
???
???
???
???
???
???
col2
20
0
22
30
23
OR result
col2
OR result
table1.col2
table2.col2
23
20
21
0
22
22
12
30
23
23
25
OR similar.....
The values of the non identical columns dont matter I just need the column name, but I wouldnt care if values would come along with it. I hope its not too diffcult.
Consifering you really want to receive only the identical columns, you might want to try an approach using unpivot. Following an example:
DECLARE #t1 TABLE(
id int
,col1 int
,col2 int
,col3 int
);
INSERT INTO #t1 VALUES
(1,14,23,45)
,(2,12,21,43)
,(3,12,22,43)
,(4,10,12,41)
,(5,11,23,44)
,(6,13,25,43);
DECLARE #t2 TABLE(
id int
,col1 int
,col2 int
,col3 int
);
INSERT INTO #t2 VALUES
(1,14,20,45)
,(2,12,0,43)
,(3,12,22,43)
,(4,10,30,41)
,(5,11,23,44)
,(6,13,NULL,43);
WITH cte1 AS(
SELECT id, col, val
FROM (SELECT id, col1, col2, col3 FROM #t1) p
UNPIVOT
(val FOR col IN (col1, col2, col3)) as unpvt
),
cte2 AS(
SELECT id, col, val
FROM (SELECT id, col1, col2, col3 FROM #t2) p
UNPIVOT
(val FOR col IN (col1, col2, col3)) as unpvt
)
SELECT DISTINCT c1.id, c1.col, c1.val
FROM cte1 c1
INNER JOIN cte2 c2 ON c2.id = c1.id AND c2.col = c1.col AND c2.val = c1.val
ORDER BY 1, 2
Sorry to say i have not in depth knowledge of SQL queries. I have to modify an existing application that records and maintain the customers installments payments.
For this purpose I have a requirement in which i want to copy date of
TableNo1 into TableNo2 except New_Amount(TableNo1) column data, and then remove the remaining rows of TableNo2 for each SalesInvoiceID.
Actually TablNo1 has modified installment payment plan and that is why it is required to modify TableNo2 accordingly.
TableNo1
New_ID SalesInvoiceID InsttNo DueDate New_Amount
1 30 1 2019-05-02 12000
2 30 2 2019-06-02 12000
3 30 3 2019-09-02 4000
4 30 4 2019-12-02 4000
TableNo2
Instt_ID SalesInvoiceID InsttNo DueDate PaymentDate Amount Status
51 30 1 2019-05-02 NULL 0 Up-Coming
52 30 2 2019-06-02 NULL 0 Up-Coming
53 30 3 2019-07-02 NULL 0 Up-Coming
54 30 4 2019-08-02 NULL 0 Up-Coming
55 30 5 2019-09-02 NULL 0 Up-Coming
56 30 6 2019-10-02 NULL 0 Up-Coming
57 30 7 2019-11-02 NULL 0 Up-Coming
58 30 8 2019-12-02 NULL 0 Up-Coming
Required Output (TableNo2)
Instt_ID SalesInvoiceID InsttNo DueDate PaymentDate Amount Status
51 30 1 2019-05-02 NULL 0 Up-Coming
52 30 2 2019-06-02 NULL 0 Up-Coming
53 30 3 2019-09-02 NULL 0 Up-Coming
54 30 4 2019-12-02 NULL 0 Up-Coming
If I'm guessing at the relationship between the tables correctly, this should be all you need, but before you delete any production data you should test this on copies of the tables.
UPDATE t2
SET t2.DueDate = t1.DueDate
FROM
dbo.TableNo2 AS t2
JOIN
dbo.TableNo1 AS t1
ON t1.SalesInvoiceID = t2.SalesInvoiceID
AND t1.InsttNo = t2.InsttNo;
DELETE t2
FROM
dbo.TableNo2 AS t2
WHERE
NOT EXISTS
(
SELECT 1
FROM dbo.TableNo1 AS t1
WHERE t1.SalesInvoiceID = t2.SalesInvoiceID
AND t1.InsttNo = t2.InsttNo
);
CREATE TABLE #Table1 (New_ID INT IDENTITY (1,1) PRIMARY KEY,SalesInvoiceID
INT,InsttNo INT,PaymentDate DATE,New_Amount INT )
CREATE TABLE #Table2 (New_ID INT IDENTITY (1,1) PRIMARY KEY,Instt_ID INT,
SalesInvoiceID INT,InsttNo INT,DueDate DATE,PaymentDate DATE,Amount INT,Status
VARCHAR(20))
INSERT INTO #Table1 (SalesInvoiceID,InsttNo,PaymentDate,New_Amount) VALUES (30
,1,'2019-05-02','12000')
INSERT INTO #Table1 (SalesInvoiceID,InsttNo,PaymentDate,New_Amount) VALUES (30
,2,'2019-06-02','12000')
INSERT INTO #Table1 (SalesInvoiceID,InsttNo,PaymentDate,New_Amount) VALUES (30
,3,'2019-09-02','4000')
INSERT INTO #Table1 (SalesInvoiceID,InsttNo,PaymentDate,New_Amount) VALUES (30
,4,'2019-12-02','4000')
INSERT INTO #Table2
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES
(51,30,1,'2019-05-02',NULL,0,'Up-Coming')
INSERT INTO #Table2
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES
(52,30,2,'2019-06-02',NULL,0,'Up-Coming')
INSERT INTO #Table2
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES
(53,30,3,'2019-07-02',NULL,0,'Up-Coming')
INSERT INTO #Table2
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES
(54,30,4,'2019-08-02',NULL,0,'Up-Coming')
INSERT INTO #Table2
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES
(55,30,5,'2019-09-02',NULL,0,'Up-Coming')
INSERT INTO #Table2
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES
(56,30,6,'2019-10-02',NULL,0,'Up-Coming')
INSERT INTO #Table2
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES
(57,30,7,'2019-11-02',NULL,0,'Up-Coming')
INSERT INTO #Table2
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES
(58,30,8,'2019-12-02',NULL,0,'Up-Coming')
SELECT
T2.Instt_ID
,T1.SalesInvoiceID
,T1.InsttNo
,T1.PaymentDate DueDate
,T2.PaymentDate
,T2.Amount
,T2.Status
FROM #Table1 t1
INNER JOIN #Table2 T2 ON t1.New_ID = t2.New_ID
DROP TABLE #Table1,#Table2
There is several examples that concatenate string dealing with one single table. In my case I have two tables to take into account.
Table A
requestid int PK
Table B
requestid int
documentname varchar(50)
Table A requestid is of course unique, where table B requestid may have multiple lines. Table B can contain multiple relationship with the same requestid from table A. Also some of the Table A requestid might not have any associated records in table B.
I need to extract and join the two tables. Table A contains ~300k lines and table B contains ~140k lines. See the data below to illustrate what I need to achieve.
Table A sample
requestid FieldA FieldB FieldC
1 33 44 22
2 15 23 73
3 26 73 34
Table B sample
requestid documentname
1 firstdoc.doc
1 seconddoc.doc
1 thirddoc.doc
3 onedoc.doc
3 lastdoc.doc
Expected result:
requestid FieldA FieldB FieldC documentname
1 33 44 22 firstdoc.doc, seconddoc.doc, thirddoc.doc
2 15 23 73 NULL
3 26 73 24 onedoc.doc, lastdoc.doc
In my solution it is very important that requestid with no document associated to it are still in the result.
Hope my question is clear, thank you in advance.
This can be easily done with a stuff
See the example below
declare #tableA table (requestid int, fieldA int, fieldB int, fieldC int)
declare #tableB table (requestid int, documentname varchar(50))
insert into #tableA values (1, 33, 44, 22), (2, 15, 23, 73), (3, 26, 73, 34)
insert into #tableB values (1, 'firstdoc.doc'), (1, 'seconddoc.doc'), (1, 'thirddoc.doc'), (3, 'onedoc.doc'), (3, 'lastdoc.doc')
select a.requestid, a.fieldA, a.fieldB, a.fieldC,
stuff(( ( select ',' + b.documentname
from #tableB b
where b.requestid = a.requestid
order by 1
For XML PATH (''))
), 1, 1, '') as docname
from #tableA a
the result is
requestid fieldA fieldB fieldC docname
--------- ------ ------ ------ --------
1 33 44 22 firstdoc.doc,seconddoc.doc,thirddoc.doc
2 15 23 73
3 26 73 34 lastdoc.doc,onedoc.doc
I'm stuck in my query on how to remove or rather skip a post if another one exists.
This is my table.
if L_ID column have value 821 AND 201 for the same P_ID then "remove" or donĀ“t use 201 and sum() then time
This would make P_ID 80 and 946 only have 2 rows.
This is probably easier than I think but I'm stuck.
Try it like this:
CREATE TABLE #YourTable(P_ID INT, L_ID INT, [Date] Date, [Time] DECIMAL(6,2));
INSERT INTO #YourTable VALUES
(80,201,{d'2015-08-01'},24.0)
,(80,821,{d'2015-08-01'},24.0)
,(80,822,{d'2015-08-01'},32.0)
,(946,201,{d'2015-08-01'},16.0)
,(946,821,{d'2015-08-01'},16.0)
,(946,819,{d'2015-08-01'},6.65)
,(6758,201,{d'2015-08-01'},7.25)
,(6758,200,{d'2015-08-01'},7.25)
;
--Test output
SELECT * FROM #YourTable;
--Set the SUMs in those lines with L_ID=821
UPDATE #YourTable SET [Time]=(SELECT SUM(x.[Time])
FROM #YourTable AS x
WHERE x.P_ID =#YourTable.[P_ID]
AND x.L_ID IN (821,201))
WHERE #YourTable.L_ID=821
--Delete the rows with L_ID=201 if there is one with 821 too
DELETE FROM #YourTable
WHERE L_ID = 201 AND EXISTS(SELECT * FROM #YourTable AS x
WHERE x.P_ID = #YourTable.P_ID AND x.L_ID =821 ) --The ID was wrong here, sorry...
--Test output
SELECT * FROM #YourTable;
--Clean up
DROP TABLE #YourTable;
Result:
P_ID L_ID Date Time
80 821 2015-08-01 48.00
80 822 2015-08-01 32.00
946 821 2015-08-01 32.00
946 819 2015-08-01 6.65
6758 201 2015-08-01 7.25
6758 200 2015-08-01 7.25
Try this code:
SELECT *,
SUM(Time) OVER(PARTITION BY P_ID, L_ID, Date) AS 'Sum'
FROM Your_Table
WHERE L_ID <> 201
AND P_ID NOT IN (
SELECT E1.P_ID
FROM Your_Table E1
INNER JOIN Your_Table E2
ON E1.P_ID = E2.P_ID
WHERE E1.L_ID = 821 AND E2.L_ID = 201)
I have a query that returns the following data.
ProductCode DealRef
1120 23
1120 76
1130 24
Is there a way that if a product code has more than one Deal ref then it will put this into a new column? So the current result would look something like;
ProductCode Deal1 Deal2
1120 23 76
1130 24
If this is not possible then I have an idea that could work. I would do a count on the DealRef column to find out many columns i would need to pivot to. I would then need to add another column to my initial query which will be able to add an id to each row displaying something similar to the below which I'm unsure how to do.
ProductCode DealRef id
1120 23 1
1120 76 2
1130 24 1
You cannot get the fitting number of columns, but you can get as many columns as you expect to be the maximum, most of them beeing NULL:
Paste this into an empty query window and execute. Adapt to your needs
DECLARE #tbl TABLE(ProductCode INT, DealRef INT);
INSERT INTO #tbl VALUES
(1120,23)
,(1120,76)
,(1130,24);
SELECT p.*
FROM
(
SELECT 'deal' + CAST(ROW_NUMBER() OVER(PARTITION BY tbl.ProductCode ORDER BY tbl.ProductCode) AS VARCHAR(10)) AS ColumnName
,tbl.ProductCode
,tbl.DealRef
FROM #tbl AS tbl
) AS x
PIVOT
(
MIN(DealRef) FOR ColumnName IN(deal1,deal2,deal3,deal4 /*Add as many Col-names as you could maximum need*/)
) AS p
Result is
ProductCode deal1 deal2 deal3 deal4
1120 23 76 NULL NULL
1130 24 NULL NULL NULL