Access database help - picking football games - database

I am trying to create a database for picking football game winners.
Simply, I have a table of available games:
| GameID | AwayTeamName | HomeTeamName |
1 Team1 Team2
2 Team3 Team4
etc.
And
Table2
| GameID | MyPick |
1 drop-down here
2 drop-down here
etc.
I'm trying to have MyPick be a drop-down showing only the AwayTeamName and HomeTeamName for GameID for me to pick from.
I've tried using the lookup wizard to no avail as well as using a query.
I'm using Access 2019.
Any suggestions? Thank you in advance.
UPDATE:
I have created this SQL query:
SELECT [Raw Data].[AWAY TEAM] AS [SELECTION]
FROM [Raw Data]
WHERE [Raw Data].[Game ID] = 20191901
UNION ALL
SELECT [Raw Data].[HOME TEAM]
FROM [Raw Data]
WHERE [Raw Data].[Game ID] = 20191901
and it works perfectly. Now I just need to figure out how to send/tell the query to use the Game ID I'm currently working on.
CLARIFICATION:
I am using a form for data input. I already have the list of games loaded. I just need to be able to select from the two teams listed on the form.
The form is something like this:
Game ID
AwayTeam
Home Team
Pick the winner: _______________
First Prev Next Last
I'm wanting to just have the two teams listed in the combobox for picking.

Related

SQL Server Management Studio - Add columns containing software name and version to SQL Query for systems with specific software

I am using Microsoft SQL Server Management Studio to query for computers with a specific software in SQL Server database. Unfortunately, I am completely new to SQL so I am trying to learn on the fly. I was able to create a basic query thanks to help on Prajwal's site.
However I would like to refine it a little bit. I want to have two extra columns which display the software name, and the software version.
To start, here is my code so far:
SELECT DISTINCT
sys.Netbios_Name0 AS [Computer Name],
sys.User_Name0 AS [User Name]
FROM
v_R_System sys
JOIN
v_Add_Remove_Programs arp ON sys.ResourceID = arp.ResourceID
WHERE
sys.ResourceID IN (SELECT sys.ResourceID
FROM v_R_System sys
JOIN v_Add_Remove_Programs arp ON sys.ResourceID = arp.ResourceID
WHERE DisplayName0 LIKE '%Adobe Reader%')
This returns the following output:
Computer Name | User Name
--------------+-----------
COMPUTER001 | Jane Doe
COMPUTER002 | John Doe
However I would like output like the below example, with the software name from the where statement as well as its version:
Computer Name | User Name | Software | Version
--------------+-----------+--------------+---------
COMPUTER001 | Jane Doe | Adobe Reader | v.XXX
COMPUTER002 | John Doe | Adobe Reader | v.XXX
I have tried adding to my Select statement these properties:
SELECT DISTINCT
sys.Netbios_Name0 AS [Computer Name],
sys.User_Name0 AS [User Name],
arp.DisplayName0 AS [Software],
arp.Version0 AS [Version]
But this returns EVERY software a computer has, like so:
Computer Name | User Name | Software | Version
--------------+-----------+--------------+--------
COMPUTER001 | Jane Doe | Adobe Reader | v.XXX
COMPUTER001 | Jane Doe | Firefox | v.XXX
COMPUTER001 | Jane Doe | Chrome | v.XXX
I need it just to give the specific software I query. I have searched on the internet to see if I could find anything to help me, and started learning some SQL basics, but haven't had much luck so far. I am going to continue researching and working on it, but if anyone can offer me any tips or advice, I would really appreciate it!
By the way, I have managed to create the query successfully in SCCM, but I am not so sure how to translate WQL into SQL and work wants me to make the query in SSMS.
If I understand correctly, you don't really need an inner query (which is BTW bad practice and should be avoided at all costs, since this query runs on every row). You query can be simplified to code below to achieve what you need.
SELECT DISTINCT
sys.Netbios_Name0 AS [Computer Name],
sys.User_Name0 AS [User Name],
arp.DisplayName0 AS [Software],
arp.Version0 AS [Version]
FROM v_R_System sys
JOIN v_Add_Remove_Programs arp ON sys.ResourceID = arp.ResourceID
WHERE arp.DisplayName0 LIKE '%Adobe Reader%'

