How to create an increasing color of background for 3 cells in one row? - background-image

I would like to create this kind of background in my xsl-fo document:
I blurred out the sensitive info, but hopefully it is still visible enough that it should be a grey color that gets increased from cell to cell.
This is my code behind it:
<!-- table header -->
<fo:table table-layout="fixed" width="100%" background-color="#FFFFFF">
<fo:table-body>
<fo:table-row>
<fo:table-cell margin-left="55pt" padding-after="10pt">
<fo:block-container xsl:use-attribute-sets="boldtext">
<fo:block>
1st column header
</fo:block>
</fo:block-container>
</fo:table-cell>
<fo:table-cell padding-left="30pt" padding-after="10pt">
<fo:block-container xsl:use-attribute-sets="boldtext">
<fo:block>
2nd column header
</fo:block>
</fo:block-container>
</fo:table-cell>
<fo:table-cell padding-left="15pt" padding-after="10pt">
<fo:block-container xsl:use-attribute-sets="boldtext">
<fo:block>
3rd column header
</fo:block>
</fo:block-container>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>

If you use RenderX you could use their extensions to scale background images. This solution would be like this. It would scale completely to the table with the number of rows and/or the size. It is unclear in your example what else would be in the table.
<fo:table table-layout="fixed" width="100%" background-image="tri.svg" background-position="bottom left" background-repeat="no-repeat" rx:background-content-width="scale-to-fit" rx:background-content-height="scale-to-fit" rx:background-scaling="non-uniform">
<fo:table-body >
<fo:table-row>
<fo:table-cell>
<fo:block-container >
<fo:block>
1st column header
</fo:block>
</fo:block-container>
</fo:table-cell>
<fo:table-cell border-left="5pt solid white" border-right="5pt solid white">
<fo:block-container >
<fo:block>
2nd column header
</fo:block>
</fo:block-container>
</fo:table-cell>
<fo:table-cell>
<fo:block-container>
<fo:block>
3rd column header
</fo:block>
</fo:block-container>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block-container >
<fo:block>
1st column header
</fo:block>
</fo:block-container>
</fo:table-cell>
<fo:table-cell border-left="5pt solid white" border-right="5pt solid white">
<fo:block-container >
<fo:block>
2nd column header
</fo:block>
</fo:block-container>
</fo:table-cell>
<fo:table-cell>
<fo:block-container>
<fo:block>
3rd column header
</fo:block>
</fo:block-container>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block-container >
<fo:block>
1st column header
</fo:block>
</fo:block-container>
</fo:table-cell>
<fo:table-cell border-left="5pt solid white" border-right="5pt solid white">
<fo:block-container >
<fo:block>
2nd column header
</fo:block>
</fo:block-container>
</fo:table-cell>
<fo:table-cell>
<fo:block-container>
<fo:block>
3rd column header
</fo:block>
</fo:block-container>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block-container >
<fo:block>
1st column header
</fo:block>
</fo:block-container>
</fo:table-cell>
<fo:table-cell border-left="5pt solid white" border-right="5pt solid white">
<fo:block-container >
<fo:block>
2nd column header
</fo:block>
</fo:block-container>
</fo:table-cell>
<fo:table-cell>
<fo:block-container>
<fo:block>
3rd column header
</fo:block>
</fo:block-container>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block-container >
<fo:block>
1st column header
</fo:block>
</fo:block-container>
</fo:table-cell>
<fo:table-cell border-left="5pt solid white" border-right="5pt solid white">
<fo:block-container >
<fo:block>
2nd column header
</fo:block>
</fo:block-container>
</fo:table-cell>
<fo:table-cell>
<fo:block-container>
<fo:block>
3rd column header
</fo:block>
</fo:block-container>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
This results in this:
If you are using FOP, it is not clear to me that such extensions exist and you would likely need to size the SVG for the table if known.

Related

Create new columns using a column value and fill values from another column

