Is it possible to use FROM in PS query expression? If not is there an alternative solution? - peoplesoft

I'm new with PS Query and SQL. I have a working SQL however I cannot translate it to PS Query.
So basically, I would like to present the comparison of my products and its amount per column.
Here are my sample data:
Product | Month | Amount
--------|-------|-------
Phone | 2 | 25
Laptop | 2 | 30
Phone | 1 | 20
Laptop | 1 | 40
Below is the code that I'm having hard time translating to PS Query:
Select ….
SUM(CASE WHEN Product1 <> Product2 AND Product2 = Laptop THEN 30
/*On my second statement, when the product are equal, i need to get the amount on the previous month.*/
WHEN Product1 = Product3 AND Product3 = Phone
THEN (SELECT Amount
FROM FINAL_TBL B
WHERE B.PERIOD = A.PERIOD - 1)
ELSE 0
END) AS Amount
From FINAL_TBL FTBL A
Where <Conditions>
My expected output should be
Product1 | Amount1 | Product2 | Amount2 | Product3 | Amount3
------------|-----------|-----------|-----------|-----------|--------
Phone | 25 | Laptop | 30 | Phone | 20

You cannot add a FROM in a PSQUERY expression.
The query you are trying to translate can be rewritten to:
Select *
SUM(CASE WHEN Product1 <> Product2 AND Product2 = Laptop
THEN 30
WHEN Product1 = Product3 AND Product3 = Phone
THEN B.Amount
ELSE 0
END) AS Amount
FROM FINAL_TBL A, FINAL_TBL B
WHERE B.PERIOD = A.PERIOD - 1
AND <Conditions>
But neither this or the original query would lead to the output you expect. What you are looking for is the PIVOT clause.

Whenever I run into a situation where I have a complicated query that's difficult or impossible to build through PSQuery, I make a view in App Designer that has the SQL.
Then make a PSQuery where you select from this view. I assume your <conditions> are user-entered data that you want to be entered when running the query, so add those conditions to the PSQuery that you build.
However, I think you should rethink what you're trying to do with your query. It looks like you're trying to list all products/prices that were bought in month X, as well as the price for the first product for month X - 1. With just two possible products (laptops and phones), this'll work, but when you start adding more types of products, you'll need more PRODUCT and PRICE columns. I don't think you'll be able to do that through PSQuery because you can't have a dynamic number of columns.
If you could explain what you're trying to accomplish and why, then someone might be able to suggest an alternative that'll work through PSQuery.

Related

Calculate Count of users every month in Kusto query language

