troubleshooting Question

Order and orderDetail combined

Avatar of Peter Nordberg
Peter NordbergFlag for Sweden asked on
Microsoft SQL ServerSQL
1 Comment1 Solution376 ViewsLast Modified:

I would like to combine seceral tables in a query. The tables are as follows:
orders (orderID, orderDate, feePercentage, customerID, paymentTypeID)
orderDetail (orderID, price, taxValue, quantity)
customers(customerID, FirstName, LastName)
orderAdminFeeDetail (orderID, dInvoiceID)
bklPaymentType(paymentTypeID, paymentType)

I've done a query like this:
SELECT        ordersAdminFeeDetail.orderID, orders.orderDate, customer.FirstName + ' ' + customer.Lastname AS Name, orders.feePercentage / 100 AS p,
                         ordersAdminFeeDetail.dInvoiceID, bklPaymentType.PaymentType, SUM(orderDetail.price) AS pris,
                         orderDetail.quantity, SUM((orderDetail.price * orderDetail.quantity) * (orderDetail.taxValue / 100)) AS tax
FROM            ordersAdminFeeDetail INNER JOIN
                         orders ON ordersAdminFeeDetail.orderID = orders.orderID INNER JOIN
                         customer ON orders.customerID = customer.customerID INNER JOIN
                         orderDetail ON orders.orderID = orderDetail.orderID INNER JOIN
                         bklPaymentType ON orders.PaymentTypeID = bklPaymentType.paymentTypeID
GROUP BY ordersAdminFeeDetail.orderID, orders.orderDate, ordersAdminFeeDetail.dInvoiceID, customer.FirstName, customer.Lastname, orders.feePercentage,
                         bklPaymentType.PaymentType, orders.handlingFee, orders.shipmentFee, orderDetail.quantity, orderDetail.taxValue
ORDER BY ordersAdminFeeDetail.orderID

The problem with that query is that when orderDetail has more than one items per orderID the orderID shows up more than one time in the resultset. I would like to have the resultset lso that each orderID only shows up once with all the data that's specified above in that one row. How can I accomplish this?


Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros