I am trying to insert a value with a subquery including a column multiplication into a table. This works perfectly, however if I query the table, it only has 0 values. Does anybody know why?
My queries are:
create table user_payments
(payment_stream number(4));
insert into user_payments (payment_stream)
select ct.min_price*s.stream_min as payment_stream
from users u
, streaming s
, content_type ct
, contract c
, media_content mc
where u.user_ID = s.user_ID
and mc.media_content_ID = s.media_content_ID
and ct.content_type_ID = mc.content_type_ID
and u.user_ID = c.user_ID
and c.contract_name = 'Pay as you go';
If I query the select query individually, I get the expected outcome, however, once the rows are inserted into the table, all values are 0.
Thanks for your help!
try datatype numeric(18,2) in table var
Related
I'm trying to select a table in which I a have 3 or 4 columns that I want to match against an array/list with 1,000 or 10,000 (passed as parameter using a function) but it takes too much time, I readed that using indexes might help but still there ir poor performance during Query.
Here is my query:
SELECT
"id", "parcel_number", "alternate_parcel_number", "parcel_tax_number"
FROM "addresses"
WHERE
"parcel_number" IN ('A080100', ... 'A0368895224')
OR "alternate_parcel_number" IN ('A080100', ... 'A0368895224')
OR "parcel_tax_number" IN ('A080100', ... 'A0368895224');
With an array/list of 1000 lines it takes about 2 min to return results.
Thanks.
I found a solution on a website and change to VALUES gives a huge speed improvement.
URL: https://www.datadoghq.com/blog/100x-faster-postgres-performance-by-changing-1-line/
SELECT
"id", "parcel_number", "alternate_parcel_number", "parcel_tax_number"
FROM "addresses"
WHERE
"parcel_number" = ANY (VALUES ('A080100'), ... ('A0368895224'))
OR "alternate_parcel_number" = ANY (VALUES ('A080100'), ... ('A0368895224'))
OR "parcel_tax_number" = ANY (VALUES ('A080100'), ... ('A0368895224'));
It's not advisable to pass 10.000 values as parameters to a function. When I need to do that, my solution was:
Insert all parameters on a temporary table and associate all of them to a key value:
CREATE TEMP_TABLE (PARAM_KEY INT, PARCEL_NO INT);
Pass only the key value to the function
Run the query:
SELECT id, parcel_number, alternate_parcel_number, parcel_tax_number
FROM "addresses" join temp_table on parcel_number = temp_param
WHERE PARAM_key = :param_key
UNION
SELECT id, parcel_number, alternate_parcel_number, parcel_tax_number
FROM "addresses" join temp_table
on alternate_parcel_number = temp_param
WHERE PARAM_key = :param_key
UNION
SELECT id, parcel_number, alternate_parcel_number, parcel_tax_number
FROM "addresses" join temp_table on parcel_tax_number = temp_param
WHERE PARAM_key = :param_key;
It is my understanding that when this query runs it would not populate any data any number of times it runs because of the where clause
where c.company_id = lot.company_id
and p.product_id = lot.product_id
and l.packlevel_id = lot.packlevel_id
It looks to me that at the very beginning when the table fact_table_lot is empty the where clause would return with empty data because it would not find anything in an empty table and it would happen everytime. Is my understanding wrong?
insert into fact_table_lot(company_id, product_id, packlevel_id, l_num, sn_count, comm_loct, comm_start, commdate_end, man_date, exp_date, user_id, created_datetime)
select c.company_id, p.product_id, l.packlevel_id, l_num, sn_count, comm_loct, comm_start, commdate_end, man_date, exp_date, user_id, sysdate
from staging_serials s
left outer join fact_table_lot lot on s.lotnumber = lot.l_num
join company c on c.lsc_company_id = s.companyid
join product p on s.compositeprodcode = p.compositeprodcode
join level l on l.unit_of_measure = p.packaginguom
where c.company_id = lot.company_id
and p.product_id = lot.product_id
and l.packlevel_id = lot.packlevel_id
and lot.created_datetime is null
In your query staging_serials s left outer join fact_table_lot lot on s.lotnumber= lot.l_num this will give the result set containing all records from staging_serials and since fact table is empty null values for those column from fact table. If you want no records to be returned use a inner join instead of left join.
Is there any other way for returning rows for the same id by joining two tables and return the row if any of the columns value for the same id is different.
Select Table1.No,Table2.No,Table1.Name,Table2.Name,Table1.ID,Table2.ID,Table1.ID_N,Table2.ID_N
From MyFirstTable Table1
JOIN MySecondTable Table2
ON Table1.No=Table2.No where Table1.ID!=Table2.ID or Table1.ID_N != Table2.ID_N
In the example above , I have only two columns I need to check but in my real case there are at least 20 .
Is there any other statment I can use instead of enumerating each column in the where codition?
...WHERE BINARY_CHECKSUM(Table1.*) <> BINARY_CHECKSUM(Table2.*)
or
...WHERE BINARY_CHECKSUM(Table1.Field1, Table1.Field2, ...) <> BINARY_CHECKSUM(Table2..Field1, Table2.Field2, ...)
*this assumes you have no blob fields in your tables
http://technet.microsoft.com/en-us/library/ms173784.aspx
If No is a PK
Select Table1.No,Table1.Name,Table1.ID,Table1.ID_N
From MyFirstTable Table1
except
Select Table1.No,Table1.Name,Table1.ID,Table1.ID_N
From MySecondTable Table1
I have 2 complex queries that are both subqueries in postgres, the results of which are:
q1_results = id , delta , metric_1
q2_results = id , delta , metric_2
i'd like to combine the results of the queries, so the outer query can access either:
results_a = id , delta , metric_1 , metric_2
results_b = id , delta , combined_metric
i can't figure out how to do this. online searches keep leading me to UNION , but that keeps the metrics in the same column. i need to keep them split.
It's not entirely clear what you're asking in the question and the comments, but it sounds like you might be looking for a full join with a bunch of coalesce statements, e.g.:
-- create view at your option, e.g.:
-- create view combined_query as
select coalesce(a.id, b.id) as id,
coalesce(a.delta, b.delta) as delta,
a.metric1 as metric1,
b.metric2 as metric2,
coalesce(a.metric1,0) + coalesce(b.metric2,0) as combined
from (...) as results_a a
full join (...) as results_b b on a.id = b.id -- and a.delta = b.delta maybe?
I know this is an often asked question, but I've tried to resolve this myself and could not.
I've got 2 tables to join and now it's returning a duplicate value from the right table.
select am.Journal
,am.EntryNumber
,am.PayInvoice
,am.PayDiscAllowed
,am.PayTaxAmtDisc
,am.PayGrossPayment
,tm.*
from CshJnlPay am right join
(select
Invoice
,SUM(NetSalesValue) as NetSalesValue
,SUM(DiscValue) as DiscValue
,SUM(TaxValue) as TaxValue
,SUM(QtyInvoiced) as QtyInvoiced
from Salesdetail
group by Invoice) tm
on am.PayInvoice = tm.Invoice
where Invoice = 'C90831'
If the query returns 2 rows with the same data from the right table then you have 2 rows in the left table with the same invoice number...
You should check the left table with this query
Select * from CshJnlPay where PayInvoice = 'C90831'
You should get two rows.