I am having trouble with adding a sub query to the following query -
SELECT state
FROM users
ORDER BY state
Output:
Alabama
Alaska
Arizona
...
I would like to include the number of users from each state so I can output it
like:
Alabama (12)
Alaska (4)
Arizona (25)
ANSWER:
$query=mysqli_query($connect,"select state,
count(*) AS count from users
GROUP BY state");
while($rows=mysqli_fetch_assoc($query))
{
echo $rows['state'];
echo $rows['count'];
}
I didn't realize I had to name count(*) AS count so I could output it.
What you need to do is do a "group by" in the query.
The way it would work is like this:
select state, count(id)
from users
group by state
order by state
what this will do is group the records by the state and show how many records exist for that state.
Related
I have the following two Access tables
Employees
id Name
1 bob smith
2 james bird
3 jane big
Events
id emp_id Notes
1 1 fell down the stairs
2 3 paper cut in the break room
I also have the following Excel file that I would like to 'suck' (import) into the Events table. The problem is the data needs to be correlated on the name/emp_id field and I'm not sure the best way to do this.
Excel_sheet
Employee Name Notes
bob smith feel asleep while driving
The access table uses references to the Employees table, whereas the Excel sheet is using names. What are some options for me to bring this Excel sheet into Events table and convert the names (bob smith) into their associated id's from the Employees table?
Assuming names are consistently spelled in both datasets and only one person exists for each name, try:
INSERT INTO Events(emp_ID, Notes) SELECT ID, Notes FROM excel_sheet INNER JOIN Employees ON Employees.Name=excel_sheet.[Employee Name];
Build that SQL in a query object or run in VBA:
CurrentDb.Execute "INSERT INTO Events(emp_ID, Notes) " & _
"SELECT ID, Notes FROM excel_sheet " & _
"INNER JOIN Employees ON Employees.Name=excel_sheet.[Employee Name];"
Suggest you test with a copy of database.
Name is a reserved word and really should not use reserved words as names for anything.
I recently took to using sql again, the last time I used it was in microsoft access 2000 so please bear with me if I'm behind the times a little.
I have 2 pointless virtual currencies on my discord server for my players to play pointless games with. Both of these currencies' transactions are currently stored in individual tables.
I wish to sum up all the transactions for each player to give them a single current amount for each currency. Individually I can do this:
SELECT
tblPlayers.PlayerID AS PlayerID,
tblPlayers.Name AS Name,
SUM(tblGorillaTears.Amount)
FROM
tblPlayers
INNER JOIN
tblGorillaTears
ON
tblPlayers.PlayerID = tblGorillaTears.PlayerID
GROUP BY
tblPlayers.PlayerID;
and
SELECT
tblPlayers.PlayerID AS PlayerID,
tblPlayers.Name AS Name,
SUM(tblKebabs.Amount)
FROM
tblPlayers
INNER JOIN
tblKebabs
ON
tblPlayers.PlayerID = tblKebabs.PlayerID
GROUP BY
tblPlayers.PlayerID;
What i need is a table that outputs the user name the id and the total for each currency on one row, but when i do this:
SELECT
tblPlayers.PlayerID AS PlayerID,
tblPlayers.Name AS Name,
SUM(tblGorillaTears.Amount) AS GT,
0 as Kebabs
FROM
tblPlayers
INNER JOIN
tblGorillaTears
ON
tblPlayers.PlayerID = tblGorillaTears.PlayerID
GROUP BY
tblPlayers.PlayerID
UNION
SELECT
tblPlayers.PlayerID AS PlayerID,
tblPlayers.Name AS Name,
0 as GP,
SUM(tblKebabs.Amount)
FROM
tblPlayers
INNER JOIN
tblKebabs
ON
tblPlayers.PlayerID = tblKebabs.PlayerID
GROUP BY
tblPlayers.PlayerID;
the results end in a row for each player for each currency. How can i make it so both currencies appear in the same row?
Previously in MSAccess i was able to create two queries and then make a query of those two queries as if they were a table, but I cannot figure out how to do that in this instance. Thanks <3
UNION will add new rows for sure, you can try like following query.
SELECT TP.playerid AS PlayerID,
TP.NAME AS NAME,
(SELECT Sum(TG.amount)
FROM tblgorillatears TG
WHERE TG.playerid = TP.playerid) AS GT,
(SELECT Sum(TG.amount)
FROM tblkebabs TG
WHERE TG.playerid = TP.playerid) AS Kebabs
FROM tblplayers TP
I have the following table:
DEST_COUNTRY_NAME ORIGIN_COUNTRY_NAME count
United States Romania 15
United States Croatia 1
United States Ireland 344
Egypt United States 15
The table is represented as a Dataset.
scala> dataDS
res187: org.apache.spark.sql.Dataset[FlightData] = [DEST_COUNTRY_NAME: string, ORIGIN_COUNTRY_NAME: string ... 1 more field]
I want to sort the table based on count column and want to see only count column. I have done it but I am doing it in 2 steps
1- I first sort to get sorted DS - dataDS.sort(col("count").desc)
2- then select on that DS- (dataDS.sort(col("count").desc)).select(col("count")).show();
The above feels like am embedded sql query to me. In sql however, I can do the same query without using an embedded query
select * from flight_data_2015 ORDER BY count ASC
Is there a better way for me to both sort and select without creating a new Dataset?
There is nothing wrong
(dataDS.sort(col("count").desc)).select(col("count")).show();
It is the right thing to do and has no negative performance implications, other than intrinsic problems of sorting as such.
Use it freely and don't worry about it anymore.
I have a database that looks like this
users
email name state id
bill#domain.com Bill CA 1
Susan WY 2
jill#domain.com Jill CA 3
phil#domain.com Phil WY 4
You'll notice that Susan does not have an email.
I'm trying to get a count of records per state, then a total number of non null emails for each state.
I was able to get the total count of states like this:
SELECT state, COUNT(*) as count FROM users GROUP BY state
That works great.
Then I tried getting the total number of emails like this:
SELECT state, COUNT(*) as count, COUNT(SELECT * FROM users WHERE email IS NOT NULL) as email_count FROM users GROUP BY state
But that returned a parse syntax error.
I'm trying to get a return dataset like this:
[
{state: 'CA', count: 2, email_count: 2},
{state: 'WY', count: 2, email_count: 1}
]
Try this!
SELECT
state,
COUNT(*) AS total_count,
SUM(CASE WHEN email is not null then 1 ELSE 0 END) AS count_email_not_null
FROM users
GROUP BY state
It will give you your intended output.
I have two tables; Leads, Territories and Referrers.
Lead has columns: ID, Name, TerritoryId
Referrers has: ID, LeadId, Name
Territory has: ID and Name
A lead always relates to a territory and a lead can optionally relate to a Referrer.
Leads and Referrer records are regularly inserted (Referrers less frequently). I want to output a report in a GridView which looks like this:
Territory | Lead Count | Ref1 Lead Count | Ref2 Lead Count | Ref3 Lead Count
Leeds 10 1 7 2
Exeter 43 9 21 8
etc...
OK, so the problem is, I want to group by Territory and Count the leads per territory.... this is fine:-
select t.Name, COUNT(1)
from Territory t inner join Lead l on l.TerritoryID = t.Id
group by t.Name
But now I want to break down count by referrer.
I understand I can do that partly with PIVOT, however, I understand that I have to explicitly state the Referrers in code. Is there any way to perform some kind of dynamic pivot which appends additional columns based on the number of rows in Referrer?
Would I have to use dynamic SQL inside an SP?
something like this:
select * from (select r.name, t.name as Territory
from referrers r join Lead l on l.Id = r.leadId
join Territory t on l. TerritoryID = t.Id) s
pivot(count(Name) for Name in ([geoff],[fred])) p
As far as I can see the referrers would have to be specified explicitly, so you would have to generate the square bracketed list in an sp if you wanted them to be dynamic.