| |

VerySource

 Forgot password?
 Register
Search
View: 1106|Reply: 7

What's wrong with this sql statement?

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-27 15:00:01
| Show all posts |Read mode
SELECT dbo.Orders.Id, dbo.Orders.OrderDate, dbo.Orders.Journal, dbo.Orders.Billing,
      Billing.Name AS BillingName, Billing.Email AS BillingEmail,
      Billing.Address + ',' + Billing.Region AS BillingAddress, Billing.Zip AS BillingZip,
      Billing.Phone AS BillingPhone, dbo.Orders.Shipping,
      Shipping.Name AS ShippingName, Shipping.Email AS ShippingEmail,
      Shipping.Address + ',' + Shipping.Region AS ShippingAddress,
      Shipping.Zip AS ShippingZip, Shipping.Phone AS ShippingPhone,
      dbo.Orders.Payment, dbo.Payment.Holder, dbo.Payment.Credit, dbo.Payment.Expire,
      COUNT (dbo.Details.ProductId) AS Quantity, SUM (dbo.Product.Price) AS Total,
      dbo.Orders.Status
FROM dbo.Orders INNER JOIN
      dbo.Details ON dbo.Orders.Id = dbo.Details.OrderId INNER JOIN
      dbo.Contact AS Shipping ON dbo.Orders.Shipping = Shipping.Id INNER JOIN
      dbo.Contact AS Billing ON dbo.Orders.Billing = Billing.Id INNER JOIN
      dbo.Product ON dbo.Details.ProductId = dbo.Product.Id INNER JOIN
      dbo.Payment ON dbo.Orders.Payment = dbo.Payment.Id
GROUP BY dbo.Orders.Id, dbo.Orders.OrderDate, dbo.Orders.Journal, dbo.Orders.Billing,
      Billing.Name, Billing.Email, Billing.Address, Billing.Region, Billing.Zip, Billing.Phone,
      dbo.Orders.Shipping, Shipping.Name, Shipping.Email, Shipping.Address,
      Shipping.Region, Shipping.Zip, Shipping.Phone, dbo.Orders.Payment,
      dbo.Payment.Holder, dbo.Payment.Credit, dbo.Payment.Expire, dbo.Orders.Status


One product order.

Orders table Orders table, which contains Billing, Shiping, Payment fields, records payment contact information, overseas shipping address information. Another Details table records detailed order information, including the corresponding OrderId and product ID ProductId, so that you can use COUNT (dbo.Details.ProductId) AS Quantity, SUM (dbo.Product.Price) AS Total, to separately count the orders The total number and total amount of products included. The question is why the total amount cannot be calculated?
Reply

Use magic Report

0

Threads

114

Posts

69.00

Credits

Newbie

Rank: 1

Credits
69.00

 China

Post time: 2020-2-20 23:15:01
| Show all posts
So many table joins, looked dazzled.
Reply

Use magic Report

1

Threads

9

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 United States

Post time: 2020-2-22 10:45:02
| Show all posts
Eyes are gone
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-2-22 15:45:01
| Show all posts
Query from four tables, the order table is Orders, and the auxiliary table Details records all the selected products, records the ProductId, and already corresponds to the OrderId of the order. Can't query the total price based on the ProductId?
Reply

Use magic Report

0

Threads

126

Posts

73.00

Credits

Newbie

Rank: 1

Credits
73.00

 United States

Post time: 2020-2-22 18:15:01
| Show all posts
dbo.Contact AS Shipping ON dbo.Orders.Shipping = Shipping.Id INNER JOIN
dbo.Contact AS Billing ON dbo.Orders.Billing = Billing.Id INNER JOIN

This is rarely used. . Common is the following
INNER JOIN dbo.Contact ON Orders.Shipping = Contact.Id AND Orders.Billing = Contact.Id

Also, see if the NULL value turns into 0 or ...
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-2-23 16:00:01
| Show all posts
Isn't this the main reason?
The key is COUNT (dbo.Details.ProductId) AS Quantity, SUM (dbo.Product.Price) AS Total,
This problem, I can remove SUM (dbo.Product.Price) AS Total, but there is no total column.
Reply

Use magic Report

0

Threads

126

Posts

73.00

Credits

Newbie

Rank: 1

Credits
73.00

 China

Post time: 2020-3-2 02:15:01
| Show all posts
Can't count the total amount?

Is the result a wrong number or an error?
. . .
Reply

Use magic Report

1

Threads

23

Posts

18.00

Credits

Newbie

Rank: 1

Credits
18.00

 China

Post time: 2020-3-2 09:15:01
| Show all posts
The landlord is too strong, the first time I saw group by followed by so many fields, I have learned!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list