Avatar of emi_sastra
emi_sastra
 asked on

SQL syntax

Hi All,

I have below queries:

1. Result one row.
2. Exception. Sub query return more than 1 value ?

What's the problem?

Thank you.


1 . First Query
 
SELECT     A.Nomor_PO, B.Tgl_PO 
FROM         TDPOBELI2009 AS A LEFT OUTER JOIN
                      THPOBELI2009 AS B ON A.Nomor_PO = B.Nomor_PO
WHERE     (A.Nomor_PO = '3115 ')
 
2. Second Query
 
SELECT     A.Nomor_PO, B.Tgl_PO, CASE WHEN YEAR(Tgl_PO) = 2009 THEN
                          (SELECT     Harga_Satuan
                            FROM          SCM_INV..TDPOBELI2009 AS Z
                            WHERE      Z.Nomor_PO = Nomor_PO AND Z.BahanCode = BahanCode) END AS Harga_Satuan_Beli
FROM         TDPOBELI2009 AS A LEFT OUTER JOIN
                      THPOBELI2009 AS B ON A.Nomor_PO = B.Nomor_PO
WHERE     (A.Nomor_PO = '3115 ')

Open in new window

SQL

Avatar of undefined
Last Comment
emi_sastra

8/22/2022 - Mon
Pratima

try this
SELECT     A.Nomor_PO, B.Tgl_PO, CASE WHEN YEAR(Tgl_PO) = 2009 THEN
                          (SELECT    top 1  Harga_Satuan
                            FROM          SCM_INV..TDPOBELI2009 AS Z
                            WHERE      Z.Nomor_PO = Nomor_PO AND Z.BahanCode = BahanCode) END AS Harga_Satuan_Beli
FROM         TDPOBELI2009 AS A LEFT OUTER JOIN
                      THPOBELI2009 AS B ON A.Nomor_PO = B.Nomor_PO
WHERE     (A.Nomor_PO = '3115 ')

Open in new window

Pratima

SELECT   Harga_Satuan
                            FROM          SCM_INV..TDPOBELI2009 AS Z
                            WHERE      Z.Nomor_PO = Nomor_PO AND Z.BahanCode = BahanCode

This query Migth returning more than one row just make sure it will return only ione row like

SELECT    top 1  Harga_Satuan
                            FROM          SCM_INV..TDPOBELI2009 AS Z
                            WHERE      Z.Nomor_PO = Nomor_PO AND Z.BahanCode = BahanCode
phoenixfire425

you can try adding a GROUPBY clause
SELECT     A.Nomor_PO, B.Tgl_PO, CASE WHEN YEAR(Tgl_PO) = 2009 THEN
                          (SELECT     Harga_Satuan
                            FROM          SCM_INV..TDPOBELI2009 Z
                            WHERE      (Nomor_PO = Nomor_PO) AND (BahanCode = BahanCode)
                            GROUP BY Harga_Satuan) END AS Harga_Satuan_Beli
FROM         SCM_INV..TDPOBELI2009 A LEFT OUTER JOIN
                      dbo.THPOBELI2009 B ON A.Nomor_PO = B.Nomor_PO
GROUP BY A.Nomor_PO, B.Tgl_PO, CASE WHEN YEAR(Tgl_PO) = 2009 THEN
                          (SELECT     Harga_Satuan
                            FROM          SCM_INV..TDPOBELI2009 Z
                            WHERE      (Nomor_PO = Nomor_PO) AND (BahanCode = BahanCode)
                            GROUP BY Harga_Satuan) END
HAVING      (A.Nomor_PO = '3115 ')

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
emi_sastra

ASKER
Hi pratima_mcs,

Why it returns more than one row?
At the first query, it returns one row:

SELECT     A.Nomor_PO, B.Tgl_PO, A.BahanCode
FROM         TDPOBELI2009 AS A LEFT OUTER JOIN
                      THPOBELI2009 AS B ON A.Nomor_PO = B.Nomor_PO
WHERE     (A.Nomor_PO = '3115 ')

Thank you.
emi_sastra

ASKER
Hi phoenixfire425,

Please see my comment to pratima.

I am not suppose to use group, since it returns only one row at first query.

Thank you.
Pratima

have you tried this top 1

SELECT    top 1  Harga_Satuan
                            FROM          SCM_INV..TDPOBELI2009 AS Z
                            WHERE      Z.Nomor_PO = Nomor_PO AND Z.BahanCode = BahanCode

is it working ?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Pratima

Top 1 will make sure that it will retun only one row
phoenixfire425

Typically when you have something returning more than one value then there is some deviation in the data.  Select all of the rows and see if you can see the difference between the two rows.
emi_sastra

ASKER
Yes, Pratima, it works.