I have a table named tab1:
Timestamp Username. sessionid
12-12-2020. Ravi. abc123
12-12-2020. Hari. oipio878
12-12-2020. Ravi. ytut987
11-12-2020. Ram. def123
10-12-2020. Ravi. jhgj54
10-12-2020. Shiv. qwee090
10-12-2020. bob. rtet4535
30-12-2020. sita. jgjye56
I want to count the number of distinct Usernames per day, so that the output would be:
day. count
10-12-2020. 3
11-12-2020. 1
12-12-2020. 2
30-12-2020. 1
Tried query:
tab1
| where timestamp > datetime(01-08-2020)
| range timestamp from datetime(01-08-2020) to now() step 1d
| extend day = dayofmonth(timestamp)
| distinct Username
| count
| project day, count
To get a very close estimation of the number of Usernames per day, just run this (the number won't be accurate, see details here):
tab1
| summarize dcount(Username) by bin(Timestamp, 1d)
If you want accurate results, then you should do this (just note that the query will be less performant than the previous one, and will only work if you have up to 1,000,000 usernames / day):
tab1
| summarize make_set(Username) by bin(Timestamp, 1d)
| project Timestamp, Count = array_length(set_Username)

Interactive Grid:Process with PL/SQL only that isn't based off a table

Env: Oracle APEX v5.1 with Oracle 12c Release 2
Firstly, I have created an Interactive Grid that isn't based off an underlying table as I will process this manually using PL/SQL.
I have been using the following as a guide:
https://apex.oracle.com/pls/apex/germancommunities/apexcommunity/tipp/6361/index-en.html
I basically have the following query:
select
level as id,
level as grid_row,
null as product,
null as product_item
from dual connect by level <= 1
Concentrating on just the product and product_item columns where the product_item column will be a readonly column and only the product number can be entered, I would like to achieve the following:
Product Product Item
---------- -------------
123456 123456-1
123456 123456-2
556677 556677-1
654321 654321-1
654321 654321-2
654321 654321-3
123456 123456-3
From the above, as the user types in the Product and then tabs out of the field, I would like a DA to fire that will add the sequence of "-1" to the end of that product number. Then is the user then adds another row within the IG and enters the same product number, I then want it to append "-2" to the end of it.
Only when the product changes number, I need the sequence to reset to "-1" for that new product as per 556677 and so forth.
Other scenarios that should also be taken into consideration are as follows:
From above IG, the user entered 123456 again but this should calculate that the next sequence for 123456 is "-3"
The same needs to be catered for, when a Product is removed from the IG but to always look at the max sequence number for that product.
I was thinking of possibly using APEX_COLLECTIONS as a means of storing what is currently in the grid, since no changes have been committed to the database.
Assuming you have a collection of product values (in this case, I am using the built-in SYS.ODCINUMBERLIST which is a VARRAY data type) then the SQL for your output would be:
SELECT id,
id AS grid_row,
product,
product || '-' || ROW_NUMBER() OVER ( PARTITION BY product ORDER BY id )
AS product_item
FROM (
SELECT ROWNUM AS id,
COLUMN_VALUE AS product
FROM TABLE(
SYS.ODCINUMBERLIST(
123456,
123456,
556677,
654321,
654321,
654321,
123456
)
)
)
ORDER BY id
Output:
ID | GRID_ROW | PRODUCT | PRODUCT_ITEM
-: | -------: | ------: | :-----------
1 | 1 | 123456 | 123456-1
2 | 2 | 123456 | 123456-2
3 | 3 | 556677 | 556677-1
4 | 4 | 654321 | 654321-1
5 | 5 | 654321 | 654321-2
6 | 6 | 654321 | 654321-3
7 | 7 | 123456 | 123456-3
db<>fiddle here
As you mentioned, the data you enter is not saved into the DB whilst you are inserting your products, so it is not in fact stored anywhere.
So you cannot go check if that value already exists and enter a -2 or other.
Some things to consider would be to maybe save the values into a temp table so you can then have a function go check how many product_item like 123456-% are in there and use that number +1 as your new product_item.
Or you could go the even harder way and do it all with javascript. For this you will need to somehow get all records in the IG, go through them all and see how many occurences of 123456 you have and then insert 123456-(no of occurences + 1).

Printing SSRS report to label printer x times

I am sorry if this is a duplicate question and please point me to the answer if it is.
Here is my situation:
I have an ASP.NET web forms site that uses SQL server database as its data source. The end user wants to print labels to a Zebra label printer. (Old printer Zebra 110XiIIIPlus-200dpi) I can install this printer on the end users system or it can run from the web server, doesn't matter it is a network printer.
I can retrieve the data from the database ok. My problem starts when I need to print. Lets say that I have four parts, p1 p2 p3 & p4. All the labels have the same format:
Job #, Mark #, Customer, Width(in), Length(in) (which all come from the SQL DB)
The only field that is pulled in query and not printed is the qty. Mark # is the part number (Don't know why it isn't just called part #). Now lets say that p1 has a qty of 12, p2 has a qty of 25, p3 has a qty 321, and p4 has a qty of 35.
When it prints I need to send 12 "copies" of the label for p1, 25 "copies" for p2, 321 "copies for p3, and 35 "copies" for p4.
How do I send 12 labels for p1 to be printed, then use the next record's data and send 24 labels etc. etc.?
I do not have any code for printing yet because i can not figure out how to do this!! Does anyone know of a way that I can do it.
I did find one article here on SO: Print a report Multiple times, (SSRS reporting services) but I am not sure how to make it work, if it even can) for what I need.
One last note I am using VB.Net in the code behind if it makes a difference.
Any help is very much appreciated!
I had to do the exact same thing, the solution I came up with was to loop though the select and union the same select by the number of items in the Quantity. By doing this you should get 12 rows for the P1 since that that is the quantity of boxes, all the data should be the same except for Page# that should auto increase by 1 until the end of the quantity.
Results would be something like:
Job# | Mark# | Quantity | Page
------------------------------
1 | P1 | 12 | 1
1 | P1 | 12 | 2
1 | P1 | 12 | 3
1 | P1 | 12 | 4
.....
1 | P1 | 12 | 12
Then you would group on Mark# and Page and Create a Page break between each instance of a group, this will make it so you get the number of pages based on the quantity.
Thanks to the help of SO user newGuy I was able to figure out how to do this. Here is the solution that I cam up with that works.
--Drop Temp tables if exists
If OBJECT_ID('tempdb..#Labels') Is Not Null Drop Table #Labels
If OBJECT_ID('tempdb..#Pieces') Is Not Null Drop Table #Pieces
--Declare variables
Declare #MarkNumber varchar(10)
Declare #Qty int
Declare #RowCount int = 0
Create Table #Labels
(
vjobnum varchar(12),
marknumber varchar(25),
customer varchar(25),
pwidth decimal(18,4),
plength decimal(18,4)
)
Create Table #Pieces
(
marknum varchar(25),
totqty int,
customer varchar(50),
jobnum varchar(12),
plength decimal(18,4),
pwidth decimal(18,4)
)
Insert Into #Pieces(marknum, totqty, customer, jobnum, plength, pwidth)
Select od.marknum, od.qty, oh.customer, oh.van_job_num, od.bbin, od.cbin From tbl_order_detail od Join tbl_order_head oh On oh.ordernum = od.ordernum Where od.ordernum = (Select Distinct ordernum From tbl_BearingBarRpt)
Set #RowCount = (Select COUNT(*) From #Pieces)
While #RowCount > 0 --Exists (Select marknum From #piecelabels)
Begin
Select #MarkNumber = (Select a.marknum From (Select ROW_NUMBER() OVER (Order By marknum) as RowNumbers, *From #Pieces) a Where a.RowNumbers = #RowCount)
Select #Qty = (Select totqty From #Pieces Where marknum = #MarkNumber)
While #Qty > 0
Begin
Insert Into #Labels(vjobnum, marknumber, customer, pwidth, plength)
Select pc.jobnum, pc.marknum, pc.customer, pwidth, plength
From #Pieces pc
Where pc.marknum = #MarkNumber
--Decrement the Qty counter
Set #Qty = #Qty - 1
End
Set #RowCount = #RowCount - 1
End
It may not be the best but it definitely works!

