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
Related
i am quite new in sqlite.
I have 2 tables (tableA, tableB) with the exact same schema.
id | CAT | country
They track the number of items per country on a special SW release
I would like to create a query that compares the count of rows per country on tableA and tableB where CAT = "AAA" on a single result like:
COUNTRY |count_tableA |count_tableB|
ARG |12 |16 |
BRA |23 |33 |
I can achieve it in separate tables but not in a single one.
Seperate table example:
select COUNTRY, count(*) as count_tableA from tableA WHERE CAT ="AAA" GROUP BY COUNTRY
select COUNTRY, count(*) as count_tableB from tableB WHERE CAT ="AAA" GROUP BY COUNTRY
Thanks for the help
One way to do it is use conditional aggregation on the UNION of the 2 tables:
select country,
sum(tablename = 'a') count_tableA,
sum(tablename = 'b') count_tableB
from (
select 'a' tablename, id, cat, country from tableA
union all
select 'b' tablename, id, cat, country from tableB
)
where cat = 'AAA'
group by country
You can use WITH and COUNT:
with t1 as (select country, count(*) as c1 from tableA where cat = 'AAA' group by country),
t2 as (select country, count(*) as c1 from tableB where cat = 'AAA' group by country)
select a1.country,
ifnull((select t1.c1 from t1 where t1.country = a1.country), 0) as count_tableA,
ifnull((select t2.c1 from t2 where t2.country = a1.country), 0) as count_tableB from (
select distinct country from tableA where cat = 'AAA'
union
select distinct country from tableB where cat = 'AAA'
) a1
group by country
I am fairly new to writing SQL queries. I have used a subquery so that I could obtain the results of aggregate functions applied to 2 different tables. Furthermore, I would like to obtain the ratio between the results from these 2 aggregate functions. In other words, Result of Aggregate function 1 / Result of Aggregate function 2.
INPUT:
TABLE USERS
Id
1
2
3
4
5
6
7
TABLE EVENTS
User_Id Event_Name
1 View_User_Profile
1 View_User_Activity
1 View_User_Profile
2 View_User_Activity
3 View_User_Activity
4 View_User_Profile
5 View_User_Activity
7 View_User_Activity
This is my code so far:
SELECT COUNT(*) AS Number_of_Users,
(SELECT COUNT(DISTINCT U.Id) AS Number_of_Users_Viewed_Profile
FROM dsv1069.Users U Left Join dsv1069.Events E
ON U.Id = E.user_id
WHERE E.Event_Name = 'view_user_profile') AS Number_of_Users_Viewed_Profile
FROM dsv1069.Users
RESULTS:
Number_of_Users: 7
Number_of_Users_Viewed_Profile: 2
EXPECTED OUTPUT:
Number_of_Users: 7
Number_of_Users_Viewed_Profile: 2
PERCENT OF USERS VIEWED PROFILE: 28.6%
ISSUE: What my code doesn't do so far is calculate the ratio 2/7 = 28.6%
I have done lots of searches on aggregate functions but can't find any information on how to use the results from those functions as part of the query. Thank you for any assistance!
I believe that you can obtain the column Number_of_Users_Viewed_Profile without the join:
SELECT COUNT(DISTINCT user_id)
FROM dsv1069.Events
WHERE Event_Name = 'view_user_profile'
So this is equivalent to your query:
SELECT
COUNT(*) AS Number_of_Users,
(
SELECT COUNT(DISTINCT user_id)
FROM dsv1069.Events
WHERE Event_Name = 'View_User_Profile'
) AS Number_of_Users_Viewed_Profile
FROM dsv1069.Users
You can get the ratio column with a CTE:
WITH cte AS (
SELECT
COUNT(*) AS Number_of_Users,
(
SELECT COUNT(DISTINCT user_id)
FROM Events
WHERE Event_Name = 'View_User_Profile'
) AS Number_of_Users_Viewed_Profile
FROM Users
)
SELECT *, ROUND(100.0 * Number_of_Users_Viewed_Profile / Number_of_Users, 1) AS ratio
FROM cte
See the demo.
Or without the CTE:
SELECT
t1.Number_of_Users,
t2.Number_of_Users_Viewed_Profile,
ROUND(100.0 * t2.Number_of_Users_Viewed_Profile / t1.Number_of_Users, 1) AS ratio
FROM (SELECT COUNT(*) AS Number_of_Users FROM Users) AS t1
CROSS JOIN (
SELECT COUNT(DISTINCT user_id) AS Number_of_Users_Viewed_Profile
FROM Events
WHERE Event_Name = 'View_User_Profile'
) AS t2
See the demo.
Results:
| Number_of_Users | Number_of_Users_Viewed_Profile | ratio |
| --------------- | ------------------------------ | ----- |
| 7 | 2 | 28.6 |
I have 3 queries in SQLite:
-query1: SELECT COUNT(id_asiento) FROM ASIENTOS_DIARIO WHERE id_paquete = 1
-query2: SELECT id_asiento FROM ASIENTOS_DIARIO
-query3: SELECT id_cta FROM CUENTASXPAQ
I want to construct a query where I can execute query2 or query3 depending of the number of rows resulting from query1
this is my code:
SELECT CASE WHEN (SELECT COUNT(id_asiento) FROM ASIENTOS_DIARIO WHERE id_paquete = 1) > 0
THEN (SELECT id_asiento FROM ASIENTOS_DIARIO)
ELSE (SELECT id_cta FROM CUENTASXPAQ)
END QUERY
but when I execute this, only returns the first row of query2 or query3 and I want to get all the rows
how to do that
SELECT id_asiento AS ID
FROM ASIENTOS_DIARIO
WHERE (SELECT COUNT(id_asiento) FROM ASIENTOS_DIARIO WHERE id_paquete = 1) > 0
UNION ALL
SELECT id_cta AS ID
FROM CUENTASXPAQ
WHERE (SELECT COUNT(id_asiento) FROM ASIENTOS_DIARIO WHERE id_paquete = 1) = 0
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
I have two tables
table1
c1t1 c2t1
1 saanu
3 abc
table2
c1t2 c2t2
2 val2
4 val4
I have to find out the values of c2t1 and c2t2 for the minimum and maximum value of c1t1 and c1t2 with one line command.
For the above example I have to find saanu and val4
I had a very similar problem and solved it with UNION ALL. The minimum of the aColumn column in tables aTable1, ... , aTableN can be computed as:
SELECT Min(aColumn)
FROM (
SELECT aColumn FROM aTable1 UNION ALL
SELECT aColumn FROM aTable2 UNION ALL
...
SELECT aColumn FROM aTableN) t;
You should be able to do Min in each of the inner selects, but I haven't found out how to do that!
One approach:
select max(case c1 when min1 then c2 end) c2_first,
max(case c1 when max1 then c2 end) c2_last
from (select c1t1 c1, c2t1 c2 from table1
union all
select c1t2 c1, c2t2 c2 from table2) u
cross join
(select min(min11, min12) min1, max(max11, max12) max1 from
(select min(c1t1) min11, max(c1t1) max11 from table1) t1
cross join
(select min(c1t2) min12, max(c1t2) max12 from table2) t2) m
SQLFiddle here.
1)
SELECT c2t1
FROM table1
ORDER BY c1t1 ASC LIMIT 1
2)
SELECT c2t2
FROM talbe2
ORDER BY c1t2 DESC LIMIT 1