具有分组和奇数日期请求的UNION和MIN / MAX

时间:2017-08-11 19:17:50

标签: sql sql-server database ssms ssms-2016

这是我的问题的新版本,因为它似乎令人困惑。抱歉。我想到了。如果您有兴趣,请参阅代码。解决的注意事项在那里。谢谢你的帮助!

我到目前为止工作,但是OriginaionL(L代表Little,B代表Big)是不正确的。这是正确的日期,但不是起源。

CREATE TABLE MyTable
(
  LoadTagID                 INT,
  EnteredDateTime           datetime,
  JobNumber                 VARCHAR(50),
  Origination               VARCHAR(50)
)

INSERT INTO MyTable VALUES
(1, '2015-02-09 00:00:00.00',   11111,   'Here')
,(2, '2015-02-09 00:00:00.00',    22222,   'There')
,(3, '2016-03-09 00:00:00.00',    11111,   'Outside')
,(4, '2016-08-09 00:00:00.00',    12578,   'Anywhere')
,(252, '2017-06-29 00:00:00.00',   12345,   'Here')
,(253, '2017-08-01 00:00:00.00',    99999,   'There')
,(254, '2017-08-04 00:00:00.00',    12345,   'Outside')
,(255, '2017-08-09 00:00:00.00',    12345,   'Anywhere')
,(256, '2017-08-10 00:00:00.00',    99999,   'Anywhere')
,(257, '2017-08-10 00:00:00.00',    123456,   'Anywhere')
,(258, '2017-08-11 00:00:00.00',    123456,   'Over Yonder')
,(259, '2017-08-13 00:00:00.00',    99999,   'Under The Bridge')

--Select * From MyTable

CREATE TABLE #LTTB1         --MAX
( 
    LoadTagID varchar(50), 
    JobNumber varchar(50), 
    EnteredDateTime varchar(50), 
    Origination varchar(50)
)
CREATE TABLE #LTTB2         --MIN
(
    LoadTagID varchar(50), 
    JobNumber varchar(50), 
    EnteredDateTime varchar(50),         
    Origination varchar(50) 
)
CREATE TABLE #LTTB3
(
    LoadTagIDL varchar(50), 
    JobNumberL varchar(50), 
    EnteredDateTimeL 
    varchar(50), 
    OriginationL varchar(50)
    , LoadTagID varchar(50), 
    JobNumber varchar(50), 
    EnteredDateTime varchar(50), 
    Origination varchar(50)
)


INSERT INTO #LTTB1
SELECT 
    MAX(LoadTagID) AS LoadTagID, 
    JobNumber,
    MAX(EnteredDateTime) AS EnteredDateTime, 
    MAX(Origination) AS Origination
FROM MyTable
WHERE CONVERT (Date, EnteredDateTime) >= CONVERT (Date, GETDATE()-10) --Gets the last 10 days.  
GROUP BY JobNumber ORDER BY JobNumber

INSERT INTO #LTTB2
SELECT  MIN(LoadTagID) AS LoadTagIDL, 
    JobNumber AS JobNumberL,
    MIN(EnteredDateTime) AS EnteredDateTimeL, 
    MAX(Origination) AS OriginationL  --MAX! This needed to be max!! Why?
FROM MyTable
Where CONVERT (Date, EnteredDateTime) >= CONVERT (Date, GETDATE()-60) --Goes further back in case one is a long. 
GROUP BY JobNumber ORDER BY JobNumber

INSERT INTO #LTTB3
SELECT    L.LoadTagID AS LoadTagIDL 
    , L.JobNumber  AS JobNumberL
    , L.EnteredDateTime AS EnteredDateTimeL
    , L.Origination AS OriginationL  
    , B.LoadTagID, B.JobNumber, B.EnteredDateTime, B.Origination 
FROM #LTTB1 B  --MAX
INNER JOIN #LTTB2 L ON B.JobNumber = L.JobNumber

Select * From #LTTB3

因此对于JobNumber 12345 6/29是正确的,但它应该是“Here”而不是“Anywhere:

对于99999,一切都是正确的,但对于8/1,它应该是“有”而不是任何地方。这似乎是集合中的中间值。我很困惑。

有谁知道为什么它会抓住这个价值?谢谢。

2 个答案:

答案 0 :(得分:0)

CREATE TABLE MyTable
(
  LoadTagID     INT,
  Date          Date,
  Job           INT,
  Origination   VARCHAR(20)
)

INSERT INTO MyTable
VALUES(
       252, '6/29/17',   12345,   'Here')
     ,(253, '8/1/17',    99999,   'There')
     ,(254, '8/4/17',    12345,   'Outside')
     ,(255, '8/8/17',    12345,   'Anywhere')

--SELECT * FROM MyTable

SELECT * INTO #Table1 
FROM MyTable
WHERE LoadTagID IN (SELECT MIN(LoadTagID) 
                    FROM MyTable)

SELECT * INTO #Table2 
FROM MyTable
WHERE LoadTagID IN (SELECT MAX(LoadTagID) 
                    FROM MyTable)

SELECT * INTO #T3
FROM ( SELECT * FROM #Table1 T1
       UNION ALL
       SELECT * FROM #Table2 T2
     ) A

SELECT  #T3.Date,
        #T3.Job,
        #T3.LoadTagID,
        #T3.Origination
FROM #T3
LEFT JOIN #Table1 T1
ON T1.Job=#T3.Job
WHERE T1.Job IS NOT NULL

答案 1 :(得分:0)

INSERT INTO #LTTB1
SELECT 
    MAX(LoadTagID) AS LoadTagID, 
    JobNumber,
    MAX(EnteredDateTime) AS EnteredDateTime, 
    MAX(Origination) AS Origination
FROM MyTable
WHERE CONVERT (Date, EnteredDateTime) >= CONVERT (Date, GETDATE()-10) --Gets the last 10 days.  
GROUP BY JobNumber ORDER BY JobNumber

INSERT INTO #LTTB2
select LoadTagID
,JobNumber
,EnteredDateTime
,Origination from (
select *, ROW_NUMBER() Over(partition by jobnumber order by EnteredDateTime) l
from MyTable
Where CONVERT (Date, EnteredDateTime) >= CONVERT (Date, GETDATE()-60) 
)lk
where lk.l=1

INSERT INTO #LTTB3
SELECT    L.LoadTagID AS LoadTagIDL 
    , L.JobNumber  AS JobNumberL
    , L.EnteredDateTime AS EnteredDateTimeL
    , L.Origination AS OriginationL  
    , B.LoadTagID, B.JobNumber, B.EnteredDateTime, B.Origination 
FROM #LTTB1 B  --MAX
INNER JOIN #LTTB2 L ON B.JobNumber = L.JobNumber


Select * From MyTable
Select * From #LTTB3

--I hope, your prob has been solved now..