Table_name
pno,pyr,cname,pname
1,2008,X,Y
1,2008,X,Z
2,2009,A,Y
If cname or pname count more than 1
How can I get the result like this
1,2008,X
1,2008,X
Related
I have string values get from a table using listagg(column,',')
so I want to loop this string list and set into where clause for another table
then I want to get a count when no any records in the table (Number of times with no record)
I'm writing this inside the plsql procedure
order_id
name
10
test1
20
test2
22
test3
25
test4
col_id
product
order_id
1
pro1
10
2
pro2
30
3
pro2
38
expected result : count(Number of times with no record) in 2nd table
count = 3
because there is no any record of 20,22,25 order ids in 2nd table
only have record for order_id - 10
my queries
SELECT listagg(ord.order_id,',')
into wk_orderids
from orders ord,
where ord.id_no = wk_id_no;
loop
-- do my stuff
end loop
wk_orderids values = ('10','20','22','25')
I want to loop this one(wk_orderids) and set it one by one into a select query where clause
then want to get the count Number of times with no record
If you want to count ORDER_IDs in the 2nd table that don't exist in ORDER_ID column of the 1st table, then your current approach looks as if you were given a task to do that in the most complicated way. Aggregating values, looping through them, adding values into a where clause (which then requires dynamic SQL) ... OK, but - why? Why not simply
select count(*)
from (select order_id from first_table
minus
select order_id from second_table
);
I have a df that looks like this:
id query
1 select * from table1 where col1 = 1
2 select a.columns FROM table2 a
I want to only select the string (table if you know sql) after the string FROM into a new column. FROM can be spelled with different capitalizations (ie From, from,FROM,etc).
How do I select the string directly after the From but not the very next string after the FROM string
I tried:
df['tableName'] = df['query'].str.extract('[^from]*$')
but this is not working. I am not sure if I should make the entire df lowercase right off the bat.
New df should look like this:
id query tableName
1 select * from table1 where col1 = 1 table1
2 select a.columns FROM table2 a table2
Thank you in advance.
You can try
df['tableName'] = df['query'].str.extract('(?i)from ([^ ]*)')
(?i) means ignore case.
print(df)
id query tableName
0 1 select * from table1 where col1 = 1 table1
1 2 select a.columns FROM table2 a table2
This will get you your answer without a regex and should account for all capitalizations types of "table"
df['Table_Name'] = df['query'].apply(lambda x : x.lower().split('from')[1]).apply(lambda x : x.split()[0])
Other similar questions deal these two problems separately, but I want to merge them in a single statement. I'm using Python3 with psycopg2 library on a PostgreSQL database.
I have a table with 3 fields: ID, name, bool (BIT)
I want to insert a row in this table only if does not exists an other row with same 'name' and 'bool' = 0 and the total count of rows with same ID is less than a given threshold.
More specific my table should contain at most a given threshold number of rows with same ID. Those rows can have the same 'name', but only one of those rows with same ID and same 'name' can have 'bool'= 0.
I tried with this:
INSERT INTO table
SELECT 12345, abcdf , 0 FROM table
WHERE NOT EXISTS(
SELECT * FROM table
WHERE ID = 12345 AND name = abcdf AND bool = 0)
HAVING (SELECT count(*) FROM table
WHERE ID = 12345) < threshold
RETURNING ID;
but the row is inserted anyway.
Then I tried the same statement replacing 'HAVING' with 'AND', but it insert all the threshold rows together.
I have a situation like this (T-SQL):
Table 1: dbo.Printers
EmulationID EmulationDescription PrinterID Name
34,15,2 NULL 12 HP 1234
15,2 NULL 13 IBM 321
15 NULL 14 XYZ
Table 2: dbo.Emulations
EmulationID Description
34 HP
15 IBM
2 Dell
EmulationID column in dbo.Printers table is nvarchar/unicode string datatype, and integer datatype in the dbo.Emulations table.
Now I have to UPDATE the **EmulationDescription** column in the dbo.Printers table using a lookup on the dbo.Emulations table through the EmulationID column.
I need to get data like this in the dbo.Printers table:
EmulationID EmulationDescription PrinterID Name
34,15,2 HP,IBM,Dell 12 HP 1234
15,2 IBM,Dell 13 IBM 321
15 IBM 14 XYZ
Can someone help me in detail, on how to get this issue resolved ?
I created the user-defined function dbo.ParseIdListToTable to convert string data in one row into multiple rows. However, I do not know to proceed further, on how to exactly join and then update.
Any suggestion will be greatly appreciated.
You could do something like this:
CREATE FUNCTION [dbo].[CSVToTable] (#InStr VARCHAR(MAX))
RETURNS #TempTab TABLE
(id int not null)
AS
BEGIN
;-- Ensure input ends with comma
SET #InStr = REPLACE(#InStr + ',', ',,', ',')
DECLARE #SP INT
DECLARE #VALUE VARCHAR(1000)
WHILE PATINDEX('%,%', #INSTR ) <> 0
BEGIN
SELECT #SP = PATINDEX('%,%',#INSTR)
SELECT #VALUE = LEFT(#INSTR , #SP - 1)
SELECT #INSTR = STUFF(#INSTR, 1, #SP, '')
INSERT INTO #TempTab(id) VALUES (#VALUE)
END
RETURN
END
GO
DECLARE #Description VARCHAR(1000)
SELECT P.EmulationID,
(SELECT #Description = COALESCE(#Description + ',', '') + QUOTENAME(Description)
FROM dbo.Emulations
WHERE EmulationID IN (SELECT * FROM dbo.CSVToTable(P.EmulationID))) AS 'Emulation Description,
P.PrinterID,
P.Name
FROM dbo.Printers P
There are two tables:
Student(sid integer, sname varchar(20), programme varchar(4), level integer, age integer)
Tutor(tid integer, tname varchar(20))
The task is to find the names of all IT students who are enrolled in a class taught by a tutor whose surname is Hoffman.
This is what I have so far:
SELECT s.sname
FROM student s, tutor t
WHERE s.programme = 'IT' AND t.tname = '%Hoffman';
However, the '%Hoffman' part will only search for a string which contains 'Hoffman', so technically if if there would be anyone with Hoffman as the first name, the query will return his data too.
The tname column is of type String and consist of a first name, a space, and a last name, and possible middle names in between.
How do you choose the last string (or index?) of a value in t.tname in this case?
You could use something like this:
WHERE s.programme = 'IT' and RIGHT(t.tname, 7)
since 'Hoffman' has 7 characters