Union Query Across Databases - ms-access

I want to run a union query on qry_1 in C:\DB\DB1.mdb and qry_2 in C:\DB\DB2.mdb
All examples I have seen only show how to run a Union Query on query's in the same database, how can you do this when the query's are in different databases?
EDIT ---
Grr, not quite displaying results like I was after. That shows each entry twice. Is there a way to only show the entry one time? For example if qry_1 returns Joe 14, Jack 16, Jimmy 12 and qry_2 returns Joe 22, Jack 48, Jimmy 66 is there a way to SUM those results in the UNION Query?
I tried changing the syntax to a group by like so, but didn't work:
Select Name, Count FROM [C:\DB\DB1.mdb].qry_1
UNION ALL
SELECT Name, Count FROM [C:\DB\DB2.mdb].qry_2
GROUP BY Name, Count

For your case nothing changed, use:
SELECT * FROM [C:\DB\DB1.mdb].qry_1
UNION ALL
SELECT * FROM [C:\DB\DB2.mdb].qry_2
UPD: To SUM them i would recommend GROUP BY. Let's say we saved upper query as qry_U then try smth like this:
SELECT [Name], SUM([Numbers]) FROM [qry_U] GROUP BY [Name]

Related

Showing a single column to multiple (n times)

I have a normal report like the below one,
Animal
Dog
Cat
Elephant
Tiger
Lion
Man
Panda
Which i want to do a break after n number of columns.
Animal Animal
Dog Cat
Elephant Tiger
Lion Man
Panda
I tried the page break which breaks into a new page. Did some grouping etc.. still didn't work out. Is there a way in SSRS to work this out. I know how to do it via SQL, iam asking about doing it via SSRS.
Edit:
I have used the below query in SSRS.
SELECT 610 AS [Period],1 AS RowNumber
UNION
select 611, 2
UNION
select 612, 3
UNION
select 613, 4
UNION
select 614, 5
UNION
select 615, 6
UNION
select 616, 7
UNION
select 617, 8
I added a new Matrix table, then i added 'Period' field to the matrix table (Screenshot Num 1). In the column grouping expression. i have used the below expressions. One at a time
=Floor(Fields!RowNumber.Value / 3) and =Fields!RowNumber.Value Mod 3
but now it shows only 3 columns now.
What i have expected to see is (Screenshot Num 3) and what iam getting is (Screenshot Num 2).
Screenshot:
You can't use SSRS aggregate functions like RowNumber in grouping expressions or dataset calculated fields. So you would need to add a number to your rows in the SQL. I would recommend using ROW_NUMBER for this. You could also use a RANK function. You don't have to partition by anything, just give it something to sort by and you're good to go.
Then in the report, you can use a column grouping expression like this to get 5 rows per column:
=Floor(Fields!RowNumber.Value / 5)
To use a specific number of columns you could use an expression like this:
=Fields!RowNumber.Value Mod 5

MS Access count query does not produce wanted results

I have a table (tblExam) showing exam data score designed as follow:
Exam Name: String
Score: number(pecent)
Basically I am trying to pull the records by Exam name where the score are less than a specific amount (0.695 in my case).
I am using the following statement to get the results:
SELECT DISTINCTROW tblExam.name, Count(tblExam.name) AS CountOfName
FROM tblExam WHERE (((tblExam.Score)<0.695))
GROUP BY tblExam.name;
This works fine but does not display the exam that have 0 records more than 0.695; in other words I am getting this:
Exam Name count
firstExam 2
secondExam 1
thirdExam 3
The count of 0 and any exams with score above 0.695 do not show up. What I would like is something like this:
Exam Name count
firstExam 2
secondExam 1
thirdExam 3
fourthExam 0
fifthExam 0
sixthExam 2
.
..
.etc...
I hope that I am making sense here. I think that I need somekind of LEFT JOIN to display all of the exam name but I can not come up with the proper syntax.
It seems you want to display all name groups and, within each group, the count of Score < 0.695. So I think you should move < 0.695 from the WHERE to the Count() expression --- actually remove the WHERE clause.
SELECT
e.name,
Count(IIf(e.Score < 0.695, 1, Null)) AS CountOfName
FROM tblExam AS e
GROUP BY e.name;
That works because Count() counts only non-Null values. You could use Sum() instead of Count() if that seems clearer:
Sum(IIf(e.Score < 0.695, 1, 0)) AS CountOfName
Note DISTINCTROW is not useful in a GROUP BY query, because the grouping makes the rows unique without it. So I removed DISTINCTROW from the query.
Do I detect a contradiction? The query calls for results <0.695 but your text says you are also looking for results >0.695. Perhaps I don't understand. Does this give you what you are looking for:
SELECT DISTINCTROW tblExam.ExamName, Count(tblExam.ExamName) AS CountOfExamName
FROM tblExam
WHERE (((tblExam.Score)<0.695 Or (tblExam.Score)>0.695))
GROUP BY tblExam.ExamName;

SQL: Add dash Line (-) at the end of query result

