I'm having some problems with the following query:
select * from
(
select inventory.location, inventory.itemnum as itemnum, item.description as itemdesc,
inventory.minlevel as olevel , invcost.avgcost from inventory join invcost on inventory.itemnum = invcost.itemnum join item on inventory.itemnum = item.itemnum
where inventory.location = 'KHSTORE' AND inventory.itemnum BETWEEN '1221' and '1221' and invcost.location = 'KHSTORE'
) table1
join
(select SUM(balvalues) as balance, itemnum from
(select ib.CURBAL as balvalues, inventory.itemnum as itemnum from invbalances ib join inventory on ib.itemnum = inventory.itemnum and ib.location = inventory.location where inventory.itemnum BETWEEN '1221' and '1221'
UNION ALL
SELECT SUM(-1*QUANTITY) , itemnum from matrectrans where itemnum BETWEEN '1221' and '1221' and TRANSDATE >= '05-MAY-2015' and tostoreloc = 'KHSTORE' group by itemnum
UNION ALL
SELECT SUM(-1*QUANTITY) , itemnum from matusetrans where itemnum BETWEEN '1221' and '1221' and TRANSDATE >= '05-MAY-2015' and storeloc = 'KHSTORE' group by itemnum
)group by itemnum
) table2 on table1.itemnum = table2.itemnum;
The query runs fine on an Oracle database but when I try to run the same on an SQL Server instance I get the following error:
Msg 156, Level 15, State 1, Line 14
Incorrect syntax near the keyword 'group'.
I'm looking for a way to change this query and make it compatible with both Orace and SQLS, or if not then at least make it run on SQL Server
Try to add alias to subquery:
select * from
(
select inventory.location, inventory.itemnum as itemnum, item.description as itemdesc,
inventory.minlevel as olevel , invcost.avgcost from inventory join invcost on inventory.itemnum = invcost.itemnum join item on inventory.itemnum = item.itemnum
where inventory.location = 'KHSTORE' AND inventory.itemnum BETWEEN '1221' and '1221' and invcost.location = 'KHSTORE'
) table1
join
(select SUM(balvalues) as balance, itemnum from
(select ib.CURBAL as balvalues, inventory.itemnum as itemnum from invbalances ib join inventory on ib.itemnum = inventory.itemnum and ib.location = inventory.location where inventory.itemnum BETWEEN '1221' and '1221'
UNION ALL
SELECT SUM(-1*QUANTITY) , itemnum from matrectrans where itemnum BETWEEN '1221' and '1221' and TRANSDATE >= '05-MAY-2015' and tostoreloc = 'KHSTORE' group by itemnum
UNION ALL
SELECT SUM(-1*QUANTITY) , itemnum from matusetrans where itemnum BETWEEN '1221' and '1221' and TRANSDATE >= '05-MAY-2015' and storeloc = 'KHSTORE' group by itemnum
) AS t /* HERE */
group by itemnum
) table2 on table1.itemnum = table2.itemnum;
Using CTE for more readability:
WITH table1 AS
(
SELECT inventory.location, inventory.itemnum as itemnum, item.description as itemdesc,
inventory.minlevel as olevel , invcost.avgcost
FROM inventory
JOIN invcost
ON inventory.itemnum = invcost.itemnum
JOIN item
ON inventory.itemnum = item.itemnum
WHERE inventory.location = 'KHSTORE'
AND inventory.itemnum BETWEEN '1221' and '1221'
AND invcost.location = 'KHSTORE'
),
table2 AS
(
SELECT SUM(balvalues) as balance,itemnum
FROM
(SELECT ib.CURBAL as balvalues, inventory.itemnum as itemnum
FROM invbalances ib
JOIN inventory
ON ib.itemnum = inventory.itemnum
AND ib.location = inventory.location
WHERE inventory.itemnum BETWEEN '1221' AND '1221'
UNION ALL
SELECT SUM(-1*QUANTITY) , itemnum
FROM matrectrans
WHERE itemnum BETWEEN '1221' AND '1221'
AND TRANSDATE >= '05-MAY-2015'
AND tostoreloc = 'KHSTORE'
GROUP BY itemnum
UNION ALL
SELECT SUM(-1*QUANTITY) , itemnum
FROM matusetrans
WHERE itemnum BETWEEN '1221' AND '1221'
AND TRANSDATE >= '05-MAY-2015'
AND storeloc = 'KHSTORE'
GROUP BY itemnum
) AS t /* HERE */
GROUP BY itemnum
)
SELECT *
FROM table1
JOIN table2
ON table2 on table1.itemnum = table2.itemnum;
EDIT
Using CTE no subqueries
WITH table1 AS
(
SELECT inventory.location, inventory.itemnum as itemnum, item.description as itemdesc,
inventory.minlevel as olevel , invcost.avgcost
FROM inventory
JOIN invcost
ON inventory.itemnum = invcost.itemnum
JOIN item
ON inventory.itemnum = item.itemnum
WHERE inventory.location = 'KHSTORE'
AND inventory.itemnum BETWEEN '1221' and '1221'
AND invcost.location = 'KHSTORE'
)
,table2_helper AS
(
SELECT ib.CURBAL as balvalues, inventory.itemnum as itemnum
FROM invbalances ib
JOIN inventory
ON ib.itemnum = inventory.itemnum
AND ib.location = inventory.location
WHERE inventory.itemnum BETWEEN '1221' AND '1221'
UNION ALL
SELECT SUM(-1*QUANTITY) , itemnum
FROM matrectrans
WHERE itemnum BETWEEN '1221' AND '1221'
AND TRANSDATE >= '05-MAY-2015'
AND tostoreloc = 'KHSTORE'
GROUP BY itemnum
UNION ALL
SELECT SUM(-1*QUANTITY) , itemnum
FROM matusetrans
WHERE itemnum BETWEEN '1221' AND '1221'
AND TRANSDATE >= '05-MAY-2015'
AND storeloc = 'KHSTORE'
GROUP BY itemnum
)
,table2 AS
(
SELECT SUM(balvalues) as balance,itemnum
FROM table2_helper
GROUP BY itemnum
)
SELECT *
FROM table1
JOIN table2
ON table2 on table1.itemnum = table2.itemnum;
Related
**Query 1:**
SELECT ID
, COUNT(DISTINCT A.order_id) AS order_count
FROM table_1 B
JOIN table_2 A
ON A.order_id = B.order_id
WHERE
AND A.order_status IN ('PROCESSING', 'COMPLETED')
and B.fullfilled_cd = 'DELIVERY'
GROUP BY ID;
**Query 2:**
SELECT ID
, COUNT(DISTINCT A.order_id) AS order_count
FROM table_1 B
JOIN table_2 A
ON A.order_id = B.order_id
WHERE
AND A.order_status IN ('PROCESSING', 'COMPLETED')
and B.fullfilled_cd = 'PURCHASE'
GROUP BY ID;
Kindly guide me how to combine these 2 queries into a single query. COUNT_IF is not possible as fullfilled_cd is not a BOOLEAN column. Can we use CASE statement?
Using conditional aggregation:
SELECT ID
,COUNT(DISTINCT CASE WHEN B.fullfilled_cd = 'DELIVERY' THEN A.order_id END) AS order_count_delivery
,COUNT(DISTINCT CASE WHEN B.fullfilled_cd = 'PURCHASE' THEN A.order_id END) AS order_count_purchase
FROM table_1 B
JOIN table_2 A
ON A.order_id = B.order_id
WHERE A.order_status IN ('PROCESSING', 'COMPLETED')
AND B.fullfilled_cd IN ('DELIVERY','PURCHASE')
GROUP BY ID;
Current query:
Select
FORMAT(Tracking.[start],'dd-MM-yyy H:m:s') as [start],
FORMAT(Tracking.[deliver],'yyy-MM-dd') as dateSort,
FORMAT(Tracking.[deliver],'dd-MM-yyyy') as deliver,
RTrim(Tracking.[orderNumber]) as orderNumber,
Tracking.[tcpState] as oTcpState,
Tracking.[transport],
Tracking.[orderType],
(SELECT Cast(SUM(Cast(stations.estimatedTime as decimal(18,2))/60 ) as decimal(18,2)) FROM stations WHERE stations.orderNumber = Tracking.orderNumber) AS oEstimatedTime,
JSON_VALUE(Tracking.[orderObj],'$."0".Referenztext') as Referenztext,
JSON_VALUE(Tracking.[orderObj],'$."0".TotalSW') as TotalSW,
stations.[id],
FORMAT(stations.[startdate],'dd-MM-yyyy') as startdate,
FORMAT(stations.[enddate],'dd-MM-yyyy') as enddate,
stations.[tcpState],
stations.[name],
stations.[estimatedTime],
stations.[pieces],
stations.[piecesDone],
FORMAT(stations.[estimatedStart],'dd-MM-yyyy') as estimatedStart,
FORMAT(stations.[estimatedEnd],'dd-MM-yyyy') as estimatedEnd,
from Tracking left JOIN(
Select * from Stations left JOIN (
Select dummy.id as dId, dummy.orderNumber as dOrderNumber, SUM(dummy.elapsedTime) as elapsedTimeStationObj From (
select orderNumber, id, DATEDIFF(mi,startdate,ISNULL(enddate,GETDATE())) as elapsedTime FROM StationObj
) dummy group by dummy.orderNumber, dummy.id
) as stationObj
on Stations.orderNumber = stationObj.dOrderNumber and Stations.id = stationObj.dId
where stations.deleted=0
) as stations
on Tracking.orderNumber = stations.orderNumber where Tracking.tcpState != 3
Would like to have the SUM from elapsedTimeStationObj as well.
Tried to do it like this below the other (SELECT Cast(SUM(Cast(stations.estimatedTime....:
(SELECT Cast(SUM(Cast(stations.elapsedTimeStationObj as decimal(18,2))) as decimal(18,2)) FROM stations WHERE stations.orderNumber = Tracking.orderNumber) AS requiredTime,
but it is not possible because elapsedTimeStationObj is not a attribute from table Stations.
Use it as a subquery, and you can use as CTE to make it readable and reusble, like this:
WITH CTE1
AS
(
SELECT DUMMY.id AS dId
,DUMMY.orderNumber AS dOrderNumber
,SUM(DUMMY.elapsedTime) AS elapsedTimeStationObj
FROM (
SELECT orderNumber
,id
,DATEDIFF(mi, startdate, ISNULL(enddate, GETDATE())) AS elapsedTime
FROM StationObj
) DUMMY
GROUP BY DUMMY.orderNumber
,DUMMY.id
), CTE2
AS
(
SELECT orderNumber, Cast(SUM(Cast(stations.estimatedTime as decimal(18,2))/60 ) as decimal(18,2)) AS oEstimatedTime
FROM stations
GROUP BY stations.orderNumber
)
SELECT FORMAT(Tracking.[start], 'dd-MM-yyy H:m:s') AS [start]
,FORMAT(Tracking.[deliver], 'yyy-MM-dd') AS dateSort
,FORMAT(Tracking.[deliver], 'dd-MM-yyyy') AS deliver
,RTrim(Tracking.[orderNumber]) AS orderNumber
,Tracking.[tcpState] AS oTcpState
,Tracking.[transport]
,Tracking.[orderType]
,s2.oEstimatedTime AS oEstimatedTime
,JSON_VALUE(Tracking.[orderObj], '$."0".Referenztext') AS Referenztext
,JSON_VALUE(Tracking.[orderObj], '$."0".TotalSW') AS TotalSW
,stations.[id]
,FORMAT(stations.[startdate], 'dd-MM-yyyy') AS startdate
,FORMAT(stations.[enddate], 'dd-MM-yyyy') AS enddate
,stations.[tcpState]
,stations.[name]
,stations.[estimatedTime]
,stations.[pieces]
,stations.[piecesDone]
,FORMAT(stations.[estimatedStart], 'dd-MM-yyyy') AS estimatedStart
,FORMAT(stations.[estimatedEnd], 'dd-MM-yyyy') AS estimatedEnd
,
FROM Tracking
LEFT JOIN (
SELECT *
FROM Stations
LEFT JOIN CTE1 AS stationObj ON Stations.orderNumber = stationObj.dOrderNumber
AND Stations.id = stationObj.dId
WHERE stations.deleted = 0
) AS stations ON Tracking.orderNumber = stations.orderNumber
LEFT JOIN CTE2 s2 ON stations.orderNumber = s2.orderNumber
WHERE Tracking.tcpState != 3
I am working on performance optimizing my query. I have implemented two CTEs as well as created a couple of temporary tables. Finally I am writing a combined query that does the union on CTEs and joins with the temp table. I am sharing the code of where I am defining the CTE's and combined query.I have the following query and would like suggestions on what would be the best practice to improve speed.
;with histTbl_CTE
as (
select
a.companyid,
a.periodenddate,
a.pricingdate,
fp.fiscalYear,
fp.fiscalQuarter,
pt.periodtypeId
from (
/* determine the cartesian product of periodenddate and pricingdate, assigning
a periodenddate to every pricing date that is less than the periodenddate */
select
per.companyid,
periodenddate,
pric.pricingdate,
datediff(day, pricingdate, periodenddate) as peqdiffdate
from #PeriodTbl per
join #PricingTbl pric
on (per.companyid = pric.companyid
and per.periodenddate >= pric.pricingdate)
) a
inner join (
/*find the different between the pricing date and the period end date */
select
per.companyid,
periodenddate,
min(datediff(day, pricingdate, periodenddate)) minpeqdiffdate
from
#PeriodTbl per
join #PricingTbl pric
on (per.companyid = pric.companyid
and per.periodenddate >= pric.pricingdate)
group by per.companyid, per.fiscalyear, per.fiscalquarter, periodenddate
)b
on a.companyid = b.companyid
and a.periodenddate = b.periodenddate
and a.peqdiffdate = b.minpeqdiffdate
left join ciqFinPeriod fp on a.companyId = fp.companyId
left join ciqFinInstance fi on (a.periodenddate = fi.periodenddate
and fi.financialPeriodId = fp.financialPeriodId)
left join ciqPeriodType pt on pt.periodTypeId = fp.periodTypeId
where fi.latestforfinancialperiodflag = 1
and latestfilingforinstanceflag = 1
and pt.periodtypeid = 4
group by a.companyid, a.periodenddate, a.pricingdate, fp.fiscalYear, fp.fiscalQuarter, pt.periodtypeid
),
recentTbl_CTE
as (
--/* use the same methodology from above to find the most recent dates */
select
temp.companyId,
max(periodenddate) as periodenddate,
max(peqdate) as pricingDate,
x.adjFY as fiscalYear,
x.adjFQ as fiscalQuarter,
periodTypeId = NULL
from(
select
a.companyId,
a.periodenddate,
a.peqdate,
b.minpeqdiffdate
from(
select
mc.companyId,
mc.pricingDate as periodenddate,
peq.pricingDate as peqdate,
datediff(day, peq.pricingDate, mc.pricingDate) as peqdiffdate
from #MarketTbl mc
join #PricingTbl peq
on (mc.companyId = peq.companyId
and mc.pricingDate >=peq.pricingDate)
group by mc.companyid, mc.pricingDate, peq.pricingDate
) a
inner join (
select
mc.companyId,
mc.pricingdate as periodenddate,
min(datediff(day, peq.pricingDate, mc.pricingDate)) as minpeqdiffdate
from #MarketTbl mc
join #PricingTbl peq
on (mc.companyId = peq.companyId
and mc.pricingDate >= peq.pricingDate)
group by mc.companyid, mc.pricingDate
) b on
a.companyId = b.companyId
and a.periodenddate = b.periodenddate
and a.peqdiffdate = b.minpeqdiffdate
) temp
join #futurePer x on x.companyId = temp.companyId
group by temp.companyid, minpeqdiffdate, adjFY, adjFQ
having minpeqdiffdate = 0
)
/* combine all table and join the data*/
select
combined.companyId,
combined.periodenddate,
combined.dataItemName,
case when combined.dataItemName = 'priceMidUSD' then combined.dataItemidue/exR.currencyRateClose * 1
when combined.dataItemName = 'sharesOutstanding' then combined.dataItemidue
when combined.dataItemName = 'marketcaptrend' then combined.dataItemidue
else combined.dataItemidue/exR.currencyRateClose * 1e6 end as dataItemidue,
combined.fiscalYear,
combined.fiscalQuarter,
combined.periodTypeId,
ctbl.tickerSymbol,
ctbl.exchangeName,
id.dataItemId
from(
select
companyId,
pricingdate,
periodenddate,
currencyId,
dataItemName,
cast(dataItemidue as float) as dataItemidue,
fiscalYear,
fiscalQuarter,
periodTypeId
from(
select
a.companyId,
a.pricingdate,
c.currencyId,
a.periodenddate,
cast(c.priceMid as nvarchar(100)) as priceMidUSD,
cast(d.marketCap as nvarchar(100)) as marketCapUSD,
cast((d.marketCap/nullif(prevmc.prevmarketCap,0)) - 1 as nvarchar(100)) as marketcaptrend,
cast(d.TEV as nvarchar(100)) as TEV,
cast(d.sharesOutstanding as nvarchar(100)) as sharesOutstanding,
a.fiscalYear,
a.fiscalQuarter,
a.periodTypeId
from (
select
companyid,
periodenddate,
pricingdate,
fiscalYear,
fiscalQuarter,
periodtypeId
from histTbl_CTE
union all
select
companyId,
periodenddate,
pricingDate,
fiscalYear,
fiscalQuarter,
periodTypeId
from recentTbl_CTE
)a
join #PricingTbl c
on (
c.pricingdate = a.pricingDate
and c.companyId = a.companyId )
join #MarketTbl d
on (
d.companyId = a.companyId
and d.pricingDate = a.periodenddate)
left join (
select a.companyId,
a.pricingDate,
a.prevperiod,
b.marketcap as prevmarketcap
from(
select
mc.companyid,
mc.pricingdate,
mc.pricingdate -365 as 'prevperiod'
from
ciqMarketCap mc
where
mc.companyid in (select id from #companyId)
) a
inner join (
select
mc.companyId,
mc.pricingdate,
mc.marketcap
from #MarketTbl mc
) b
on (a.companyid = b.companyid
and a.prevperiod = b.pricingdate)
) prevmc
on (prevmc.companyid = d.companyid
and prevmc.pricingdate = d.pricingdate)
group by a.companyid, a.periodenddate, a.pricingDate, a.fiscalYear, a.fiscalQuarter, a.periodtypeid, c.currencyId,
c.pricemid, d.marketCap, d.TEV, d.sharesOutstanding, prevmc.prevperiod, prevmc.prevmarketcap
) c
unpivot
(dataItemidue for dataItemName in
(TEV, marketCapUSD, sharesOutstanding, marketcaptrend, priceMidUSD)
) as unpvt
) combined
join #CompanyInfoTbl ctbl on ctbl.companyId = combined.companyId
--UNION gives me error
;WITH TopSelling
AS
(
SELECT p.Name AS ProductName,SUM(LineTotal) AS TotalAmount
FROM Product AS p INNER JOIN SalesOrderDetail AS sod
ON p.ProductID = sod.ProductID
GROUP BY p.Name
)
SELECT ProductName, TotalAmount
FROM TopSelling
ORDER BY TotalAmount DESC
OFFSET 0 ROWS
FETCH NEXT 5 ROWS ONLY
UNION ALL
SELECT 'Grand Total' AS ProductName, sum(TotalAmount) AS TotalAmount
FROM TopSelling
;WITH TopSelling
AS
(
SELECT p.Name AS ProductName,SUM(LineTotal) AS TotalAmount
FROM Product AS p INNER JOIN SalesOrderDetail AS sod
ON p.ProductID = sod.ProductID
GROUP BY p.Name
),
topSelling1 as
(
SELECT ProductName, TotalAmount
FROM TopSelling
ORDER BY TotalAmount DESC
OFFSET 0 ROWS
FETCH NEXT 5 ROWS ONLY
),
total as
(
SELECT sum(TotalAmount) AS TotalAmount
FROM TopSelling;
)
select ProductName, TotalAmount
FROM TopSelling1
UNION ALL
SELECT 'Grand Total' AS ProductName, TotalAmount
FROM Total;
So don't use it. Use GROUPING SETS instead:
SELECT COALESCE(p.Name, 'Grand Total') AS ProductName, -- The lazy way
SUM(LineTotal) AS TotalAmount
FROM Product p INNER JOIN
SalesOrderDetail sod
ON p.ProductID = sod.ProductID
GROUP BY GROUPING SETS ( (p.Name), () )
ORDER BY TotalAmount DESC;
EDIT:
Oh, I see, you want the first five rows and then the overall total.
Use a subquery:
WITH TopSelling AS
(SELECT p.Name as ProductName, SUM(LineTotal) as TotalAmount
FROM Product p INNER JOIN
SalesOrderDetail sod
ON p.ProductID = sod.ProductID
GROUP BY p.Name
)
SELECT ProductName, TotalAmount
FROM ((SELECT TOP 5 ProductName, TotalAmount, 1 as Priority
FROM TopSelling
ORDER BY TotalAmount DESC
) UNION ALL
(SELECT 'Grand Total' AS ProductName, sum(TotalAmount) AS TotalAmount, 2 as Priority
FROM TopSelling
)
) t
ORDER BY Priority, TotalAmount DESC;
I find TOP 5 more natural in this case than OFFSET/FETCH. The latter, of course, is more sensible for paging.
remove the alias form the second select
;WITH TopSelling
AS
(
SELECT p.Name AS ProductName,SUM(LineTotal) AS TotalAmount
FROM Product AS p INNER JOIN SalesOrderDetail AS sod
ON p.ProductID = sod.ProductID
GROUP BY p.Name
)
SELECT ProductName, TotalAmount
FROM TopSelling
ORDER BY TotalAmount DESC
OFFSET 0 ROWS
FETCH NEXT 5 ROWS ONLY
UNION ALL
SELECT 'Grand Total' , sum(TotalAmount)
FROM TopSelling
What I'm trying to do: read the logs and insert the necessary data into 3 different tables that get information from each other.
LOG_ITEM201303 is found on gamelogs db.
Mail_Item_Table, Mail_List_Table, Mail_Message_Table is found on game db.
The Mail Tables are connected via the Indexes.
CHAR_KEY, NAME, ITEMNUM are the values I need to use for my queries.
The query for me to get the data from the logs:
SELECT CHAR_KEY, NAME, ITEMNUM
FROM LOG_ITEM201303
where
(
ITEMNUM = 14317
OR ITEMNUM = 14318
OR ITEMNUM = 15478
OR ITEMNUM = 15479
OR ITEMNUM = 14301
OR ITEMNUM = 14302
OR ITEMNUM = 15476
OR ITEMNUM = 15477
OR ITEMNUM = 15018
OR ITEMNUM = 15019
OR ITEMNUM = 15020
OR ITEMNUM = 15021
OR ITEMNUM = 15022
OR ITEMNUM = 15023
OR ITEMNUM = 15024
OR ITEMNUM = 15025
OR ITEMNUM = 14437
OR ITEMNUM = 14438
OR ITEMNUM = 15656
OR ITEMNUM = 15657
OR ITEMNUM = 15658
OR ITEMNUM = 15659
OR ITEMNUM = 15660
OR ITEMNUM = 15661
OR ITEMNUM = 15662
OR ITEMNUM = 15663
) AND (KIND = 133) AND (Convert(varchar, OCCUR_TIME,111) < '2013/03/22')
Sample result of logs query above(total actual results are in 600+):
CHAR_KEY NAME ITEMNUM
-----------+----------------+-----------
28257 | clarkailey | 14438
894367 | Wolf | 15023
2869858 | HOPEINME | 14437
Now I need to automatically insert each row into this query:
CHAR_KEY NAME ITEMNUM
-----------+----------------+-----------
2869858 | HOPEINME | 14437
(this query shows an example of the 3rd sample data above being inserted...
instead of making this query for each entry is there a way for this to done faster?)
INSERT INTO Mail_Item_Table
(ItemNumber, ItemInfo, ReceiveDate)
VALUES
(14437, --this is the ITEMNUM
(SELECT CONVERT(BINARY(16), REVERSE(CONVERT(BINARY(16), 14437)))), NULL)
INSERT INTO Mail_Message_Table
(Message)
VALUES
('Automated Message from the ADMIN.')
INSERT INTO Mail_List_Table
(ReceiverCharKey, MailListIndex, MailItemIndex, MailMessageIndex, Sender, Receiver, SendDate)
VALUES
(2869858, --this is the CHAR_KEY
(SELECT TOP 1 MailListIndex+1 as last_entry
FROM Mail_List_Table
WHERE sender = 'SENDER'
ORDER BY MailListIndex DESC),
(SELECT TOP 1 MailItemIndex AS last_entry
FROM Mail_Item_Table
ORDER BY MailItemIndex DESC),
(SELECT TOP 1 MailMessageIndex AS last_entry
FROM Mail_Message_Table
ORDER BY MailMessageIndex DESC),
'SENDER',
'HOPEINME', --this is the NAME
getdate())
My question:
How to automate all this, that the query will read all the logs and insert the data row by row.
Thank you very much.
Can I use #variables for this?
You can use the following syntax for inserts
INSERT INTO dbo.Destination (Col1, Col2, Col3)
SELECT Col1, Col2, Col3
FROM dbo.Source
If you had tables with the same columns or a result set that had the same columns as your destination, you don't have to specify columns in the INSERT.
INSERT INTO dbo.Destination
SELECT *
FROM dbo.Source
Both of these are predicated on your Destination table already being created. These are NOT the same as SELECT * INTO dbo.Destination FROM dbo.Source