But why it shows the message when the actual row only one?

Thank you.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
emi_sastra

ASKER
Hi phoenixfire425,

-Typically when you have something returning more than one value then there is some deviation in the data.  Select all of the rows and see if you can see the difference between the two rows.

I don't get it?

Thank you.
Pratima

Not sure
might for some case it is returning multiple rows
phoenixfire425

Run this and then compare the two rows.  If you see a difference in the columns then thats why you are creating multiple rows.
SELECT     *, CASE WHEN YEAR(Tgl_PO) = 2009 THEN
                          (SELECT     Harga_Satuan
                            FROM          SCM_INV..TDPOBELI2009 AS Z
                            WHERE      Z.Nomor_PO = Nomor_PO AND Z.BahanCode = BahanCode) END AS Harga_Satuan_Beli
FROM         TDPOBELI2009 AS A LEFT OUTER JOIN
                      THPOBELI2009 AS B ON A.Nomor_PO = B.Nomor_PO
WHERE     (A.Nomor_PO = '3115 ')

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Pratima

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
emi_sastra

ASKER
Hi phoenixfire425,

SELECT     *, CASE WHEN YEAR(Tgl_PO) = 2009 THEN
                          (SELECT     Harga_Satuan
                            FROM          SCM_INV..TDPOBELI2009 AS Z
                            WHERE      Z.Nomor_PO = Nomor_PO AND Z.BahanCode = BahanCode) END AS Harga_Satuan_Beli
FROM         TDPOBELI2009 AS A LEFT OUTER JOIN
                      THPOBELI2009 AS B ON A.Nomor_PO = B.Nomor_PO
WHERE     (A.Nomor_PO = '3115 ')


Exception occurs.


SELECT     *
FROM         TDPOBELI2009 AS A LEFT OUTER JOIN
                      THPOBELI2009 AS B ON A.Nomor_PO = B.Nomor_PO
WHERE     (A.Nomor_PO = '3115 ')

Return just one row?

Thank you.
emi_sastra

ASKER
Hi Pratima,

SELECT     A.Nomor_PO, B.Tgl_PO, CASE WHEN YEAR(Tgl_PO) = 2009 THEN
                          (SELECT     COUNT(Harga_Satuan)
                            FROM          SCM_INV..TDPOBELI2009 AS Z
                            WHERE      Z.Nomor_PO = Nomor_PO AND Z.BahanCode = BahanCode) END AS Harga_Satuan_Beli
FROM         TDPOBELI2009 AS A LEFT OUTER JOIN
                      THPOBELI2009 AS B ON A.Nomor_PO = B.Nomor_PO
WHERE     (A.Nomor_PO = '3115 ')

Result :

Harga_Satuan_Beli = 310



SELECT     COUNT(*) AS Count
FROM         TDPOBELI2009
WHERE     (Nomor_PO = '3115                          ')

Result :

Count = 1  

Weird?

Thank you.
SOLUTION
phoenixfire425

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
emi_sastra

ASKER
Hi phoenixfire425,

It has many rows.

Thank you.
Your help has saved me hundreds of hours of internet surfing.
fblack61
phoenixfire425

your welcome
emi_sastra

ASKER
Hi phoenixfire425,

I try below and return one rows.

SELECT     Code, Nomor_PO, BahanCode, Qty_PO, Harga_Satuan, Pct_Disc, Harga_Net, Qty_DO, Qty_SJ, Qty_Fkt, Qty_Retur, Satuan, KdSts, CrtId, CrtDate, UpdId,
                      UpdDate
FROM         TDPOBELI2009 AS Z
WHERE     (Nomor_PO = Nomor_PO) AND (BahanCode = BahanCode) AND (Nomor_PO = '3115 ')

Thank you.
emi_sastra

ASKER
Hi All,

I know the problem now.

SELECT     A.Nomor_PO, B.Tgl_PO, CASE WHEN YEAR(Tgl_PO) = 2009 THEN
                          (SELECT     Harga_Satuan
                            FROM          SCM_INV..TDPOBELI2009 AS Z
                            WHERE      Z.Nomor_PO = A.Nomor_PO AND Z.BahanCode = A.BahanCode) END AS Harga_Satuan_Beli
FROM         TDPOBELI2009 AS A LEFT OUTER JOIN
                      THPOBELI2009 AS B ON A.Nomor_PO = B.Nomor_PO
WHERE     (A.Nomor_PO = '3115 ')


WHERE Z.Nomor_PO = Nomor_PO AND Z.BahanCode = BahanCode

Should be

WHERE  Z.Nomor_PO = A.Nomor_PO AND Z.BahanCode = A.BahanCode

Thank you.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
emi_sastra

ASKER
Thanks to both of you.