Here Im trying to add extra row by using union query between data and result data.
select color, item, sum(qua) from inventory
Group by color, item
UNION
Select '----','----','----'
Union
select Count(color), 'total', sum(qua)
from inventory
Result would be:
ITME COLOR QUA
---- ----- ----
chair Black 520
chair pink 1028
chair Red 1050
chair Yellow 524
table Black 1048
table Blue 124
table pink 624
table Red 524
table Yellow 548
----- ----- ----- <----This extra row.
13 total 5990
I used above query but It show me Data type mismatch error.
Conversion failed when converting the varchar value '----' to data type int.
I'm using Microsoft SQL server management studio r2 2008
No, you shouldn't do this. To do it you'd have to cast the value of sum(qua) to a varchar, but this is a bad idea.
Drawing a line should be handled in your presentation layer.
Use UNION ALL instead of UNION
UNION The UNION command is used to select related information from two tables, much like the JOIN command. However, when using the
UNION command all selected columns need to be of the same data
type. With UNION, only distinct values are selected.
UNION ALL The UNION ALL command is equal to the UNION command, except that UNION ALL selects all values.
Reference
You should always be using UNION ALL unless you are removing duplicates.
There are two things here. First is that types of sum(qua) and '----' are different (number vs. text).
select color, item, convert(nvarchar(30),sum(qua)) from inventory
Group by color, item
UNION
Select '----','----','----'
Union
select Count(color), 'total', sum(qua)
from inventory
Second is that you expect SQL to return data in order of SELECTs and that is not the case. Rows from unions can come in any order. To achieve the result you want an ordering trick is neccessary:
select color, item, summary from
(
select 0 as ToOrder, color, item, convert(nvarchar(30),sum(qua)) as summary from inventory
Group by color, item
UNION
Select 1, '----','----','----'
Union
select 2, Count(color), 'total', sum(qua)
from inventory
) as x
order by ToOrder asc
This should be used for testing in the query window only otherwise, follow Mark Byers advice
select color, item, CAST(sum(qua) as VARCHAR) As QUA from inventory
Group by color, item
UNION ALL
Select '----','----','----'
UNION ALL
select CAST(Count(color) AS VARCHAR), 'total', CAST(sum(qua) AS VARCHAR)
from inventory

Selecting most recent as part of group by (or other solution ...)

I've got a table where the columns that matter look like this:
username
source
description
My goal is to get the 10 most recent records where a user/source combination is unique. From the following data:
1 katie facebook loved it!
2 katie facebook it could have been better.
3 tom twitter less then 140
4 katie twitter Wowzers!
The query should return records 2,3 and 4 (assume higher IDs are more recent - the actual table uses a timestamp column).
My current solution 'works' but requires 1 select to generate the 10 records, then 1 select to get the proper description per row (so 11 selects to generate 10 records) ... I have to imagine there's a better way to go. That solution is:
SELECT max(id) as MAX_ID, username, source, topic
FROM events
GROUP BY source, username
ORDER BY MAX_ID desc;
It returns the proper ids, but the wrong descriptions so I can then select the proper descriptions by the record ID.
Untested, but you should be able to handle this with a join:
SELECT
fullEvent.id,
fullEvent.username,
fullEvent.source,
fullEvent.topic
FROM
events fullEvent JOIN
(
SELECT max(id) as MAX_ID, username, source
FROM events
GROUP BY source, username
) maxEvent ON maxEvent.MAX_ID = fullEvent.id
ORDER BY fullEvent.id desc;

MySql : UNION is not getting executed when executed as a view

I am trying to create a view for a UNION of 2 select statements that I have created.
The UNION is working fine when executed individually
But the problem is only the 1st part of the UNION is getting executed when I am executing it as a view.
The query I am using is as below
SELECT DISTINCT products.pid AS id,
products.pname AS name,
products.p_desc AS description,
products.p_uid AS userid,
products.p_loc AS location,
products.isaproduct AS whatisit
FROM products
UNION
SELECT DISTINCT services.s_id AS id,
services.s_name AS name,
services.s_desc AS description,
services.s_uid AS userid,
services.s_location AS location,
services.isaservice AS whatisit
FROM services
WHERE services.s_name
The above works fine when i execute it separately. But when I use it as a view, it does not give me the results of the services part.
Could someone please help me with this?
If you could give the result set for each individual query above and then also give the result set for the UNION query, we could probably provide a better answer to your question. My gut reaction is that the second query may be returning a duplicate value, and since you are using UNION, duplicates are being removed. If you used UNION ALL, then all duplicate rows would be returned. For example, if the first query returned the row:
1 name1 description1 10 Home Y
2 name2 description2 20 Work Y
and the second row returned:
1 name1 description1 10 Home Y
The resulting output would be:
1 name1 description1 10 Home Y
2 name2 description2 20 Work Y
If you want all of the rows returned:
1 name1 description1 10 Home Y
2 name2 description2 20 Work Y
1 name1 description1 10 Home Y
Then you would use a UNION ALL instead of a UNION statement.
i think your fields userid and location are swapped in the two selects from the union, if of diferent data types, you will get an error, if not, you will get wrong results...is that it?