How to insert next max value using insert select statement - sql

I have a two tables. I want to insert table1 data into table2 if records from table1 are not present in the table2.
I can do this. main problem with my query.
create table #Customer
(
ID int,
Name nvarchar(20),
Email nvarchar(20)
)
create table #Customer2
(
ID int,
Name nvarchar(20),
Email nvarchar(20),
Value varchar(20)
)
insert into #Customer values (1,'AAA','A#mail.com')
insert into #Customer values (2,'BBB','B#mail.com')
insert into #Customer values (3,'CCC','C#mail.com')
insert into #Customer values (4,'DDD','D#mail.com')
insert into #Customer values (5,'EEE','E#mail.com')
insert into #Customer values (6,'FFF','F#mail.com')
insert into #Customer values (7,'GGG','G#mail.com')
insert into #Customer2 values (3,'x','asa#mail.com','10001')
insert into #Customer2 values (6,'y','B#mail.com','10002')
insert into #Customer2 values (8,'z','z#mail.com','10003')
update C2
set C2.Email = C1.Email, C2.Name = C1.Name
from #Customer C1
inner join #Customer2 C2 on C2.ID = C1.ID
insert into #Customer2
select C1.ID, C1.Name, C1.Email, (SELECT MAX(CONVERT(int, Value))+1 from #Customer2
) from #Customer C1
left join #Customer2 C2 on C2.ID = C1.ID
where C2.ID is null
select ID,value from #Customer2
drop table #Customer
drop table #Customer2
The result is
id value
3 10001
6 10002
8 10003
1 10004
2 10004
4 10004
5 10004
7 10004
I want it as
id value
3 10001
6 10002
8 10003
1 10004
2 10005
4 10006
5 10007
7 10008
Please advice!
Thanking you in anticipation.

