In PL SQL is there a way to produce the Order Count per customer as follows... Thanks for your help.
Cust Order# Order Count
ABC1 011 1
ABC1 052 2
ABC1 199 3
BBA1 150 1
BBA1 158 2
Thanks
Gavin
If I understood you correctly, a little bit of analytics might do the job. Here's an example:
SQL> with test (cust, order#) as
2 (select 'ABC1', '011' from dual union all
3 select 'ABC1', '052' from dual union all
4 select 'ABC1', '199' from dual union all
5 select 'BBA1', '150' from dual union all
6 select 'BBA1', '158' from dual
7 )
8 select cust, order#,
9 row_number() over (partition by cust order by order#) order_count
10 from test;
CUST ORD ORDER_COUNT
---- --- -----------
ABC1 011 1
ABC1 052 2
ABC1 199 3
BBA1 150 1
BBA1 158 2
SQL>
sounds like you want a GROUP BY such as
select cust, SUM(order_count)
from MyTable
group by cust;
which should yield
cust SUM
ABC1 6
BBA1 3
Related
family_id<-c(1,2,3)
age_mother<-c(30,27,29)
dob_child1<-c("1998-11-12","1999-12-12","1996-04-12")##child one birth day
dob_child2<-c(NA,"1997-09-09",NA)##if no child,NA
dob_child3<-c(NA,"1999-09-01","1996-09-09")
DT<-data.table(family_id,age_mother,dob_child1,dob_child2,dob_child3)
Now I have DT, how can I use this table to know how many children each family have using syntax like this:
DT[,apply..,keyby=family_id]##this code is wrong
This may also work:
> DT$total_child <- as.vector(rowSums(!is.na(DT[, c("dob_child1",
"dob_child2", "dob_child3")])))
> DT
family_id age_mother dob_child1 dob_child2 dob_child3 total_child
1 1 30 1998-11-12 <NA> <NA> 1
2 2 27 1999-12-12 1997-09-09 1999-09-01 3
3 3 29 1996-04-12 <NA> 1996-09-09 2
You can use sqldf package, to use a SQL query in R.
I duplicated your DT.
family_id<-c(1,2,3)
age_mother<-c(30,27,29)
dob_child1<-c("1998-11-12","1999-12-12","1996-04-12")##child one birth day
dob_child2<-c(NA,"1997-09-09",NA)##if no child,NA
dob_child3<-c(NA,"1999-09-01","1996-09-09")
DT<-data.table(family_id,age_mother,dob_child1,dob_child2,dob_child3)
library(sqldf)
sqldf('select distinct (count(dob_child3)+count(dob_child2)+count(dob_child1)) as total_child,
family_id from DT group by family_id')
The result is the following:
total_child family_id
1 1 1
2 3 2
3 2 3
It is correct for you?
I am new to PL/SQL
I have a code like this
SELECT f.code,f.date,f.amt, row_number() OVER (PARTITION BY f.code ORDER BY f.date DESC) ranki
FROM advance.alloc f
and shows
CODE DATE AMT ranki
122 12/31/2016 3 1
122 12/31/2015 7 2
122 12/31/2014 3 3
123 6/30/2015 3 1
125 6/30/2015 2 1
125 12/31/2014 8 2
Logic is this
if DATE = 12/__/__ AND ranki = 1 THEN ranki 1, so 122 picks 12/31/2016 3
if DATE = 6/30/__ AND ranki = 1 AND if ranki = 2 exists THEN then pick the second one,so 125 picks 12/31/2014 8
if 6/30__ and ranki is ONLY 1 shows Blank on date LIKE 123
so I would like to show
122 12/31/2016 3
123 __________ 3
125 12/31/2014 8
How can I code like this PL/SQL?
WHEN to_char(af.date,'MM') = 12 AND af.ranki = 1 THEN af.date END
I could code first logic, but I can not figure out how to code the rest of the logic
Thanks
Why in PL/SQL? Or do you mean "in Oracle SQL"? (The solution below uses standard analytic functions, so it is not specific to Oracle.)
Add more information through analytic functions, in addition to ranki. Extract the month from the row with ranki = 1, and also the total count for each code. Then the WHERE clause can follow your logic step by step.
with
f ( code, dt, amount ) as (
select 122, to_date('12/31/2016', 'mm/dd/yyyy'), 3 from dual union all
select 122, to_date('12/31/2015', 'mm/dd/yyyy'), 7 from dual union all
select 122, to_date('12/31/2014', 'mm/dd/yyyy'), 3 from dual union all
select 123, to_date( '6/30/2015', 'mm/dd/yyyy'), 3 from dual union all
select 125, to_date( '6/30/2015', 'mm/dd/yyyy'), 2 from dual union all
select 125, to_date('12/31/2014', 'mm/dd/yyyy'), 8 from dual
)
-- End of simulated data (for testing purposes only, not part of the solution).
-- SQL query begins BELOW THIS LINE.
select code, case when mth = 12 or ranki = 2 then dt end as dt, amount
from ( select code, dt, amount,
first_value(extract (month from dt))
over (partition by code order by dt desc) as mth,
row_number() over (partition by code order by dt desc) as ranki,
count(*) over (partition by code) as cnt
from f
)
where mth = 12 and ranki = 1
or cnt = 1
or mth = 6 and ranki = 2
;
CODE DT AMOUNT
---- ---------- ------
122 12/31/2016 3
123 3
125 12/31/2014 8
I want to group by lead function by two column. Here is my table data.
Id Name_Id Name Item_Id Item_Name date
1 1 Car 1 SUV 1-Jan-2015
2 1 Car 1 SUV 12-March-2015
3 1 Car 1 SUV 20-April-2015
4 1 Car 2 Sport 23-April-2015
5 2 Bike 1 SUV 18-July-2015
6 2 Bike 1 SUV 20-Aug-2015
7 2 Bike 2 Sport 18-Sept-2015
8 2 Bike 3 Honda 20-OCT-2015
And I need result from above table like.
Id Name_Id Name Item_Id Item_Name start date end date
1 1 Car 1 SUV 1-Jan-2015 20-April-2015
2 1 Car 2 Sport 20-April-2015 23-April-2015
3 2 Bike 1 SUV 18-July-2015 20-Aug-2015
4 2 Bike 2 Sport 20-Aug-2015 18-Sept-2015
5 2 Bike 3 Honda 18-Sept-2015 20-OCT-2015
Any suggestion really appreciated.
I don't think you need to use LEAD here. The CTE below computes, for each Item_Id, the earliest and latest date. This is then joined to your original table to restrict to records corresponding to the earliest Item_Id. At the same time, the end date is also pulled in during the join.
WITH cte AS (
SELECT Name,
Item_Id,
MIN(date) AS start_date,
MAX(date) AS end_date
FROM yourTable
GROUP BY Name, Item_Id
)
SELECT t1.Id, t1.Name_Id, t1.Name, t1.Item_Id, t1.Item_Name,
t2.start_date,
t2.end_date
FROM yourTable t1
INNER JOIN cte t2
ON t1.Item_Id = t2.Item_Id AND
t1.Name = t2.Name AND
t1.date = t2.start_date
This is how I have a sample table in SQLITE
ID NAME AGE ADDRESS SALARY
1 Paul 32 California 20000.0
2 Allen 25 Texas 15000.0
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
7 Paul 32 California 20000.0
8 Allen 25 Texas 15000.0
9 Teddy 23 Norway 20000.0
What I want to achieve is a join on my SQLITE table on these two queries
select AGE, count(*) as SALARYLESSTHAN45 from company where salary < 45000 group by salary
select AGE, count(*) as SALARYMORETHAN45 from company where salary > 45000 group by salary
I tried the following
select AGE, count(*) as SALARYLESSTHAN45 from company where salary < 45000 group by salary ) T1
INNER JOIN
select AGE, count(*) as SALARYMORETHAN45 from company where salary > 45000 group by salary ) T2
ON T1.AGE = T2.AGE
but cannot get this to work...
Can someone share an example of how to achieve this in SQLITE ?
A join on two different tables would look like this:
SELECT ... FROM Tab1 JOIN Tab2 ON ...
To do the join on the result of a query, you have to replace the table name with a subquery:
select AGE,
SALARYLESSTHAN45,
SALARYMORETHAN45
from (select AGE,
count(*) as SALARYLESSTHAN45
from company
where salary < 45000
group by salary)
join (select AGE,
count(*) as SALARYMORETHAN45
from company
where salary > 45000
group by salary)
using (AGE);
I need some help to build SQL Query. I have table having data like:
ID Date Value1 Value2 Code
1 12/01/2009 4 3.5 abc
2 12/02/2009 3 4.0 abc
3 11/03/2009 6 8.5 xyz
4 11/01/2009 2 5.5 abc
5 11/02/2009 4 6.0 xyz
6 12/03/2009 5 7.0 xyz
I need to show result something like...
---------
Code | Data | November(Sum of Values in month) December Jan Feb
abc | Value1 | 2 7 0 0
| Value2 | 5 7 0 0
xyz | Value1 | 10 5 0 0
| Value2 | 14 7 0 0
----------
I need sum of value in each month as in above data in columns group by code.
Have a look at this solution, and let me know what you think.
You have to use both PIVOT and UNPIVOT in this instance to get the result you are looking for. Hope this helps.
DECLARE #Table TABLE(
ID INT,
Date DATETIME,
Value1 INT,
Value2 FLOAT,
Code VARCHAR(10)
)
INSERT INTO #Table (ID,Date,Value1,Value2,Code) SELECT 1,'12/01/2009',4,3,'abc'
INSERT INTO #Table (ID,Date,Value1,Value2,Code) SELECT 2,'12/02/2009',3,4,'abc'
INSERT INTO #Table (ID,Date,Value1,Value2,Code) SELECT 3,'11/03/2009',6,8,'xyz'
INSERT INTO #Table (ID,Date,Value1,Value2,Code) SELECT 4,'11/01/2009',2,5,'abc'
INSERT INTO #Table (ID,Date,Value1,Value2,Code) SELECT 5,'11/02/2009',4,6,'xyz'
INSERT INTO #Table (ID,Date,Value1,Value2,Code) SELECT 6,'12/03/2009',5,7,'xyz'
;WITH UnPvt AS (
SELECT *
FROM (
SELECT Code,
DATENAME(MM, Date) MonthNameVal,
SUM(Value1) Value1,
SUM(Value2) Value2
FROM (
SELECT Code,
Date,
CAST(Value1 AS FLOAT) Value1,
Value2
FROM #Table
) v
GROUP BY Code,
DATENAME(MM, Date)
) Sub
UNPIVOT
(
Vals FOR RowValues IN (Value1, Value2)
) AS UnPvt
)
SELECT *
FROM UnPvt
PIVOT (
SUM(Vals)
FOR MonthNameVal IN ([January],[February],[March],[April],[May],[June],[July],[August],[September],[October],[November], [December])
) AS pvt
ORDER BY Code, RowValues
Have a look at
SQL SERVER – PIVOT and UNPIVOT Table
Examples
Give the New PIVOT and UNPIVOT
Commands in SQL Server 2005 a
Whirl
Using PIVOT and UNPIVOT
This isn't quite what you asked for because the number of columns is fixed, but I think it's a better way to what you want in SQL:
SELECT Code, 'Value1' As Data, MONTH(Date) AS Month, YEAR(Date) AS Year, SUM(Value1) AS Sum
FROM Table1
GROUP BY Code, MONTH(Date), YEAR(Date)
UNION ALL
SELECT Code, 'Value2' As Data, MONTH(Date) AS Month, YEAR(Date) AS Year, SUM(Value2) AS Sum
FROM Table1
GROUP BY Code, MONTH(Date), YEAR(Date)
ORDER BY Code, Data, Month, Year
Example output:
Code Data Month Year Sum
abc Value1 11 2009 2
abc Value1 12 2009 7
abc Value2 11 2009 5
abc Value2 12 2009 7
xyz Value1 11 2009 10
xyz Value1 12 2009 5
xyz Value2 11 2009 14
xyz Value2 12 2009 7
I'd recommend that you use a bit of non-SQL code to reformat the result into exactly what you asked for before displaying it to the user rather than trying to return a variable number of columns in SQL.