I have the following table in PostgreSQL
id type name
146 INN Ofloxacin
146 TRADE_NAME Ocuflox
146 TRADE_NAME Ofloxacin
146 TRADE_NAME Tarivid i.v.
146 TRADE_NAME Tarivid 400
147 TRADE_NAME Mictral
147 TRADE_NAME Neggram
543 INN Amphetamine
543 INN Amfetamine
543 TRADE_NAME Adzenys xr-odt
543 TRADE_NAME Adzenys er
543 TRADE_NAME Dyanavel xr
I would like to create two new columns trade_name and inn and fill their respective value (copying over or concatenate the INN values) from column 'name'. I am expecting the following output
id trade_name inn
146 Ocuflox Ofloxacin
146 Ofloxacin Ofloxacin
146 Tarivid i.v. Ofloxacin
146 Tarivid 400 Ofloxacin
147 Mictral Ofloxacin
147 Neggram Ofloxacin
543 Adzenys xr-odt Amphetamine | Amfetamine
543 Adzenys er Amphetamine | Amfetamine
543 Dyanavel xr Amphetamine | Amfetamine
Any help is highly appreciated.
You can get a result set of distinct ids and then join that back to the same table. Once to get trade_names and once to get inn records:
SELECT ids.id,
tradenames.name as trade_name,
inns.name as inn
FROM
(SELECT DISTINCT id FROM yourtable) as ids
LEFT OUTER JOIN yourtable as tradenames
ON ids.id = tradenames.id
AND tradenames.type = 'TRADE_NAME'
LEFT OUTER JOIN yourtable as inns
ON ids.id = inns.id
AND inns.type = 'INNS';
You might also be able to pull this off with a pivot, but I think that would be overkill for the two output columns you are after.

How to delete duplicate rows without unique ID

Id
SleepDay
TotalMinutesAsleep
TotalTimeInBed
8378563200
4/20/2016
381
409
8378563200
4/21/2016
396
417
8378563200
4/22/2016
441
469
8378563200
4/23/2016
565
591
8378563200
4/24/2016
458
492
8378563200
4/25/2016
388
402 ---> this is the duplicate
8378563200
4/25/2016
388
402
8378563200
4/26/2016
550
584
8378563200
4/27/2016
531
600
This is part of my table and how can I delete the duplicate row? I use CTE clause but it deleted all records of id #8378563200 on 4/25/2016.
Use:
DELETE
FROM table1
WHERE ctid IN (SELECT ctid
FROM (SELECT ctid,
ROW_NUMBER() OVER (
PARTITION BY Id, SleepDay,TotalMinutesAsleep,TotalTimeInBed ) AS rn
FROM table1) t
WHERE rn > 1);
Replace table1 with your own table name.
Without column(s) to identify a unique row?
Then you could use ctid.
ctid
The physical location of the row version within its table. Note
that although the ctid can be used to locate the row version very
quickly, a row's ctid will change if it is updated or moved by VACUUM
FULL. Therefore ctid is useless as a long-term row identifier. A
primary key should be used to identify logical rows
For example:
delete
from SleepLogs log1
using SleepLogs log2
where log2.Id = log1.Id
and log2.SleepDay = log1.SleepDay
and log2.TotalMinutesAsleep = log1.TotalMinutesAsleep
and log2.TotalTimeInBed = log1.TotalTimeInBed
and log2.ctid < log1.ctid;
1 rows affected
select * from SleepLogs
id
sleepday
totalminutesasleep
totaltimeinbed
8378563200
2016-04-20
381
409
8378563200
2016-04-21
396
417
8378563200
2016-04-22
441
469
8378563200
2016-04-23
565
591
8378563200
2016-04-24
458
492
8378563200
2016-04-25
388
402
8378563200
2016-04-26
550
584
8378563200
2016-04-27
531
600
Test on db<>fiddle here

results of except query change after repeated consecutive runs in redshift

