db2 query to select the first row fetched - select

I have a query like
UPDATE PRD_PRODUCT_L10N ppl
SET ( CATCHING_PHRASE
, GENERIC_NAME
, INGREDIENTS
, QUANTITY
, DOSE
, NUTRITION_FACTS
, PRODUCT_DESCRIPTION
, PROMOTION_MESSAGE
, MESSAGE
) = (
SELECT distinct CATCHING_PHRASE
, GENERIC_NAME
, INGREDIENTS
, QUANTITY
, DOSE
, NUTRITION_FACTS
, PRODUCT_DESCRIPTION
, PROMOTION_MESSAGE
, MESSAGE
FROM TEMP_UPLOAD_PRODUCT_ATTRIBUTES tupa
INNER JOIN
PRD_PRODUCT pp
ON pp .EISIDENTIFIER = tupa.EISIDENTIFIER
WHERE ppl.PRODUCTGUID = pp.GUID
AND ppl.LOCALEGUID = tupa.LOCALEGUID
)
WHERE EXISTS (
SELECT 0
FROM TEMP_UPLOAD_PRODUCT_ATTRIBUTES tupa
INNER JOIN
PRD_PRODUCT pp
ON pp .EISIDENTIFIER = tupa.EISIDENTIFIER
WHERE ppl.PRODUCTGUID = pp .GUID
AND ppl.LOCALEGUID = tupa.LOCALEGUID
)
the subquery returns more than 1 row and I would like to insert the first selected. How do I do that in DB2 database?
Please advice.
Thanks

Depending on your DB2 version (i think 8 upwards) you can use fetch at your subquery
(select * from table fetch first 1 rows only)
http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2.doc.admin%2Ffrstnrw.htm

Add FETCH FIRST ROW ONLY to your subquery. Search for fetch-first-clause on the page linked for more info. This is for DB2 on Linux/Unix/Windows.
If you're on the Mainframe (v9), then you want this page for more info (or version 10).

Related

How to SELECT only the "nth" record from a query result?

I have a T-SQL query that return X records ordered.
I want to get only on record , for instance, only the 5th record from that result: how ?
Thanks
For that you have to update your query.
I.e in oracle we have rownum that assign rownumber to every row.
You can do like this,
Select * from(
Select a.*, rownum as n from your table) where n = 3;
Something like this.
Try this:
WITH NumberedTable AS
(
SELECT
RowNo = ROW_NUMBER() OVER(ORDER BY <'Order Column here'>)
, *
FROM <'Table Name here'>
)
SELECT *
FROM NumberedTable
WHERE RowNo = <'Record No. here'>

Change column value after left join SQL

I have the following statement:
select distinct x_order.code , x_order.status , x_order.project , project.active , project.code as projectcode
from project
left join x_order on x_order.project = project.code
where status = 'B' and active = 0
Which gives me a nice table with all the records I want to change. I now need to change the values in the 'status' column from B to D in this table. How can I do that?
I tried UPDATE, but to no success.
Not sure of the exact table structures you're using, but would a CTE work for you like this...
;WITH CTE
AS (
select distinct x_order.code as ordercode , x_order.status , x_order.project , project.active , project.code as projectcode
from project
left join x_order on x_order.project = project.code
where status = 'B' and active = 0
)
UPDATE X
SET [status] = 'D'
FROM x_order X
JOIN CTE C
ON X.code = C.ordercode

How to make a Select sentence with acumulate in final column

I'm trying to make a query for acum the sum of one of fields in a new column, but i dont get it in SqlServer 2008 r2
I have the next table:
Fields: id,Codigo,tipo,cantidad
I want to make a query for get the next result
When the field tipo is 2, the acum begins
Is it possible?
Thanks.
finally i found a good sentence for me
with AcumulaCant as (
select
d.idrecno,
d.tipmov,
d.codart,
d.codalm,
d.cant
from movsto d
)
select *,
CantAcum = (
select SUM(cant)
from AcumulaCant c
where c.idrecno <= AcumulaCant.idrecno
and c.codart = AcumulaCant.codart
and c.codalm = AcumulaCant.codalm),
ROW_NUMBER() over(partition by codart,codalm order by idrecno desc) as rn
from AcumulaCant
order by AcumulaCant.codart,AcumulaCant.codalm,AcumulaCant.idrecno desc
With this sentence i get the acum of the sum quantities by ref.
Thanks to all for your comments.

Hive SQL query with empty table

The problem is that when I run the query I got an empty table which is wrong base on the data I have, so is my logic wrong or I'm missing something?
Based on the provided tables write a query in HIV SQL to answer question 1
employees ( employee_num , last_name , first_name , status , hire_date , last_date_worked , job_title , job_code , home_branch )
transactions ( branch_num , contract_num , customer_num , invoice_date ,invoice_num , product_num , sales_amount , employee_num , service_date , system_period )
Question 1:
Show the employees who serviced customers before 1/1/2005. Employee
names must be concatenated to produce this format: LastName,
FirstName. For example, “Doe, John”. The result should have two
columns: employee name and service date.
My answer:
SELECT concat(e.first_name, ', ', e.last_name) AS employee_name, t.service_date FROM employees e
FULL OUTER JOIN transactions t
ON e.employee_num = t.employee_num WHERE t.service_date < '1/1/2005';

Update a column that has duplicates

I am using SQL server 2008R2 and have a table called AirNewformat .This table is mostly searched by ID column which has many duplicates. I wanted to update same table by adding a column called processed where first value of ID is updated as 'N' in processed column and rest of the duplicates updated as 'D' in the processed column. Currently i have all the records updated either as N or D but i want see an update like below example. Any help is greatly appreciated.
ID Processed
--- ---------
12 N
12 D
12 D
13 N
13 D
13 D
Please try this code if you might ( SQL Fiddle for this: http://sqlfiddle.com/#!3/03991/6 ):
Update first record in every ID group as N:
UPDATE
A
SET
A.Processed = 'N'
FROM
(
SELECT ID, Processed, ROW_NUMBER() OVER(ORDER BY ID) AS row_num FROM AirNewformat
) AS A
INNER JOIN
(
SELECT
C.ID,
MIN(C.row_num) AS min_row_num
FROM
(
SELECT ID, ROW_NUMBER() OVER(ORDER BY ID) AS row_num FROM AirNewformat
) AS C
GROUP BY C.ID
) AS B ON A.ID = B.ID AND A.row_num = B.min_row_num;
Update all the rest of records as D:
UPDATE
AirNewformat
SET
AirNewformat.Processed = 'D'
WHERE
ISNULL(AirNewformat.Processed, '') = '';
You can try this:
;with cte as
(
select ROW_NUMBER() over (PARTITION by ID order by ID) N, processed from airNewformat
)
update cte
set processed = case when N = 1 then 'N'
when N >1 then 'D' end;