I've got my brain squeezed out trying to olve following issue. Hope you may help.
My datasource:
JrNo JrLineNo Employee LineDescription
-----------------------------------------------
1 1 BBB Target Description 01
1 2 AAA Target Description 02
2 1 BBB Target Description 03
2 2 DDD Target Description 04
2 3 AAA Target Description 05
2 4 CCC Target Description 06
2 5 DDD Target Description 07
3 1 AAA Target Description 08
3 2 AAA Target Description 09
3 3 BBB Target Description 10
4 1 AAA Target Description 11
4 2 DDD Target Description 12
4 3 AAA Target Description 13
4 4 CCC Target Description 14
I have created report which is grouped by Employees (AAA, BBB, CCC, DDD)
On each group footer I want to see Description field of "LineDescription" column, filtered by the highest journal number and highest journal line number (within that journal) for every Employee.
For example: The highest journal number in above table is 4, but the highest journal line number is 5 (in jounal 2). If I roughly take Max of JrNo and Max of JrLineNo, it simply will not give expected result.
Expected result:
AAA Target Description 13
BBB Target Description 10
CCC Target Description 14
DDD Target Description 12
If I'd use SQL my query would look like:
SELECT LineDescription
FROM table
WHERE JrNo = (SELECT Max(JrNo) from table WHERE Employee = 'AAA')
AND JrLineNo = (SELECT Max(JrLineNo) FROM table
WHERE JrNo = (SELECT Max(JrNo) from table WHERE Employee = 'AAA'))
But I need to develop this in ssrs Expression way. And Iif function does not help much.
Any ideas - how to build expression which selects only one record based on multiple value filters and subqueries? What is alternative of WHERE clause?
Related
I want to have a SQL result look like this (match result query):
ID Arena Winner Loser Winner_score Loser_score
-----------------------------------------------------------------
1 1 Johnny Mark 16 8
2 2 Andrew Luke 16 7
Here are my tables (simplified)
Player_tbl
ID Player
-------------
1 Johnny
2 Mark
3 Andrew
4 Luke
Match_tbl
ID Match Arena
----------------------
1 Match 1 1
2 Match 2 2
Match_results_tbl
ID Match player_id player_score
-----------------------------------------
1 Match 1 1 16
2 Match 1 2 8
1 Match 2 3 16
2 Match 2 4 7
I am wondering how to structure my query to have the desired result.
Ok, I figured out my own issue. 1st, the match_results must be put in a table with a different structure, and 2nd, the inner sql query needed to be adjusted to pull from the proper database tables.
Please, can someone Help?
I have 2 Tables with complete info about CITY (table1) and COORDINATES (table2).
In phpMyAdmin and MySQL how can I add 3 new columns: City Name, Latitude, Longitude to a new table with only Street and Number like this example:
TABLE 1 - City
ID StateCODE CityCODE City Name
1 01 23 City1
2 01 09 City2
3 02 12 City3
4 03 10 City4
TABLE 2 - Coordinates
ID StateCODE CityCODE Street Number Latitude Longitude
1 01 23 AAAA AAA 0.0000 0.0000
2 01 09 BBBB BBC 0.0001 0.0001
3 02 12 DDDD DDF 0.0002 0.0002
4 03 10 CCCC CCV 0.0003 0.0003
I need to complete one new table were I already have this data:
TABLE 3 - Address
ID Street Number
1 DFAB AAA
2 AAAA AAA
3 CGFT CGF
And add 3 new columns to complete all information like this:
ID Street Number City Name Latitude Longitude
1 DFAB AAA ???? ???? ????
2 AAAA AAA ???? ???? ????
3 CGFT CGF ???? ???? ????
Can someone help?
Thanks!
First add those columns in the table:
ALTER TABLE Address ADD 'City Name' varchar(100), Latitude float, Longitude float;
Now add the values according to the condition:
UPDATE Address
JOIN Coordinates
ON Address.Street = Coordinates.Street and Address.Number = Coordinates.Number
JOIN City
ON City.CityCODE = Coordinates.CityCODE and City.StateCODE = Coordinates.StateCODE
SET Address.'City Name' = City.'City Name'
Address.Latitude = Coordinates.Latitude
Address.Longitude = Coordinates.Longitude
I haven't ran this code, so there maybe errors. This is just for giving you the idea behind it.
Table A has Registration_id and Client_No.
Table B has Last_Login_Email,Last_Login_Mobile and Last_Login_OFX timestamp of each Registration_id.
My requirement is below :-
Fetch all Registration_id's which are tied to multiple Client_no and out of these skip Registration_id with maximum time-stamp and fetch others.
Example:-
Table A
Registration_id Client_No
AAA 111
BBB 111
CCC 111
DDD 444
EEE 555
FFF 666
GGG 666
So in above table AAA,BBB,CCC are tied to same Client_no which is 111.Also FFF,GGG tied to same CLient_no which is 666.
So these Registration_id (AAA,BBB,CCC) and (FFF,GGG) qualified for first part of my condition.
Now out of 1st set of Registration_id which are tied to same Client_no i want to skip the Registration_id with maximum time-stamp and fetch other.And same for 2nd set and so on.
Example: (for typing convinicence i have used Date instead of timestamp in below example)
Table B
Table B DD/MM/YYYY DD/MM/YYYY DD/MM/YYYY
Registration_id Last_Login_Email Last_Login_Mobile Last_Login_OFX
AAA 01/12/2017 02/12/2017 01/11/2017
BBB 01/01/2018 02/01/2018 03/01/2018
CCC 01/11/2017 02/11/2017 03/11/2017
DDD 01/01/2018 02/01/2018 03/01/2018
EEE 21/01/2018 22/01/2018 23/01/2018
FFF 12/01/2018 13/01/2018 14/01/2018
GGG 29/01/2018 28/01/2018 31/01/2018
Note:- In above table B we dont have to do anything with DDD and EEE as they are not qualified under 1st part of condition.I have given in above table just for sake of completeness.
Lets take 1st set here which is AAA,BBB,CCC
From Table B
Maximum-Timestamp out of Last_Login_Email,Last_Login_Mobile
andLast_Login_OFX
AAA 02/12/2017
BBB 03/01/2018
CCC 03/11/2017
Above we can see maximum timestamp is for BBB(out of AAA,BBB,CCC) so i want to skip BBB here and fetch AAA and CCC.
Same goes to other set which is FFF,GGG
From Table B
Maximum-Timestamp out of Last_Login_Email,Last_Login_Mobile and
Last_Login_OFX
FFF 14/01/2018
GGG 31/01/2018
Above we can see maximum timestamp is for GGG so i need to skip GGG and fetch FFF.
So my overall logic should fetch AAA,CCC and FFF.
Hope i am clear with my requiremet.
This is one way to do that.
As you didn't provide test case, I was - in turn - too lazy to type full names. Though, you explained it quite well, thank you for that.
TA and TB are your A and B tables
REG_ID is your REGISTRATION_ID
QUALI_1 and QUALI_2 represent your two qualification steps
Here we go:
SQL> with quali_1 as
2 (select reg_id, client_no
3 from ta
4 where client_no in (select client_no
5 from ta
6 group by client_no
7 having count(distinct reg_id) > 1)
8 ),
9 quali_2 as
10 (select b.reg_id, q.client_no,
11 greatest(b.ll_email, b.ll_mobile, b.ll_ofx) ll_max
12 from tb b join quali_1 q on b.reg_id = q.reg_id
13 )
14 select reg_id
15 from quali_2
16 where (client_no, ll_max) not in
17 (select client_no, max(ll_max) ll_max
18 from quali_2
19 group by client_no)
20 order by reg_id;
REG
---
aaa
ccc
fff
SQL>
i have problem group query
cmpid = [222,232,343,232,232]
ProductAttribute.select(:txt,:val).where("cmpid IN (?)", cmpid).group(:txt)
Tabel looks
-----------------------
id | txt | val | cmpid
-----------------------
1 aaa ddd 222
2 aaa eee 232
3 aaa ddd 343
4 bbb ded 232
5 vvv ddd 232
But when I group id by txt its not return txt=>"vvv" because it's grouped by "ddd" value - and its correct (sql sense) but how to group items with check whether :txt is also same or different?
My table structure is as follows
id int
name varchar 50
catid int
Sample data
id name catid
---------------------------------------------------------
1 AAA 1
2 BBB 1
3 CCC 1
4 DDD 2
5 EEE 2
6 FFF 1
7 GGG 2
8 HHH 2
9 III 1
I want query such as it get me 1 row from each category for each page in pagination
Now for 1st Page I need data as
id name catid
---------------------------------------------------------
1 AAA 1
4 DDD 2
Now for 2nd Page I need data as
id name catid
---------------------------------------------------------
2 BBB 1
5 EEE 2
Now for 3rd Page I need data as
id name catid
---------------------------------------------------------
3 CCC 1
7 GGG 2
and so on.
How can I achieve this.
SELECT * FROM table WHERE catid = 1 LIMIT :pageno 1
SELECT * FROM table WHERE catid = 2 LIMIT :pageno 1
I think, this will be simplest query to get required result.
This worked for me (MySQL 5.1 on Linux)
SELECT id, name, catid FROM
(SELECT id, name, catid FROM t WHERE catid=1 LIMIT 2,1) AS t1
UNION (SELECT id, name, catid FROM t WHERE catid=2 LIMIT 2,1)
ORDER BY catid
The 2 in the limit is the page number. Hence the output of this one is for the second page.
Yes, as Pradeep said, a LIMIT clause would be the simplest way to do it in MySQL. You'll still need a little bit of coding in PHP to produce clickable page numbers for your users.