I'm scratching my head over this one as I can get this code to work in SQL but when I transfer it to a simple gridview the code errors out stating 'Invalid Object name tablename'.
What I'm attempting to do is take a static number and subtract it from three different columns in the same table.
This is what I have that works within SQL:
SELECT
(3000)
-
(SELECT COUNT(column1) from table1 where column1 = 'Agreed')
+
(SELECT COUNT(column2) from table1 where column2 = 'Agreed')
+
(SELECT COUNT(column3) from table1 where column3 = 'Agreed')
AS subtract
I've tried moving the static total around as such
(3000)
-
SELECT
(SELECT COUNT(column1) from table1 where column1 = 'Agreed')
+
(SELECT COUNT(column2) from table1 where column2 = 'Agreed')
+
(SELECT COUNT(column3) from table1 where column3 = 'Agreed')
AS subtract
And....
'3000'
-
SELECT
(SELECT COUNT(column1) from table1 where column1 = 'Agreed')
+
(SELECT COUNT(column2) from table1 where column2 = 'Agreed')
+
(SELECT COUNT(column3) from table1 where column3 = 'Agreed')
AS subtract
But both return syntax errors in SQL.
What I'm hoping to get is the remainder of taking the totals of the three select statements from the static number.
Am I missing something simple here? I'm stumped as to why this would work within SQL but not when I transfer the code to a gridview.
::edit::
See Answer below for the solution. Had to re-write the code to get it to work.
SELECT
(3000)
-
a.c
+
b.c
+
c.c
from (SELECT COUNT(*) AS c from table1 where column1 = 'Agreed') a
left join (SELECT COUNT(*) AS c from table1 where column2 = 'Agreed') b on 1=1
left join (SELECT COUNT(*) AS c from table1 where column3 = 'Agreed') c on 1=1
The first and second queries are not the same at all :
SELECT 3000 - 10
Is not the same that :
3000 - SELECT 10
The second one is not valid because it do not begin by the SELECT statement (as you did in the first). It is not a ASP.NET specifiec issue.
EDIT :
What about this query :
SELECT
(3000)
-
a.c
+
b.c
+
c.c
from (SELECT COUNT(*) AS c from table1 where column1 = 'Agreed') AS a
left join (SELECT COUNT(*) AS c from table1 where column2 = 'Agreed') AS b on 1=1
left join (SELECT COUNT(*) AS c from table1 where column3 = 'Agreed') AS c on 1=1
Related
I have a join SQL Request where the output of 2 tables is produced :
ss= 'Select t.*, p.* from table1 t, table1 p where t.table2_id=p.table1_id'
df = pd.read_sql_query(ss, self.db_con)
However, some columns have same name since the table is stripped.
(ex: id, id ...)
How to get table.colname in the dataframe ?
Thanks
You have to rename the conflicting columns with AS:
SELECT t.id AS t_id, p.id AS p_id, FROM table1 t, table2 p WHERE t.id = p.id
I want to find the distinct pairs of names in the table which have the same exact items in the items column. For instance:
CREATE TABLE t
(
name VARCHAR(255),
item VARCHAR(255)
);
INSERT INTO t VALUES("Alice", "Orange");
INSERT INTO t VALUES("Alice", "Pear");
INSERT INTO t VALUES("Alice", "Lemon");
INSERT INTO t VALUES("Bob", "Orange");
INSERT INTO t VALUES("Bob", "Pear");
INSERT INTO t VALUES("Bob", "Lemon");
INSERT INTO t VALUES("Charlie", "Pear");
INSERT INTO t VALUES("Charlie", "Lemon");
The answer here would be Alice,Bob because they took the exact same items.
I want to do it with double negation (using NOT EXISTS/NOT IN) only which I think is more well-suited to this question, but I couldn't come up with anything that is remotely close to being functional.
This is somewhat similar to this question but I'm using SQLite so I cannot use GROUP_CONCAT() but I was wondering how it would be done using relational division using NOT EXISTS/NOT IN.
To get the number of common items between all pairs of names you can use the following query:
SELECT t1.name AS name1, t2.name AS name2, COUNT(*) AS cnt
FROM t AS t1
INNER JOIN t AS t2 ON t1.item = t2.item AND t1.name < t2.name
GROUP BY t1.name, t2.name
Output:
name1 name2 cnt
------------------------
Alice Bob 3
Alice Charlie 2
Bob Charlie 2
Now all you want is to filter out (name1, name2) pairs having a count that is not equal to the number of items of name1 and name2. You can do this using a HAVING clause with correlated subqueries:
SELECT t1.name AS name1, t2.name AS name2
FROM t AS t1
INNER JOIN t AS t2 ON t1.item = t2.item AND t1.name < t2.name
GROUP BY t1.name, t2.name
HAVING COUNT(*) = (SELECT COUNT(*) FROM t WHERE name = t1.name) AND
COUNT(*) = (SELECT COUNT(*) FROM t WHERE name = t2.name)
Demo here
With compound queries:
SELECT t1.name, t2.name
FROM t AS t1, t AS t2
GROUP BY t1.name, t2.name
HAVING t1.name < t2.name
AND NOT EXISTS (SELECT item FROM t WHERE name = t1.name
EXCEPT
SELECT item FROM t WHERE name = t2.name)
AND NOT EXISTS (SELECT item FROM t WHERE name = t2.name
EXCEPT
SELECT item FROM t WHERE name = t1.name);
Using NOT IN is possible, bit expresses exactly the same mechanism with more complexity:
SELECT t1.name, t2.name
FROM t AS t1, t AS t2
GROUP BY t1.name, t2.name
HAVING t1.name < t2.name
AND NOT EXISTS (SELECT item
FROM t
WHERE name = t1.name
AND item NOT IN (SELECT item
FROM t
WHERE name = t2.name))
AND NOT EXISTS (SELECT item
FROM t
WHERE name = t2.name
AND item NOT IN (SELECT item
FROM t
WHERE name = t1.name));
This seems to be working with SQLLite
select t1.name
from t t1
join t t2 on t1.name <> t2.name and t1.item = t2.item
join (select name, count(*) as cnt from t group by name) t3 on t3.name = t1.name
join (select name, count(*) as cnt from t group by name) t4 on t4.name = t2.name
group by t1.name, t3.cnt, t4.cnt
having count(*) = max(t3.cnt, t4.cnt)
I might have found a solution to your issue. Mine was tested using MySQL, but it's not using GROUP_CONCAT(). It might work for your SQLite database. My query is used to find people who have bought the same exact items.
Try using this statement:
SELECT DISTINCT e1.name, e2.name from t e1, t e2 WHERE e1.item=e2.item AND e1.name != e2.name GROUP BY e1.item HAVING count(*) >1;
https://gyazo.com/5e5e9d0ddfb33cb47439a674297108ed
I have a table in my database I do a Select all on:
SELECT * FROM TableA;
I want to append a column that is true or false if there's a related column in anther table. I can do it with this:
SELECT *, (SELECT COUNT(Id) > 0 FROM TableB WHERE Id = TableA.Id) FROM TableA;
But I don't want to have to count EVERY row in TableB to work this out as its ineffient. I essentially want an EXISTS check instead of count.
How do I replace the COUNT with EXISTS?
Thanks!
By using a left join
SELECT a.*, b.id is not null as condition_check
FROM TableA a
LEFT JOIN TableB b ON a.Id = b.Id
Ah - just realised the answer to my own question
SELECT *, EXISTS(SELECT Id FROM TableB WHERE Id= TableA.Id) AS DoesExist FROM TableA
I want to do the following in LINQ to SQL:
Select count(*) as count_1,
(select count(*) from tableName2) as count_2 FROM tableName
Where x = y
The result should be
Column 1 | column 2
--------------------
50 34
What you need to do is something like this:
select
(select count(*)
from tableName
where x = y) as count_1,
(select count(*)
from tableName2) as count_2
I have a query in SQLite where I group by a certain column and use an aggregate function MAX on another column in the select statement. Now I also want the rowid of the row which holds the value that is displayed by the MAX aggregate. I know that this must be a unique row because of the primary key constraint. I can't figure out how to write the query. See the following example:
create table t1 (c1, c2, constraint t1_pk primary key (c1, c2));
insert into t1 values ('boys', 1);
insert into t1 values ('boys', 2);
insert into t1 values ('girls', 1);
insert into t1 values ('girls', 2);
Now I have the table with the primary constraint over both columns. A SELECT query for the table gives the following output:
sqlite> select rowid, * from t1;
rowid|c1|c2
1|boys|1
2|boys|2
3|girls|1
4|girls|2
Now I want to group by c1 and select the MAX of c2. Then I want the rowid of the row which holds the values displayed now. See the following queries:
sqlite> select rowid, c1, max(c2) from t1 group by c1;
rowid|c1|max(c2)
2|boys|2
4|girls|2
sqlite> select rowid, c1, min(c2) from t1 group by c1;
rowid|c1|min(c2)
2|boys|1
4|girls|1
The second query with the MIN aggregate should return the rowids of the rows holding the MIN values, this is what I want to achieve:
rowid|c1|min(c2)
1|boys|1
3|girls|1
Now I've tried the following subselect, which doesn't work either because it gives an error:
sqlite> select (select rowid from t1 b where b.c1 = a.c1 and b.c2 = max(a.c2)), a.c1, max(a.c2) from t1 a group by a.c1;
Error: misuse of aggregate function max()
sqlite> select (select rowid from t1 b where b.c1 = a.c1 and b.c2 = min(a.c2)), a.c1, min(a.c2) from t1 a group by a.c1;
Error: misuse of aggregate function min()
The last thing I've tried is a subquery in the FROM clause, which also doesn't work:
sqlite> select
...> (select rowid from t1 b where b.c1 = c.c1 and b.c2 = c.c2),
...> c1,
...> c2
...> from
...> (select a.c1, max(a.c2) as c2 from t1 a group by a.c1) c;
Error: misuse of aggregate: max()
sqlite> select
...> (select rowid from t1 b where b.c1 = c.c1 and b.c2 = max(c.c2)),
...> c.c1,
...> max(c.c2)
...> from
...> (select a.c1, a.c2 from t1 a group by a.c1) c;
Error: misuse of aggregate function max()
Is there any solution for my problem? I really don't know what else I could try.
If I understood your question correctly, try like this:
select rowid, c1, min(c2) from t1 a
where c2=(select min(c2) from t1 b where b.c1=a.c1)
group by rowid,c1;
check the FIDDLE
Indeed, following the answer by #Pradeeshnarayan, I have been obliged to improve it to make it work in Oracle.
"Group by" clause is useless
select rowid, c1, c2 from t1 a
where c2=(select min(c2) from t1 b where b.c1=a.c1);