i have a very large select, and i have to join two results, but i cant find a way to make it work. the code is something like:
(select isnull(sum(fn.tqq),0) from fn where fn.icecream in (1,2,3,4,5,6,7,8,9) and (fn.fdata BETWEEN #2# AND #3#) and fn.usr1 not like '%'+'CHOC'+'%') as Total1,
(select isnull(sum(fn.tqq),0) from fn where fn.icecream in (1,2,3,4,5,6,7,8,9) and (fn.fdata BETWEEN #4# AND #5#) and pn.usr1 not like '%'+'Portes'+'%') as Total2,
thing is, i have to join, in the end, the results from icream 3 and 7, and the icecream 8 and 9. any thoughts?
Although tagged as VFP, that sample SQL doesn't sound to be a VFP query, nor an MS SQL server query. If for a second we thought that was a valid SQL then you could get what you initially propose in code like this:
Select t1.Total1, t2.Total2 From ;
(Select Nvl(Sum(fn.tqq),0) As Total1 ;
from fn ;
where fn.icecream In (1,2,3,4,5,6,7,8,9) And ;
(fn.fdata Between #2# And #3#) And ;
fn.usr1 Not Like '%'+'CHOC'+'%') t1, ;
(Select Nvl(Sum(fn.tqq),0) As Total2 ;
from fn ;
where fn.icecream In (1,2,3,4,5,6,7,8,9) And ;
(fn.fdata Between #4# And #5#) And ;
fn.usr1 Not Like '%'+'Portes'+'%') t2
Or (a more VFP specific query):
Select ;
Sum(Iif( InList(icecream,1,2,3,4,5,6,7,8,9) And ;
Between(fdata, #2# , #3#) And ;
!('CHOC'$usr1), tqq, 0)) as Total1, ;
Sum(Iif( InList(icecream,1,2,3,4,5,6,7,8,9) And ;
Between(fdata, #4# , #5#) And ;
!('Portes'$usr1), tqq, 0)) as Total2 ;
from fn
However, reading your last sentence, it is more confusing what you really want. If the above is not what you were after, provide us some sample data and the result you want to reach so we can offer a better one.
ok, i´m kind of lost right now, so here is the full code:
select
X.VENDEDOR AS VENDEDOR,
X.VENDNM AS VENDNM,
X.NO AS NO,
X.nome AS nome,
isnull(sum(x.qtt_1),0) as Qtt_1,
isnull(sum(x.qtt_2),0) as Qtt_2,
#2# as Data1,
#3# as Data2,
#4# as Data3,
#5# as Data4,
(SELECT DESIGN FROM ST WHERE ST.forref ='*br1000*') AS ARTIGO,
(select isnull(sum(pn.qtt),0) from pn where pn.vendedor in (1,2,3,4,5,6,7,8,9) and (pn.fdata BETWEEN #2# AND #3#) and pn.usr1 not like '%'+'Portes'+'%') as Total1,
(select isnull(sum(pn.qtt),0) from pn where pn.vendedor in (1,2,3,4,5,6,7,8,9) and (pn.fdata BETWEEN #4# AND #5#) and pn.usr1 not like '%'+'Portes'+'%') as Total2,
((select isnull(sum(pn.qtt),0) from pn where pn.vendnm like x.vendnm and (pn.fdata BETWEEN #4# AND #5#) and pn.usr1 not like '%'+'Portes'+'%' )-(select isnull(sum(pn.qtt),0) from pn where pn.vendnm like x.vendnm and (pn.fdata BETWEEN #2# AND #3#) and pn.usr1 not like '%'+'Portes'+'%')) as Dif_Total_Vend,
isnull(
round((((select sum(pn.qtt) from pn where pn.vendedor in (1,2,3,4,5,6,7,8,9) and (pn.fdata BETWEEN #4# AND #5#) and pn.usr1 not like '%'+'Portes'+'%')
-
(select sum(pn.qtt) from pn where pn.vendedor in (1,2,3,4,5,6,7,8,9) and (pn.fdata BETWEEN #2# AND #3#) and pn.usr1 not like '%'+'Portes'+'%'))/
(select sum(pn.qtt) from pn where pn.vendedor in (1,2,3,4,5,6,7,8,9) and (pn.fdata BETWEEN #2# AND #3#) and pn.usr1 not like '%'+'Portes'+'%'))*100,2),0) as Perc_Dif,
isnull(
round((((select sum(pn.qtt) from pn where pn.vendnm like x.vendnm and (pn.fdata BETWEEN #4# AND #5#) and pn.usr1 not like '%'+'Portes'+'%')
-
(select sum(pn.qtt) from pn where pn.vendnm like x.vendnm and (pn.fdata BETWEEN #2# AND #3#) and pn.usr1 not like '%'+'Portes'+'%'))/
(select sum(pn.qtt) from pn where pn.vendnm like x.vendnm and (pn.fdata BETWEEN #2# AND #3#) and pn.usr1 not like '%'+'Portes'+'%'))*100,2),0) as Perc_Dif_Vend
from
(
select
pn.vendedor as Vendedor,
pn.vendnm as Vendnm,
pn.no as No,
(select nome from cl where cl.no=pn.no) as nome,
pn.qtt as Qtt_1,
0 as Qtt_2
from pn
where (pn.fdata BETWEEN #2# AND #3#)
union all
select
pn.vendedor as Vendedor,
pn.vendnm as Vendnm,
pn.no as No,
(select nome from cl where cl.no=pn.no) as nome,
0 as Qtt_1,
pn.qtt as Qtt_2
from pn
where (pn.fdata BETWEEN #4# AND #5#) AND (pn.Vendedor '3' == pn.Vendedor '7')
)
x
group by x.no,x.nome, x.vendedor,x.vendnm
order by x.nome,x.vendedor
what i want to happen is, the vendor '3' came to replace number '7', and number '8' came to replace '9'. i´m getting duplicate results, when what i want is the results from number '3' and '7' as one (same thing with number '8' and '9').
Related
The table like this.
bh sl productdate
a1 100 2022-1-1
a1 220 2022-1-2
a1 220 2022-1-3
a2 200 2022-1-1
a2 350 2022-1-2
a2 350 2022-1-3
The result like this.
bh sl_q(sl_before) sl_h(sl_after) sl_b(changeValue) productdate
a1 100 220 120 2022-1-2
a2 200 350 150 2022-1-2
Rules:the same field bh, when the field sl change,then get the record.
We can use a ROW_NUMBER trick here:
WITH cte AS (
SELECT t.*, ROW_NUMBER() OVER (PARTITION BY bh ORDER BY productdate) rn1,
ROW_NUMBER() OVER (PARTITION BY bh ORDER BY productdate DESC) rn2
FROM yourTable t
)
SELECT bh, MAX(CASE WHEN rn1 = 1 THEN sl END) AS sl_q,
MAX(CASE WHEN rn2 = 1 THEN sl END) AS sl_h,
MAX(CASE WHEN rn2 = 1 THEN sl END) -
MAX(CASE WHEN rn1 = 1 THEN sl END) AS sl_b
FROM cte
GROUP BY bh;
My goal is to get 25th number. For instance I have 4 row, such as 3,4,5 and 7.
My goal is to get 1.25th(=(4+1)0.25).
Expected result is 3.25 which is obtained by interpolating(3+0.25(4-3)).
I have tried as below.
But is there any other efficient way?
WITH DATASET AS (
SELECT 3 C1 FROM DUAL
UNION
SELECT 4 FROM DUAL
UNION
SELECT 5 FROM DUAL
UNION
SELECT 7 FROM DUAL
)
SELECT
--RNK, C1, NEXTC1-C1, FIRSTQLOCAION, FIRSTQLOCAION-RNK, C1+(NEXTC1-C1)*(FIRSTQLOCAION-RNK)
C1+(NEXTC1-C1)*(FIRSTQLOCAION-RNK)
FROM(
SELECT C1,
LEAD(C1, 1) OVER (ORDER BY C1) as NEXTC1 ,
RANK() OVER (ORDER BY C1) AS RNK,
((SUM(1) OVER (PARTITION BY NULL)) +1) * 0.25 AS FIRSTQLOCAION
FROM DATASET
)
WHERE
FIRSTQLOCAION>=RNK AND FIRSTQLOCAION<=RNK+1;
You can use analytical function as follows:
Select c1,
c1 + (
(((Count(1) over () + 1)*0.25) - 1) * (lead(c1) over (order by c1) - c1)
) as calculated_number from
From your_table t
In this solution last record will have calculated value null as lead value will be null and you will have to adjust its value as per your requirement.
If your expectation is a single number from query then usw following:
Select min(c1) +
0.25 * (min(case when rn = 2 then c1 end)
- min(case when rn = 1 then c1 end)) as calculated_number
from
(Select t.*,
Row_number() over (order by c1)
From t)
WITH t AS (
SELECT 3 C1 FROM DUAL
UNION
SELECT 4 FROM DUAL
UNION
SELECT 5 FROM DUAL
UNION
SELECT 7 FROM DUAL
)
SELECT rn,location, calculated
FROM (
Select rn, c1,
C1 +(Count(1) over () + 1)*0.25
-trunc( (Count(1) over () + 1)*0.25 ) *(lead(c1) over (order by c1) - c1) as calculated, --
trunc( (Count(1) over () + 1)*0.25 ) as location --
From (Select t.*, Row_number() over (order by c1) rn From t) ) WHERE rn=location;
I am to create a query which will be used for printing labels in our project and find it difficult since the count of the number of labels is based on a string. I have made a query that looks like this:
SELECT
wipdatavalue
, containername
, l
, q as qtybox
, d
, qtyperbox AS q
, productname
, dt
, dsn
, CASE
WHEN instr(wipdatavalue, '-') = 0
THEN
to_number(wipdatavalue)
ELSE
to_number(substr(wipdatavalue, 1, instr(wipdatavalue, '-') - 1))
END AS una
, CASE
WHEN instr(wipdatavalue, '-') = 0
THEN
to_number(wipdatavalue)
ELSE
to_number(substr(wipdatavalue, instr(wipdatavalue, '-') + 1))
END AS dulo
, ROW_NUMBER() OVER (ORDER BY containername) AS n
, count(*) over() m
FROM trprinting_ls
WHERE containername = 'TRALTESTU0A'
GROUP BY wipdatavalue, containername, l, q, d, qtyperbox, productname, dt, dsn
ORDER BY wipdatavalue
The query above will result to below:
But actually, I have to display the first Item (Wipdatavalue 1-4) not only once but four times to look something like this:
I have tried another query that runs fine but when I try to load it in our project, it does not print the label. We found out that it is because of the WITH statement and we don't know why. The query is:
WITH DATA (WIPDATAVALUE, CONTAINERNAME, L, Q, D, QTYBOX, PRODUCTNAME, DT, una, dulo, m1, n)
AS (SELECT WIPDATAVALUE, CONTAINERNAME, L, Q, D, QTYBOX, PRODUCTNAME, DT, una, dulo,(dulo - una) + 1 AS m1,una n
FROM (SELECT WIPDATAVALUE, CONTAINERNAME, L, Q, D, QTYPERBOX AS QTYBOX, PRODUCTNAME, DT,
CASE
WHEN instr(wipdatavalue, '-') = 0
THEN
to_number(wipdatavalue)
ELSE
to_number(substr(wipdatavalue, 1, instr(wipdatavalue, '-') - 1))
END AS una,
CASE
WHEN instr(wipdatavalue, '-') = 0
THEN
to_number(wipdatavalue)
ELSE
to_number(substr(wipdatavalue, instr(wipdatavalue, '-') + 1))
END AS dulo
FROM trprinting_ls
WHERE containername = 'TRALTESTU0A'
)
UNION ALL
SELECT WIPDATAVALUE, CONTAINERNAME, L, Q, D, QTYBOX, PRODUCTNAME, DT, una, dulo, m1, n + 1
FROM DATA
WHERE n + 1 <= dulo)
SELECT WIPDATAVALUE, CONTAINERNAME, L, Q, D, QTYBOX, PRODUCTNAME, DT, una, dulo, n,
count(*) OVER () m
FROM DATA
ORDER BY n, wipdatavalue
Thanks guys for helping out.
Try this
select *
from your_data
start with instr(Wipdatavalue, '1') > 0
connect by level between regexp_substr(Wipdatavalue, '^\d+')
and regexp_substr(Wipdatavalue, '\d+$')
It's a simplified example
The regexp_substr can be replaced with substr and instr if you like (may also be faster)
Here is a sqlfiddle demo
Try this Query
select column_name, count(column_name)
from table
group by column_name
having count (column_name) > 1;
I'm trying to change my code to have Oracle do all the heavy lifting. Originally, I had a function which did the following:
<!--- get common data --->
<cfquery name="getCommon">
SELECT x, NVL(SUM(y), 0) as y
FROM table_1
WHERE z between #this# and #that#
GROUP BY x
</cfquery>
<!--- place common values into a struct --->
<cfset oCommon = StructNew() >
<cfloop query="getCommon">
<cfset oCommon[x] = y >
</cfloop>
<!--- get records to loop through --->
<cfquery name="getSomething">
SELECT a, b, c/d as e from (
SELECT DISTINCT t2.a,
t2.b,
c,
sum(d)as d
FROM table_2 t2
LEFT JOIN table_3 t3
ON t2.a = t3.a
AND t2.b = t3.b
GROUP BY t2.a, t2.b, c
)
</cfquery>
<!--- loop through the results --->
<cfloop query="getSomething">
<!--- do an update to the target table if conditions are met --->
<!--- calculate my important new value --->
<cfset new_value = #getSomething.e# * #oCommon[b]# >
<!--- finally, insert my new values into the target table --->
<cfquery name="insertTarget">
INSERT INTO target_table(
a,
b,
c
)
VALUES (
#getSomething.a#,
#getSomething.b#,
#new_value#
)
</cfquery>
</cfloop>
I've already figured out how to use Oracle's MERGE to replace the update statement in the loop. My problem is the SQL to replace the insert statement. I'd like to be able to calculate the new value inside the SQL... once I get that, I think I can figure out the MERGE/insert part fairly easily.
So I want to do something like:
SELECT a, b, c/d*y as e from (
SELECT DISTINCT t2.a,
t2.b,
c,
sum(d)as d,
(
SELECT y FROM (
SELECT x, NVL(SUM(y), 0) as y
FROM table_1
WHERE z between #this# and #that#
AND x = t2.a
GROUP BY x
)
)
FROM table_2 t2
LEFT JOIN table_3 t3
ON t2.a = t3.a
AND t2.b = t3.b
GROUP BY t2.a, t2.b, c
)
which, perhaps obviously, doesn't work. Currently I get an ORA-00904: "t2"."a": Invalid Identifier, which isn't surprising.
So, I basically need to figure out how to get the sum of y from table_1, correlated by x = table_2.a, into the original getSomething query.
Edit: I tried this, but it's not quite right:
SELECT a, b, c/d*y as e from (
SELECT DISTINCT t2.a,
t2.b,
c,
sum(d)as d,
NVL(SUM(y), 0) as y
FROM table_2 t2
LEFT JOIN table_3 t3
ON t2.a = t3.a
AND t2.b = t3.b
LEFT JOIN table_1 t1
ON t1.x = t2.a
AND t1.z between #this# and #that#
GROUP BY t2.a, t2.b, c
)
You could write your query like this:
SELECT a, b, c / d * y AS e
FROM (SELECT DISTINCT t2.a,
t2.b,
c,
sum(d) AS d,
v.y
FROM table_2 t2
LEFT JOIN table_3 t3
ON t2.a = t3.a
AND t2.b = t3.b
LEFT JOIN (SELECT x, nvl(sum(y), 0) AS y
FROM table_1
WHERE z BETWEEN #this# AND #that#
GROUP BY x) v
ON t2.a = v.x
GROUP BY t2.a, t2.b, c, v.y)
The optimizer should be able to push the predicate t2.a = v.x into the subquery if it is convenient (for example if there is an index on table_1.x).
The sql sentence that worked for me is te following:
Select p.id,p.name, ( select sum(quantity) from details d where d.idproduct=p.id) sum from products p
I have folowing Dataout put from my query
**Date** **HIGH** **LOW** **IMAGE** **TYPE**
1/28/2012 69 42 1.jpg SUN
1/29/2012 70 42 2.jpg MSUN
I want to Convert this Output into
**1/28/2012** **1/29/2012**
1.jpg 2.jpg
Sun MSUN
69 72
42 42
Here is My query
SELECT
W_DATE,HIGH, LOW,
W_TYPE, IMAGE
FROM WEATHER
ORDER BY W_DATE ASC
And Also I have multiple dates in rows i want to display only 4 dates and they should change when system date is chanaged
About all the possibilities how get from rows to columns in oracle you can read up here:
http://www.dba-oracle.com/t_converting_rows_columns.htm
I don't see a straight forward solution for that from database point of view - would suggest to do the formatting on application side, otherwise it could look as lame as this:
SELECT
to_char(w1.w_Date,'MM/DD/YYYY'), to_char(w2.w_Date,'MM/DD/YYYY'),
to_char(w3.w_Date,'MM/DD/YYYY'), to_char(w4.w_Date,'MM/DD/YYYY')
FROM
(select * from weather where w_date = trunc(sysdate)) w1,
(select * from weather where w_date = trunc(sysdate) + 1) w2,
(select * from weather where w_date = trunc(sysdate) + 2) w3,
(select * from weather where w_date = trunc(sysdate) + 3) w4
UNION ALL
SELECT
w1.image, w2.image, w3.image , w4.image
FROM
(select * from weather where w_date = trunc(sysdate)) w1,
(select * from weather where w_date = trunc(sysdate) + 1) w2,
(select * from weather where w_date = trunc(sysdate) + 2) w3,
(select * from weather where w_date = trunc(sysdate) + 3) w4
UNION ALL
SELECT
w1.w_type, w2.w_type, w3.w_type , w4.w_type
FROM
(select * from weather where w_date = trunc(sysdate)) w1,
(select * from weather where w_date = trunc(sysdate) + 1) w2,
(select * from weather where w_date = trunc(sysdate) + 2) w3,
(select * from weather where w_date = trunc(sysdate) + 3) w4
UNION ALL
SELECT
to_char(w1.high), to_char(w2.high), to_char(w3.high) , to_char(w4.high)
FROM
(select * from weather where w_date = trunc(sysdate)) w1,
(select * from weather where w_date = trunc(sysdate) + 1) w2,
(select * from weather where w_date = trunc(sysdate) + 2) w3,
(select * from weather where w_date = trunc(sysdate) + 3) w4
UNION ALL
SELECT
to_char(w1.low), to_char(w2.low), to_char(w3.low) , to_char(w4.low)
FROM
(select * from weather where w_date = trunc(sysdate)) w1,
(select * from weather where w_date = trunc(sysdate) + 1) w2,
(select * from weather where w_date = trunc(sysdate) + 2) w3,
(select * from weather where w_date = trunc(sysdate) + 3) w4;
/