Two queries in one statment - sqlite

I want in put two queries in one statement how can i do that in this state?
1
stmt = `SELECT Comments.*, Users.username,Users.avatar from Users
INNER JOIN Comments ON Comments.users_id =Users.users_id
WHERE Comments.post_id= 1`
2
`SELECT COUNT(*) comment FROM Comments WHERE Comments.post_id= 1`;

Cross join the 1st query to the 2nd:
SELECT c.*, u.username, u.avatar, t.counter
FROM Users u INNER JOIN Comments c
ON c.users_id = u.users_id
CROSS JOIN (SELECT COUNT(*) counter FROM Comments WHERE post_id = 1) t
WHERE c.post_id = 1

I don't know much about SQLite, but in SQL Server you can use ";" to use multiple queries.
Maybe it does work for SQLite.

You can use GROUP BY on comments table in a way:
'SELECT COUNT(Comments.<id>), Comments.*, Users.username,Users.avatar
from Users INNER JOIN Comments ON Comments.users_id =Users.users_id
WHERE Comments.post_id = 1 GROUP BY Comments.<id>';
*This syntax of GROUP BY clause follows PostgreSQL. You might need to tweak according to the syntax followed by sqlite.

Related

EF Core - Count from a specific column

I almost have my EF Core query working... This is the SQL getting produced (notice the Count(*):
SELECT [u].[Key], [u].[Url], [u].[CreatedBy], [u].[CreatedOn], COUNT(*) AS [Clicks]
FROM [URLs] AS [u]
LEFT JOIN [OwnerUrls] AS [o] ON [u].[Key] = [o].[ShortUrlKey]
LEFT JOIN [Clicks] AS [c] ON [u].[Key] = [c].[ShortUrlKey]
GROUP BY [u].[Key], [u].[Url], [u].[CreatedBy], [u].[CreatedOn]
What I need is (have Count look at a specific column/table)
SELECT [u].[Key], [u].[Url], [u].[CreatedBy], [u].[CreatedOn], COUNT(c.ID) AS [Clicks]
FROM [URLs] AS [u]
LEFT JOIN [OwnerUrls] AS [o] ON [u].[Key] = [o].[ShortUrlKey]
LEFT JOIN [Clicks] AS [c] ON [u].[Key] = [c].[ShortUrlKey]
GROUP BY [u].[Key], [u].[Url], [u].[CreatedBy], [u].[CreatedOn]
Here is the EF Query that I'm using...
query = (from u in db.URLs
join ou in db.OwnerUrls on u.Key equals ou.ShortUrlKey into urlOwners
from subSet in urlOwners.DefaultIfEmpty()
join c in db.Clicks on u.Key equals c.ShortUrlKey into urlClicks
from subClicks in urlClicks.DefaultIfEmpty()
group subClicks by new { u.Key, u.Url, u.CreatedBy, u.CreatedOn } into g
select new ShortURL()
{
Key = g.Key.Key,
Url = g.Key.Url,
CreatedBy = g.Key.CreatedBy,
CreatedOn = g.Key.CreatedOn,
Clicks = g.Count()
});
I've tried changing the g.Count() to g.Select(x=>x.Id).Count() and that just causes EF Core to barf and complain about client side evaluation vs server side evaluation etc..
I should mention that the reason I'm joining the first model (OwnerUrls) is to support a where clause that I didn't include here...
Thanks!
I'm not a EF developer, but have worked with SQL Server for a while now. In SQL Server i would use COUNT(DISTINCT c.ID) to eliminate any duplicates you might get from JOINS.
If duplicates are impossible due to the model the COUNT(*) shoud be sufficient.
Maybe this might help:
https://entityframeworkcore.com/knowledge-base/51892585/linq-select-distinct-count-performed-in-memory

Subquery with alias in SQLite

I have a query that I run in PostgreSQL like this:
select
c_count, count(*) as custdist
from (
select
c_custkey,
count(o_orderkey)
from
customer left outer join orders on
c_custkey = o_custkey
and o_comment not like '%special%requests%'
group by
c_custkey
)as c_orders (c_custkey, c_count)
group by
c_count
order by
custdist desc,
c_count desc;
And I wanted to run it on SQLite, but I got this error: Error: near" (": syntax error. Maybe he doesn't recognize this as c_orders (c_custkey, c_count).
Is there any way to rewrite this query to execute in SQLite?
SQLite does not allow redefining/renaming columns for an nested, aliased query. You can do that with a WITH clause (i.e. Common Table Expression; CTE). Or you can add aliases to the nested query columns directly using AS keyword.
Interesting that this is exactly how the outer query columns are named. Just use the same pattern for the nested query. I don't use PostgreSQL, but why not add aliases directly on each column and complicate it by using different syntax for each part of the query?
select
c_count, count(*) as custdist
from (
select
c_custkey,
count(o_orderkey) AS c_count
from
customer left outer join orders on
c_custkey = o_custkey
and o_comment not like '%special%requests%'
group by
c_custkey
) AS c_orders
group by
c_count
order by
custdist desc,
c_count desc;

How do i join two tables and get data from both of the table to show to users on form field?

Table A (goradid) has a field which is a FK to Table B(Spriden). I need two sets of data (spriden_id and Goradid_additional_id) from Table A and Table B and insert it into one of the field called e.g. bannerid and partyid. please see my code below as i am also getting a error SQL command not properly ended in PLSQL.
SELECT spriden_id, goradid_additional_id
FROM goradid, spriden
INNER JOIN spriden ON spriden_pidm = goradid_pidm
INTO bannerid, partyid;
The correct syntax is SELECT <COLUMN LIST> INTO <VARIABLE LIST>. So the query should be:
SELECT spriden_id, goradid_additional_id
INTO bannerid, partyid
FROM goradid a INNER JOIN spriden b ON b.spriden_pidm = a.goradid_pidm;
you can use aliasing for this.
it's simple.
SELECT spriden_id as bannerid, goradid_additional_id as partyid FROM goradid INNER JOIN spriden ON spriden_pidm = goradid_pidm;
Note: if you want to use into then you have to initialize the variable.
we use into usually.
declare
v_author author%rowtype;
begin
select * into v_author from author where author_key = 'A103';
dbms_output.put_line('Name:'||v_author.author_first_name||' '|| v_author.author_last_name);
end;

T-SQL Select and count from different table?

I have a table (Threads) containing a field (id). I would like to select every row from Threads, as well as the number of rows in the table Posts where the field Posts.thread is the same as Threads.id.
How can this be done in SQL?
(Something like this pseudo-SQL: SELECT *, COUNT(* FROM Posts WHERE Posts.id=Threads.id) FROM Threads)
Sure - something like this?
SELECT
t.ThreadID,
(SELECT COUNT(*) FROM dbo.Posts p WHERE p.ThreadID = t.ThreadID)
FROM
dbo.Threads t
SELECT t.id, COUNT(p.thread)
FROM Threads AS t
LEFT OUTER JOIN Posts AS p
ON t.id = p.thread
GROUP BY t.id

Count the number of elements in sql server table

I have 2 tables which looks like this:
ARTICLES TABLE:
and the output should look like this:
How can I accomplish this using both sql query (i'm using sql server 2005) and using linq to sql query ?
BTW i'm using sql server 2005, asp.net with c# in Visual studio 2008.
Please help me
Thanks in anticipation
Update: Added Linq experssion that can be used if you require an OUTER join.
INNER JOIN
For an inner join ie. only get back the articles that have been bought at least once, you can use the following.
LINQ 2 SQL
from a in Articles
join c in CustomersRecords on
a.Article_Name equals c.Article_Name
group a by new {a.SNo, a.Article_Name} into g
select new
{
SNo = g.Key.SNo,
Article_Name = g.Key.Article_Name,
Total_Items_Bought = g.Count()
}
The above translates to the following SQL
SELECT COUNT(*) AS [Total_Items_Bought], [t0].[SNo], [t0].[Article_Name]
FROM [Articles] AS [t0]
INNER JOIN [CustomersRecord] AS [t1] ON [t0].[Article_Name] = [t1].[Article_Name]
GROUP BY [t0].[SNo], [t0].[Article_Name]
Which when cleaned-up a little gives you
SELECT a.SNo,
a.Article_Name,
COUNT(*) AS Total_Items_Bought
FROM Articles AS a
INNER JOIN CustomersRecord AS c ON a.Article_Name = c.Article_Name
GROUP BY a.SNo, a.Article_Name
LEFT OUTER JOIN
For a left outer join ie. get back all articles event those that have never been bought, you can use the following.
LINQ 2 SQL
from a in Articles
join c in CustomersRecords on
a.Article_Name equals c.Article_Name into apc
select new
{
SNo = a.SNo,
Article_Name = a.Article_Name,
Total_Items_Bought = apc.Count()
}
This translates to the following SQL
SELECT [t0].[SNo], [t0].[Article_Name], (
SELECT COUNT(*)
FROM [CustomersRecord] AS [t1]
WHERE [t0].[Article_Name] = [t1].[Article_Name]
) AS [Total_Items_Bought]
FROM [Articles] AS [t0]
select
A.SNo,
A.Article_Name,
count(C.Article_Name) as Total_Items_Bought
from Articles as A
left outer join CustomersRecord as C
on A.Article_Name = C.Article_Name
group by A.SNo, A.Article_Name
order by A.SNo
Use this for SQL
SELECT
SNO,Article_Name,
(SELECT COUNT(*) FROM CustomersRecord AS cr
WHERE cr.Article_Name = Article_Name) AS Total_Items_Bought
FROM ARTICLES

Resources