I'm running the postgresql query below in aws redshift. Each time I run this query I'm getting a different result for the number of records that are different on daily_table.product_repeat_sub_query side, using the except operator. Neither the daily_table.product_repeat_sub_query table or the daily_table.daily_sku_t are being updated during this time. the daily_table.product_repeat_sub_query table and the product_repeat_sub_query query both have the same record count. the schema for the daily_table.daily_sku_t is below, the matching fields in the daily_table.product_repeat_sub_query have the same data types. I've also included some sample records from the tables below. does anyone have an idea how the results of the except query can come out differently each time this query is run, when the underlying tables aren't changing?
daily_table.daily_sku_t schema:
customer_uuid string
boardname_12 string
producttype string
productsubtype string
storeid int
product_id string
dateclosed date
Size string
query:
with product_repeat_sub_query as
(
select
dateclosed, t.product_id, t.storeid, t.producttype, t.productsubtype, t.size, t.boardname_12,
case
when ticketid = first_value(ticketid) over (partition by t.product_id, customer_uuid
ORDER BY
dateclosed ASC rows between unbounded preceding and unbounded following) then 0
else grossreceipts
end as product_repeat_gross, datediff(day,
lag(dateclosed, 1) over (partition by t.boardname_12, customer_uuid, t.product_id
ORDER BY
dateclosed ASC ),
dateclosed) as product_cycle_days
from
daily_table.daily_sku_t t )
select count(*) from
(
select dateclosed, storeid, boardname_12, producttype, productsubtype, size, product_id, product_cycle_days from daily_table.product_repeat_sub_query
except
select dateclosed, storeid, boardname_12, producttype, productsubtype, size, product_id, product_cycle_days from product_repeat_sub_query
);
-- 36843
-- 36887
-- 36188
data:
daily_table.product_repeat_sub_query
dateclosed storeid boardname_12 producttype productsubtype size product_id product_cycle_days
2021-04-23 427 22RED DRUMER 1T 000011aa-4f03-4f0b-a621-xxxxxxxxxxxx 2
2021-04-24 427 22RED DRUMER 1T 000011aa-4f03-4f0b-a621-xxxxxxxxxxxx 6
2021-04-26 427 22RED DRUMER 1T 000011aa-4f03-4f0b-a621-xxxxxxxxxxxx 8
2021-04-26 427 22RED DRUMER 1T 000011aa-4f03-4f0b-a621-xxxxxxxxxxxx 3
2021-05-01 427 22RED DRUMER 1T 000011aa-4f03-4f0b-a621-xxxxxxxxxxxx 13
2020-06-18 61 FLAV RX WINGER BEVERAGE 100MT 0000265d-6b81-4d79-90cf-xxxxxxxxxxxx 5
2020-06-29
product_repeat_subquery
dateclosed storeid boardname_12 producttype productsubtype size product_id product_cycle_days
2021-04-23 427 22RED DRUMER 1T 000011aa-4f03-4f0b-a621-xxxxxxxxxxxx 2
2021-04-24 427 22RED DRUMER 1T 000011aa-4f03-4f0b-a621-xxxxxxxxxxxx 6
2021-04-26 427 22RED DRUMER 1T 000011aa-4f03-4f0b-a621-xxxxxxxxxxxx 8
2021-04-26 427 22RED DRUMER 1T 000011aa-4f03-4f0b-a621-xxxxxxxxxxxx 3
2021-05-01 427 22RED DRUMER 1T 000011aa-4f03-4f0b-a621-xxxxxxxxxxxx 13
2020-06-18 61 FLAV RX WINGER BEVERAGE 100MT 0000265d-6b81-4d79-90cf-xxxxxxxxxxxx 5
2020-06-29
update:
with product_repeat_sub_query as
(
select customer_uuid,
dateclosed, t.product_id, t.storeid, t.producttype, t.productsubtype, t.size, t.boardname_12,
case
when ticketid = first_value(ticketid) over (partition by t.product_id, customer_uuid
ORDER BY
dateclosed ASC rows between unbounded preceding and unbounded following) then 0
else grossreceipts
end as product_repeat_gross, datediff(day,
lag(dateclosed, 1) over (partition by t.boardname_12, customer_uuid, t.product_id
ORDER BY
dateclosed ASC,t.boardname_12, customer_uuid, t.product_id ),
dateclosed) as product_cycle_days
from
daily_table.daily_sku_t t
where (t.customer_uuid is not null)
and (trim(t.customer_uuid) != '')
and (t.product_id is not null)
and (trim(t.product_id) != '')
)
select count(*) from
(
select customer_uuid, dateclosed, storeid, boardname_12, producttype, productsubtype, size, product_id, product_cycle_days from daily_table.product_repeat_sub_query
except
select customer_uuid, dateclosed, storeid, boardname_12, producttype, productsubtype, size, product_id, product_cycle_days from product_repeat_sub_query
);
even after adding all the fields from the partition to the order by and filtering our nulls or blanks in the id fields, I'm still getting a different count each time.
Your window functions don't have fully qualified order by clauses. You have repeated "dateclosed" values within partitions. This means that Redshift can have different row orders for the lag and first-value functions. I expect that these "random" ordering differences are causing your changing results.

