I have multi group query
I want the only first one to show and plus sign next to it so when user click on the plus sign it opens the lower level group and it has another plus sign
+USA
+Australia
When click the sign next to USA it should show this
-USA
+Texas
+California
+Australia
when click on the sign next to Texas it should show this
-USA
-Texas
+Dallas
+Austin
+Houston
+California
+Australia
When click on Dallas it shows
-USA
-Texas
-Dallas
Tom
+Austin
+Houston
+California
+Australia
I have done the report by making all rows hidden and toggled by the row above
but for some reason the when i do it for CName toggled by C3 it works, now when I make C3 hidden and toggled by C2 for some reason CName shows again!!
I dont know why, any one know why?
Thanks
This is my query
select 'USA'as C1,'Texas' as C2,'Dallas'as C3,'Tom' as CName UNION
select 'USA'as C1,'Texas' as C2,'Austin'as C3,'Adam' as CName UNION
select 'USA'as C1,'Texas' as C2,'Huoston'as C3,'Ken' as CName UNION
select 'USA'as C1,'California' as C2,'Los Angeles'as C3,'Dave' as CName UNION
select 'USA'as C1,'California' as C2,'San Fransisco'as C3,'Sam' as CName UNION
select 'USA'as C1,'California' as C2,'Hollywood'as C3,'Sean' as CName UNION
select 'Australia'as C1,'NSW' as C2,'Sydney'as C3,'Richard' as CName UNION
select 'Australia'as C1,'NSW' as C2,'Dubbo'as C3,'Arnold' as CName UNION
select 'Australia'as C1,'VIC' as C2,'Melbourne'as C3,'Mike' as CName UNION
select 'Australia'as C1,'VIC' as C2,'Doncaster'as C3,'Matt' as CName UNION
select 'Australia'as C1,'VIC' as C2,'Craigieburn'as C3,'Kate' as CName UNION
select 'Australia'as C1,'QLD' as C2,'Brisbane'as C3,'Edward
If I'm understanding correctly, I think the simplest/easiest option is going through the report wizard and create a new tabular report, group by C1, C2, and C3 with CName being the details. Make it stepped and enable DrillDown. The final result should function as you wish.
Related
i have a table that stores amenities of a room (wifi,tv etc) a room can have many amenities i want to make a column where every amenity will be atomic
id
amenity_name
1
tv
2
wifi
3
bed
4
Smokling allowed
current table :
id
Another header
1
Wifi,Breakfast
2
Wifi,Kitchen,Smoking allowed,Pets allowed,Heating,Washer,Essentials,Lock on bedroom door,24-hour check-in,Hangers,Hair dryer,Laptop friendly workspace
i have tried using regexp_split_to_table but i can't make anything out from this function
any ideas?
thanks.
Try a lateral join:
SELECT tab.id, a.name
FROM tab
CROSS JOIN LATERAL regexp_split_to_table(tab.amenity_name, ',') AS a(name);
I am looking to select the first and last click by each ID, along with the corresponding source. Here is a sample table:
ID Click Source
--------------------------
1 1 Google
1 2 Facebook
1 3 Yahoo
2 1 Google
2 2 Yahoo
3 1 Facebook
4 1 Yahoo
5 1 Pinterest
5 2 Google
Here is the desired result:
ID First Last
-------------------------
1 Google Yahoo
2 Google Yahoo
3 Facebook Facebook
4 Yahoo Yahoo
5 Pinterest Google
I've already managed to get the first click by simply setting click=1 in the where clause. I am not able to get MAX(click) without grouping by ID and Source. When I include the Source in the group by I don't get the results I want.
You can join two derived tables getting the first and last click per id using DISTINCT ON on the common ID.
SELECT f.id,
f.source "first",
s.source "last"
FROM (SELECT DISTINCT ON (id)
id,
source
FROM elbat
ORDER BY id ASC,
click ASC) f
INNER JOIN (SELECT DISTINCT ON (id)
id,
source
FROM elbat
ORDER BY id ASC,
click DESC) s
ON s.id = f.id;
db<>fiddle
sticky bit's solution is quite good, but you can also do this with window functions. You should test to see which one works better for you:
select distinct id,
first_value(source) OVER (partition by id order by click),
last_value(source) OVER (partition by id order by click
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
FROM your_table
ORDER BY id;
I have a table "user" with columns ip,os,country and browser. I want to find the ip,os,country and browser with maximum count.Is there any query for that in PostgreSQL
The current query I'm using is
SELECT *
FROM
(
SELECT COUNT(ip),ip FROM user GROUP BY ip
UNION ALL
SELECT COUNT(os),os FROM user GROUP BY os
UNION ALL
SELECT COUNT(country),country FROM user GROUP BY country
UNION ALL
SELECT COUNT(browser),browser FROM user GROUP BY browser
) user
it shows all ip,os,country and browser and their count
what i really want is a column name the max count of that column
is it possible to do that in a single query?
Im expecting something like this
os count ip count
linux 50 xx:xx:xx:xx 95
SELECT *
FROM
(SELECT COUNT(ip) as cnt_ip, ip FROM user GROUP BY ip ORDER BY 1 DESC LIMIT 1) as t_ip,
(SELECT COUNT(os) as cnt_os, os FROM user GROUP BY os ORDER BY 1 DESC LIMIT 1) as t_os,
(SELECT COUNT(country) as cnt_country, country FROM user GROUP BY country ORDER BY 1 DESC LIMIT 1) as t_country,
(SELECT COUNT(browser) as cnt_browser, browser FROM user GROUP BY browser ORDER BY 1 DESC LIMIT 1) as t_browser
You may use HAVING and ALL for that. Due to readability purpose, I'll show just for one column
SELECT COUNT(ip),ip
FROM user
GROUP BY ip
HAVING COUNT(ip) >= all
(
SELECT COUNT(ip)
FROM user
GROUP BY ip
)
I have two tables from which I want to make a join with some columns to provide a view for my java/hibernate application. It looks like this:
CREATE VIEW customer_contacts AS cc
SELECT DISTINCT ON (cust.id) cust.id
cust.company
cust.zip
...
con.name
con.forename
...
FROM contacts con
LEFT JOIN customer cust ON con.customer = cust.id
ORDER BY cust.id
So far so good. Very simple.
If I make a SELECT on the view like:
SELECT *
FROM cc
WHERE name ilike '%schult%'
I get 13 results.
If I make the same query directly with the view statement
SELECT DISTINCT ON (cust.id) cust.id
cust.company
cust.zip
...
con.name
con.forename
...
FROM contacts con
LEFT JOIN customer cust ON con.customer = cust.id
WHERE name ilike '%schult%'
ORDER BY cust.id
I got 75 results!
I figured out that it is the DISTINCT that corrupts the result. But why?
And how can I use it correctly?
Your queries (view based and direct) have different order of applying condition:
direct query searches for %shult% and then applies distinct on
view applies distinct on and then searches for %shult%
Are you aware how distinct on works?
It selects first row (it may be undeterministic if proper sort is not defined) for given attributes and leaves other.
For instance:
Let's say we have customer with id=1 and two connected contacts one with name='Schultz' and one with name='Schmidt'.
Now view based select will apply distinct on and select customer with some contact (first one, undeterministic in this case), then name ilike '%schult%' will be applied - it may happen that Schultz will be removed by distinct on.
Recommended reading:
https://www.postgresql.org/docs/9.0/static/sql-select.html#SQL-DISTINCT
This may be a really simple problem that I'm over-looking but i have this query:
SELECT is_accountant from users where customer_id='cus_4znUZe3lAy26FT'
(the customer id will vary, I'm using a node js script to grab a bunch of different customer ids to find out if they're accountants or not)
If there is no result, I want to search in a table called deleted_users for the same customer id ie:
SELECT is_accountant from deleted_users where customer_id='cus_4znUZe3lAy26FT'
Is there a way to do this within Postgresql?
SELECT coalesce(u.is_accountant, d.is_accountant)
from deleted_users d
full outer join users u on u.id = d.id
where 'cus_4znUZe3lAy26FT' in (d.customer_id, u.customer_id)
SELECT is_accountant from users where customer_id='cus_4znUZe3lAy26FT'
union all
SELECT is_accountant from deleted_users where customer_id='cus_4znUZe3lAy26FT'
and not exists
(SELECT is_accountant from users where customer_id='cus_4znUZe3lAy26FT')
or, as you are interested to know if user "is (or was)" accountant:
(does it matter in which table the user is? Agree?)
SELECT is_accountant from users where customer_id='cus_4znUZe3lAy26FT'
union
SELECT is_accountant from deleted_users where customer_id='cus_4znUZe3lAy26FT'