How to back tab in dbeaver? - dbeaver

I pressed tab too many times. Usually to undo the tab I press ctrl+[ in vscode. What is the equivalent for dbeaver?
--For example
select productname
from products
where productid in
(select productid
from
...
--to return back to this
select productname
from products
where productid in
(select productid
from ...

The equivalent for dbeaver is Alt + Tab

Related

Select columns outside of group by

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;

SSRS hidden row become visible with multi group toggle

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.

How to select row from table but if it doesn't exist in that table, select from different table?

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'

Microsoft Access 2010 - Foreign Key as Dropdown

I have two tables that I display in a form:
tblUsers -> user_id, firstname, lastname, group_id
tblGroups -> group_id, groupName, groupDesc
I can get the data that I want with:
SELECT tblUsers.firstname, tblUsers.lastname, tblGroups.groupName
FROM tblGroups INNER JOIN tblUsers ON tblGroups.[group_id] = tblUsers.[group_id];
But what I need is a form that shows the user information and a dropdown list for the group but showing the name of the group not the ID and so the group can be changed for a specific user e.g. Manager, Editor etc...
Thanks,
Gareth
Right-click on the ComboBox (assuming it's a ComboBox...) and open the Properties window.
Set the Row Source to:
Select Group_ID, GroupName from tblGroups
Set the Column Count to 2, because you want the combo to store both the ID and the Group name
Set the Column Widths to "0; 2" (without the quotes). This will essentially hide the ID because the column width of the ID field is 0.
Set the Bound Column to 1, because you want to bind to the ID column and not the Group Name column, because the Group ID is easier to query with.
Then you reference the combo with Me!MyComboboxName.Value to get the Group ID.

SQL Temp Table not Throwing Invalid Column Name Error when it should

I have found the following issue in SQL Server 2008 Management Studio. When I run the following script as whole I expect errors (due to a "copy and paste error"), but don't receive them.
IF OBJECT_ID('Foo') IS NOT NULL
BEGIN
DROP TABLE Foo
END
IF OBJECT_ID('Bar') IS NOT NULL
BEGIN
DROP TABLE Bar
END
CREATE TABLE Foo (
FooID int
)
Create Table Bar (
BarID int
, FooID int
)
INSERT INTO Foo
SELECT 1 UNION ALL SELECT 2
INSERT INTO Bar
SELECT 1,1 UNION ALL SELECT 2,1 UNION ALL SELECT 3,1
GO
IF OBJECT_ID('tempdb..#temp') IS NOT NULL
BEGIN
DROP TABLE #TEMP
END
GO
CREATE TABLE #TEMP (
FooID int
)
INSERT INTO #TEMP
SELECT FooID FROM Bar
GO
SELECT * FROM Foo WHERE FooID IN (SELECT FooID FROM #TEMP)
GO
SELECT * FROM Bar WHERE BarID IN (SELECT BarID FROM #TEMP)
GO
SELECT * FROM #TEMP
GO
The second last statement containing the where clause filter on "SELECT BarID FROM #TEMP" runs, but there is no BarID column in #TEMP. When running the script as a whole I receive no error and the script return all rows in Foo. However when I run the command on its own then I get the error informing me that there is no BarId in #TEMP.
Is there a reason for this? Is it my code?
Your second to last query actually is working correctly.
Other commenters please correct me, but I understand this to be a correlated subquery. Table aliasing will show what is actually going on. Your query is equivalent to this:
SELECT * FROM Bar x WHERE x.BarID IN (SELECT x.BarID FROM #TEMP)
For each row in #TEMP, the nested select is actually returning the current value of BarID from table Bar, so yes, BarID in (BarID) is true, so every row in Bar is matched, so every row is returned.
To show that you're not crazy, try
SELECT * FROM Bar WHERE BarID IN (SELECT NonExistentFieldName FROM #TEMP)
This raises the error I think you're expecting.
For some background on Neil's answer, please see the following KB article:
http://support.microsoft.com/kb/298674
The example they use (that works though it appears that it shouldn't) is quite similar to your example (though without #temp tables):
CREATE TABLE X1 (ColA INT, ColB INT)
CREATE TABLE X2 (ColC INT, ColD INT)
SELECT ColA FROM X1 WHERE ColA IN (Select ColB FROM X2)
They also show that adding the table alias (which is a good practice for many reasons, but would prevent this problem from getting past compilation) makes the query fail as you originally expected.
This is actually following the ANSI standard for column resolution, and is not going to change (unless they implement Erland's SET STRICT_CHECKS ON).
Some others who have complained about this bug (and a lot more background on why the engine has to work this way):
http://connect.microsoft.com/SQL/feedback/details/542289/
http://connect.microsoft.com/SQL/feedback/details/422252/
http://connect.microsoft.com/SQL/feedback/details/126785/
Have to agree with #BalamBalam's comment: why are you writing invalid code, and then complaining that it's mistakenly taken as valid?