Combining a TRANSFORM......PIVOT and a UNION Query - mysql

it's my first time posting here but this site has supplied me with lots of answers in the past so I'll give it a try.
Background information: I'm using Acces 2013, connecting to a MySQL database through ODBC 3.51.
I'm busy pulling reports out of a Call Detail record, namely I want to make an overview of the times different employees have spent receiving and making outgoing calls on different phone lines.
I have already found out I can't make two seperate TRANSFORM...PIVOT query's and UNION those. That makes sense, because there's no way the code knows my 2 query's will return the same amount of Pivots.
Is it possible to turn this process around? (First collect the data you need in a UNION, and then transforming the data).
I created the following Union query collecting the relevant data:
(SELECT dbo_IpPbxCDR.DestinationName AS [Name], Sum(DateDiff('n',[dbo_IpPbxCDR.starttime],[dbo_IpPbxCDR.endtime])) AS [Called Time], IIf((dbo_IpPbxCDR.CalledName Like "*0900*") OR (dbo_IpPbxCDR_1.CalledName like "*0900*"), "0900", "abo") AS [Called Line]
FROM (dbo_IpPbxCDR AS dbo_IpPbxCDR_1 RIGHT JOIN dbo_IpPbxCDR ON dbo_IpPbxCDR_1.CallId = dbo_IpPbxCDR.TransferredCallId2)
WHERE <Where clause relevant to the data I want. It’s pretty long so I omitted it>
GROUP BY dbo_IpPbxCDR.DestinationName, IIf((dbo_IpPbxCDR.CalledName Like "*0900*") OR (dbo_IpPbxCDR_1.CalledName like "*0900*"), "0900", "abo"))
UNION
(SELECT dbo_IpPbxCDR.OriginationName AS [Name], Sum(DateDiff('n',[dbo_IpPbxCDR.starttime],[dbo_IpPbxCDR.endtime])) AS [Called Time], 'uit' AS [Called Line]
FROM dbo_IpPbxCDR
WHERE <Where clause relevant to the data I want. It’s pretty long so I omitted it>
GROUP BY dbo_IpPbxCDR.OriginationName)
I'm trying to transform on Sum(DateDiff('n',[dbo_IpPbxCDR.starttime],[dbo_IpPbxCDR.endtime])) so I can get data relevant to certain months with pivot Month([dbo_IpPbxCDR.starttime])
I'd appreciate any help/advice I can get!

Related

Access Report from Query inexplicably multiplies columns

I have a weird problem. First off, I'm not well versed in Access, but I'm using it for a project to track visibility of log sources and detections and map them to the MITRE ATT&CK Framework. I tables such as Techniques to track the MITRE Techniques, Data_Sources to track the various sources used by those Techniques, and Data_Sources_Coverage that has a visibility ranking for each data source for the company. I have a query built with the below SQL (probably a little messy):
SELECT Technique.Name, Technique.ID, Technique.[Tactic ID], Count(Technique.[Data Sources].Value) AS Technique_Sources, Count(Data_Source_Coverage.[Data Source]) AS Sources_Visible
FROM (Data_Sources INNER JOIN Technique ON Data_Sources.Name = Technique.[Data Sources].Value) INNER JOIN Data_Source_Coverage ON Data_Sources.Name = Data_Source_Coverage.[Data Source]
WHERE (Data_Source_Coverage.Coverage)>0)
GROUP BY Technique.Name, Technique.ID, Technique.[Tactic ID];
The query works great, prints the results as I expect them. However, when I generate a report from that query things go off the rails. The report inexplicably (I did nothing to the data), multiplies the Technique_Sources column by the Sources_Visible column and displays that in the Technique_Sources Column, and then every entry in the Sources_Visible column becomes a '1'. Pics below. Thanks for the help.

SQL Is this Outer or Inner Join and how to join on Array

Seemed simple when I started and have done this before, now I confused myself and at a road block.
Have two tables: News_Table and a People_Table. Under the News_Table there is a field: News_People_Contributed and it has the ID's of the People_Table in array format (1,4,7,10) thus Four People contributed. I am creating a search parameter that looks up News_Header AND News_People_Contributed and can't figure how to create the search column.
News_Table
News_ID
News_Header
News_People_Contributed
People_Table
People_ID
People_First_Name...
Is it something like...
Select*
From News_Table
Left Join News_Table
On People_Table.People_ID IN (News_Table.News_People_Contributed)
Where Search_Param Like '%News_Header%' OR Search_Param Like '%People_First_Name%'
The problem is (News_Table.News_People_Contributed) is a string and the ID's are not. Plus I may not have people contributed etc. To make the issue even more complex, I'm doing this in MS Access instead of MySql, so have to code it "old school" sql for work around.
Perform a cross join and filter on matches in the string list. It says nothing about efficiency or form (as already commented on), but it works.
SELECT *
FROM News_Table, People_Table
WHERE InStr([News_People_Contributed],CStr([People_ID])) > 0;
This only answers part of the problem: The join -- the issue everyone seemed concerned about in the initial comments. There are not enough details about about the Search_Parameter to provide help on that. Supply more detail if you need more help there.

