Daily Snippet, Developer to developer

Subquery Problem

Saya punya sub query seperti di bawah :
SELECT ID_GD,
NO_GARDU, (SELECT DISTINCT TGD.PASANG_TRAFO
FROM ATRAFO_GD TGD
WHERE TGD.ID_GD=GD.ID_GD) PASANG_TRAFO,
JML_TIANG, ALAMAT, KAPASITAS_MAX FROM AGARDU_DISTRIBUSI GD Ketika saya coba untuk insert resultset hasil query ke atas dengan script ini:
INSERT INTO DW_GD_ALL_UNIT (ID_GD, NO_GARDU, PASANG_TRAFO, JML_TIANG, ALAMAT, KAPASITAS_MAX) SELECT ID_GD,
NO_GARDU, (SELECT DISTINCT TGD.PASANG_TRAFO
FROM ATRAFO_GD TGD
WHERE TGD.ID_GD=GD.ID_GD) PASANG_TRAFO,
JML_TIANG, ALAMAT, KAPASITAS_MAX FROM AGARDU_DISTRIBUSI GD
It's not work. Subquery returns more than one row. Solusinya disarankan untuk tidak menggunakan DISTINCT dalam subquery.
Ganti dengan fungsi MAX().

SELECT ID_GD,
NO_GARDU, (SELECT MAX(TGD.PASANG_TRAFO)
FROM ATRAFO_GD TGD
WHERE TGD.ID_GD=GD.ID_GD) PASANG_TRAFO,
JML_TIANG, ALAMAT, KAPASITAS_MAX FROM AGARDU_DISTRIBUSI GD

Standard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s