Update postgresql table column with increasing reference - sql

I have a postgresql table that have a reference column (different than the id).
I want to iterate on the table and add a reference code ('ITEM001', 'ITEM002", etc) for each line.
If I want to change for all line with the same reference, I do:
UPDATE stock SET reference = 'ITEM001';
How could I update the column of all table based on the id ?
Current situation:
id name reference
1 item-name-1
2 item-name-2
3 item-name-3
10 item-name-10
11 item-name-11
Desired result:
id name reference
1 item-name-1 IT001
2 item-name-2 IT002
3 item-name-3 IT003
10 item-name-10 IT010
11 item-name-11 IT011

You can use to_char() to format the number:
UPDATE stock
SET reference = 'IT' || to_char(id, 'FM000');
The FM parameter for the format mask makes sure that the formatted value does not contain any blanks.
But in general it is a bad idea to store information that can easily be derived from existing data.
The better solution would be to create a view that returns that information:
create or replace view stock_with_reference
as
select *, 'IT' || to_char(id, 'FM000') as reference
from stock;

Related

How can I use a row value to dynamically select a column name in Oracle SQL 11g?

I have two tables, one with a single row for each "batch_number" and another with defect details for each batch. The first table has a "defect_of_interest" column which I would like to link to one of the columns in the second table. I am trying to write a query that would then pick the maximum value in that dynamically linked column for any "unit_number" in the "batch_number".
Here is the SQLFiddle with example data for each table: http://sqlfiddle.com/#!9/a1c27d
For example, the maximum value in the DEFECT_DETAILS.SCRATCHES column for BATCH_NUMBER = A1 is 12.
Here is my desired output:
BATCH_NUMBER DEFECT_OF_INTEREST MAXIMUM_DEFECT_COUNT
------------ ------------------ --------------------
A1 SCRATCHES 12
B3 BUMPS 4
C2 STAINS 9
I have tried using the PIVOT function, but I can't get it to work. Not sure if it works in cases like this. Any help would be much appreciated.
If the number of columns is fixed (it seems to be) you can use CASE to select the specific value according to the related table. Then aggregating is simple.
For example:
select
batch_number,
max(defect_of_interest) as defect_of_interest,
max(defect_count) as maximum_defect_count
from (
select
d.batch_number,
b.defect_of_interest,
case when b.defect_of_interest = 'SCRATCHES' then d.scratches
when b.defect_of_interest = 'BUMPS' then d.bumps
when b.defect_of_interest = 'STAINS' then d.stains
end as defect_count
from defect_details d
join batches b on b.batch_number = d.batch_number
) x
group by batch_number
order by batch_number;
See Oracle example in db<>fiddle.

How to combine two selects of one table into an internal table?

I have a self-created Z-table of this structure:
SPWOC NUMC 6
VKORG CHAR 4
MATNR CHAR 18
KUNNR CHAR 10
OLFMNG QUAN 13 (reference VOLEH)
WADAT DATS 8
VOLEH UNIT 3
How can I create an internal table with three additional fields:
SPWOC2
OLFMNG2
WADAT2
With two different calendar weeks I want to fill this internal table to be able to compare SPWOC and SPWOC2, OLFMNG an OLFMNG2 and WADAT and WADAT2.
With
SELECT * FROM ZTABLE INTO CORRESPONDING FIELDS of TABLE it_table where spwoc = l_kw1.
I get the calendar week 1 into internal table, but how can I add data of the second week into same rows?
In Your program:
TYPES: BEGIN OF gty_zextend,
INCLUDE TYPE your_ztype,
SPWOC2 TYPE referring_type,
OLFMNG2 TYPE referring_type,
WADAT2 type referring_type,
END OF gty_zextend.
DATA: lt_itab TYPE STANDARD TABLE OF gty_zextend.
There You have it.
And, remember, keyword "AS" is also possible for table-fields(columns), what makes it relatively easy, to use "into corresponding fields of table" once, You specify like this , column name "another_date" as wadat2 for example. But I think, Your source for the other three field is another table, right ?

DAX - selecting rows with partial match

I have a powerpivot table that contains 2 columns:
Column 1 contains strings.
Column 2 contains comma delimited strings.
I would like to be able to display all the rows from column 1 when rows from column 2 contains the selection from a filter or slicer. For example:
String Values
ABCD A,A,B
EFGH A,C
if A is selected I would display both rows, if B is selected I would display only row 1...etc.
I know I can split the records - but this is not practical for me - the above is only the top of the iceberg. VBA is out of the question since this will published in SharePoint. Anybody has an idea on how I could do that ? Thanks.
I found the solution in a blog from Javier Guillem:
http://javierguillen.wordpress.com/2012/02/10/simulating-an-approximate-match-vlookup-in-powerpivot/
If in my example the name of the table is "facts", I create a second unlinked table called dimRef that I populate with all possible values that I am interested to match: A,B,C,D...etc.
Then I define the measure M as:
M:=If ( Hasonevalue(facts[Values] ),
Calculate (
LASTNONBLANK (dimRef[String], 1 ),
Filter ( dimRef, SEARCH(dimRef[String],Values(facts[String]),1,0) > 0 )
)
)
I can then use the string column of the facts table and the measure in a pivot table and use dimRef as a selector. If filters the row as per the selection.
One small detail: the measure is not available in PowerView...Anybody knows why ?

Crystal Reports - side by side details from actual and last year, using UNION ALL

I'm creating a report that shows in the details section the current and last year of sales.
The rows of the report have a structure like this:
Article |||| LastYearQty | LastYearPrice |||| ActualYearQty | ActualYearPrice
There are two parameters at the report, the StartDate and EndDate.
I created a view view_RowsDocs, that will replace the original table RowsDocs, with the aditional boolean field IsLastYear to filter each line, but i'm having some problems to filter the date, classifying the rows at the last year or at the actual year.
For example, with StartDate 1-1-2014 EndDate 28-4-2014:
IsLastYear || Article || Qty || Price || Date
1 A0001 12 13,12 12-2-2013
0 A0001 13 13,11 12-7-2013
What can be the best way to update the IsLastYear field?
I think the fact that you are using a View to get this relation with the IsLastYear attribute might be screwing you up. This is because by using a view, you are actually hitting the base relation. Your logic that determines the IsLastYear attribute is doing something screwy.
Instead, consider using a temp table to instantiate the relation with the IsLastYear attribute:
select
...
,[Date]
,CASE
WHEN YEAR([Date]) = YEAR(GETDATE()) - 1
THEN 1
ELSE 0
END as [IsLastYear]
into #myTempTbl
from RowsDocs
If you are producing this dataset with a stored procedure, include a DROP TABLE #myTempTbl at the end of the sproc.

SQL Where and Like in PHP imploded string

I have text field in my pgsql DB which contains users identificators. One field can contain one or more ids separated by #. On saving numbers are imploded using PHP, so I can have for example:
Row 1: 3#36#66#33
Row 2: 5#56#33#55.
How should I prepare SQL query to select field with one or more ids without any ambiguity?
Example:
SELECT product FROM cms_product WHERE product_mod LIKE '3'
Both row 1 and 2 can be selected which I don't want. How I can fix it?
EDIT:
I will have to select all rows that contains user ID in this filed, for example:
UserId: 3
Field 1:
55#64#333#2
Field 2:
12#3#55#6423
Field 3;
654#33#11#98
Only field 2 should be selected.
Although you should really work to normalize your DB this is the answer to the posted question
select product
from cms_product
where '3' = any(regexp_split_to_array(product_mod, '#'))