How can I insert autoincrement value using SQL insert into select statement - sql-server

I have the following code to insert data from a table in another database, but how can I insert into a primary key column by incrementing the last record in ID column value by 1? In my case [WORK ORDER #] is a primary key it doesn't allow null.
[WORK ORDER #] is nvarchar(10)
INSERT INTO DB1.dbo.WORKORDERS ([WORK ORDER #], [CUSTOMER], [SO DATE], [SO NUMBER])
SELECT *
FROM OPENQUERY([DB29],
'SELECT DISTINCT
NULL, --need to set auto increment value here
Customers.Customer_Bill_Name,
JrnlHdr.TransactionDate,
JrnlHdr.Reference)
FROM Customers
INNER JOIN JrnlHdr ON Customers.CustomerRecordNumber = JrnlHdr.CustVendId
WHERE JrnlHdr.JrnlKey_Journal = 11
AND JrnlHdr.TransactionDate = CURDATE()
-------------------// i tried as follows-----
--> You only do this one time...not with each query
create sequence dbo.WorkOrderSequence
as int
start with 43236
--> I took out the part that failed (you got option 1 and 3 kinda
--> mashed together)
insert DB1.dbo.WORKORDERS
([WORK ORDER #],[CUSTOMER],[SO DATE],[SO NUMBER],[ASSY PN-S],[CUSTOMER PN],[SHIP VIA],[PROMISED DATE],[COMMENTS],[PO #],[WO Notes])
select
convert(varchar(10), next value for DB1.dbo.WorkOrderSequence ),
x.Customer_Bill_Name,
x.TransactionDate,
x.Reference,
x.ItemID,
x.PartNumber,
x.WhichShipVia,
x.ShipByDate,
x.Comment2,
x.CustomerInvoiceNo,
x.SalesDescription
from
openquery
([DB29],
'select distinct
Customers.Customer_Bill_Name,
JrnlHdr.TransactionDate,
JrnlHdr.Reference,
LineItem.ItemID ,
LineItem.PartNumber,
Customers.WhichShipVia,
JrnlHdr.ShipByDate,
JrnlHdr.Comment2,
JrnlHdr.CustomerInvoiceNo,
LineItem.SalesDescription
FROM Customers
INNER JOIN JrnlHdr
ON Customers.CustomerRecordNumber = JrnlHdr.CustVendId
LEFT OUTER JOIN Address
ON Customers.CustomerRecordNumber = Address.CustomerRecordNumber
INNER JOIN JrnlRow
ON JrnlHdr.PostOrder = JrnlRow.PostOrder
INNER JOIN LineItem
ON JrnlRow.ItemRecordNumber = LineItem.ItemRecordNumber
WHERE JrnlHdr.JrnlKey_Journal = 11 AND JrnlHdr.TransactionDate = CURDATE()
AND JrnlHdr.PostOrder = JrnlRow.PostOrder
AND JrnlHdr.CustVendId = Customers.CustomerRecordNumber
AND JrnlRow.ItemRecordNumber = LineItem.ItemRecordNumber
AND JrnlHdr.POSOisClosed = 0'
) as x

Option 1
If you're on at least SQL Server 2012 (you didn't mention a specific version), you have a general sequence number generator that you can use. I like it a lot for this kind of scenario. In the DB1 database, you'd add your sequence like this:
create sequence dbo.WorkOrderSequence
as int
start with 5002230 --> pick a starting number greater
--> than any existing [WorkOrder #]
Then, you can just get the next number(s) in your insert-for-select statement:
insert DB1.dbo.WORKORDERS
([WORK ORDER #], [CUSTOMER], [SO DATE], [SO NUMBER])
select
convert(varchar(10), next value for DB1.dbo.WorkOrderSequence ),
x.Customer_Bill_Name,
x.TransactionDate,
x.Reference
from
openquery
([DB29],
'select distinct
Customers.Customer_Bill_Name,
JrnlHdr.TransactionDate,
JrnlHdr.Reference
from
Customers
inner join
JrnlHdr on
Customers.CustomerRecordNumber = JrnlHdr.CustVendId
where
JrnlHdr.JrnlKey_Journal = 11
and
JrnlHdr.TransactionDate = CURDATE()'
) as x
The sequence is a standalone auto-incrementing number. Every time you use the next value for dbo.WorkOrderSequence, it auto-increments. This way, you don't have to modify any table definitions.
Option 2
Alternatively, you could alter the DB1.dbo.WORKORDERS table so that the default value to use the expression...
alter table dbo.WORKORDERS
alter column [Work Order #] nvarchar(10) not null
default( convert( nvarchar(10), next value for dbo.WorkOrderSequence ) )
If you do this, then you can completely omit inserting the [Work Order #] altogether and let the default do the magic.
Option 3
If you're not on 2012, but on at least 2008, you can still get there...but it's a little trickier because you have to get the current starting [Work Order #]:
insert DB1.dbo.WORKORDERS
([WORK ORDER #], [CUSTOMER], [SO DATE], [SO NUMBER])
select
convert(varchar(10), x.RowNum + y.MaxOrderNum ),
x.Customer_Bill_Name,
x.TransactionDate,
x.Reference
from
openquery
([DB29],
'select distinct
row_number() over( order by JrnlHdr.TransactionDate ) as RowNum,
Customers.Customer_Bill_Name,
JrnlHdr.TransactionDate,
JrnlHdr.Reference
from
Customers
inner join
JrnlHdr on
Customers.CustomerRecordNumber = JrnlHdr.CustVendId
where
JrnlHdr.JrnlKey_Journal = 11
and
JrnlHdr.TransactionDate = CURDATE()'
) as x
cross join
(
select
convert( int, max( [Work Order #] ) ) as MaxOrderNum
from
Db1.dbo.WORKORDERS
) as y
Option 4
If you're on something earlier than 2008...you'll probably want a stored procedure to do the insert in two steps: inserting the work orders into a temporary table (one with an auto-increment [Work Order #] starting at the current table's max( [Work Order #] ) + 1 )...and then step 2 would insert the temp table into WORKORDERS with a convert.

I've dabbled a little with SQL but not to create tables as much. I have used postgreSQL along with knex to do so and I believe the solution that should fit for your needs as well is using the value unique. I apologize if that's not correct since I am junior to coding, but hopefully looking at this will help or looking at the SQL docs :) difference between primary key and unique key

Related

left join 2 tables into one field

I want to left join the 2 tables below (h and he) to the main table but so I only have one field [mpxn] rather than 2 [h.mpxn & he.mpxn]
Is there away to do this? there shouldn't be any instances where there is a result in both h and he tables.
SELECT distinct [Interaction ID]
,[BP Number]
,he.mpxn
FROM [DOMCustomers].[Bart].[OpenComplaints] as c
Left join DOMCustomers.Bart.MeterHealth as h on c.[BP Number]=h.[Business Partner]
Left join DOMCustomers.Bart.ENAMeterHealth as he on c.[BP Number]=he.[Business Partner]
Not sure if you are asking for the 2 column values to be concatenated together, then joined to the main table - or
if you are trying to combine the results of the 2 tables (h, he) into one result in one column, then join that to the table?
Anyway, here is some examples of how to achieve what I think you may be asking:
CREATE TABLE h
(
mpxn VARCHAR(100)
,[Business Partner] VARCHAR(100)
);
CREATE TABLE he
(
mpxn VARCHAR(100)
,[Business Partner] VARCHAR(100)
);
CREATE TABLE OpenComplaints
(
[Interaction ID] VARCHAR(100)
,[BP Number] VARCHAR(100)
);
INSERT INTO h VALUES ('h',1);
INSERT INTO he VALUES ('he',1);
INSERT INTO OpenComplaints VALUES ('1',1);
Now for some queries.
This one does a union, then joins to the main table:
;WITH a AS (
SELECT
h.mpxn
,h.[Business Partner]
FROM h
UNION ALL
SELECT
he.mpxn
,he.[Business Partner]
FROM he
)
SELECT
c.[Interaction ID]
,c.[BP Number]
,h.mpxn
FROM OpenComplaints c
left join a AS h on c.[BP Number]=h.[Business Partner]
[
This one concatenates the columns together, then joins:
;WITH b AS (
SELECT
h.mpxn+' '+he.mpxn AS mpxn
,h.[Business Partner]
FROM h
JOIN he ON h.[Business Partner] =he.[Business Partner] )
SELECT
c.[Interaction ID]
,c.[BP Number]
,h.mpxn
FROM OpenComplaints c
left join b AS h on c.[BP Number]=h.[Business Partner]
Here is a SQL Fiddle example
You could UNION the two tables together prior to the LEFT JOIN
with mpxn_cte([BP Number], mpxn) as (
select [BP Number], mpxn from DOMCustomers.Bart.MeterHealth
union
select [BP Number], mpxn from DOMCustomers.Bart.ENAMeterHealth)
select distinct c.[Interaction ID]
,c.[BP Number]
m.mpxn
from M [DOMCustomers].[Bart].[OpenComplaints] as c
left jion mpxn_cte m on c.[BP Number]=m.[BP Number];
With the assumption that there are never overlapping entries in MeterHealth and ENAMeterHealth this is probably the query result expected:
SELECT
opencomplaint.[Interaction ID],
opencomplaint.[BP Number],
Coalesce( health.mxpn, enahealth.mpxn, 0) AS mxpn
FROM
[DOMCustomers].[Bart].[OpenComplaints] AS opencomplaint
LEFT OUTER JOIN DOMCustomers.Bart.MeterHealth AS health
ON opencomplaint.[BP Number] = health.[Business Partner]
LEFT OUTER JOIN DOMCustomers.Bart.ENAMeterHealth AS enahealth
ON opencomplaint.[BP Number] = enahealth.[Business Partner]
GROUP BY
opencomplaint.[Interaction ID],
opencomplaint.[BP Number],
Coalesce( health.mxpn, enahealth.mpxn, 0) AS mxpn
;
Couple of points:
Using column names with spaces is counterproductive for SQL code reading
GROUP BY is always more secure than DISTINCT
Using one or two letter aliases might work up to three tables, after that and with more complicated transformations reading code becomes an exercise in "I do not like my colleagues at all"
Use semicolons to terminate SQL statements - again this makes code easier to read (caveat emptor: some tools just can not handle standard ANSI SQL after 20 years!)
Keep joins at same level on same level of indentation
The Coalesce works best with a default fallback value that instinctively tells the user that no value from the outer joins came back - 0 in this case
Coalesce introduces a first come, first serve transformation rule - best comment your business logic above the use of the function why it was written like it is

Return only rows from the joined table with the latest date

By running the following query I realized that I have duplicates on the column QueryExecutionId.
SELECT DISTINCT qe.QueryExecutionid AS QueryExecutionId,
wfi.workflowdefinitionid AS FlowId,
qe.publishing_date AS [Date],
c.typename AS [Type],
c.name As Name
INTO #Send
FROM
[QueryExecutions] qe
JOIN [Campaign] c ON qe.target_campaign_id = c.campaignid
LEFT JOIN [WorkflowInstanceCampaignActivities] wfica ON wfica.queryexecutionresultid = qe.executionresultid
LEFT JOIN [WorkflowInstances] wfi ON wfica.workflowinstanceid = wfi.workflowinstanceid
WHERE qe.[customer_idhash] IS NOT NULL;
E.g. When I test with one of these QueryExecutionIds, I can two results
select * from ##Send
where QueryExecutionId = 169237
We realized the reason is that these two rows have a different FlowId (second returned value in the first query). After discussing this issue, we decided to take the record with a FlowId that has the latest date. This date is a column called lastexecutiontime that sits in the third joined table [WorkflowInstances] which is also the table where FlowId comes from.
How do I only get unique values of QueryExecutionId with the latest value of WorkflowInstances.lastexecution time and remove the duplicates?
You can use a derived table with first_value partitioned by workflowinstanceid ordered by lastexecutiontime desc:
SELECT DISTINCT qe.QueryExecutionid AS QueryExecutionId,
wfi.FlowId,
qe.publishing_date AS [Date],
c.typename AS [Type],
c.name As Name
INTO #Send
FROM
[QueryExecutions] qe
JOIN [Campaign] c ON qe.target_campaign_id = c.campaignid
LEFT JOIN [WorkflowInstanceCampaignActivities] wfica ON wfica.queryexecutionresultid = qe.executionresultid
LEFT JOIN
(
SELECT DISTINCT workflowinstanceid, FIRST_VALUE(workflowdefinitionid) OVER(PARTITION BY workflowinstanceid ORDER BY lastexecutiontime DESC) As FlowId
FROM [WorkflowInstances]
) wfi ON wfica.workflowinstanceid = wfi.workflowinstanceid
WHERE qe.[customer_idhash] IS NOT NULL;
Please note that your distinct query is pertaining to the selected variables,
eg. Data 1 (QueryExecutionId = 169237 and typename = test 1)
    Data 2 (QueryExecutionId = 169237 and typename = test 2)
The above 2 data are considered as distinct
Try partition by and selection the [seq] = 1 (the below code are partition by their date)
SELECT *
into #Send
FROM
(
SELECT *,ROW_NUMBER() OVER (PARTITION BY [QueryExecutionid] ORDER BY [Date] DESC) [Seq]
FROM
(
SELECT qe.QueryExecutionid AS QueryExecutionId,
wfi.FlowId,
qe.publishing_date AS [Date], --should not have any null values
qe.[customer_idhash]
c.typename AS [Type],
c.name As Name
FROM [QueryExecutions] qe
JOIN [Campaign] c
ON qe.target_campaign_id = c.campaignid
LEFT JOIN [WorkflowInstanceCampaignActivities] wfica
ON wfica.queryexecutionresultid = qe.executionresultid
LEFT JOIN
(
SELECT DISTINCT workflowinstanceid, FIRST_VALUE(workflowdefinitionid) OVER(PARTITION BY workflowinstanceid ORDER BY lastexecutiontime DESC) As FlowId
FROM [WorkflowInstances]
) wfi ON wfica.workflowinstanceid = wfi.workflowinstanceid
) a
WHERE [customer_idhash] IS NOT NULL
) b
WHERE [Seq] = 1
ORDER BY [QueryExecutionid]

cant have expected result; find the names of sailors who have reserved all boats called Interlake

these are the 3 tables(sailors , boats , reserves) and i want to know the sailors who reserved both interlake boats(101,102) .The bID should be calculated by the computer
`select * from sailors
where sid in(select sid from reserves
inner join boats on reserves.bid = boats.bid
where boats.bname='Interlake')`
a link to see the image of tables https://i.stack.imgur.com/hfHpH.png
or link to pdf form http://www.cs.toronto.edu/~avaisman/csc343summer/tutorials/Tutorial_5.pdf
result = dustin luber horatio
expected = dustin horation
thanks for ur help
You can use the following
Explanation
Know the number of boats where name = interlake using cte report
Find all the sailors that reserved the boat with name interlake using summary cte
Find all sailors who reserved all the boats
Query
declare #boat varchar(50) = 'Interlake'
;with report as (
select bname,count(*) as [Count]
from boats b
where b.bname=#boat
group by b.bname
), summary as(
select s.sname, b.bname, count(*) as [Count]
from sailors s
inner join reserves r on r.sid = s.sid
inner join boats b on b.bid = r.bid
where b.bname=#boat
group by s.sname,b.bname
)
select s.*
from summary s
inner join report r on r.bname = s.bname and r.[Count] = s.[Count]
Here a working demo
Hope this will help you
The following SQL will build temp table variables, populate them and then use joins to show you which sailors have reserved the InterLake boats.
--Build sailors temp table variable and insert test data
declare #sailors table
([sid] int
,[name] varchar(50))
insert into #sailors
([sid],[name])
values
(1,'Sailor One'),
(2,'Sailor Two'),
(3,'Sailor Three')
--Build boats temp table variable and insert test data
declare #boats table
(bid int
,[name] varchar(50))
insert into #boats
(bid,[name])
values
(1,'10 foot John Boat'),
(2,'14 foot John Boat'),
(3,'18 foot Ski Boat'),
(4,'Interlake')
--Build reserves temp table variable and insert test data
declare #reserves table
(rid int
,[sid] int
,bid int)
insert into #reserves
(rid,[sid],bid)
values
(1, 1, 4),
(2, 3, 1),
(3, 2, 4)
--Query to select which sailors have reserved Interlake boats
select s.name as Sailor
,b.name as Boat
from #sailors s
inner join #reserves r on r.[sid] = s.[sid]
inner join #boats b on b.bid = r.bid
where b.name = 'InterLake'
--Results
Sailor Boat
Sailor One Interlake
Sailor Two Interlake
First we will join the two tables on where sailors sid matches reserves sid AND where the bid is 101 or 102, because that is what Interlake's bid is. If you want to do by where bname = interlake, you will join boats into reserves. This is something you have already done in your original query using inner join. I choose to use left:
select s.*
from sailors s
left join reserves r on r.sid = s.sid
where r.bid in (101, 102)
Now that I have everyone who has reserved the boats 101 or 102. I want to filter out those who only have both. In this case, I want to query from my filtered results. I decided to use a derived table, but you can use a cte alternatively.
Something like:
select *
from
(select s.*
from sailors s
left join reserves r on r.sid = s.sid
where r.bid in (101, 102)) t1 --t1 is the alias
But I don't want everything from this filtered table. I want the names.
select sname
from
(select s.*
from sailors s
left join reserves r on r.sid = s.sid
where r.bid in (101, 102)) t1
But I only want the names of those who have rented both 101 and 102. In this case if I count by the name, that means you could have only rented both if your count is GREATER than 1:
select sname --select the column of names I want
,count(sname) as total --here's the count of names, completely optional
from
(select s.*
from sailors s
left join reserves r on r.sid = s.sid
where r.bid in (101, 102)) t1
group by sname --anything you use an aggregation you'll need to likely group by something
having count(sname) > 1 --my filter after I group my names
Here is a rextester sample you can play with.
SELECT DISTINCT S.SID,S.SNAME,S.RATING,S.AGE
FROM SAILORS S,RESERVES R,BOATS B
WHERE S.SID=R.SID AND R.BID=B.BID
AND NOT EXISTS
((SELECT B.BID
FROM BOATS B WHERE B.BNAME NOT LIKE '%INTERLAKE%')
EXCEPT
(SELECT R.BID
FROM RESERVES R
WHERE R.SID=S.SID))

Getting most recent date from multiple SQL columns

The suggested answer, in this post, works great for two columns.
I have about 50 different date columns, where I need to be able to report on the most recent interaction, regardless of table.
In this case, I am bringing the columns in to a view, since they are coming from different tables in two different databases...
CREATE VIEW vMyView
AS
SELECT
comp_name AS Customer
, Comp_UpdatedDate AS Last_Change
, CmLi_UpdatedDate AS Last_Communication
, Case_UpdatedDate AS Last_Case
, AdLi_UpdatedDate AS Address_Change
FROM Company
LEFT JOIN Comm_Link on Comp_CompanyId = CmLi_Comm_CompanyId
LEFT JOIN Cases ON Comp_CompanyId = Case_PrimaryCompanyId
LEFT JOIN Address_Link on Comp_CompanyId = AdLi_CompanyID
...
My question is, how I would easily account for the many possibilities of one column being greater than the others?
Using only the two first columns, as per the example above, works great. But considering that one row could have column 3 as the highest value, another row could have column 14 etc...
SELECT Customer, MAX(CASE WHEN (Last_Change IS NULL OR Last_Communication> Last_Change)
THEN Last_Communication ELSE Last_Change
END) AS MaxDate
FROM vMyView
GROUP BY Customer
So, how can I easily grab the highest value for each row in any of the 50(ish) columns?
I am using SQL Server 2008 R2, but I also need this to work in versions 2012 and 2014.
Any help would be greatly appreciated.
EDIT:
I just discovered that the second database is storing the dates in NUMERIC fields, rather than DATETIME. (Stupid! I know!)
So I get the error:
The type of column "ARCUS" conflicts with the type of other columns specified in the UNPIVOT list.
I tried to resolve this with a CAST to make it DATETIME, but that only resulted in more errors.
;WITH X AS
(
SELECT Customer
,Value [Date]
,ColumnName [Entity]
,BusinessEmail
,ROW_NUMBER() OVER (PARTITION BY Customer ORDER BY Value DESC) rn
FROM (
SELECT comp_name AS Customer
, Pers_EmailAddress AS BusinessEmail
, Comp_UpdatedDate AS Company
, CmLi_UpdatedDate AS Communication
, Case_UpdatedDate AS [Case]
, AdLi_UpdatedDate AS [Address]
, PLink_UpdatedDate AS Phone
, ELink_UpdatedDate AS Email
, Pers_UpdatedDate AS Person
, oppo_updateddate as Opportunity
, samdat.dbo.ARCUS.AUDTDATE AS ARCUS
FROM vCompanyPE
LEFT JOIN Comm_Link on Comp_CompanyId = CmLi_Comm_CompanyId
LEFT JOIN Cases ON Comp_CompanyId = Case_PrimaryCompanyId
LEFT JOIN Address_Link on Comp_CompanyId = AdLi_CompanyID
LEFT JOIN PhoneLink on Comp_CompanyId = PLink_RecordID
LEFT JOIN EmailLink on Comp_CompanyId = ELink_RecordID
LEFT JOIN vPersonPE on Comp_CompanyId = Pers_CompanyId
LEFT JOIN Opportunity on Comp_CompanyId = Oppo_PrimaryCompanyId
LEFT JOIN Orders on Oppo_OpportunityId = Orde_opportunityid
LEFT JOIN SAMDAT.DBO.ARCUS on IDCUST = Comp_IdCust
COLLATE Latin1_General_CI_AS
WHERE Comp_IdCust IS NOT NULL
AND Comp_deleted IS NULL
) t
UNPIVOT (Value FOR ColumnName IN
(
Company
,Communication
,[Case]
,[Address]
,Phone
,Email
,Person
,Opportunity
,ARCUS
)
)up
)
SELECT Customer
, BusinessEmail
,[Date]
,[Entity]
FROM X
WHERE rn = 1 AND [DATE] >= DATEADD(year,-2,GETDATE()) and BusinessEmail is not null
You could use CROSS APPLY to manually pivot your fields, then use MAX()
SELECT
vMyView.*,
greatest.val
FROM
vMyView
CROSS APPLY
(
SELECT
MAX(val) AS val
FROM
(
SELECT vMyView.field01 AS val
UNION ALL SELECT vMyView.field02 AS val
...
UNION ALL SELECT vMyView.field50 AS val
)
AS manual_pivot
)
AS greatest
The inner most query will pivot each field in to a new row, then the MAX() re-aggregate them back in to a single row. (Also skipping NULLs, so you don't need to explicitly cater for them.)
;WITH X AS
(
SELECT Customer
,Value [Date]
,ColumnName [CommunicationType]
,ROW_NUMBER() OVER (PARTITION BY Customer ORDER BY Value DESC) rn
FROM (
SELECT comp_name AS Customer
, Comp_UpdatedDate AS Last_Change
, CmLi_UpdatedDate AS Last_Communication
, Case_UpdatedDate AS Last_Case
, AdLi_UpdatedDate AS Address_Change
FROM Company
LEFT JOIN Comm_Link on Comp_CompanyId = CmLi_Comm_CompanyId
LEFT JOIN Cases ON Comp_CompanyId = Case_PrimaryCompanyId
LEFT JOIN Address_Link on Comp_CompanyId = AdLi_CompanyID
) t
UNPIVOT (Value FOR ColumnName IN (Last_Change,Last_Communication,
Last_Case,Address_Change))up
)
SELECT Customer
,[Date]
,[CommunicationType]
FROM X
WHERE rn = 1

Max date sql server

I have a table with data and I am trying to find max date verified
Create table staging(ID varchar(5) not null, Name varchar(200) not null, dateverified datetime not null,dateinserted datetime not null)
ID,Name,DateVerified,DateInserted
42851775,384,2014-05-24 08:48:20.000,2014-05-28 14:28:10.000
42851775,384,2014-05-28 13:13:07.000,2014-05-28 14:36:12.000
42851775,a1d,2014-05-28 09:17:22.000,2014-05-28 14:36:12.000
42851775,a1d,2014-05-28 09:17:22.000,2014-05-28 14:28:10.000
42851775,a1d,2014-05-28 09:17:22.000,2014-05-28 14:29:08.000
42851775,bc5,2014-05-28 09:17:21.000,2014-05-28 14:29:08.000
42851775,bc5,2014-05-28 09:17:21.000,2014-05-28 14:28:10.000
42851775,bc5,2014-05-28 09:17:21.000,2014-05-28 14:36:12.000
I want to display max dateverified for each keyid i.e.
42851775,384,2014-05-28 13:13:07.000,2014-05-28 14:36:12.000
42851775,a1d,2014-05-28 09:17:22.000,2014-05-28 14:36:12.000
42851775,bc5,2014-05-28 09:17:21.000,2014-05-28 14:29:08.000
SELECT i.[ID],i.name,i.dateinserted,r.maxdate
FROM (select id,name,max(dateverified) as maxdate from
[dbo].[staging] where id=42851775 group by id,name) r
inner join
[dbo].[staging] i
on r.id=i.id and r.jobpostingurl=i.jobpostingurl and r.maxdate=i.dateverified
group by i.id,i.jobpostingurl,r.maxdate
I get an error,dateinserted is invalid as it is not contained in group by clause. But if I add it in group by clause I get all 8 records. How to handle this?
Thanks
R
SELECT
KeyID,
MAX(yourDate)
FROM
Staging
GROUP BY
KeyID
If you want additional information join this to another table for instance:
SELECT
b.KeyID,
a.dateinserted,
b.TheDate
FROM YourTable a
INNER JOIN
(
SELECT
KeyID,
MAX(yourDate) AS TheDate
FROM
Staging
GROUP BY
KeyID
) b
ON
b.KeyID = a.KeyID
If you need to get the dateinserted you can use a cte and join it back to the original table:
WITH cte
AS ( SELECT [ID] ,
name ,
MAX(dateverified) AS dateverified
FROM [dbo].[staging]
GROUP BY ID ,
name
)
SELECT cte.[ID] ,
cte.NAME ,
cte.dateverified ,
s.Dateinserted
FROM cte
INNER JOIN dbo.staging s ON cte.[ID] = s.[ID]
AND cte.NAME = s.NAME
AND cte.dateverified = s.dateverified

Resources