I have a list of languages and I need to show how many users subscribe to each.
I work on my project with Angular 13 and ASP.NET Core 6 and SQL Server but there are two blocked ways
Use Angular services in NgFor but return infinite loops and crash.
Use .NET to return the list of languages and loop a second query to get count users but not work.
The last is to create something with SQL Server (I don't know how)
[
In your API you will need to return a payload that you can use to show the results you expect. From the API layer, you need to access the database and transform data to the meet the former challenge. The query to obtain the results is rather straightforward.
SELECT
L.lang_id,
L.title,
L.description,
L.image,
COUNT(*) as UserCount
FROM
Languages L
INNER JOIN Users U on U._id_lang = L.id_lang
GROUP BY
L.id_lang
#ross bush thank you
SELECT
CAST(L.id_Lang AS NVARCHAR(200)) AS idLang ,
CAST(L.title AS NVARCHAR(200)) AS TitleLang ,
CAST(L.img AS NVARCHAR(200)) AS ImgLang ,
COUNT(*) AS UserCount
FROM
dbo.Languages L
INNER JOIN dbo.Users U on U.id_lang = L.id_Lang
GROUP BY
CAST(L.id_Lang AS NVARCHAR(200)) ,CAST(L.title AS NVARCHAR(200)) , CAST(L.img AS NVARCHAR(200))
Related
One of my clients has two Head Offices.
Each HO has almost 300 workstation and my client doesn't want to replicate all the info into one HO, so he asked if i can get some info into secondary tables with jobs from HO no. 2 to HO no. 1 for some reports...
This part is done and i had no issue but the real problem appeared when i had to modify the stored procedure for the reports.
Before i had the code like this:
SELECT D.IdDocument, D.etc...
FROM Document D (NOLOCK)
JOIN OtherTable OT (NOLOCK) on etc...
Now i've tried it like this:
1) First attempt:
SELECT DXD.IdDocument, DXD.etc...
FROM
(
SELECT D.*
FROM Document D (NOLOCK)
UNION ALL
SELECT D2.*
FROM RemoteDocument D2 (NOLOCK)
) DXD
JOIN OtherTable OT on etc...
2) Second attempt:
CREATE TABLE #TempDocument
(
IdDocument INT,
IdLocation INT,
etc..
)
CREATE INDEX IDX_TMP_Document ON #TempDocument (IdDocument, IdLocation)
INSERT INTO #TempDocument
SELECT DXD.*
FROM
(
SELECT D.*
FROM Document D (NOLOCK)
UNION ALL
SELECT D2.*
FROM RemoteDocument D2 (NOLOCK)
) DXD
SELECT DXD.IdDocument, DXD.etc...
FROM #TempDocument DXD
JOIN OtherTable OT (NOLOCK) on etc...
The problem is that before the sp ran in 5-10 minutes and now 30-40 minutes, the main issue that execution plan detected is in the union/insert using the union...
Can someone tell me a better/faster way to concatenate the info before using it?
I would recommend that you utilize a linked server. Then once you have created the linked server. Create a view to the linked server on one of your systems.
Server 1:
Linked Servers:
Server 2
Views:
MyNewView(Select * from openquery(linkedServer,'Select * from SomeDB.dbo.SomeTable')
When doing this. Your main server caches data from the view. So you can now work on one server and the return time should be fairly quick depending on the amount of data you're filtering through and returning.
for setting up a linked server please review the following article: Here.
This does not actually consolidate but it hopefully should improve some speed issues as well as allow you to write like you are in a single database. Cleaning up your code and also preventing any other communication problems as the only communication issue # the linked server end.
I'll start by saying hello! This forum has been a great help to me over the past few months, but have only now joined and asking my first question.
I'm working with the Northwind database in SQL Server 2008 r2 to build a vb.net application. I've been wrecking my head for a week trying to figure out how to make an order/invoice form. I can get the information I need to display using separate stored procs (GetCustInfo, GetOrderInfo, GetProductInfo, or something like that), but I'm having trouble figuring out how to display them on the form.
When I select all the info I need in one sp (as in the Invoice view which comes built in the db), I get 2155 rows, which is the number of items which have been ordered in the company history.
What I want to do is display this information, but navigate by OrderID (which would give me 830 rows, each with a certain number of products related to the OrderID). So I'm thinking I need different stored procs related which can be related in some way.
I'd really appreciate any help that can be given on this.
Many thanks in advance.
p.s. I have screenshots of the Northwind sample app which shipped/ships with Access, which is really what I'm trying to recreate in SQL Server. Unfortunately, no code!
MM
Yes you can achieve it by many ways and SP is one. Just create a SP to select that related products passing OrderId as a input parameter.
Some options (with contrived examples):
You can ALTER existing stored procedures to get what you want (not recommended if you want to use the existing procedures for other queries).
ALTER PROCEDURE usp_ExistingProcedure
AS
BEGIN
SELECT t1.Value
, t2.Value
-- Supose that this was the addition we made to an existing stored procedure
, t2.ValueTwo
FROM TableOne t1
INNER JOIN TableTwo t2 ON t1.ID = t2.ID
END
You can CREATE new stored procedures for your queries; in the above example, it would be a create procedure with a new name.
You may be able to create a VIEW to obtain what you need - this will operate a little differently.
CREATE VIEW uv_ApplicationView
AS
SELECT t1.Value
, t2.Value
, t2.ValueTwo
FROM TableOne t1
INNER JOIN TableTwo t2 ON t1.ID = t2.ID
You can pull the query directly from the VB application, though if you want to reuse it for something else, I wouldn't recommend this approach.
// A re-usable approach calling a stored procedure
SqlCommand myQuery = new SqlCommand("EXECUTE usp_myQuery", sqlConn);
// A query directly in the C# code:
string msQuery = "SELECT t1.Value, t2.Value, t2.ValueTwo FROM TableOne t1 INNER JOIN TableTwo t2 ON t1.ID = t2.ID"
// Later ...
SqlCommand myQuery = new SqlCommand(msQuery, sqlConn);
I'm trying to write a query in PostgreSQL and I'm getting a little frustrated because it works in other database engines. I need to select the top 5 users from a given joins table like this:
SELECT users.*,
COUNT(deals.id) AS num_deals
FROM users, deals
WHERE deals.users_id = users.id
GROUP BY users.id
ORDER BY num_deals LIMIT 5;
I need the top 5 users. This code works in sqlite, mysql, etc, yet PostgreSQL refuses to select additional fields that aren't used in aggregate functions. I'm getting the following error:
PGError: ERROR: column "users.id" must appear in the GROUP BY clause or be used in an aggregate function
How can I do this in PostgreSQL??
You could try:
SELECT users.*, a.num_deals FROM users, (
SELECT deal.id as dealid, COUNT(deals.id) AS num_deals
FROM deals
GROUP BY deal.id
) a where users.id = a.dealid
ORDER BY a.num_deals DESC
LIMIT 5
Assuming that users.id IS a PK, then you can either
wait for 9.1
group by all fields
use an aggregate (i.e. max() ) on all fields
One other solution that works is to use all attributes implicitly in GROUP BY
Thus following will be final query
SELECT users.*,
COUNT(deals.id) AS num_deals
FROM users, deals
WHERE deals.users_id = users.id
GROUP BY users.id, users.name, users.attrib1, ..., users.attribN
ORDER BY num_deals LIMIT 5;
If you are using framework like rails then you can implement this easily with Model.column_names function.
Just in case of somebody wants ANSI-92 standard solution and doesn't like 'Oracle' way to join tables...
SELECT users.*, num_deals
FROM users
JOIN
(SELECT deals.users_id as users_id, count(deals.users_id) as num_deals
FROM deals
GROUP BY deals.id) grouped_user_deals
ON grouped_user_deals.users_id = users.id
ORDER BY num_deals DESC
LIMIT 5;
I'm new to LINQ and EF, but I've been able to stumble through for the majority of the queries I have, but this one has me completely confused. No matter what I try, it comes up in SQL Profiler as a big mess :-).
I have two tables: Users and UsersProjects. The goal of this query is to list all the users who are working on projects with the specified user. Here is the query as I have it written in SQL. It's a subquery, but I don't know of a way to simplify it further, but I'm open to suggestions there as well.
SELECT DISTINCT Users.FirstName, Users.LastName
FROM Users INNER JOIN UsersProjects ON Users.ID=UsersProjects.UserID
WHERE UsersProjects.ProjectID IN
(SELECT ProjectID FROM UsersProjects WHERE UserID=#UserID)
Anybody able to help?? It seems like a fairly simple subquery in SQL, but in LINQ, I'm baffled.
Thanks,
Jorin
Something like this I guess:
from u in Users
from projectId in UsersProjects.Where(up => up.UserId == #userId).Select(p => p.ProjectId)
where u.UsersProjects.Any(up => projectId == up.ProjectId)
select u
or (it's your sql query in linq)
(from u in Users
join up in UsersProjects on #userId equals up.UserId
where u.UsersProjects.Any(up2 => up2.ProjectId == up.ProjectId)
select u).Distinct()
So suppose I have a bunch of blog entries and I want to find out the most recent comment in each of them, how would I go about finding that out in SQL Server.
I have a list of integer id's of these blog entries in a temp table. Something like select top 1 does not work in this case.
The approach coming to my mind is looping, and we all know how much people prefer to avoid loops in SQL Server.
You can use a subquery in the SELECT statement. Something like:
SELECT post.id,
most_recent_comment_id =
(SELECT TOP 1 comment.id
FROM comment
WHERE comment.post_id = post.id
ORDER BY comment.date DESC)
FROM posts
ORDER BY posts.date
or something similar to that.
Well, here is one way:
SELECT c.*
FROM BlogComments c
JOIN #TempEntries t ON c.EntryID = t.EntryID
JOIN (
SELECT m.EntryID, MAX(m.CommentID) AS CommentID
FROM BlogComments m
GROUP BY m.EntryID
) m
ON m.EntryID = c.EntryID
AND m.CommentID = c.CommentID