Query in access not returning correct result due to join?

I'm having a bit of difficulty wrapping my head around some queries that I need joined correctly to each other, in order to get the correct result.
Basically I got a database with a sewage network in it, the network has manholes with IDs and pipes with IDs, all pipes are defined with one UpstreamManholeID (OpstroemKnudeID) and a DownstreamManholeID (NedstroemKnudeID) (Knude table = manhole).
When I try to make a query based on observations in the pipe, I keep getting the same PipeID listed for pipes with different Upstream and downstream ID. As shown in the picture below, probably because it only checks for the upstream manhole id before choosing a pipe id.
I tried changing the query, but that returns blanks in the PipeID instead.
To sum up, I need to list the PipeID based on both upstream and downstream node, as it is, I can only get it to check for either upstream or downstream.
I'm having a hard time explaining, so feel free to ask any clarifying questions you need.
At the moment I'm running two queries, one calling the other, in order to get the almost correct result.
SELECT TVInspektion.Startpunktnr AS OpstrømsKnudenavn, TVInspektion.Slutpunktnr AS NedstrømsKnudenavn, Knude.ID AS OP, Knude_1.ID AS Ned
FROM (TVInspektion LEFT JOIN Knude ON TVInspektion.Startpunktnr = Knude.Knudenavn) LEFT JOIN Knude AS Knude_1 ON TVInspektion.Slutpunktnr = Knude_1.Knudenavn;
SELECT [Copy Of Vurderes].OP, [Copy Of Vurderes].OpstrømsKnudenavn, [Copy Of Vurderes].Ned, [Copy Of Vurderes].NedstrømsKnudenavn, Ledning.ID
FROM [Copy Of Vurderes] LEFT JOIN Ledning ON ([Copy Of Vurderes].Ned = Ledning.NedstroemKnudeID) AND ([Copy Of Vurderes].OP = Ledning.OpstroemKnudeID);
That returns the following result:

MySQL finding data if any 4 of 5 columns are found in a row

I have an imported table of several thousand customers, the development I am working on runs on the basis of anonymity for purchase checkouts (customers do not need to log in to check out), but if enough of their details match the database record then do a soft match and email the (probably new) email address and eventually associate the anonymous checkout with the account record on file.
This is rolling out this way due to the age of the records, many people have the same postal address or names but not the same email address, likewise some people will have moved house and some people will have changed name (marriage etc).
What I think I am looking for is a MySQL CASE system, however the CASE questions on Stack Overflow I've found don't appear to cover what I'm trying to get from this query.
The query should work something like this:
$input[0] = postcode (zip code)
$input[1] = postal address
$input[2] = phone number
$input[3] = surname
$input[4] = forename
SELECT account_id FROM account WHERE <4 or more of the variables listed match the same row>
The only way I KNOW I can do this is with a massive bunch of OR statements but that's excessive and I'm sure there's a cleaner more concise method.
I also apologise in advance if this is relatively easy but I don't [think I] know the keyword to research constructing this. As I say, CASE is my best guess.
I'm having trouble working out how to manipulate CASE to fit what I'm trying to do. I do not need to return the values only the account_id from the valid row (only) that matches 4 or 5 of the given inputs.
I imagine that I could construct a layout that does this:
SELECT account_id CASE <if postcode_column=postcode_var> X=X+1
CASE <if surname_column=surname_var> X=X+1
...
...
WHERE X > 3
Is CASE the right idea?
If not, What is the process I need to use to achieve the desired results?
What is [another] MySQL keyword / syntax I need to research, if not CASE.
Here is your pseudo query:
SELECT account_id
FROM account
WHERE (postcode = 'pc')+
(postal_address = 'pa')+
(phone_number = '12345678901')+
(surname = 'sn')+
(forename= 'fn') > 3

inner query of subqery returning multiple rows

I am not that experience in sql so please forgive if its not a good question to ask,but i researched around almost for 3-4 days but no able to solve.
My problem is i have a table which have multiple image names in it,so what i have to do is whoever is the follower of a particular user i have to get the imaged from this table,so one user there can be multiple followers,so i have to fetch the images posted by all the followers.
Here is the subquery code snippet i am using.
SELECT id,
outfit_image,
img_title,
description
FROM outfitpic_list r2
WHERE Email=ANY(SELECT being_followed
FROM follower_table
WHERE follower='test#gmail.com')
So the inner query here returns multiple values,for each value(being_followed) i have to fetch all the images and display it,but with this query each time i get only one image.I tried IN also but didnot work out.
Table structure:-
Outfitpic_list table
id|outfit_image|datetime|Email|image_title|description
Follower_table
bring_followed|follower
Please help,I am stuck..!!
Thank you..!!
I think your problem may be the = sign between "E-mail" and "Any". Try this statement:
SELECT
id,
outfit_image,
img_title,
description
FROM outfitpic_list r2
WHERE Email IN
(
SELECT being_followed
FROM follower_table
WHERE follower='test#gmail.com'
)
It's the same statement, without the = sign, and the ANY keyword replaced with IN. (I cleaned it up a little to make it more readable)