I'm trying to count how many times data repeats in a table in SQL

I have a database that stores customer information in 2 tables.
Table stores
(tbl.contacts)
| Companyname | CountryID
and the second table (tbl_geo_country)
| ID | Countrycode | Name |
Now I want to create a report that can show me how many customers are from what country. example output
| Country | QNT |
Norway 5
USA 3
Sweden 2
I dont know how many different countries it has stored so it also needs to check that.
Seems like a JOIN and GROUP BY to me:
SELECT country.Name, COUNT(contact.ID) as QNT
FROM tbl_geo_country country
INNER JOIN tbl.contacts contact ON country.ID = contact.CountryID
GROUP BY country.Name
ORDER BY COUNT(contact.ID)
Keep in mind that this would return only countries, that have at least one contact. If you also need countries that have no contacts, you need to change INNER JOIN to LEFT JOIN.

join tables without common field in sqlserver one in veritcal and other in horizontal

I have 2 tables one is accounts where users user stored his multiple accounts and other is reports table where we have stored the number of reports but there is no link between them.
table 1 (accounts)
accountid | contactId | accountname
121 12 personal
122 12 official
table 2 (Reports)
Reportid | ReportName
1 bankstatment
2 panreport
3 other report
Now I want this output
account | contactid| reportid | reportname | ReportId | ReportName | ReportId | ReportName
121 12 1 bankstatment 2 panreport 3 other report
122 12 1 bankstatment 2 panreport 3 other report
How I can make it in SQL Server query ? I am asking this because I am not able to identify which account has selected which report in mvc.net in grid form
Explanation
I am basically CROSS JOINing the accounts table with the PIVOT on the reports table.
This allows us to generate the desired output of all reports in a single row for each account.
Please try this query:
select
a.*,
1 as reportid,
[1] as reportname,
2 as reportid,
[2] as reportname,
3 as reportid,
[3] as reportname
from
accounts a
cross join
(
select [1],[2],[3]
from
(select * from Reports) s
pivot
(Max(ReportName) for Reportid in ([1],[2],[3]))p
)t
sql fiddle for demo : http://sqlfiddle.com/#!6/703c4/9
Simple answer: Not Possible
If there are 5 Bank Statements report in your report table, and 5 entries in your Accounts table, how would you know which report belongs to which account?
Also the last comment "I am not able to identify which account has selected which report in mvc.net in grid form" of your question is a different thing to what you have shown in your example. If you have fixed number of reports and you want a column for each of those report type, then yes, but if you want selected reports for each account, then you need to store that selection somewhere.

SQL make rows into columns, PIVOT maybe

