Cant put sqLite query in tableAdapter - vb.net

I have a question for you guys.
I have an sqlite query that i test on SqlFiddle and i know it work, but i cant put it in a table adapter in Vb.net.
here's my query:
select cola, colb,
SUM(case when (tbl = 'a') then 1 else 0 END) as TableA,
SUM(case when (tbl = 'b') then 1 else 0 END) as TableB,
SUM(case when (tbl = 'c') then 1 else 0 END) as TableC
from
(
select cola,colb,'a' as tbl
from TableA
union all
select cola, colb,'b' as tbl
from TableB
union all
select cola, colb,'c' as tbl
from TableC
) d
group by cola, colb
The error that i get in Visual studio 2010 is:
Error in select clause: expression near 'END'
Missing FROM clause
Error in select clause: expression near ','
Error in select clause: expression near 'FROM'
Unable to parse query text.
Am I doing something wrong?
Thanks in advance.

Related

How to create a pivot table in PostgreSQL

I am looking to essentially create a pivot view using PostgreSQL, such that the table below:
Column A
Column B
Happy
Sad
Sad
Happy
Happy
Sad
becomes
Count
Column A
Column B
Happy
2
1
Sad
1
2
I've been able to use case/when operators far enough such that I can see the counts under independent columns,
SELECT
COUNT(CASE WHEN column1 = 'Happy' THEN 1 END) AS column1_Happy_count,
COUNT(CASE WHEN column1 = 'Sad' THEN 1 END) AS column1_Sad_count,
COUNT(CASE WHEN column2 = 'Happy' THEN 1 END) AS column2_Happy_count,
COUNT(CASE WHEN column2 = 'Sad' THEN 1 END) AS column2_Sad_count,
COUNT(CASE WHEN column3 = 'Happy' THEN 1 END) AS column3_Happy_count,
COUNT(CASE WHEN column3 = 'Sad' THEN 1 END) AS column3_Sad_count
FROM your_table;
but am missing the step to essentially each the pair of columns vertically.
I'm unable to use extensions such as tablefunc and crosstab.
Try this:
CREATE TABLE my_table (
column_a varchar(10),
column_b varchar(10)
);
INSERT INTO my_table (column_a, column_b)
VALUES ('Happy', 'Sad'),
('Sad', 'Happy'),
('Happy', 'Sad'),
('Good', 'Bad');
WITH DataSource (col, val) AS
(
SELECT 'a', column_a
FROM my_table
UNION ALL
SELECT 'b', column_b
FROM my_table
)
SELECT uniq.val AS "Count"
,MAX(case when counts.col = 'a' then counts end) AS "Column A"
,MAX(case when counts.col = 'b' then counts end) AS "Column B"
FROM
(
SELECT DISTINCT val
FROM DataSource
) uniq
INNER JOIN
(
SELECT col
,val
,COUNT(*) counts
FROM DataSource
GROUP BY col
,val
) counts
ON uniq.val = counts.val
GROUP BY uniq.val
will give you this:
You may aggregate for ColumnA, aggregate for ColumnB then do a full join as the following:
select coalesce(A.ColumnA ,B.ColumnB) as "Count",
A.cnt as "Column A",
B.cnt as "Column B"
from
(
select ColumnA, count(*) cnt
from tbl_name
group by ColumnA
) A
full join
(
select ColumnB, count(*) cnt
from tbl_name
group by ColumnB
) B
on A.ColumnA = B.ColumnB
If the distinct values in ColumnA are the same as the distinct values of ColumnB then you can use join instead of the full join.
See demo.

How to find the common values for a column which is present in many tables

I have a lot of tables in oracle DB. All tables have one common column named as 'COLUMN_FILTER' .All tables have lots of unique values for 'COLUMN_FILTER'. Is there any way to find the common records for 'COLUMN_FILTER' , present in all tables ? For sample please refer to the below scenario, where I have provided unique values for 'COLUMN_FILTER'.
Table A: 'X','Y','Z'
Table B: 'W','X'
Table C: 'Z'
Table D: 'Y','Z'
I am expecting the output to be 'Z','W' (any possible minimum set). So that I can put this filter on all tables.
Use HAVING COUNT(*)>1 clause along with GROUPing BY that column
SELECT column_filter
FROM
(
SELECT column_filter FROM tableA
UNION ALL
SELECT column_filter FROM tableB
UNION ALL
SELECT column_filter FROM tableC
UNION ALL
SELECT column_filter FROM tableD
)
GROUP BY column_filter
HAVING COUNT(*)>1
To find out the most common element you could have a query as follows
The highest value of cnt_vals indicate the tables which has the most common values of common_filter across all tables
select table_name
,column_filter
,count(column_filter) over(partition by 1) as cnt_vals
from (
select distinct column_filter,'A' as table_name
from tablea
union all
select distinct column_filter,'B' as table_name
from tableb
union all
select distinct column_filter,'C' as table_name
from tablec
union all
select distinct column_filter,'D' as table_name
from tabled
)x
order by 3 desc
You can find pairs using a join:
with f as (
select distinct column_filter, 'A' as table_name
from tablea
union all
select distinct column_filter, 'B' as table_name
from tableb
union all
select distinct column_filter, 'C' as table_name
from tablec
union all
select distinct column_filter, 'D' as table_name
from tabled
)
select f1.column_filter, f2.column_filter
from f f1 join
f f2
on f1.column_filter < f2.column_filter
group by f1.column_filter, f2.column_filter
having sum(case when 'A' in (f1.table_name, f2.table_name) then 1 else 0 end) > 0 and
sum(case when 'B' in (f1.table_name, f2.table_name) then 1 else 0 end) > 0 and
sum(case when 'C' in (f1.table_name, f2.table_name) then 1 else 0 end) > 0 and
sum(case when 'D' in (f1.table_name, f2.table_name) then 1 else 0 end) > 0;
You can easily extend this for triples:
with f as (
select distinct column_filter, 'A' as table_name
from tablea
union all
select distinct column_filter, 'B' as table_name
from tableb
union all
select distinct column_filter, 'C' as table_name
from tablec
union all
select distinct column_filter, 'D' as table_name
from tabled
)
select f1.column_filter, f2.column_filter
from f f1 join
f f2
on f1.column_filter < f2.column_filter join
f f3
on f3.column_filter < f2.column_filter
group by f1.column_filter, f2.column_filter, fd.column_filter
having sum(case when 'A' in (f1.table_name, f2.table_name, f3.table_name) then 1 else 0 end) > 0 and
sum(case when 'B' in (f1.table_name, f2.table_name, f3.table_name) then 1 else 0 end) > 0 and
sum(case when 'C' in (f1.table_name, f2.table_name, f3.table_name) then 1 else 0 end) > 0 and
sum(case when 'D' in (f1.table_name, f2.table_name, f3.table_name) then 1 else 0 end) > 0;
It is possible to express this as a recursive query, if you don't want to try more and more combinations. In Oracle 12C+, I would recommend a recursive CTE. In earlier versions, you have to adapt using connect by.

