So I have two tables,
From the first table app_catalog, I want to select all the data,
the complete the condition for the second table name statistics_log
So at the second table, I want to select app_id that relates to the first table and the most important thing I want to count the field log_type for each app and do a grouping and get 10 with max number.
I am really trying to get this done, I would appreciate every help thank you!!
Thank you but i manage to solve it,
SELECT * FROM public.app_catalog as a , (SELECT app_id , COUNT(*) as count
FROM public.statistics_log
where logged_at between '2019-08-20 12:22:40.186' and '2019-08-22 12:22:40.186'
GROUP BY app_id
LIMIT 10) AS App_id
where App_id.app_id = a.app_catalog_id
Related
This may be a simple fix but Im somehow drawing a blank. I have this code below, and I want the results that I got from it to be added into their own column in an existing table. How would i go about doing this.
Select full_name, SUM(total) as sum_sales
FROM loyalty
where invoiceyear = 2013
GROUP BY full_name
order by sum_sales DESC;
This leaves me with one column with the name of employee and the second with their sales from that year.
How can i just take these results and add them into a column in addition to the table
Is it as simple as...
Alter table loyalty
Add column "2013 sales"
and then add in some sort of condition?
Any help would be greatly appreciated!
If i got your question right, you need to first alter the table allowing the new field to be null (you can change it later on) then you could use an insert clause to store the value permanently.
I have a problem to formulate an sql question in postgresql, hoping to get some help here
I have a table called visitor that contains an column called fk_employee_id, fk_employee_id contains different number between 1-10, example:
1,3,4,6,4,6,7,3,2,1,6,7,6
Now I want to find out which value that is the most frequent in this column (in this case 6) I have made an question that seem to solve my question;
SELECT fk_employee_id
FROM visitor
GROUP BY fk_employee_id
ORDER BY COUNT(fk_employee_id) DESC
LIMIT 1
but this question, doesn't get right if it is two values that are the most frequent one. So instead I try to write a question which contains max function but cant figure out how, anyone now how to do this?
We can use RANK here to slightly modify your current query:
WITH cte AS (
SELECT
fk_employee_id,
RANK() OVER (ORDER BY COUNT(*) DESC) rank
FROM visitor
GROUP BY fk_employee_id
)
SELECT fk_employee_id
FROM cte
WHERE rank = 1;
Demo
I would be grateful if you could help me.
I would like to write a select statement which would do the following:
"Select a random ID from the 4 lowest ID in the same table"
Note: ID may be for example the score of students from a list of around 100 records in a table. I would like to get the 4 students with the lowest score and finally pick one record randomly.
Thanks lots
I find out about the following code:
SELECT * FROM (select * from tablename ORDER BY status ASC limit 4) q ORDER BY RAND() LIMIT 0,1;
Kindly confirmed whether it is a good answers and proper way to do the select. Any answers would be welcomes
thanks much
I have PostgreSQL table:
Username1 SomeBytes1
Username2 SomeBytes1
Username1 SomeBytes1
Username1 SomeBytes1
I need to get some rows from with name Username1 but from the end of the table. For example i need last to rows with Username1
select from my_table where user = Username1 LIMIT 2
Gives me first 2 rows, but i need last two.
How can i select it?
Thank you.
first and last in a table is very arbitrary. In order to have a good predictable result you should always have an order by clause. And if you have that, then getting the last two rows will become easy.
For instance, if you have a primary key or something like an ID (which is populated by a sequence), then you can do:
select * from my_table where user = 'Username1' order by ID desc limit 2.
desc tells the database to sort the rows in reverse order, which means that last will be first.
Does your table have a primary key ? / Can your table be sorted?
Because the notion of 'first' and 'last' implies some sorting of the tuples. If this is the case, you could sort the data the other way around, so that your 'last' entries are on top. Then you can access them with the statement you tried.
To view tail of a table you may use ctid. It is a temporary physical identifier of a record in PostgreSQL.
SELECT * from my_table
WHERE user = Username1
ORDER BY ctid DESC
LIMIT 2
I have to fetch only 50 records at a time from database (DB2), for this I have been using Row_Number but now the persons are telling that this Row_Number is not stable and has bugs in it so now I have to write a different query for the same as I have to fetch only 50 records at a time.
So please can any body help me out for the same?
Thanks in advance.
The query which I have been using is
SELECT PLC.*
FROM
(SELECT
ROW_NUMBER() OVER (ORDER BY PRDLN_CTLG_OID) AS Row,
PRDLN_CTLG_OID,
PRODUCT_LINE_OID AS PRODUCT_LINE_OID,
RTRIM(CATALOG_ID) AS CATALOG_ID,
FROM
PROD_LINE_CATALOG
WHERE
PRODUCT_LINE_OID = :productLineOID AND ACTV_IND = 1
ORDER BY CATALOG_ID) PLC
WHERE
Row >= :startIndex AND Row <= :endIndex
ORDER BY
PLC.CATALOG_ID DESC
WITH UR
use the fetch clause
FETCH FIRST 50 ROWS ONLY
edit
It looks like you use a startIndex and endIndex value. I realize you compare these to Row, but do you have another index to compare? If not, and you insist on moving from Row_Number, maybe the table needs another index.
Select PLC.* From (Select omitting row_number())
Where yournewindex >= startIndex
Fetch first 50 rows only
Someone already answered that question. You are asking for pagination if I'm not wrong.
Fastest most/efficient way to do pagination with SQL searching DB2