15,886,199 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View Python questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
Comments by 7prince (Top 15 by date)
7prince
15-Mar-13 21:44pm
View
This error is pointing to 'AddLineItem' procedure that also EXECUTES GETDETAIL procedure.
Currently got stuck in this error:
Msg 515, Level 16, State 2, Procedure AddLineItem, Line 33
Cannot insert the value NULL into column table Salesdb.test.ORDERITEMS column does not allow nulls. INSERT fails.
The statement has been terminated.
/*
--------------------------------------------------------------------------------
The TRANSACTION, this procedure calls GetNewDetail and performs an INSERT
to the ORDERITEMS table which in turn performs an UPDATE to the INVENTORY table.
Error handling determines COMMIT/ROLLBACK.
--------------------------------------------------------------------------------
*/
IF EXISTS (SELECT name FROM SYSOBJECTS WHERE name = 'AddLineItem')
BEGIN DROP PROCEDURE AddLineItem; END;
GO
CREATE PROCEDURE AddLineItem --with orderid, partid and qty input
@vOrderId VARCHAR(10),
@vPartId SMALLINT,
@vQty SMALLINT,
@Stockqty SMALLINT,
@vNewDetail SMALLINT OUTPUT
AS
DECLARE @vErr SMALLINT -- Error indicator
DECLARE @vErrStr VARCHAR(80) -- Hold error message
BEGIN TRANSACTION --- this is the only BEGIN TRANSACTION
SET @vErr = 0
-- Check that requested Order No exists
SELECT @vOrderid = Orderid
FROM ORDERITEMS
WHERE OrderID = @vOrderID
--Call
EXECUTE GetNewDetail @vOrderId, @vNewDetail OUTPUT;
IF (@@ERROR <> 0 OR @@ROWCOUNT = 0)
BEGIN
SET @vErr = 1
SET @vErrStr = 'ERROR1 in AddlineItemSP: Order Number ' + @vOrderID +
' does not exist. Unable to complete order.'
END;
ELSE
BEGIN
-- Insert order record into OrderItems table
INSERT INTO ORDERITEMS
(orderid, partid, qty)
VALUES (@vorderid,@vpartid,@vqty);
--ERROR HANDLING
IF (@@ERROR <> 0)
BEGIN
-- Error somewhere during the Insert
SET @vErr = 2
SET @vErrStr = 'ERROR2 in AddlineItemSP: Unable to Insert Order No ' + @vOrderID
END
--END IF
END;
-- END IF
-- The use of the system procedure RAISERROR allows identifying errors that occur elsewhere
-- and doing any rollback/commit here, not in triggers
IF (@vErr <> 0)
BEGIN
PRINT @vErrStr
PRINT 'Order No ' + @vOrderid +
' for ' + CONVERT(VARCHAR(4),@vQty) +
' terminated with failure.'
ROLLBACK TRANSACTION
END;
ELSE
BEGIN
PRINT @vErrStr
PRINT 'Order No ' + @vOrderid +
' for ' + CONVERT(VARCHAR(4),@vQty) +
' completed successfully'
COMMIT TRANSACTION
END;
-- END IF
-- END TRANSACTION; --leave it commented out
--END;
GO
-- No AddLineItem tests, saved for main block testing
--EXECUTE AddLineItem 1007,1001,10,4
GO
7prince
9-Feb-13 20:09pm
View
the query gets the TopProfitable salesperson. so instead of using ROWNUM, How can I do the somEthing using RANK or DENSE_RANK ?
7prince
8-Feb-13 23:13pm
View
Here is the latest revision: But, I am still getting 0 rows updated message.
Update salespersons set salespersons.salary = salespersons.salary * 1.12 where salespersons.empid =
(SELECT salespersons.empid
FROM (SELECT *
FROM (SELECT *
FROM (SELECT s.empid employeeid,
s.ename employeename,
SUM(i.price * oi.qty) - s.salary Topprofit
FROM salespersons s
LEFT JOIN orders o ON s.empid = o.empid
LEFT JOIN orderitems oi ON o.orderid = oi.orderid
LEFT JOIN inventory i ON oi.partid = i.partid
GROUP BY s.empid, s.ename, s.salary)
ORDER BY Topprofit DESC)
WHERE ROWNUM = 2
ORDER BY Topprofit ASC)
WHERE ROWNUM = 1 and Topprofit is not null);
7prince
8-Feb-13 23:13pm
View
Here is the latest revision: But, I am still getting 0 rows updated message.
Update salespersons set salespersons.salary = salespersons.salary * 1.12 where salespersons.empid =
(SELECT salespersons.empid
FROM (SELECT *
FROM (SELECT *
FROM (SELECT s.empid employeeid,
s.ename employeename,
SUM(i.price * oi.qty) - s.salary Topprofit
FROM salespersons s
LEFT JOIN orders o ON s.empid = o.empid
LEFT JOIN orderitems oi ON o.orderid = oi.orderid
LEFT JOIN inventory i ON oi.partid = i.partid
GROUP BY s.empid, s.ename, s.salary)
ORDER BY Topprofit DESC)
WHERE ROWNUM = 2
ORDER BY Topprofit ASC)
WHERE ROWNUM = 1 and Topprofit is not null);
7prince
8-Feb-13 15:17pm
View
I ran the query and for some reason it say 0 row updated. Not sure why?
Update salespersons set salespersons.salary = salespersons.salary * 1.12 where salespersons.empid =
(SELECT salespersons.empid
FROM (SELECT *
FROM (SELECT *
FROM (SELECT s.empid employeeid,
s.ename employeename,
SUM(i.price * oi.qty) - s.salary Topprofit
FROM salespersons s
LEFT JOIN orders o ON s.empid = o.empid
LEFT JOIN orderitems oi ON o.orderid = oi.orderid
LEFT JOIN inventory i ON oi.partid = i.partid
GROUP BY s.empid, s.ename, s.salary)
ORDER BY Topprofit DESC)
WHERE ROWNUM = 2
ORDER BY Topprofit ASC)
WHERE ROWNUM = 1 and Topprofit is not null);
7prince
8-Feb-13 11:03am
View
Thanks. How can I do that?
7prince
20-Jan-13 22:45pm
View
that would be fine. or a subquery. it doesn't matter. Thanks.
7prince
20-Jan-13 22:38pm
View
basicall the query should include:
An order (orderid) is to contain gadgets and gizmos and nothing else.
At a minimum, then, an order would have one gadget and one gizmo, period.
But an order could contain any number of different kinds of gadgets AND any number of different kinds of gizmos as long as it had NO OTHER PARTS. Meaning, an orderid can have gadget or gizmo and the same orderid can have many other descriptions. I need only the orderids that has only gadget and gismo and those orderids not tied to any other description.
7prince
20-Jan-13 21:07pm
View
what about including the INVENTORY table for all partids and descriptions that are NOT gadget and are NOT gizmo and need to get the orderids that do NOT contain those parts. Basicall query should give out 3 conditions:
1. those with gadgets
2. AND those with gizmos
3. AND those not with not gadget or gizmos. --- I need to get this one as well and add to the subquery below.
7prince
20-Jan-13 20:46pm
View
yes. your query gives 2 rows. Should give one.
7prince
20-Jan-13 11:37am
View
adding this query: SELECT SUM(ORDERITEMS.qty*INVENTORY.PRICE) / COUNT(DISTINCT ORDERITEMS.ORDERID) AS 'OrderItems_Average'
AS 'OrderItems_Average'
FROM ORDERITEMS LEFT OUTER JOIN
INVENTORY ON ORDERITEMS.PARTID = INVENTORY.PARTID RIGHT OUTER JOIN
ORDERS ON ORDERITEMS.ORDERID = ORDERS.ORDERID
to this one as a subquery
SELECT SUM(ORDERITEMS.qty*INVENTORY.PRICE) / COUNT(DISTINCT ORDERS.ORDERID) AS 'Orders_Average'
AS 'OrderItems_Average'
FROM ORDERITEMS LEFT OUTER JOIN
INVENTORY ON ORDERITEMS.PARTID = INVENTORY.PARTID RIGHT OUTER JOIN
ORDERS ON ORDERITEMS.ORDERID = ORDERS.ORDERID
7prince
19-Jan-13 18:30pm
View
I adjusted it as follows: and got qty >=10 and qty >=5 orders included but this time qty >=5 orders column for 'Total Final Price of Each Product After Discount' shows NULL. What is it that I am doing wrong?
SELECT ORDERS.orderid,
INVENTORY.partid,
Inventory.description,
ORDERITEMS.qty,
Inventory.price,
(OrderItems.qty * Inventory.price) as 'Total Original Price',
--If an order contains five or more units of a given product, give a 2% discount for that line item.
(OrderItems.qty * Inventory.price *
CASE WHEN OrderItems.qty >=5 THEN 0.05 ELSE 0.1 END) as 'Total Discounted Value',
--If an order contains ten or more units of a given product, give a 5% discount on that line item.
(OrderItems.qty * Inventory.price *
CASE WHEN qty >=10 THEN 0.95/0.9 END) as 'Total Final Price of Each Product After Discount'
FROM orders, inventory, orderitems
WHERE ORDERS.ORDERID = ORDERITEMS.ORDERID AND ORDERITEMS.PARTID = INVENTORY.PARTID
AND ORDERITEMS.qty >=5
ORDER BY ORDERS.orderid, INVENTORY.partid
7prince
19-Jan-13 16:11pm
View
yes. That is right. I switched it to SELECT
(SUM(Inventory.price) / COUNT(Orders.orderid)) AS Orders_Average,
(SUM(Inventory.price) / COUNT(OrderItems.orderid)) AS OrderItems_Average
But inner join doesn't work as it gives the same amount for both averages.
P.S. The number of orders in the ORDERS table is different from the number of orders in the ORDERITEMS table.
so after modifying the query:
SELECT SUM(Inventory.price) / COUNT(Orders.orderid) AS Orders_Average, SUM(Inventory.price) / COUNT(OrderItems.orderid) AS OrderItems_Average
FROM OrderItems LEFT OUTER JOIN
Inventory ON OrderItems.partid = Inventory.partid RIGHT OUTER JOIN
Orders ON OrderItems.orderid = Orders.orderid
it gives slightly differnt averages now. Any feedback if this one is correct or not/ thank you
7prince
18-Jan-13 23:13pm
View
yes that is correct. I am asking average value for orders, and average value for order items.
I would like to get the output in this format:
two columns: "Orders Average" and "OrderItems Average") or in one column with each output in its own row. Thank you.
7prince
18-Jan-13 13:05pm
View
I cahnged ny code to: How can I show "average dollar value for Orders" and "average dollar value for OrderItems" side by side in columns.
SELECT (COUNT(Orders.orderid) / SUM(Inventory.price)) AS Orders_Average
FROM ORDERITEMS LEFT OUTER JOIN
Inventory ON OrderItems.partid = Inventory.partid RIGHT OUTER JOIN
Orders ON OrderItems.orderid = Orders.orderid
WHERE NOT EXISTS
(SELECT *
FROM ORDERITEMS
WHERE OrderItems.orderid <> Orders.orderid
GROUP BY ORDERITEMS.orderid)
Show More