在另一个表

时间:2017-12-19 11:19:55

标签: sql sql-server

这里是表DBtrans,其中包含不同行中的相同日期

+-------+-------+---------------------------+
|  ID   | dbID  |           pDate           |
+-------+-------+---------------------------+
| 14969 |  4982 |  2010-01-06 00:00:00.000  |
| 14970 |  4982 |  2010-01-13 00:00:00.000  |
| 14971 |  4982 |  2010-01-20 00:00:00.000  |
| 14972 |  4982 |  2010-01-27 00:00:00.000  |
+-------+-------+---------------------------+

虽然我有点了解SQL的相关知识,但我尝试的却是..

select
    DailyBooking.ID
    ,CONCAT( pDate,' , ', pDate) AS InsDates
    ,InvNo
    ,AdCaption
    ,CONCAT( AdCM,'x', AdCOL) AS SIZE
    ,NetAmt
    ,RecievedAmount
From DailyBooking
inner join DBTrans
    on DailyBooking.ID  = DBTrans.dbID
ORDER BY DBTrans.dbID

结果:

enter image description here

取代CONCAT专栏!我想通过此ID

在DDtrans中显示所有现有日期

请帮助

1 个答案:

答案 0 :(得分:0)

我认为这可以通过以下查询来实现

IF OBJECT_ID ('dbtrans') IS NOT NULL
    DROP TABLE dbtrans
GO

CREATE TABLE dbtrans
    (
    ID    INT NULL,
    dbID  INT NULL,
    pdate DATETIME NULL
    )
GO


INSERT INTO dbtrans (ID, dbID, pdate) VALUES (1, 4982, '2017-10-10')
GO
INSERT INTO dbtrans (ID, dbID, pdate) VALUES (2, 4982, '2017-10-11')
GO
INSERT INTO dbtrans (ID, dbID, pdate) VALUES (3, 4982, '2017-10-12')
GO
INSERT INTO dbtrans (ID, dbID, pdate) VALUES (4, 4982, '2017-10-13')
GO
INSERT INTO dbtrans (ID, dbID, pdate) VALUES (5, 4982, '2017-10-14')
GO
INSERT INTO dbtrans (ID, dbID, pdate) VALUES (6, 4982, '2017-10-15')
GO
INSERT INTO dbtrans (ID, dbID, pdate) VALUES (7, 4982, '2017-10-15')
GO
   select dbid,
    stuff((
    SELECT
    ',' + CONVERT(varchar(30), t.pdate, 120)

        from dbtrans  t
        where t.dbID = uk.dbID  
        for xml path('')
    ),1,1,'') as datelist
from dbtrans uk
group by dbid