You can use Row_Number starting with the maximum value in Customer1 table to generate the ID's in customer 2 and the except operator to insert the data which is not present:
INSERT INTO #Customer2(ID,Name,Email,Value)
select ID,name,email, (SELECT MAX(value) from #customer2) + row_number() over (order by id) value
from (select ID,Name,email from #Customer
except SELECT ID,name,email from #Customer2)ValueForInserting

Related

Get exact match between two table

I have a issue that is hard to explain. I have two tables
table1: (this is something like shipping table)
ID ShippingId ProductId1 ProductId2
1 100 A A1
2 100 A A2
3 100 A A3
4 100 A A4
5 100 A A5
6 200 B B1
7 200 B B2
8 300 B A1
9 300 B A2
table2: (and this is about relation between ProductId1 and ProductId2)
ID ProductId1 ProductId2
1 A A1
2 A A2
3 A A3
4 A A4
5 A A5
6 B B1
7 B B2
In the case the shipment "100" includes all items of "A" so this should "true"
and the shipments "200" and "300" does not include all parts of their main products. So expected output should be like
ShippingId ProductId1 IsIncludeAll
100 A true
200 B false
300 A true
can you guys help me?
DECLARE #table1 AS TABLE
(
ShippingID INT,
ProductId1 INT,
ProductId2 INT
)
DECLARE #table2 AS TABLE
(
ProductId1 INT,
ProductId2 INT
)
INSERT INTO #table1 (ShippingId,ProductId1,ProductId2) VALUES (100,111, 1119)
INSERT INTO #table1 (ShippingId,ProductId1,ProductId2) VALUES (100,111, 1118)
INSERT INTO #table1 (ShippingId,ProductId1,ProductId2) VALUES (100,111, 1117)
INSERT INTO #table1 (ShippingId,ProductId1,ProductId2) VALUES (100,111, 1116)
INSERT INTO #table1 (ShippingId,ProductId1,ProductId2) VALUES (100,111, 1115)
INSERT INTO #table1 (ShippingId,ProductId1,ProductId2) VALUES (200,222, 2229)
INSERT INTO #table1 (ShippingId,ProductId1,ProductId2) VALUES (200,222, 2228)
INSERT INTO #table1 (ShippingId,ProductId1,ProductId2) VALUES (300,111, 1117)
INSERT INTO #table1 (ShippingId,ProductId1,ProductId2) VALUES (300,111, 1116)
INSERT INTO #table2 (ProductId1,ProductId2) VALUES ( 111, 1119)
INSERT INTO #table2 (ProductId1,ProductId2) VALUES ( 111, 1118)
INSERT INTO #table2 (ProductId1,ProductId2) VALUES ( 111, 1117)
INSERT INTO #table2 (ProductId1,ProductId2) VALUES ( 111, 1116)
INSERT INTO #table2 (ProductId1,ProductId2) VALUES ( 111, 1115)
INSERT INTO #table2 (ProductId1,ProductId2) VALUES ( 222, 2229)
INSERT INTO #table2 (ProductId1,ProductId2) VALUES ( 222, 2228)
ShippingId ProductId1 IsIncludeAll
100 A true
200 B false
300 A false
A total guess, as the sample DDL and DML don't match the sample data, but perhaps this?
SELECT S.ShippingID,
T2.ProductId1,
CASE COUNT(CASE WHEN T1.ProductId2 IS NULL THEN 1 END) WHEN 0 THEN 'true' ELSE 'false' END AS IsIncludeAll
FROM #table2 T2
CROSS APPLY (SELECT DISTINCT
sq.ShippingID,
sq.ProductId1
FROM #table1 sq
WHERE sq.ProductId1 = T2.ProductId1) S
LEFT JOIN #table1 T1 ON T2.ProductId1 = T2.ProductId1
AND T1.ProductId2 = T2.ProductId2
AND S.ShippingID = T1.ShippingID
GROUP BY S.ShippingID,
T2.ProductId1;
Little confused on your sample data and output. From my thought Check this query and output.
------Step 1. concatenate product1 and product2 with ShippingID wise,product1 wise order by ShippingID,ProductId1,ProductId2-------------------
declare #Shipping as table
(
ShippingID INT,
ProductId1 INT,
ProductDesc varchar(max)
)
insert #Shipping
SELECT Shipping.ShippingID,Shipping.ProductId1,
LEFT(Shipping.prod_desc,Len(Shipping.prod_desc)-1) As prod_desc
FROM
(
SELECT DISTINCT T2.ShippingID, T2.ProductId1,
(
SELECT cast(T1.ProductId1 as varchar(10))+'-' +cast(T1.ProductId2 as varchar(10))+ '|' AS [text()]
FROM dbo.table1 T1
WHERE T1.ShippingID = T2.ShippingID and T1.ProductId1=T2.ProductId1
ORDER BY T1.ShippingID,ProductId1,ProductId2
FOR XML PATH ('')
) prod_desc
FROM dbo.table1 T2
) Shipping
------Step 2. concatenate product1 and product2 with product1 wise order by ProductId1,ProductId2-------------------
declare #relation as table
(
ProductId1 INT,
ProductDesc varchar(max)
)
insert #relation
SELECT relation.ProductId1,LEFT(relation.prod_desc,Len(relation.prod_desc)-1) As prod_desc
FROM
(
SELECT DISTINCT T2.ProductId1,
(
SELECT cast(T1.ProductId1 as varchar(10))+'-' +cast(T1.ProductId2 as varchar(10))+ '|' AS [text()]
FROM dbo.table2 T1
WHERE T1.ProductId1=T2.ProductId1
ORDER BY ProductId1,ProductId2
FOR XML PATH ('')
) prod_desc
FROM dbo.table2 T2
) relation
------Step 1. use left join to match with concatinated string of every product1. if matches return True otherwise False-------------------
select a.ShippingID,a.ProductId1,case when b.ProductDesc is null then 'False' else 'True' end as IsIncludeAll
from #Shipping a
left join #relation b
on a.ProductId1=b.ProductId1 and a.ProductDesc=b.ProductDesc
-----Result-----------
------------+---------------+--------------
ShippingID | ProductId1 | IsIncludeAll
------------+---------------+--------------
100 | 111 | True
200 | 222 | True
300 | 111 | False

Getting MAX date from two tables after INNER JOIN

I have below tables:
declare #tbl1 table (Id1 int, crtdate datetime, InvcNbr varchar(10), ShipperId varchar(10), InvtId varchar(10))
insert into #tbl1 values (1,'01/01/2016','001','S111','111')
insert into #tbl1 values (2,'01/10/2016','002','S111','111')
insert into #tbl1 values (3,'01/02/2016','003','S112','112')
insert into #tbl1 values (4,'01/08/2016','004','S112','112')
insert into #tbl1 values (5,'01/04/2016','005','S113','113')
insert into #tbl1 values (6,'01/05/2016','006','S114','113')
declare #tbl2 table (Id2 int, SerialNo varchar(10), InvcNbr varchar(10), ShipperId varchar(10), InvtId varchar(10))
insert into #tbl2 values (1,'1111111','001','S111','111')
insert into #tbl2 values (2,'1111111','002','S111','111')
insert into #tbl2 values (3,'1111112','003','S112','112')
insert into #tbl2 values (4,'1111112','004','S112','112')
insert into #tbl2 values (5,'1111113','005','S113','113')
insert into #tbl2 values (6,'1111113','006','S114','113')
These two tables are related by fields: InvcNbr, ShipperId and InvtId
Serials from #tbl2 are present in two different invoices (InvcNbr). How to show only the results from the latest InvcNbr
The result should be like this:
Id1 crtdate InvcNbr ShipperId InvtId Id2 SerialNo InvcNbr ShipperId InvtId
2 2016-01-10 00:00:00.000 002 S111 111 2 1111111 002 S111 111
4 2016-01-08 00:00:00.000 004 S112 112 4 1111112 004 S112 112
6 2016-01-05 00:00:00.000 006 S114 113 6 1111113 006 S114 113
with lastInvoices as (
select SerialNo, MAX(InvcNbr) LastInvcNbr
from #tbl2
group by SerialNo
)
select t1.*,t2.*
from lastInvoices li
join #tbl2 t2 on (li.SerialNo = t2.SerialNo and li.LastInvcNbr=t2.InvcNbr)
join #tbl1 t1 on (t1.InvcNbr = t2.InvcNbr and t1.ShipperId = t2.ShipperId and t1.InvtId = t2.InvtId)
Below script will give you the desired result..
;with cte_1
as
(SELECT Id1,a.crtdate,a.InvcNbr InvcNbr1 ,a.ShipperId ShipperId1 ,a.InvtId InvtId1,b.Id2,b.SerialNo,b.InvcNbr InvcNbr2,b.ShipperId ShipperId2
,ROW_NUMBER()OVER(PARTITION BY b.serialNo ORDER BY a.crtdate desc) Rno
FROM #tbl1 a
JOIN #tbl2 b on a.InvcNbr=b.InvcNbr AND a.ShipperId=b.ShipperId and a.InvtId=b.InvtId)
SELECT *
FROM cte_1
WHERE Rno=1

Merge a two way relation in the same table in SQL Server

Current Data
ID | Name1 | Name2
<guid1> | XMind | MindNode
<guid2> | MindNode | XMind
<guid3> | avast | Hitman Pro
<guid4> | Hitman Pro | avast
<guid5> | PPLive | Hola!
<guid6> | ZenMate | Hola!
<guid7> | Hola! | PPLive
<guid8> | Hola! | ZenMate
Required Output
ID1 | ID2 | Name1 | Name2
<guid1> | <guid2> | XMind | MindNode
<guid3> | <guid4> | avast | Hitman Pro
<guid5> | <guid7> | PPLive | Hola!
<guid6> | <guid8> | Hola! | ZenMate
These are relations between apps. I want to show that Avast and Hitman has a relation but in this view i do not need to show in what "direction" they have an relation. It's a given in this view that the relation goes both ways.
EDIT: Seems like my example was to simple. The solution doesn't work with more data.
DECLARE #a TABLE (ID INT, Name1 VARCHAR(50), Name2 VARCHAR(50))
INSERT INTO #a VALUES ( 1, 'XMind', 'MindNode' )
INSERT INTO #a VALUES ( 2, 'MindNode', 'XMind' )
INSERT INTO #a VALUES ( 3, 'avast', 'Hitman Pro' )
INSERT INTO #a VALUES ( 4, 'Hitman Pro', 'avast' )
INSERT INTO #a VALUES ( 5, 'PPLive Video Accelerator', 'Hola! Better Internet' )
INSERT INTO #a VALUES ( 6, 'ZenMate', 'Hola! Better Internet' )
INSERT INTO #a VALUES ( 7, 'Hola! Better Internet', 'PPLive Video Accelerator' )
INSERT INTO #a VALUES ( 8, 'Hola! Better Internet', 'ZenMate' )
SELECT a1.ID AS ID1 ,
a2.ID AS ID2 ,
a1.Name1 ,
a2.Name1 AS Name2
FROM #a a1
JOIN #a a2 ON a1.Name1 = a2.Name2
AND a1.ID < a2.ID -- avoid duplicates
This works however so i guess it's the Guid that is messing with me.
EDIT AGAIN:
I haven't looked at this for a while and i thought it worked but i just realized it does not. I've struggled all morning with this but i must admit that SQL is not really my strong suite. The thing is this.
DECLARE #a TABLE (ID int, Name1 VARCHAR(50), Name2 VARCHAR(50))
INSERT INTO #a VALUES ( 1, 'XMind', 'MindNode' )
INSERT INTO #a VALUES ( 2, 'MindNode', 'XMind' )
INSERT INTO #a VALUES ( 3, 'avast', 'Hitman Pro' )
INSERT INTO #a VALUES ( 4, 'PPLive Video Accelerator', 'Hola! Better Internet' )
INSERT INTO #a VALUES ( 5, 'ZenMate', 'Hola! Better Internet' )
INSERT INTO #a VALUES ( 6, 'Hitman Pro', 'avast' )
INSERT INTO #a VALUES ( 7, 'Hola! Better Internet', 'PPLive Video Accelerator' )
INSERT INTO #a VALUES ( 8, 'Hola! Better Internet', 'ZenMate' )
INSERT INTO #a VALUES ( 9, 'XX', 'A' )
INSERT INTO #a VALUES ( 10, 'XX', 'BB' )
INSERT INTO #a VALUES ( 11, 'BB', 'XX' )
INSERT INTO #a VALUES ( 12, 'A', 'XX' )
INSERT INTO #a VALUES ( 13, 'XX', 'CC' )
INSERT INTO #a VALUES ( 14, 'CC', 'XX' )
;With CTE as
(
SELECT a1.ID AS ID1 ,
a2.ID AS ID2 ,
a1.Name1 ,
a2.Name1 AS Name2,
CheckSum(Case when a1.Name1>a2.Name1 then a2.Name1+a1.Name1 else a1.Name1+a2.Name1 end) ck, -- just for display
Row_Number() over (Partition by CheckSum(Case when a1.Name1>a2.Name1 then a2.Name1+a1.Name1 else a1.Name1+a2.Name1 end)
order by CheckSum(Case when a1.Name1>a2.Name1 then a2.Name1+a1.Name1 else a1.Name1+a2.Name1 end)) as rn
FROM #a a1
JOIN #a a2 ON a1.Name1 = a2.Name2
)
Select ID1, ID2,Name1, Name2
from CTE C1
where rn=1
When i use this code it sure works fine with the names but it doesn't match the ID's correctly.
The result is
ID1 | ID2 | Name1 | Name2
12 | 9 | A | X (Correct)
7 | 5 | Hola! | ZenMate (Not Correct)
[..]
I've pulled my hair all morning but i can't figure this out. I still use Guid's as ID's and just use Int's here to make it a bit more readable.
DECLARE #a TABLE (ID INT, Name1 VARCHAR(50), Name2 VARCHAR(50))
INSERT INTO #a VALUES ( 1, 'XMind', 'MindNode' )
INSERT INTO #a VALUES ( 2, 'MindNode', 'XMind' )
INSERT INTO #a VALUES ( 3, 'avast', 'Hitman Pro' )
INSERT INTO #a VALUES ( 4, 'Hitman Pro', 'avast' )
SELECT a1.ID AS ID1 ,
a2.ID AS ID2 ,
a1.Name1 ,
a2.Name1 AS Name2
FROM #a a1
JOIN #a a2 ON a1.Name1 = a2.Name2
AND a1.ID < a2.ID -- avoid duplicates
Referring to the amendment and extension of your question, a more complicated solution is required.
We form a CHECKSUM on a1.Name1,a2.Name (to get an identical we exchanged on size).
Using this we generate with ROW_NUMBER (Transact-SQL) a number and use only rows from the result with number 1.
DECLARE #a TABLE (ID uniqueIdentifier, Name1 VARCHAR(50), Name2 VARCHAR(50))
INSERT INTO #a VALUES ( NewID(), 'XMind', 'MindNode' )
INSERT INTO #a VALUES ( NewID(), 'MindNode', 'XMind' )
INSERT INTO #a VALUES ( NewID(), 'avast', 'Hitman Pro' )
INSERT INTO #a VALUES ( NewID(), 'Hitman Pro', 'avast' )
INSERT INTO #a VALUES ( NewID(), 'PPLive Video Accelerator', 'Hola! Better Internet' )
INSERT INTO #a VALUES ( NewID(), 'ZenMate', 'Hola! Better Internet' )
INSERT INTO #a VALUES ( NewID(), 'Hola! Better Internet', 'PPLive Video Accelerator' )
INSERT INTO #a VALUES ( NewID(), 'Hola! Better Internet', 'ZenMate' )
INSERT INTO #a VALUES ( NewID(), 'XX', 'A' )
INSERT INTO #a VALUES ( NewID(), 'A', 'XX' )
INSERT INTO #a VALUES ( NewID(), 'XX', 'BB' )
INSERT INTO #a VALUES ( NewID(), 'BB', 'XX' )
INSERT INTO #a VALUES ( NewID(), 'XX', 'CC' )
INSERT INTO #a VALUES ( NewID(), 'CC', 'XX' )
;With CTE as
(
SELECT a1.ID AS ID1 ,
a2.ID AS ID2 ,
a1.Name1 ,
a2.Name1 AS Name2,
CheckSum(Case when a1.Name1>a2.Name1 then a2.Name1+a1.Name1 else a1.Name1+a2.Name1 end) ck, -- just for display
Row_Number() over (Partition by CheckSum(Case when a1.Name1>a2.Name1 then a2.Name1+a1.Name1 else a1.Name1+a2.Name1 end)
order by CheckSum(Case when a1.Name1>a2.Name1 then a2.Name1+a1.Name1 else a1.Name1+a2.Name1 end)) as rn
FROM #a a1
JOIN #a a2 ON a1.Name1 = a2.Name2
)
Select *
from CTE C1
where rn=1
Edit:
If you only want to get those where both fields are fitting the needed query would simply be:
SELECT a1.ID AS ID1 , a2.ID AS ID2 , a1.Name1 , a2.Name1 AS Name2
FROM #a a1
JOIN #a a2 ON a1.Name1 = a2.Name2 and a1.Name2 = a2.Name1 AND a1.ID < a2.ID
If the output should contain only two-way relations ('XX' + 'A') AND ('A' + 'XX'), try this:
;
WITH m (ID1, ID2, Name1, Name2) AS (
SELECT ID1, ID2, Name1, Name2
FROM (
SELECT a1.ID AS ID1
,a2.ID AS ID2
,a1.Name1 AS Name1
,a2.Name1 AS Name2
,ROW_NUMBER() OVER (PARTITION BY a1.Name1, a2.Name1 ORDER BY (SELECT 1)) AS n
FROM #a AS a1
JOIN #a AS a2
ON a1.Name1 = a2.Name2
AND a1.Name2 = a2.Name1
) AS T
WHERE n = 1
)
SELECT DISTINCT *
FROM (
SELECT ID1, ID2, Name1, Name2
FROM m
WHERE ID1 <= ID2
UNION ALL
SELECT ID2, ID1, Name2, Name1
FROM m
WHERE ID1 > ID2
) AS dm
It produces the output as follows:
+------+-----+--------------------------+-----------------------+
| ID1 | ID2 | Name1 | Name2 |
+------+-----+--------------------------+-----------------------+
| 1 | 2 | XMind | MindNode |
| 3 | 6 | avast | Hitman Pro |
| 4 | 7 | PPLive Video Accelerator | Hola! Better Internet |
| 5 | 8 | ZenMate | Hola! Better Internet |
| 9 | 12 | XX | A |
| 10 | 11 | XX | BB |
| 13 | 14 | XX | CC |
+------+-----+--------------------------+-----------------------+
Just rank your rows with ROW_NUMBER function and use this rank in join instead of original ID column:
DECLARE #a TABLE (ID UNIQUEIDENTIFIER, Name1 VARCHAR(50), Name2 VARCHAR(50))
INSERT INTO #a VALUES ( NEWID(), 'XMind', 'MindNode' )
INSERT INTO #a VALUES ( NEWID(), 'MindNode', 'XMind' )
INSERT INTO #a VALUES ( NEWID(), 'avast', 'Hitman Pro' )
INSERT INTO #a VALUES ( NEWID(), 'Hitman Pro', 'avast' )
INSERT INTO #a VALUES ( NEWID(), 'PPLive Video Accelerator', 'Hola! Better Internet' )
INSERT INTO #a VALUES ( NEWID(), 'ZenMate', 'Hola! Better Internet' )
INSERT INTO #a VALUES ( NEWID(), 'Hola! Better Internet', 'PPLive Video Accelerator' )
INSERT INTO #a VALUES ( NEWID(), 'Hola! Better Internet', 'ZenMate' )
;WITH cte AS(SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT 1)) rn FROM #a)
SELECT a1.ID AS ID1 ,
a2.ID AS ID2 ,
a1.Name1 ,
a2.Name1 AS Name2
FROM cte a1
JOIN cte a2 ON a1.Name1 = a2.Name2 AND
a2.Name1 = a1.Name2 AND
a1.rn < a2.rn
Output:
ID1 ID2 Name1 Name2
Guid Guid XMind MindNode
Guid Guid avast Hitman Pro
Guid Guid PPLive Video Accelerator Hola! Better Internet
Guid Guid ZenMate Hola! Better Internet
I suggest you to use this simple way:
SELECT
t2.ID, t3.ID ID2,
t1.Name1,t1.Name2
FROM (
SELECT DISTINCT
CASE WHEN Name1 <= Name2 THEN Name1 ELSE Name2 END AS Name1,
CASE WHEN Name1 <= Name2 THEN Name2 ELSE Name1 END AS Name2
FROM
#a) t1
JOIN
#a t2 ON t1.Name1+t1.Name2 = t2.Name1+t2.Name2
JOIN
#a t3 ON t1.Name1+t1.Name2 = t3.Name2+t3.Name1
For this:
ID | ID2 | Name1 | Name2
----+-----+-----------------------+---------------------------
12 | 9 | A | XX
3 | 4 | avast | Hitman Pro
11 | 10 | BB | XX
14 | 13 | CC | XX
7 | 5 | Hola! Better Internet | PPLive Video Accelerator
8 | 6 | Hola! Better Internet | ZenMate
2 | 1 | MindNode | XMind
You can solve this using a CROSS APPLY
SELECT a2.ID ID_1,a1.ID ID_2, a2.Name1 , a2.Name2
FROM #a a1
CROSS APPLY
(
SELECT ID, Name2, Name1
FROM #a aa
WHERE aa.Name1 = a1.Name2 AND a1.Name1 = aa.Name2 AND a1.ID > aa.ID
) a2
You can try also:
select min(ID) ID1,
max(ID) ID2,
Name1,
Name2
from ( -- Here I get all the IDs and each couple sorted
-- Change > to < if you don't like the order
select ID,
case
when Name1 > Name2 then Name1
else Name2
end Name1,
case
when Name1 > Name2 then Name2
else Name1
end Name2
from table1
) as t
group by Name1,
Name2
You can even tansform this in a simgle query, without the inner one, but I think in this way it's more readable and you can understand better my approach.

Adding multiple columns of multiple tables into one column

I have two tables with same column name I have to add the oprId column values for some specific condition on both tables.
Table 1
something oprId
abc 1
qwe 2
Table 2
something oprId
abc 2
qwe 5
Result should be
oprId
3
7
declare #T1 table (something varchar(3), oprId int)
declare #T2 table (something varchar(3), oprId int)
insert into #T1 values ('abc', 1),('qwe', 2)
insert into #T2 values ('abc', 2),('qwe', 5)
select T1.oprId+T2.oprId as oprId
from #T1 as T1
inner join #T2 as T2
on T1.something = T2.something
Result:
oprId
------
3
7
SELECT ISNULL(A.something,B.something) Something,
ISNULL(A.oprId,0)ÍSNULL(B.oprId,0) oprId
FROM Table1 A
FULL JOIN Table2 B
ON A.something = B.something

SQL Query without Temporary Table

I have written a query which works great on my local SQL Server 2005. I uploaded the query to my hosting server and somehow they say that temporary table creation is disabled on their server.
My query looks like this
create table #tmp
(
srno int identity (1,1) ,
orderid int,
orderdate datetime,
product_code varchar(255),
product_name varchar(255),
shipping_cost decimal(18,2)
)
insert into #tmp (orderid, orderdate, product_code, product_name, shipping_cost)
(select distinct
ord.orderid, ord.orderdate, odn.productcode,
odn.productname, ord.totalshippingcost
from OrderNew ord
inner join order_detailsnew odn on ord.orderid = odn.orderid)
declare #rowcount int, #flag int, #orderid int
set #rowcount = (select ##ROWCOUNT)
set #flag = 0
while (#flag <#rowcount)
begin
set #orderid = (select orderid from #tmp where srno = #flag + 1)
if exists (select 1 from #tmp where orderid = #orderid )
begin
update #tmp
set shipping_cost = 0.0
where srno IN (select srno from #tmp
where orderid = #orderid
AND srno NOT IN (SELECT TOP 1 srno FROM #tmp where orderid = #orderid))
end
set #flag = #flag+1
end
select * from #tmp
drop table #tmp
So not sure if this query can be written without a temporary table, joins etc not sure if it will work ? Any advise ?
I presume this query is to feed into a report which is why you only want the total shipping cost once, while you don't need a temp table for this for reference you can always do this instead if you need to:
DECLARE #tmp TABLE
(
srno int identity (1,1) ,
orderid int,
orderdate datetime,
product_code varchar(255),
product_name varchar(255),
shipping_cost decimal(18,2)
)
and use #tmp rather than #tmp
But you shouldn't need a temp table for this, see below:
SELECT ord.orderid, ord.orderdate, odn.productcode, odn.productname, ord.totalshippingcost
FROM OrderNew AS ord
INNER JOIN order_detailsnew AS odn ON odn.orderid = ord.orderid
WHERE odn.productcode = (SELECT MIN(productcode) FROM OrderNew AS odn2 WHERE odn2.orderid = ord.orderid)
UNION ALL
SELECT ord.orderid, ord.orderdate, odn.productcode, odn.productname, 0.0 AS totalshippingcost
FROM OrderNew AS ord
INNER JOIN order_detailsnew AS odn ON odn.orderid = ord.orderid
WHERE odn.productcode > (SELECT MIN(productcode) FROM OrderNew AS odn2 WHERE odn2.orderid = ord.orderid)
ORDER BY ord.orderid, ord.orderdate, odn.productcode
Works fine for me with the following test script:
DECLARE #ord TABLE
(
orderid int,
orderdate datetime,
totalshippingcost decimal(18,2)
)
DECLARE #odn TABLE
(
orderid int,
productcode varchar(255),
productname varchar(255)
)
INSERT INTO #ord VALUES(1, CAST('20110101' AS DATETIME), 50.25)
INSERT INTO #ord VALUES(2, CAST('20110105' AS DATETIME), 78.15)
INSERT INTO #ord VALUES(3, CAST('20110112' AS DATETIME), 65.50)
INSERT INTO #ord VALUES(4, CAST('20110112' AS DATETIME), 128.00)
INSERT INTO #odn VALUES(1, 'aa', 'AAA')
INSERT INTO #odn VALUES(1, 'bb', 'BBB')
INSERT INTO #odn VALUES(1, 'cc', 'CCC')
INSERT INTO #odn VALUES(2, 'aa', 'AAA')
INSERT INTO #odn VALUES(2, 'bb', 'BBB')
INSERT INTO #odn VALUES(3, 'bb', 'BBB')
INSERT INTO #odn VALUES(3, 'cc', 'CCC')
INSERT INTO #odn VALUES(4, 'cc', 'CCC')
And my results:
Result Set (8 items)
orderid | orderdate | productcode | productname | totalshippingcost
1 | 01/01/2011 00:00:00 | aa | AAA | 50.25
1 | 01/01/2011 00:00:00 | bb | BBB | 0.00
1 | 01/01/2011 00:00:00 | cc | CCC | 0.00
2 | 05/01/2011 00:00:00 | aa | AAA | 78.15
2 | 05/01/2011 00:00:00 | bb | BBB | 0.00
3 | 12/01/2011 00:00:00 | bb | BBB | 65.50
3 | 12/01/2011 00:00:00 | cc | CCC | 0.00
4 | 12/01/2011 00:00:00 | cc | CCC | 128.00
edit: I wasn't happy with the above solution, here's a uch faster and more elegant way of doing it:
SELECT ord.orderid, ord.orderdate, ord.productcode, ord.productname, CASE WHEN row_no = 1 THEN ord.totalshippingcost ELSE 0.0 END AS totalshippingcost
FROM
(
SELECT ROW_NUMBER() OVER(PARTITION BY ord.orderid ORDER BY ord.orderid, ord.orderdate, odn.productcode) AS row_no, ord.orderid, ord.orderdate, odn.productcode, odn.productname, ord.totalshippingcost
FROM OrderNew AS ord
INNER JOIN order_detailsnew AS odn ON odn.orderid = ord.orderid
) ord
ORDER BY ord.orderid, ord.orderdate, ord.productcode
Results match perfectly.
Edit for user580950, to insert nulls into every second row:
You change the first SELECT line to be:
SELECT CASE D.N WHEN 1 THEN ord.orderid END AS orderid, ...
And you chance the ORDER BY line to be:
CROSS JOIN (SELECT 1 UNION ALL SELECT 2) AS D(N)
ORDER BY ord.orderid, ord.orderdate, ord.productcode, D.N
But as the comments say said in your other question SQL Query Add an Alternate Blank Records, this is something that you should be doing at your presentation layer and not in the database.