2016-04-05 11 views
0

Sorunun muhtemelen çok belirsiz ve ilk bakışta anlaşılması zor olduğunu biliyorum, ve 30 dakika uygun bir başlık hakkında düşündüm. Ancak veritabanı bilgim çok kısıtlı, bu yüzden henüz kendimi düzgün bir şekilde formüle etmekte zorlanıyorum.Belirli satırın parçası olan öznitelik örneklerinden MIN değeri olan SELECT özniteliği?

enter image description here

ve sistemin yapılmış bir ER diyagramı:

Aşağıdaki Şu anda yapıyorum bir okul ödevi, bir parçasıdır

elde etmek çalışıyorum budur enter image description here

Yapmaya çalıştığım şey, en düşük miktarda (akım stoku) olan bilgisayar sistemindeki bileşen miktarını seçmekti, böylece veri kümesinde yazdırıyorum, tam olarak kaç tane olduğunu belirleyebiliyorum. mağazada satılan her bilgisayar sistemi Bilgisayar sisteminden oluşan herhangi bir bileşenin en düşük akım miktarına dayanmaktadır.

Bunu yapmak için şu anda çalıştığım sorgu budur, ancak her düzeltmeye çalıştığımda miktarın belirsiz ve diğer hatalarla karşılaştığı birden çok sorunla karşılaştım. Dersten bir düzine arkadaşa danıştım, ama şanssız.

SELECT 
computer_system.NAME, 
cpu.name as cpu, 
gpu.name as gpu, 
board.name as mainboard, 
pccase.name as pc_case, 
ram.name as ram, 
component.quantity as qty, 
(cpu.price *1.3+ board.price*1.3 + pccase.price*1.3 + ram.price*1.3 + gpu.price*1.3) as computer_system_price 

FROM computer_system, component 
join component cpu on cpu.id = computer_system.cpu 
join component gpu on gpu.id = computer_system.gpu 
join component board on board.id = computer_system.mainboard 
join component pccase on pccase.id = computer_system.pc_case 
join component ram on ram.id = computer_system.ram 
JOIN component qty ON qty.quantity = (SELECT MIN(component.quantity) FROM component WHERE component.id IN 
       (computer_system.pc_case, 
       computer_system.mainboard, 
       computer_system.cpu, 
       computer_system.gpu, 
     computer_system.ram)) 

cevap

0

şu kod benim için sabit:

SELECT computer_system.name AS "System name", 
     cpu.name    AS "CPU", 
     gpu.name    AS "GPU", 
     pc_case.name   AS "Case", 
     mainboard.name  AS "Mainboard", 
     ram.name    AS "RAM", 
     FLOOR((cpu.price + mainboard.price + pc_case.price + ram.price + coalesce(gpu.price, 0))*1.3/100)*100+99 AS "System price", 
     SYSTEM.maxamount 

FROM computer_system 
     join (SELECT name, 
        id, 
        price 
      FROM component) AS cpu 
     ON cpu.id = computer_system.cpu 
    left join (SELECT name, 
        id, 
        price 
      FROM component) AS gpu 
     ON coalesce(gpu.id,0) = computer_system.gpu 
     join (SELECT name, 
        id, 
        price 
      FROM component) AS pc_case 
     ON pc_case.id = computer_system.pc_case 
     join (SELECT name, 
        id, 
        price 
      FROM component) AS mainboard 
     ON mainboard.id = computer_system.mainboard 
     join (SELECT name, 
        id, 
        price 
      FROM component) AS ram 
     ON ram.id = computer_system.ram 
     join (SELECT computer_system.name, 
        Min(component.quantity) AS maxamount 
      FROM computer_system, 
        component 
      WHERE computer_system.cpu = component.id 
        OR computer_system.gpu = component.id 
        OR computer_system.mainboard = component.id 
        OR computer_system.pc_case = component.id 
        OR computer_system.ram = component.id 
      GROUP BY computer_system.name) AS SYSTEM 
     ON SYSTEM.name = computer_system.name; 
İlgili konular