Compare two records - crystal-reports

My report needs to compare order quotations, grouped by a sales part number. These order quotations generally have mostly the same sales parts within them, but occasionally they differ. I need:
Quote 1 (Q1) - Total sales = Q1 sales qty * Q1 bid price
Quote 2 (Q1) - Total sales = Q2 sales qty * Q2 bid price
Desired info - Total sales = Q2 sales qty * Q1 bid price (If sales part does not exist on Q2, then - Desired info - Total sales = Q1 sales qty * Q1 bid price)
I'm pulling data from a simple SQL query and using the two quote numbers as parameters. How can I accomplish this?

Related

Would like to get sum of payment in interval during 36 month from date of file creation - Postgres

I have two tables.
In first table "customers" I have 3 colums, first column is "id" (unique for every customer) and in second column is "date_created" (the date when the customer file was created). Dates are ranging in format 'yyyy-mm-dd' from 2007 till date. The third column is "client_name" (the name of client to which customer belongs)
the second table is "payments" table with 3 colums, "id" (unique for every customer), in second column is "amount" of payment and in third column is "date_payment".
What I would like to achieve is next. From the first table I would like to choose files, created within a date range (for example from 2018-06-01 till 31.12.2018) and get sum of payment from second table after one month of creation, etc.. till month 36. Interval is attached for every particular claim, so it is different for every separate claim. The purpose of this is to get success rates from on month of creation, 2 month from creation, till month 36...
The result would be:
interval client_name sum
1 Boden 100
2 Boden 220
etc... till 36
Close results i get with next query but it is very time consuming, so I would need some quicker solution that would return results for 36 month intervals for every each claim.
select customers.client_name, sum(payments.amount) as sum_amount
from customers
left join payments on customers.id=payments.id
where customers.date_created >= '2018-06-01' and customersdate_created <= '2018-12-31'
and date_payment <= date(date_created + interval '1 month)
and client_name ilike '%Boden%'
group by customers.client_name
Can anyone help, please?
UPDATE
Nobody answered but I kind of figure it out myself. Solution below:
select customers.client_name, sum(payments.amount) as sum_amount, 1 as sortorder
from customers
left join payments on customers.id=payments.id
where customers.date_created >= '2018-06-01' and customersdate_created <= '2018-12-31'
and date_payment <= date(date_created + interval '1 month)
and client_name ilike '%Boden%'
group by customers.client_name
UNION
select customers.client_name, sum(payments.amount) as sum_amount, 2 as sortorder
from customers
left join payments on customers.id=payments.id
where customers.date_created >= '2018-06-01' and customersdate_created <= '2018-12-31'
and date_payment <= date(date_created + interval '2 month)
and client_name ilike '%Boden%'
group by customers.client_name

Need to find duplicate records for records which exist after a specified date

My requirement is two-fold, I have a huge source table with millions of records, and I need to get records after a cut-off date and then find if there are any relating records with the same metadata and group them. The relating records could be from a date before the cut-off date.
Original Table
Account Company Ledger P01 P02---P12 Date
1000 XYZ XX 0 20 07/06/18
2000 XYZ XX 0 30 07/06/18
1000 XYZ XX 10 30 07/01/18
3000 XYZ XX 20 50 06/05/18
Cutoff date = 07/05/18
Expected result
Account Company Ledger P01 P02---P12
1000 XYZ XX 10 50
2000 XYZ XX 0 30
I have not tested it on iSeries
SELECT account, company, ledger, sum(P01) , sum(P02)
from test_1 t
where exists (select 1 from test_1
where date1 > '2018-07-05'
and account = t.account
and company = t.company
and ledger = t.ledger)
group by account, company, ledger
Due to your sparse descrption the grouping columns or column names may need some adjustment
I think the following should get you want you want.
with MYDATA as (
select distinct Account as Act, Company as Cmp, Ledger as Ldgr
from MYTABLE
where Date > Cutoff_Date)
select Account, Company, Ledger, sum(P01), sum(P02)
from MYTABLE join MYDATA
on Account = Act and Company = Cmp and Ledger = Ldgr
group by Account, Company, Ledger

How to achieve matching with same table in postgresql?

I want to count number of sites partition by country on the basis of below criteria:-
For each TrialSite with Trial_Start_Date = X and Site_Activated = Y, you should be counting all rows that meet these conditions:
Trial_Start_Date <= Y, AND
TrialSite_Activation_Date >= X
i.e. all rows where there is some overlapping period with that row's Trial Start Date to TrialSite Activation Date.
sample data example:
Trial id start_date country site_id trialSite_activation_date
Trial A 01-01-2017 India site1 01-02-2017 ----> 2 (only overlaps with itself, and with 2nd row)
Trial A 01-01-2017 India site 2 01-04-2017 ----> 4 (overlaps with all rows, including itself)
Trial B 02-03-2017 India site3 01-04-2017 ----> 3 (does not overlap with first row, since Trial_Start_Date > 01-02-2017)
Trial B 02-03-2017 India site4 01-04-2017 ----> 3
This data can contain multiple countries and this logic needs to be applied with same country records.
You could use the “overlaps” operator && on dateranges:
SELECT t1, count(*)
FROM trial t1
JOIN trial t2
ON daterange(t1.trial_start_date, t1.trialsite_activation_date, '[]')
&& daterange(t2.trial_start_date, t2.trialsite_activation_date, '[]')
GROUP BY t1;
t1 | count
-----------------------------------------------+-------
("Trial A",2017-01-01,India,site1,2017-02-01) | 2
("Trial A",2017-01-01,India,site2,2017-04-01) | 4
("Trial B",2017-03-02,India,site3,2017-04-01) | 3
("Trial B",2017-03-02,India,site4,2017-04-01) | 3
(4 rows)
Instead of using the whole-row reference t1 in the SELECT list, you can specify individual columns there, but then you gave to list them in the GROUP BY clause as well.

Ordering the Amount range values (Ascending Order )in Postgres

Hi I want to show the Result set in ascending order. I have created the SQL FIDDLE for the same.
select amount_range as amount_range, count(*) as number_of_items,
sum(amount) as total_amount
from (
select *,case
when amount between 0.00 and 2500.00 then '<=$2,500.00'
when amount between 2500.01 and 5000.00 then '$2,500.01 - $5,000.00'
when amount between 5000.01 and 7500.00 then '$5,000.01 - $7,500.00'
when amount between 7500.01 and 10000.00 then '$7,500.01 - $10,000.00'
else '>$10,000.01' end as amount_range
from Sales ) a
group by amount_range order by amount_range;
My Results should be like
<=$2,500.00 4 5000
$2,500.01 - $5,000.00 3 12000
$5,000.01 - $7,500.00 2 13000
$7,500.01 - $10,000.00 1 10000
>$10,000.01 1 15000
The easiest method will be to sort off of a value in each grouping, for example the minimum amount:
select amount_range as amount_range,
count(*) as number_of_items,
sum(amount) as total_amount
from (
select *,case
when amount between 0.00 and 2500.00 then '<=$2,500.00'
when amount between 2500.01 and 5000.00 then '$2,500.01 - $5,000.00'
when amount between 5000.01 and 7500.00 then '$5,000.01 - $7,000.00'
when amount between 7500.01 and 10000.00 then '$7,500.01 - $10,000.00'
else '>$10,000.01' end as amount_range
from Sales ) a
group by amount_range
order by min(amount);
In Postgres, your subquery could also return an array where the first element is the desired position and the second is the string describing the bucket. Then, the outer query can ORDER BY your positioning value.
select amount_range[2] as amount_range,
count(*) as number_of_items,
sum(amount) as total_amount
from (
select *,case
when amount between 0.00 and 2500.00 then ARRAY['1','<=$2,500.00']
when amount between 2500.01 and 5000.00 then ARRAY['2','$2,500.01 - $5,000.00']
when amount between 5000.01 and 7500.00 then ARRAY['3', '$5,000.01 - $7,000.00']
when amount between 7500.01 and 10000.00 then ARRAY['4', '$7,500.01 - $10,000.00']
else ARRAY['5','>$10,000.01'] end as amount_range
from Sales ) a
group by amount_range
order by amount_range[1];
The first method happens to be simpler for your example. The second method would be useful if you were bucketing by something more complicated than ranges.

Postgres calculate growth rate over two month

I would like to calculate growth rate for customers for following data.
month | customers
-------------------------
01-2015 | 1
02-2015 | 10
03-2014 | 10
06-2015 | 15
I have used following formula to calculate the growth rate, it works only for one month interval as well as not able to give expected output due to gap between 3rd and 6th month as shown in above table
select
month, total,
(total::float / lag(total) over (order by month) - 1) * 100 growth
from (
select to_char(created, 'yyyy-mm') as month, count(id) total
from customers
group by month
) s
order by month;
I think this can be done by creating a date range and group by that range.
I expect two main output separately
1) Generate growth rate with exact one month difference
2) Growth rate with interval of 2 month instead of single month only. In above data sum the two month result and group by two month instead of month
Still not sure about the second part. Here's growth from your adapted query and twon month growth column:
select
month, total,
(total::float / lag(total) over (order by m) - 1) * 100 growth,m,m2
from (
select created, (sum(customers) over (order by m))::float total,customers,m,m2,to_char(created, 'yyyy-mm') as month
from customers c
right outer join (
select generate_series('2015-01-01','2015-06-01','1 month'::interval) m
) m1 on m=c.created
left outer join (
select generate_series('2015-01-01','2015-06-01','2 month'::interval) m2
) m2 on m2=m
order by m
) s
order by m;
basically answer is use generate_series