MS Access Query counting instances

I have a sample table with following values
SNO | Mon
-----+-------
100 | 1
101 | 1
102 | 1
100 | 2
101 | 2
102 | 2
100 | 3
101 | 3
Now I need a query to count the total sno's which are in 3 months
The result should be 2, as 100 & 101 are in mon 1,2 and 3. However, 102 is only present in mon 1,2.
Thanks,
RK
This Query in theory should work.
SELECT
tmpTbl.sNo
FROM
tmpTbl
GROUP BY
tmpTbl.sNo
HAVING
Count(tmpTbl.monNo) = (SELECT Count(*) FROM (SELECT tmpTbl.monNo FROM tmpTbl GROUP BY tmpTbl.monNo));
The result would be,
sNo
----
100
101
I have used two SubQueries to get the result. Teh both are used in the HAVING clause of the SQL. First SqubQuery (inner most). Will get the number of Unique Month's available in your table, the outer SubQuery will then Count the number of Unique months. So the Overall Query can be translated as "SELECT the serial number FROM the table HAVING the Count of Month equal to the Number of unique records in the same table".
The reason I used SbQuery instead of a number is because of the fact this will also be applicable when your month number increases. Hope this helps !
EDIT
Here is the Query for getting the count.
SELECT
Count(*) As simpleCount
FROM
(
SELECT
tmpTbl.sNo
FROM
tmpTbl
GROUP BY
tmpTbl.sNo
HAVING
Count(tmpTbl.monNo) = (SELECT Count(*) FROM (SELECT tmpTbl.monNo FROM tmpTbl GROUP BY tmpTbl.monNo))
);

Complicated query with SQLite

I have a complicated query that I just cant create. I'm thinking of doing it with JOIN's but don't know where to start. I'll just try to explain my problem as clearly as possible.
I am creating software that deals with compensations. Every compensation can have several links (like in a chain). Each link is basically a customer. So, I'll just tell you the table structures:
Customer
CustomerID | Name
Compensation
CompensationID | CustomerID | Date
Link
LinkID | CompensationID | CustomerID | Sequential
Now, the Sequential field increases with every link added. Let me demonstrate by filling the tables with some data:
CustomerID | Name
-----------+-----
0 | Foo
1 | Bar
2 | Baz
CompensationID | CustomerID | Date
---------------+------------+------
0 | 0 | 2-2-2010
1 | 1 | 2-3-2010
LinkID | CompensationID | CustomerID | Sequential
-------+----------------+------------+-----------
0 0 0 0
1 0 2 1
2 0 1 2
So no matter what the LinkID/CompensationID/CustomerID the Sequential always goes from 0 to N in the Links table (depending on how many links a compensation has).
Now here is the problem: I want to list all Compensation's whose Link's meet the following requirements:
Search in Links
CustomerID = A
Sequential = 0 (first link) AND Sequential = LAST (in this case 2)
List compensations
Here is what I got so far:
SELECT * FROM Compensation JOIN Link ON Compensation.ID = Link.CompensationID
WHERE Link.CustomerID=A AND Link.Sequential = 0 AND Link.Sequential=LAST
This is more or less pseudo SQL since I know Link.Sequential cant be 0 and another value at the same time, but I don't know how to do this.
Any help would be appreciated.
Thank you.
P.S.
Sorry for the big wall of text.
If subqueries in where statements work how I remember:
select *
from
Compensation
left join
Link
on Compensation.CompensationID = Link.CompensationID
where
Link.CustomerID = :A
AND (
Link.Sequential = 0
OR
Link.Sequential = (
select MAX(Sequential) from Link where Link.CustomerID = :A
)
)
Try
SELECT c.*
FROM Compensation c
JOIN (select CompensationID, MAX(Sequential) AS LastSeq FROM Link GROUP BY CompensationID) AS LastOnes ON c.ID = LastOnes.CompensationID
JOIN (select CompensationID FROM Link WHERE CustomerID=A AND Sequential=0) AS FirststOnes ON c.ID = FirststOnes.CompensationID
JOIN Link AS l on l.CompensationID=c.CompensationID AND l.CustomerID=A AND l.Sequential=LastOnes.LastSeq

Resources