DISTINCT Still gives me dups records in results

I am getting double results for every part...so I'm obviously not using Distinct right here or need to use grouping?
example:
select DISTINCT p.PartNum,
p.PartID,
pn.Name,
d.[Description],
n.Note as PartNote
from Part p
join PartName pn on pn.PartNameID = p.PartNameID
join ApplicationPaint ap on ap.partID = p.PartID
join [Application] a on a.ApplicationID = ap.ApplicationID
join [Description] d on d.DescriptionID = ap.DescriptionID
join Note n on n.NoteID = a.NoteID
join MYConfig mmy on mmy.MMYConfigID = a.MYConfigID
join Model mo on mo.ModelID = mmy.ModelID
where mmy.ModelId = 2673
and substring(n.Note, CHARINDEX(']', n.Note) + 2, LEN(n.Note))= 'Johnson'
results:
T50015 765963 Some Part Name SomeNoteA [342] Johnson
T50015 765963 Some Part Name SomeNoteA [343] Johnson
T60024 766068 Some Part Name SomeNoteB [342] Johnson
T60024 766068 Some Part Name SomeNoteB [343] Johnson
T60231 766093 Some Part Name SomeNoteA [342] Johnson
T60231 766093 Some Part Name SomeNoteA [343] Johnson
T60232 766094 Some Part Name SomeNoteA [342] Johnson
T60232 766094 Some Part Name SomeNoteA [343] Johnson
T70134 766150 Some Part Name SomeNoteA [342] Johnson
T70134 766150 Some Part Name SomeNoteA [343] Johnson
T70230 766153 Some Part Name SomeNoteC [342] Johnson
T70230 766153 Some Part Name SomeNoteC [342] Johnson
T70230 766153 Some Part Name SomeNoteC [343] Johnson
Y50078 766253 Some Part Name SomeNoteH [342] Johnson
N30026 766352 Some Part Name SomeNoteT [342] Johnson
N30026 766352 Some Part Name SomeNoteT [343] Johnson
N50041 766465 Some Part Name SomeNoteK [342] Johnson
N50041 766465 Some Part Name SomeNoteK [343] Johnson
N60176 766499 Some Part Name SomeNoteX [342] Johnson
N60176 766499 Some Part Name SomeNoteX [343] Johnson
N60750 766503 Some Part Name SomeNoteU [342] Johnson
N60750 766503 Some Part Name SomeNoteU [343] Johnson
so I'm getting dups even triples on every PartNumber
T70230 766153 Some Part Name SomeNoteC [342] Johnson
T70230 766153 Some Part Name SomeNoteC [342] Johnson
T70230 766153 Some Part Name SomeNoteC [343] Johnson
T50015 765963 Some Part Name SomeNoteA [342] Johnson
T50015 765963 Some Part Name SomeNoteA [343] Johnson
so what I want to see is this:
T50015 765963 Some Part Name SomeNoteA [342] Johnson
T60024 766068 Some Part Name SomeNoteB [342] Johnson
T60231 766093 Some Part Name SomeNoteA [342] Johnson
T60232 766094 Some Part Name SomeNoteA [342] Johnson
T70134 766150 Some Part Name SomeNoteA [342] Johnson
T70230 766153 Some Part Name SomeNoteC [342] Johnson
Y50078 766253 Some Part Name SomeNoteH [342] Johnson
N30026 766352 Some Part Name SomeNoteT [342] Johnson
N50041 766465 Some Part Name SomeNoteK [342] Johnson
N60176 766499 Some Part Name SomeNoteX [342] Johnson
N60750 766503 Some Part Name SomeNoteU [342] Johnson
So I want only one unique row for each Unique part number, not dup part number rows showing here.
So to put it in other words for example I want this (one row only for a partID):
T70230 766153 Some Part Name SomeNoteC [342] Johnson
vs. dups:
T70230 766153 Some Part Name SomeNoteC [342] Johnson
T70230 766153 Some Part Name SomeNoteC [342] Johnson
T70230 766153 Some Part Name SomeNoteC [343] Johnson
You've omitted part names and notes from your example, but I believe DISTINCT means that it should omit rows from the results where all of the columns you've specified are duplicated, not any.
So since you've specified p.PartNum, p.PartID, pn.Name, d.[Description], and n.Note, only rows where all of those values are duplicated will be removed.
For example, you've said your results included:
T70230 766153 Some Part Name SomeNoteC [342] Johnson
T70230 766153 Some Part Name SomeNoteC [342] Johnson
T70230 766153 Some Part Name SomeNoteC [343] Johnson
If those rows were really:
T70230 766153 CoolWidget1 "So much fun!" [342] Johnson
T70230 766153 CoolWidget1 "Buy one today!" [342] Johnson
T70230 766153 CoolWidget2 "Buy one today!" [343] Johnson
Then all three rows will remain, as none have the exact same values for all five of the column names you've listed for the DISTINCT operator.
What's happening is that you have duplicated rows in your join. Remember, the Distinct doesn't necessarily filter on the columns in the select list (read up on this here: http://weblogs.sqlteam.com/jeffs/archive/2007/12/13/select-distinct-order-by-error.aspx).
There's a number of solutions, fort this from:
SELECT DISTINCT * FROM (
select p.PartNum,
p.PartID,
pn.Name,
d.[Description],
n.Note as PartNote
from Part p
join PartName pn on pn.PartNameID = p.PartNameID
join ApplicationPaint ap on ap.partID = p.PartID
join [Application] a on a.ApplicationID = ap.ApplicationID
join [Description] d on d.DescriptionID = ap.DescriptionID
join Note n on n.NoteID = a.NoteID
join MYConfig mmy on mmy.MMYConfigID = a.MYConfigID
join Model mo on mo.ModelID = mmy.ModelID
where mmy.ModelId = 2673
and substring(n.Note, CHARINDEX(']', n.Note) + 2, LEN(n.Note))= 'Johnson'
)
To using GROUP BY instead of distinct, to modifying the JOIN that is creating the duplicated rows. Something like:
select DISTINCT p.PartNum,
p.PartID,
pn.Name,
d.[Description],
n.Note as PartNote
from Part p
join (SELECT Distinct Name, PartNameID
FROM PartName) pn ON pn.PartNameId = p.PartNameID
join ApplicationPaint ap on ap.partID = p.PartID
join [Application] a on a.ApplicationID = ap.ApplicationID
join [Description] d on d.DescriptionID = ap.DescriptionID
join Note n on n.NoteID = a.NoteID
join MYConfig mmy on mmy.MMYConfigID = a.MYConfigID
join Model mo on mo.ModelID = mmy.ModelID
where mmy.ModelId = 2673
and substring(n.Note, CHARINDEX(']', n.Note) + 2, LEN(n.Note))= 'Johnson'
DISTINCT will combine rows, but they have to be exactly the same. Leaving off the PartNote field from your select will give you the unique set.
To get the PartNote as you've shown it in your example, the following should work ...
select DISTINCT p.PartNum,
p.PartID,
pn.Name,
d.[Description],
min(PartNote)
....
group by p.PartNum, p.PartID, pn.Name, d.[Description]

How to transpose a XML table with T-SQL XQuery?

Let's say we have the following XML:
<root>
<row>
<column>row 1 col 1</column>
<column>row 1 col 2</column>
<column>row 1 col 3</column>
</row>
<row>
<column>row 2 col 1</column>
<column>row 2 col 2</column>
<column>row 2 col 3</column>
</row>
<row>
<column>row 3 col 1</column>
<column>row 3 col 2</column>
<column>row 3 col 3</column>
</row>
</root>
How do I transpose this, using T-SQL XQuery to:
<root>
<column>
<row>row 1 col 1</row>
<row>row 2 col 1</row>
<row>row 3 col 1</row>
</column>
<column>
<row>row 1 col 2</row>
<row>row 2 col 2</row>
<row>row 3 col 2</row>
</column>
<column>
<row>row 1 col 3</row>
<row>row 2 col 3</row>
<row>row 3 col 3</row>
</column>
</root>
I suspect there might be a really nice approach using PIVOT, but I don't know it well enough to be able to say for sure. What I offer here works. I have split it up into chunks for better formatting and to provide commentary:
To start with let's capture the example data
-- Sample data
DECLARE #x3 xml
SET #x3 = '
<root>
<row>
<column>row 1 col 1</column>
<column>row 1 col 2</column>
<column>row 1 col 3</column>
</row>
<row>
<column>row 2 col 1</column>
<column>row 2 col 2</column>
<column>row 2 col 3</column>
</row>
<row>
<column>row 3 col 1</column>
<column>row 3 col 2</column>
<column>row 3 col 3</column>
</row>
</root>
'
DECLARE #x xml
SET #x = #x3
-- #x is now our input
Now the actual transposing code:
Establish the size of the matrix:
WITH Size(Size) AS
(
SELECT CAST(SQRT(COUNT(*)) AS int)
FROM #x.nodes('/root/row/column') T(C)
)
Shred the data, use ROW_NUMBER to capture the index (the -1 is to make it zero based), and use modulo and integer divide on the index to work out the new row and column numbers:
,Flattened(NewRow, NewCol, Value) AS
(
SELECT
-- i/#size as old_r, i % #size as old_c,
i % (SELECT TOP 1 Size FROM Size) AS NewRow,
i / (SELECT TOP 1 Size FROM Size) AS NewCol,
Value
FROM (
SELECT
(ROW_NUMBER() OVER (ORDER BY C)) - 1 AS i,
C.value('.', 'nvarchar(100)') AS Value
FROM #x.nodes('/root/row/column') T(C)
) ShreddedInput
)
With this CTE FlattenedInput available, all we now need to do is get the FOR XML options and query structure right and we're done:
SELECT
(
SELECT Value 'column'
FROM
Flattened t_inner
WHERE
t_inner.NewRow = t_outer.NewRow
FOR XML PATH(''), TYPE
) row
FROM
Flattened t_outer
GROUP BY NewRow
FOR XML PATH(''), ROOT('root')
Sample output:
<root>
<row>
<column>row 1 col 1</column>
<column>row 2 col 1</column>
<column>row 3 col 1</column>
</row>
<row>
<column>row 1 col 2</column>
<column>row 2 col 2</column>
<column>row 3 col 2</column>
</row>
<row>
<column>row 1 col 3</column>
<column>row 2 col 3</column>
<column>row 3 col 3</column>
</row>
</root>
Works on any size 'square' data. Note the lack of sanity checking / error handling.
SET #x3 = '
<root>
<row>
<column>row 1 col 1</column>
<column>row 1 col 2</column>
<column>row 1 col 3</column>
</row>
<row>
<column>row 2 col 1</column>
<column>row 2 col 2</column>
<column>row 2 col 3</column>
</row>
<row>
<column>row 3 col 1</column>
<column>row 3 col 2</column>
<column>row 3 col 3</column>
</row>
</root>
'
select #x3 = replace(#x3,'<row>','<rowtemp>')
select #x3 = replace(#x3,'<column>','<row>')
select #x3 = replace(#x3,'<rowtemp>','<column>')
select #x3