2017-09-25 56 views
0

我必須爲CAR Wash公司開發新網站,該公司擁有三臺清潔汽車,從我的系統接收訂單,每臺汽車每天最多訂單爲5個訂單,所以我應在同一車行的每輛車旁增加5個綠地。 (EX)如果汽車在綠色背景的兩種秩序,使兩個領域有背景的黃色和其他三個領域根據行中的第一個字段加載行數據

我有兩個表

  1. 汽車
  2. 訂單

車表

ID CarName 
1 Car1 
2 Car2 
3 Car3 

訂單表

ID ClientName Phone  Date       CarID 
1  Jack  11111  22-7-2017 10:22:00 AM   2 
2  Susan  22222  22-7-2017 01:30:00 PM   2 
3  Hany  33333  22-7-2017 10:22:00 AM   3 
4  Karim  44444  21-7-2017 08:22:00 AM   1 
5  Halaa  55555  22-7-2017 05:22:00 PM   3 

我想根據從dateTimePicker的一天的選擇,所以我會選擇22-07

Cars       Orders 

car1   Empty  Empty  Empty Empty Empty 
car2   Jack(111111) Susan(22222) Empty Empty Empty 
car3   Hany(333333) HalaSusan(55555) Empty Empty Empty 

所以,如果可以怎樣做呢訂單,每天一列選擇所有的訂單,每車我使用GridView和什麼是最好的查詢

+0

這樣做的一個相當標準的方法是使用['STUFF FOR XML PATH('')'](https://stackoverflow.com/search?q=STUFF+FOR+XML+PATH) – ZLK

回答

0

下應該做的伎倆......

IF OBJECT_ID('tempdb..#Vehicle', 'U') IS NOT NULL 
DROP TABLE #Vehicle; 
GO 
CREATE TABLE #Vehicle (
    VehicleID INT NOT NULL PRIMARY KEY CLUSTERED, 
    VehicleName VARCHAR(10) NOT NULL 
    ); 
GO 
INSERT #Vehicle (VehicleID, VehicleName) VALUES 
    (1, 'Car1'), (2, 'Car2'), (3, 'Car3'); 
GO 
IF OBJECT_ID('tempdb..#Orders', 'U') IS NOT NULL 
DROP TABLE #Orders; 
GO 
CREATE TABLE #Orders (
    OrderID INT NOT NULL PRIMARY KEY CLUSTERED, 
    ClientName VARCHAR(15) NOT NULL, 
    PhoneNumber VARCHAR(10) NOT NULL, 
    ServiceDate DATETIME NOT NULL, 
    VehicleID INT NOT NULL -- create FK refference back to Vehicle.VehicleID) 
    ); 
GO 
-- create an index to support the row_number() function... 
-- The sort cost is greater than the difference between a seek & scan... 
CREATE NONCLUSTERED INDEX ix_Orders_VehicleID_ServiceDate 
    ON #Orders (VehicleID, ServiceDate) 
    INCLUDE (ClientName, PhoneNumber); 
GO 
INSERT #Orders (OrderID, ClientName, PhoneNumber, ServiceDate, VehicleID) VALUES 
    (1, ' Jack ', '11111', '07-22-2017 10:22:00 AM', 2), 
    (2, ' Susan', '22222', '07-22-2017 01:30:00 PM', 2), 
    (3, ' Hany ', '33333', '07-22-2017 10:22:00 AM', 3), 
    (4, ' Karim', '44444', '07-21-2017 08:22:00 AM', 1), 
    (5, ' Halaa', '55555', '07-22-2017 05:22:00 PM', 3); 
GO 

--SELECT * FROM #Vehicle v; 
--SELECT * FROM #Orders o; 

--======================================================================= 
DECLARE @WorkDate DATE = '2017-07-22'; 

WITH 
    cte_AddRN AS (
     SELECT 
      o.OrderID, o.ClientName, o.PhoneNumber, o.ServiceDate, o.VehicleID, 
      RN = ROW_NUMBER() OVER (PARTITION BY o.VehicleID ORDER BY o.ServiceDate) 
     FROM 
      #Orders o 
     WHERE 
      CAST(o.ServiceDate AS DATE) = @WorkDate 
     ) 
SELECT 
    VehicleName = MAX(v.VehicleName), 
    Order1 = MAX(CASE WHEN ar.RN = 1 THEN ar.ClientName + ' (' + ar.PhoneNumber + ')' ELSE '' END), 
    Order2 = MAX(CASE WHEN ar.RN = 2 THEN ar.ClientName + ' (' + ar.PhoneNumber + ')' ELSE '' END), 
    Order3 = MAX(CASE WHEN ar.RN = 3 THEN ar.ClientName + ' (' + ar.PhoneNumber + ')' ELSE '' END), 
    Order4 = MAX(CASE WHEN ar.RN = 4 THEN ar.ClientName + ' (' + ar.PhoneNumber + ')' ELSE '' END), 
    Order5 = MAX(CASE WHEN ar.RN = 5 THEN ar.ClientName + ' (' + ar.PhoneNumber + ')' ELSE '' END) 
FROM 
    #Vehicle v 
    LEFT JOIN cte_AddRN ar 
     ON v.VehicleID = ar.VehicleID 
GROUP BY 
    v.VehicleID; 

結果...

VehicleName Order1   Order2   Order3   Order4   Order5 
----------- --------------- --------------- --------------- --------------- --------------- 
Car1                   
Car2   Jack (11111) Susan (22222)         
Car3   Hany (33333) Halaa (55555) 
相關問題