情境:
我有一个订单是完整(1)或不完整(0)的情况。我有2个查询可以根据添加它们的工作人员计算完成/不完整的订单。
查看:
CREATE VIEW CompleteOrdersView AS
SELECT
Staffname,
COUNT(OrderComplete) AS OrdersComplete
FROM OrderTable
WHERE OrderComplete = 1
GROUP BY Staffname
CREATE VIEW InCompleteOrdersView AS
SELECT
Staffname,
COUNT(OrderComplete) AS OrdersComplete
FROM OrderTable
WHERE OrderComplete = 0
GROUP BY Staffname
问题:
要将此数据与2个当前视图进行比较,这意味着我需要创建第3个视图,而不是1.是否可以将这两个结果都放到1个视图中,然后执行以下3个任务:
表格和数据:
CREATE TABLE OrderTable (OrderID INT IDENTITY (1,1) NOT NULL,
Staffname VARCHAR (50),
OrderComplete BIT)
ALTER TABLE OrderTable ADD CONSTRAINT OrderID_PK PRIMARY KEY (OrderID)
INSERT INTO OrderTable VALUES ('Joe Bloggs', 0)
INSERT INTO OrderTable VALUES ('Joe Bloggs', 0)
INSERT INTO OrderTable VALUES ('Joe Bloggs', 0)
INSERT INTO OrderTable VALUES ('Joe Bloggs', 1)
INSERT INTO OrderTable VALUES ('Joe Bloggs', 1)
INSERT INTO OrderTable VALUES ('Joe Bloggs', 1)
INSERT INTO OrderTable VALUES ('Joe Bloggs', 1)
INSERT INTO OrderTable VALUES ('Joe Bloggs', 1)
INSERT INTO OrderTable VALUES ('Joe Bloggs', 1)
INSERT INTO OrderTable VALUES ('Joe Bloggs', 1)
以上问题的示例输出:
Staffname OrdersComplete InCompleteOrders TotalOrders CompletePercentage InCompletePercentage
Joe Bloggs 7 3 10 70% 30%
谢谢你对此的帮助
答案 0 :(得分:2)
为什么你需要这么多观点?一个简单的查询工作:
SET @trimString = SUBSTRING(@have, 0, LEN(@have)-1)