In oracle SQL , how to count the no of records based on conditions

I have table with below structure :
Col2
A
A
B
B
E
E
I wanted the SQL query to output me the following :
Internal 4
External 2
Total 6
Logic : If the values in the Col2 are A,B then it should be summed up as Internal , If E then it should be summed up as External.
To map your column values use DECODE, simple providing the list of the original and new values for the column.
select decode(col2,'A','Internal','B','Internal','E','External') col from tab
To calculate the total you do not need to rescan the whole table (performance drops to the half) but use group by rollup that calculates the Total
with t as (
select decode(col2,'A','Internal','B','Internal','E','External') col from tab)
select nvl(col,'Total') col, count(*) cnt
from t
group by rollup (col)
Result
COL CNT
-------- ----------
External 2
Internal 4
Total 6
select sum(case when col2 in ('A', 'B') then 1 else 0 end) as internal,
sum(case when col2 = 'E' then 1 else 0 end) as external,
count(col2) as total
from your_table
select 'Internal' "summed up as"
,sum(case when Col2 in ('A', 'B') then 1
else 0
end) "sum"
from test
union
select 'External' "summed up as"
,sum(case when Col2 = 'E' then 1
else 0
end) "sum"
from test
union
select 'Total' "summed up as"
, count(Col2) "sum"
from test;
Here is a DEMO
try like below using union all and make customize group
select case when col2 in ('A','B') then 'Internal' else 'External' end,
count(*) as result
from table_name
group by case when col2 in ('A','B') then 'Internal' else 'External' end
union all
select 'total', count(*) from table_name
select sum(Col2Count) as Internal from (SELECT Col2 as Col2, count( Col2 ) as Col2Count
FROM tablename group by Col2) where Col2 in (A,B);
This will give you result as :
Internal
4

Conditional group by and select query sql

i am having a query with conditional group by , i was just wondering is there a way to have select statement with that condition
my query is
select sum(totalamount),typetitle,
from #temp group by (case when Type ='1' then typetitle else Type end)
with this query i get error at typetitle as its not in group by statement , is there any workaround for this?
Tr like below
select sum(totalamount),
(case when Type ='1' then typetitle else Type end)
from #temp
group by (case when Type =1 then typetitle else Type end)
In group by projection column name and group by column have to be same
If you don't want to repeat the expression, then apply is one method:
select v.typegroup, sum(v.totalamount),
from #temp t cross apply
(values (case when Type ='1' then typetitle else Type end)
) v(typegroup)
group by v.typegroup;

How to case handle a field in Group by

I am trying to group the below CTE based on StudentID and I would like to compute the result with a rule like, if he passes in one subject he is passed overall , if not failed.
WITH mycte(StudentId,SubjectId,Result) as
(SELECT 1,1,'pass'
UNION ALL SELECT 1,2,'fail'
UNION ALL SELECT 1,3,'pass'
UNION ALL SELECT 2,1,'fail'
UNION ALL SELECT 2,2,'fail'
UNION ALL SELECT 3,1,NULL
)
Can you help me in understanding how to achieve this logic.
Expected Result is like
StudentID Result
1 pass
2 fail
3 NULL
Try this simple way
SELECT StudentId,
Result = Max(Result)
FROM mycte
GROUP BY StudentId
Trick here is Pass will be ordered after Fail in string ordering. So when you use Max aggregate we will get Pass if at least one Pass is present
You can aggregate over students and count the number of passes. According to your logic, if even one pass is seen then the student should be reckoned as passing overall.
SELECT DISTINCT
t1.StudentId,
t2.Result
FROM mycte t1
LEFT JOIN
(
SELECT
StudentId,
CASE WHEN SUM(CASE WHEN Result = 'pass' THEN 1 ELSE 0 END) > 0
THEN 'pass'
WHEN SUM(CASE WHEN Result = 'fail' THEN 1 ELSE 0 END) > 0
THEN 'fail'
ELSE NULL END AS Result
FROM mycte
GROUP BY StudentId
) t2
ON t1.StudentId = t2.StudentId