T-SQL Help - Pivot Like function to merge up some data
I have a T-SQL Question to ask, normally SQL is my forte, but for some reason I am stumped on the following. So here it goes.
I have a table with the following format:
CREATE TABLE [tmp_LoadDataWithAutoID](
[DataID] int PRIMARY KEY IDENTITY(1,1),
[SiteType] [varchar](800) NULL,
[RegionId] [varchar](800) NULL,
[RegionName] [varchar](800) NULL,
[Site] [varchar](800) NULL,
[SiteName] [varchar](800) NULL,
[PluCode] [varchar](800) NULL,
[Quantity] [varchar](800) NULL,
[SalesValue] [varchar](800) NULL,
) ON [PRIMARY]
This is loaded in from a TXT Fule using BCP.
Th problem with the file is that the Store information is not given on every row, and each record does not tell me what the store information is, it just automatcially the row above that has the information in it.
1 F 2 OR AUCKLAND 100021 KAITAIA 1 1 11.95 SHH LIMITED 2038 1688 732
2 10 626.95 4620.16
3 2 71278.466 121049.29 NULL
4 2807 12 -3.48 NULL
5 29470000105 68 217.6
So what I have done is ran query that grabs the store information that I have with its DataID
DataID SiteType RegionId RegionName SiteSite SiteName PluCode Quantity SalesincGST Dealer FuelOnly ShopOnly Both
1 F 2 OR AUCKLAND 100021 KAITAIA 1 1 11.95 SHIRBERT LIMITED 2038 1688 732
628 F 2 OR AUCKLAND 100062 KAIKOHE 2 78382.017 130780.28 STAGE 3 LIMITED 4097 1761 1159
1301 F 2 OR AUCKLAND 100074 PAIHIA 2 51332.505 86250.69 STAGE 3 LIMITED 1860 996 709
1859 F 7 OR BAY OF PLENTY/CENTRAL PLATEAU 101232 OPOTIKI 2 28869.128 48474.4 D & L INVESTMENTS LIMITED 1660 1558 606
So I have these ID's 1 through to 627, 628 - 1301 AND 1302 - 1858.
The question is, how can iIpivot and join this table back onto itself to work out what is the min and max DataID for each store?
***EDIT ****
To make it easier I will describe a smaller Subset of the table.
DataID SiteSite SiteName
1 100021 STORE KAITAIA
628 100062 STORE KAIKOHE
1301 100074 STORE PAIHIA
1859 101232 STORE OPOTIKI
2383 101250 STORE TE KUMI
3135 101359 STORE MIDWAY
4055 101381 STORE WAIROA
4988 101429 STORE TARADALE
5540 101448 STORE TAMATEA
6062 101460 STORE HAVELOCK NORTH
6556 101481 STORE KARAMU ROAD
6971 101500 STORE PARKVALE
7423 101605 STORE SEAVIEW ROAD
7812 101624 STORE MERRILANDS
8334 101646 STORE INGLEWOOD
I need to create query that inverts the table something like this:
SiteSite, SiteName, MinDataID, MaxDataID
100021 STORE KAITAIA 1 627
100062 STORE KAIKOHE 628 1300
100074 STORE PAIHIA 开发者_运维百科 1301 1858
Once you have Site information for each row . You can use the below query to get the Minimum and Maximum Data ID for each store.
First part to incorporate the row numbers
Select ROW_NUMBER() Over(Order by dataId) as RowNo, * into #temp1 from [tmp_LoadDataWithAutoID]
Second part will get the minimum dataid
Select Min(dataid) as [MinDataID]
into #temp2 From [tmp_LoadDataWithAutoID]
Group By Site
Here you can get the min and max DataId
Select t2.[MinDataID] MinDataID,
(Case When
(Select DataID - 1 from #temp1 Where RowNo = t1.RowNo + 1) is Null Then t2.[MinDataID]
Else
(Select DataID - 1 from #temp1 Where RowNo = t1.RowNo + 1)
End) MaxDataID
From #temp1 t1
Inner Join #temp2 t2 on t1.DataID = t2.[MinDataID]
Finally drop the temporary tables
drop table #temp1
drop table #temp2
Worked it out finally, Thanks for looking at it though.
Answer was:
SELECT DISTINCT DMRA.SiteSite, DMRA.SiteName, MaxDataID = DMRA.DataID, MinDataID =( MIN(DMRB.DataID))
FROM #tmp_DataMarketRecords DMRA
LEFT JOIN #tmp_DataMarketRecords DMRB ON DMRA.DataID < DMRB.DataID
GROUP BY DMRA.DataID, DMRA.SiteSite, DMRA.SiteName
ORDER BY MaxDataID
精彩评论