i'm back again, and it's another school topic
I'm restricted to the Commands WHERE FROM SELECT
The subject is basically show the name from table customers beginning with A or B and ending with S or P, i've tried multiple OR solutions but none of them seem to work, the answer is 1 column 6 rows
i can't get this code to work
USE Northwind
SELECT
CompanyName
FROM Customers
WHERE CompanyName LIKE '[A/B]%' or CompanyName LIKE '%[S/P]'
go
With this code i get 31 rows :/
How about combining together:
SELECT
CompanyName
FROM Customers
WHERE CompanyName LIKE '[A/B]%[S/P]'
SQL Fiddle Demo
I believe this should accomplish what you want:
SELECT CompanyName
FROM Customers
WHERE
(LEFT(CompanyName,1) = "A" OR LEFT(CompanyName,1) = "B")
AND (RIGHT(CompanyName,1) = "S" OR RIGHT(CompanyName,1) = "P")
Related
What do I have to change to get different results from different names.The table should give me the debts of each of them, this is calculated by the amount and the price of the drink. Now it should show all the names with the corresponding invoice that happens after the select
%sql select name, sum(getraenk.preis*schulden.menge) schulden from schulden \
join person on (fk_person = person.id)\
join getraenk on (fk_getraenk = getraenk.id)\
where name like ("dani")
Edit: it should spend all the names with their debts, that is:
dani = 8.5
michael = 12.5
...
Just in case your problem is very simple, you should be able to see all names and values with an SQL that looks like this:
select name, getraenk.preis*schulden.menge schulden
from schulden
join person on (fk_person = person.id)
join getraenk on (fk_getraenk = getraenk.id)
Note that I removed the where clause... this was the part that limited it to one name.
You also don't need the sum clause here unless you are doing a group by
Have you considered simply using GROUP BY name at the end of this query?
https://www.w3schools.com/sql/sql_groupby.asp
This will give you the sum of total debt for all names in your table which sounds like the result you are looking for.
You're missing
GROUP BY name
in the query.
I have two queries. The first gives me a list of BusinessUnitIds along with a count for each:
SELECT [b].[BusinessUnitId], COUNT([b].[BusinessUnitId]) AS bucount
FROM [dbo].[ComponentTeamBusinessUnit] [b]
WHERE [b].[GlobalClientFiscalYearId] = #GlobalClientFiscalYearId
AND [b].[ComponentTeamId] IN (SELECT items FROM [dbo].[fnSplit](#ComponentTeamIds, ','))
GROUP BY [b].[BusinessUnitId])
I want to take the BusinessUnitIds in this result and join them to a second query which will retrieve the Business Unit Name associated with the BusinessUnitIds. Something like the following:
Select [c].Name, [first query result].Count from [dbo].[BusinessUnit] [c]
INNER JOIN [first query result]
WHERE [c].BusinessUnitId = [first query result].BusinessUnitId
Ultimately, what I want is a listing of Business Names, along with a count of each. I haven't been able to figure out how to do this. Can anyone help? To do both queries in a single statement would be tops. Thank you.
Exmaple:
SELECT [b].[BusinessUnitId],A.Name, COUNT([b].[BusinessUnitId]) AS bucount
FROM [dbo].[ComponentTeamBusinessUnit] [b]
LEFT JOIN NameTable as A
ON A.BusinessUnitId = b.BusinessUnitId
WHERE [b].[GlobalClientFiscalYearId] = #GlobalClientFiscalYearId
AND [b].[ComponentTeamId] IN (SELECT items FROM [dbo].[fnSplit](#ComponentTeamIds, ','))
GROUP BY [b].[BusinessUnitId],A.Name
If tables are One to One, will be neat, if one to many, you will see the result like:
id name count
1 A 5
1 B 6
And if you want to group id 1, to get:
id name count
1 A,B 11
That you need to use FOR XML PATH() together with STUFF, or STRING_SPLIT, really depends on your real case.
Query to generate a list of companies that have “prior to” more than once in their names.
For Example:
Company Name
Ittal PLC (adz ll **prior to** 04/2012) (Z Amp C **prior to** 02/2009)
One simple way is to use like:
where CompanyName like '%prior to%prior to%'
Try this
select *
from Yourtable
where len([Company Name]) - len(replace([Company Name],'prior to','')) > 1
and len([Company Name]) - len(replace([Company Name],'prior to','')) <> len('prior to')
SQL FIDDLE DEMO
I'd probably implement this as a two-step process.
1) Find all records matching prior to.
E.g.
SELECT Company_name FROM COMPANY_TABLE WHERE Company_name LIKE '%prior to%'
2) Iterate through all the records and find only those that have 2 occurrences of the prior to substring (in whatever language you're using).
I'am trying to convert this query (already working):
Select building.Name, Count(people.ID) as NumberOfUser
From tblBuilding as building left outer join tblPeople as people on building.ID = people.buildingID
Group by building.Name
which gave the result like this:
Name | NumberOfUser
----------------------------
WestBuilding 50
EastBuilding 70
SouthBuilding 0
NorthBuilding 2
To Linq to SQL. Look at my trying:
Dim db As New MyDatabaseDataContext
Dim query = From building In db.tblBuilding
From people In db.tblPeople.Where(Function(x) building.ID = x.buildingID).DefaultIfEmpty
Group building, people By building.ID, building.Name Into grp
Select ID, Name, grp.Count
Return query.ToList
It appeared to work at first. But when I checked a building that contained no people, the Count column said there is 1. Like this:
Name | NumberOfUser
----------------------------
WestBuilding 50
EastBuilding 70
SouthBuilding 1
NorthBuilding 2
Because although that building had no people, it did appeared once in the result query so it counted as 1. In another word, the Count statement actually count how many time each building appear in result table instead of how many people in each building, thus making the result wrong for no people building.
Any work around ?
You are correct, to solve it you need to test whether there are people, like you did with count(people.Id)
Dim db As New MyDatabaseDataContext
Dim query = From building In db.tblBuilding
From people In db.tblPeople.Where(Function(x) building.ID = x.buildingID).DefaultIfEmpty
Group building, people By building.ID, building.Name Into Group, Count(Not IsNothing(people))
'Select ID, Name, grp.Count
Return query.ToList
I have a query here that doesn't work and having trouble pin pointing my mistake.
Any help would be great.
Thanks
I am trying to retrieve records with a program name starting with 'C' but my query returns zero records.
My PROGRAM table has an entry of a ProgName of Chemistry.
SELECT P.ProgNumber, ProgName, StudID, DateEnrolled
FROM PROGRAM AS P, STUDENT AS S
WHERE P.ProgNo = S.ProgNo
AND ProgName LIKE 'C%';
Use
LIKE "C*"
MSAccess doesn't use % as the wildcard
SELECT
P.ProgNumber, P.ProgName, S.StudID, S.DateEnrolled
FROM
PROGRAM P
JOIN STUDENT S ON S.ProgNo = P.ProgNo
WHERE
P.ProgName LIKE 'C%';
should work... you said you changed it to ='Chemistry', do you get the same result if you use lowercase c in chemistry?
You need to join the different tables like this...Try this...
SELECT P.ProgNumber, P.ProgName, S.StudID, S.DateEnrolled
FROM PROGRAM P
JOIN STUDENT S
ON P.ProgNo = S.ProgNo
WHERE P.ProgName LIKE 'C*'; -- Asterisk because its Access not MS-SQL