I have an MS SQL Server with a database for an E-commerce storefront.
This is some of the tables I have:
Products:
Id | Name | Price
ProductAttributeTypes: -Color, Size, Format
Id | Name
ProductAttributes: --Red, Green, 12x20 cm, Mirrored
Id | ProductAttributeTypeId | Name
Orders:
Id | DateCreated
OrderItems:
Id | OrderId | ProductId
OrderItemsToProductAttributes: --Relates an OrderItem to its product and selected attributes
OrderItemId | ProductAttributeId | ProductAttributeTypeId | ProductId
I want to select from the OrderItems table, to see which items have been purchased.
To see what kind of variants (ProductAtriibutes) was selected, I want those as "dynamic" columns in the resultset.
So the resultset should look like this:
OrderItemId | ProductId | ProductName | Color | Size | Format
1234 123 Mount. Bike Red 2x20 Mirror
I don't know if PIVOT is the thing to use? I'm not using any aggregate functions, so I guess not...
Is there any SQL Ninjas that can help me out?
If you are using sql2005 or 2008 you can use the pivot command. See here.
In the example below the OrderAttributes set will look like:
OrderItemId AttName AttValue
----- ------ -----
100 Color Red
100 Size Small
101 Color Blue
101 Size Small
102 Color Red
102 Size Small
103 Color Blue
103 Size Large
The final results after the PIVOT will be:
OrderItemId Size Color
----- ------ -----
100 Small Red
101 Small Blue
102 Small Red
103 Large Blue
WITH OrderAttributes(OrderItemId, AttName, AttValue)
AS (
SELECT
OrderItemId,
pat.Name AS AttName,
pa.Name AS AttValue
FROM OrderItemsToProductAttributes x
INNER JOIN ProductAttributes pa
ON x.ProductAttributeId = pa.id
INNER JOIN ProductAttributeTypes pat
ON pa.ProductAttributeTypeId = pat.Id
)
SELECT AttrPivot.OrderItemId,
[Size] AS [Size],
[Color] AS Color
FROM OrderAttributes
PIVOT (
MAX([AttValue])
FOR [AttName] IN ([Color],[Size])
) AS AttrPivot
ORDER BY AttrPivot.OrderItemId
There is a way to dynamically build the columns (i.e. the Color and Size columns), as can be seen here. Make sure your database compatibility level on your database is set to something greater than 2000 or you will get strange errors.
In the past, I've created physical tables for read purposes only. The structure you have above is GREAT for storage, but terrible for reporting.
So you could do the following:
Write a script (that is scheduled nightly) or a trigger (on data change) that does the following tasks:
First, you would dynamically go through each Product and build a static table "Product_[ProductName]"
Then go through each ProductAttributeTypes for each product and create/update/delete a physical column on the corresponding Product table.
Then, fill that table with the proper values based on OrderItemsToProductAttributes and ProductAttributes
This is just a rough idea. Make sure you are storing OrderID in the "Static"/"Flattened" tables. And make sure you do everything else you need to do. But after that, you should be able to start pulling from those flattened tables to get the data you need.
Pivot is your best bet, but what I did for reporting purposes, and to make it work well with SSIS is to create a view, which then has this query:
SELECT [InputSetID], [InputSetName], CAST([470] AS int) AS [Created By], CAST([480] AS datetime) AS [Created], CAST([479] AS int) AS [Updated By], CAST([460] AS datetime)
AS [Updated]
FROM (SELECT st.InputSetID, st.InputSetName, avt.InputSetID AS avtID, avt.AttributeID, avt.Value
FROM app.InputSetAttributeValue avt JOIN
app.InputSets st ON avt.InputSetID = st.InputSetID) AS p PIVOT (MAX(Value) FOR AttributeID IN ([470], [480], [479], [460])) AS pvt
Then I can just interact with the view, but, I have a trigger on the table that any new dynamic attributes must be added to, which recreates this view, so I can assume the view is always correct.

Outputting Results from complicated database structure (SQL Server)

