Sql server 添加要查询的列,而不更改现有查询的结果
我有一个类似这样的查询Sql server 添加要查询的列,而不更改现有查询的结果,sql-server,Sql Server,我有一个类似这样的查询 WITH A -- Get a list of unique combinations of Shipmentnumber and ShipmentType AS ( SELECT Distinct f_Shipmentnumber , f_ShipmentType FROM t_shipment, t_pilot where t_Pilot.f_ProviderID='11' and f_date >='2016-01-01' and
WITH A -- Get a list of unique combinations of Shipmentnumber and ShipmentType
AS (
SELECT Distinct
f_Shipmentnumber
, f_ShipmentType
FROM t_shipment, t_pilot where t_Pilot.f_ProviderID='11' and f_date >='2016-01-01' and f_date <='2017-01-01'
)
, B -- Get a list of all those shipmentnumbers values that have more than one shipmentType associated
AS (
SELECT f_Shipmentnumber
FROM A
GROUP BY
f_Shipmentnumber
HAVING COUNT(*) > 1
)
SELECT
A.f_Shipmentnumber,
A.f_ShipmentType
这个表有很多列,但我只想返回上面的数据和其他数据。e、 g.f_装运ID、f_日期、f_引航员ID、f_imo。f_装运ID是唯一的。我希望这能让你更好地了解我在做什么
我在这方面做了更多的工作,我现在离这里越来越近了
WITH A -- Get a list of unique combinations of Shipmentnumber and ShipmentType
AS (
SELECT Distinct
f_Shipmentnumber, f_ShipmentType, f_Shipment_ID, f_date, f_Pilot_ID, f_SailedFrom, f_SailedTo
FROM t_shipment
join t_Pilot pilot on pilot.f_PilotID=t_shipment.f_Pilot_ID
where pilot.f_ProviderID='11' and t_shipment.f_date >='2016-01-01' and t_shipment.f_date <='2017-01-01'
)
, B -- Get a list of all those shipmentnumbers values that have more than one shipmentType associated
AS (
SELECT f_Shipmentnumber
FROM A
GROUP BY
f_Shipmentnumber
HAVING COUNT(*) > 1
)
SELECT
A.f_Shipment_ID, A.f_Pilot_ID, A.f_Shipmentnumber
, A.f_ShipmentType, A.f_date, f_SailedFrom, f_SailedTo
FROM A
JOIN B
ON A.f_Shipmentnumber = B.f_Shipmentnumber
此时,我只想显示具有重复ShipmentNumber和不同装运类型的行,这是我在添加额外字段之前得到的。需要删除具有相同shipmenttype的dupe ShipmentNumber。谢谢 首先,请向我们提供有关您的表以及您正在尝试执行的操作的一些信息。这是一个很好的起点。同时,您可能希望阅读本文并开始使用ANSI-92样式的联接。我有一个名为Shipping的表,表中有f_Shipping number字段,可以多次用于引航,但公司意外地开始重用ShippmentNumber,因此我无法再使用distinct子句计算引航次数,因此,我需要检查装运类型和日期的不同之处。可能您没有提出正确的问题,请添加表架构、样本数据和所需结果。除非您愿意提供有关查询的详细信息,否则任何人都无法提供帮助。您向我们展示了一个不太有效的查询,并向我们展示了所需的输出。但我们不知道你的数据是什么样子的。您发布的查询不完整。在此之后,您有一个cte和一个部分查询。如果您发布足够的详细信息让我们提供帮助,这将非常简单。
WITH A -- Get a list of unique combinations of Shipmentnumber and ShipmentType
AS (
SELECT Distinct
f_Shipmentnumber, f_ShipmentType, f_Shipment_ID, f_date, f_Pilot_ID, f_SailedFrom, f_SailedTo
FROM t_shipment
join t_Pilot pilot on pilot.f_PilotID=t_shipment.f_Pilot_ID
where pilot.f_ProviderID='11' and t_shipment.f_date >='2016-01-01' and t_shipment.f_date <='2017-01-01'
)
, B -- Get a list of all those shipmentnumbers values that have more than one shipmentType associated
AS (
SELECT f_Shipmentnumber
FROM A
GROUP BY
f_Shipmentnumber
HAVING COUNT(*) > 1
)
SELECT
A.f_Shipment_ID, A.f_Pilot_ID, A.f_Shipmentnumber
, A.f_ShipmentType, A.f_date, f_SailedFrom, f_SailedTo
FROM A
JOIN B
ON A.f_Shipmentnumber = B.f_Shipmentnumber
f_Shipment_ID f_Pilot_ID f_Shipmentnumber f_ShipmentType f_date f_SailedFrom f_SailedTo
198084 216 286506 2 2016-01-05 11:00:00.000 Kadetrenden 1 Skagen 3 -- P/B
198504 1 286506 2 2016-01-05 11:00:00.000 Kadetrenden 1 Skagen 3 -- P/B
200266 77 286511 1 2016-01-07 10:00:00.000 Langeland T -- P/B Skagen 3 -- P/B
198538 275 286511 2 2016-01-08 11:00:00.000 Kadetrenden 1 Skagen 3 -- P/B
198130 160 286524 1 2016-01-07 02:00:00.000 Korsør -- P/B Nyborg Havn
198134 120 286524 1 2016-01-06 04:00:00.000 Langeland H -- P/B Nyborg -- P/B
199111 45 286524 1 2016-01-06 03:00:00.000 Langeland H -- P/B Nyborg -- P/B
198081 271 286531 2 2016-01-06 05:00:00.000 Korsør -- P/B Alfa/4 -- P/B