This will be a long question so I'll try and explain it as best as I can.
I've developed a simple reporting tool in which a number of results are stored and given a report id, these results were generated from a particular quote being used on the main system, with a huge list of these being stored in a quotes table. Here are the current batch:
REPORTS
REP_ID DESC QUOTE_ID
-----------------------------------
1 Test 1
2 Today 1
3 Last Week 2
RESULTS
RES_ID TITLE REFERENCE REP_ID
---------------------------------------------------
1 Equipment Toby 1
2 Inventory Carl 1
3 Stocks Guest 2
4 Portfolios Guest 3
QUOTE
QUOTE_ID QUOTE
------------------------------------
1 Booking a meeting room
2 Car Park Policy
3 New User Guide
So far, so good, a simple stored procedure was able to pull all the information necessary.
Now, the feature list has been upped to include categories and groups of the quotes. In the Reports table quote_id has been changed to group_id to link to the following tables.
REPORTS
- REPORT_ID
- DESC
- GROUP_ID
GROUP
- GROUP_ID
- GROUP
GROUP_CAT_JOIN
- GCJ_ID
- CAT_ID
- GROUP_ID
CATEGORIES
- CAT_ID
- CATEGORY
CAT_QUOTE_JOIN
- CQJ_ID
- CAT_ID
- QUOTE_ID
The idea of these changes is so that instead of running a report on a quote I should now write a report for a group where a group is a set of quotes for certain occasions. I should also be able to run a report on a category where a category is also a set of quotes for certain departments. The trick is that several categories can fall into one group.
To explain it further, the results table has a report_id that links to reports, reports has a group_id that links to groups, groups and categories are linked through a group_cat_join table, the same with categories and quotes through a cat_quote_join table.
In basic terms I should be able to pull all the results from either a group of quotes or a category of quotes. The query will aim to pull all the results from a certain report under either a certain category, a group or both. This puzzle has left me stumped for days now as inner joins don't appear to be working and I'm struggling to find other ways to solve the problem using SQL.
Can anyone here help me?
Here's some extra clarification.
I want to be able to return all the results within a category, but as of right now the solution below and the ones I've tried always output every solution within a description, which is not what I want.
Here's an example of the data I have in there at the moment
Results
RES_ID TITLE REFERENCE REP_ID
---------------------------------------------------
1 Equipment Toby 1
2 Inventory Carl 1
3 Stocks Guest 2
4 Portfolios Guest 3
Reports
REP_ID DESC GROUP_ID
-----------------------------------
1 Test 1
2 Today 1
3 Last Week 2
GROUP
GROUP_ID GROUP
---------------------------------
1 Standard
2 Target Week
GROUP_CAT_JOIN
GCJ_ID GROUP_ID CAT_ID
----------------------------------
1 1 1
2 1 2
3 2 3
CATEGORIES
CAT_ID CAT
-------------------------------
1 York Office
2 Glasgow Office
3 Aberdeen Office
CAT_QUOTE_JOIN
CQJ_ID CAT_ID QUOTE_ID
-----------------------------------
1 1 1
2 2 2
3 3 3
QUOTE
QUOTE_ID QUOTE
------------------------------------
1 Booking a meeting room
2 Car Park Policy
3 New User Guide
This is the test data I am using at the moment and to my knowledge it is similar to what will be run through once this is done. In all honesty I'm still trying to get my head around this structure.
The result I am looking for is if I choose to search by group I'll get everything within a group, if I choose everything inside a category I get everything just inside that category, and if I choose something from a category in a group I get everything inside that category. The problem at the moment is that whenever the group is referenced everything inside every category that's linked to the group is pulled.
The following will get the necessary rows from the results:
select
a.*
from
results a
inner join reports b on
a.rep_id = b.rep_id
and (-1 = #GroupID or
b.group_id = #GroupID)
and (-1 = #CatID or
b.cat_id = #CatID)
Note that I used -1 as the placeholder for all Groups and Categories. Obviously, use a value that makes sense to you. However, this way, you can specify a specific group_id or a specific cat_id and get the results that you want.
Additionally, if you want Group/Category/Quote details, you can always append more inner joins to get that info.
Also note that I added the Group_ID and Cat_ID conditions to the Reports table. This would be the SQL necessary if and only if you add a Cat_ID column to the Reports table. I know that your current table structure doesn't support this, but it needs to. Otherwise, as my grandfather used to say, "Boy, you can't get there from here." The issue here is that you want to limit reports by group and category, but reports only knows about group. Therefore, we need to tie something to the category from reports. Otherwise, it will never, ever, ever limit reports by category. The only thing that you can limit by both group and category is quotes. And that doesn't seem to be your requirement.
As an addendum: If you add cat_id to results instead of reports, the join condition should be:
and (-1 = #CatID or
a.cat_id = #CatID)
Is this what you are looking for?
SELECT a.*
FROM Results a
JOIN Reports b ON a.REP_Id = c.REP_Id
WHERE EXISTS (
SELECT * FROM CAT_QUOTE_JOIN c
WHERE c.QUOTE_ID = b.QUOTE_ID -- correlation to the outer query
AND c.CAT_ID = #CAT_ID -- parameterization
)
OR EXISTS (
-- note that subquery table aliases are not visible to other subqueries
-- so we can reuse the same letters
SELECT * FROM CAT_QUOTE_JOIN c, GROUP_CAT_JOIN d
WHERE c.CAT_ID = d.CAT_ID -- subquery join
AND c.QUOTE_ID = b.QUOTE_ID -- correlation to the outer query
AND d.GROUP_ID = #GROUP_ID -- parameterization
)

Resources