I created a report with multiple fields and I have a field named Ageddays that calculated the aging days on each row of data. I need to group by Ageddays <31, <61, <91, and <121. The report should look like this below and any helps is appreciated.
Header ClientName Item Purdate Ageddays Location Salesperson Paid
Group by**Ageddays < 31**
Detail XYX LLC toy1 12/21/2017 10 ABC address Sam No
AAA LLC toy2 11/11/2017 20 ABC address Sam No
**Ageddays < 61**
CDF LLC toy3 08/21/2017 40 EEB address Rey No
AAA co. toy4 07/11/2017 50 YYY address Sam No
**Ageddays < 91**
MMY Co. toy3 06/21/2017 60 EEB address Eri No
GGG Co. toy4 05/11/2017 70 YYY address Abe No
Create an additional column based on the condition you want. I mean the new column value should be
Case when agedays<31 then 1
When agedays<61 then 2
When agedays<91 then 3
When agedays<121 then 4
Else 4 end as groupcategory.
Then you can group the data using this groupcategory field.
Related
I have two tables A and B for joining, with a common column at both tables. But the ID in both the tables are not unique, so it may have multiple rows in each table with same id.
Table A:
OFF_ID
OFF_NAME
YEAR
1
abc_tech
4yrs
2
def_tech
6yrs
3
ghi_tech
2yrs
4
xyz_tech
10yrs
5
lmn_tech
14yrs
Table B:
ID
NAME
DOB
CITY
1
George
2001
chennai
1
paul
2005
bangalore
1
hem
1997
mumbai
2
vasanth
1997
kerala
2
vicky
1997
gujarat
3
narayan
1997
kerala
I require an output in which the multiple row ID should display once and remain empty for other rows.
output required:
OFFICE_ID
OFFICE_NAME
YEAR
ID
NAME
DOB
CITY
1
abc_tech
4yrs
1
george
2001
chennai
-
-
-
1
paul
2005
bangalore
-
-
-
1
hem
1997
mumbai
2
def_tech
6yrs
2
vasanth
1997
kerala
-
-
-
2
vicky
1997
gujarat
3
ghi_tech
2yrs
3
narayan
1997
kerala
4
xyz_tech
10yrs
-
-
-
-
5
lmn_tech
14yrs
-
-
-
-
it will be helpful for me if i get query as mysql query.
Thanks in advance.**
If I get you right, the output you want as described in your last code block can hardly be achieved via mysql. Where do you want to output it? When you loop over your output, you can take care of it at that point.
SELECT * FROM table1 LEFT JOIN table2 on table1.id = table2.id
how to represent the below Excel table as a Mysql DB structure format where in a column has multiple sub columns as given below
Name Mon-Fri Sat Sun
Asia UK USA Asia UK USA Asia UK USA
Name1 0.25 0.25 0.5 0.5 - - - 0.5 -
Name2 0.5 0.5 1 1 - - - - -
Name3 0.25 0.25 0.5 0.5 - - - 0.5 -
You usually do this by adding a Time column to the data and "normalize" it:
Name Region Time Amount
------|-------|--------|-------
Name1 Asia Mon-Fri 0.25
Name1 Asia Sat 0.5
Name1 Asia Sun -
Name2 UK Mon-Fri 0.25
Name2 UK Sat -
...
See https://en.wikipedia.org/wiki/Database_normalization as well
For this first create a table of Days
tbl_days
id,
dayname
Then Create a group with these days. Group may be with one day or multiple days
tbl_group
id,
groupname,
days (make it comma separted with day id)
Then create a country master
tbl_countries
id,
country_name
Then assign the calculation like below
tbl_calculation
id,
group_id,
country_id,
value (nullable)
You can go with following Table Structure
tbl_Region -
ID , Name
1,UK
2,Asia
tbl_Date_allocation -
ID,WEEK_DAY_NAME
1, Mon
2 , Wed
3 ,...
tbl_types
Id, Name
1, Name 1
2, Name 2
......
tbl_allocation
id,region_id,date_allocation_id,type_id,value
1,1,2,1,0.25
id - auto increment
region_id,date_allocation_id,type_id - foreign keys to other table
What you have are two entities: something-that-has-a-name, let's call them People, and Geographic Area -- three of which are the values Asia, UK and USA. Then you have represented relationships between People and GAs. As seen from your data, a Person may relate to 0 or more GAs and a GA may relate to 0 or more people. Such a many to many relationship is maintained by an intersection or cross table (two names for the same thing). You further show two aspects of this relationship: a term ("Mon-Fri", "Sat" and "Sun") and an Amount.
The fact that Term values are days of the week are not really significant. There are three possible values so let's call them 1, 2 and 3. Amount looks like it could be any floating point value.
create table PersonGO(
PersonID int not null references People( ID ),
GAID int not null references Areas( ID ),
Term int not null check( Term in (1, 2, 3 )),
Amount float not null,
constraint PK_PersonGO primary key( PersonID, GRID )
);
The data stored in such table would look like this:
PersonID GAID Term Amount
1 1 1 0.25 -- Name1 Asia Mon-Fri
1 1 2 0.50 -- Name1 Asia Sat
1 2 1 0.25 -- Name1 UK Mon-Fri
1 2 3 0.50 -- Name1 UK Sun
1 3 1 0.50 -- Name1 USA Mon-Fri
2 1 1 0.50 -- Name2 Asia Mon-Fri
2 1 2 1.00 -- Name2 Asia Sat
2 2 1 0.50 -- Name2 UK Mon-Fri
2 3 1 1.00 -- Name2 USA Mon-Fri
3 1 1 0.25 -- Name3 Asia Mon-Fri
3 1 2 0.50 -- Name3 Asia Sat
3 2 1 0.25 -- Name3 UK Mon-Fri
3 2 3 0.50 -- Name3 UK Sun
3 3 1 0.50 -- Name3 USA Mon-Fri
Where there is no relationship for a particular person to a particular area on a particular term, such as Name1 with UK on Saturday, there is no entry. You would not have an entry with 0 or NULL in the Amount field. That would be unnecessary.
As your sample data looks like one week's worth of data and there would probably be one set of these entries for each week of a year, the table might have another field, WeekNum, with possible values such as 201601 for the first week of 2016, 201602 for the second week and so forth. You have a good deal of flexibility here. You could just as well merge this with the Term field: 2016011 for Mon-Fri of the first week, 2016012 for Sunday of the first week and so on. You could come up with something entirely different. Whatever makes the most sense to you.
So I am using Visual Studios 2015 and MySQL 5.7.
First, I have a table called "items" which contain the following columns and rows:
BranchNo itemNo itemName Qty Pkey
1 1 Chicken 99 11
1 2 Coke 99 12
1 3 Applie Pie 99 13
Then I have another table called "setmeal" which uses the Pkey (itemNo in "setmeal" table) of table "items":
setmealno branchno itemNo Name Price SetMealID entryNo
1 1 11 1pc Fried Chicken 69 11 1
1 1 12 1pc Fried Chicken 69 11 2
2 1 13 Apple Pie Single 50 12 3
3 1 12 Coke Drink 20 13 4
Basically what table "setmeal" does is, is if there are 2 itemNo's with same "SetMealID", then they belong to the same set.
Then I have a "cart" which consists of (assuming I ordered):
TransactionNo UserNo SetMealNo Name Price Date BranchNo
1 1 11 1pc Fried Chicken 69 2015 1
So what I want to do in Visual Studios (2015) is whatever you put in the "cart", there's a code that's automatically going to deduct 1 in "Qty" in table "items" if the SetmealNo is the same with table "setmeal".
I need a code in visual studios to successfully deduct 1 from Qty whenever the conditions above are met.
Although, I think joining the 3 tables is the first step. (Although I also have no idea how to do it in MySQL Select Statements)
EDIT: I found how to merge the 3 tables
SELECT s.setmealid, t.setmealNo, i.pKey, s.itemNo, i.qty
FROM items i, setmeal s, cart t
WHERE s.itemNo = i.pKey and t.setmealno != 0
AND s.setmealID = t.setmealNo
AND t.transactionNo = '" & Form2.TextBoxTransNo.Text & "';
It Produces this table
setmealid setmealNo pKey itemNo qty
11 11 11 11 99
11 11 12 12 99
All that's left is a Visual Studios code to deduct from "qty" although I do not know how to do it.
I have table with ID DOB AMOUNT RECEIVER_NAME SENDER_NAME SETTLE_FEE columns.
Sample Data
ID DOB AMOUNT RECEIVER_NAME SENDER_NAME SETTLE_FEE
-------------------------------------------------------------------
1 10-06-1990 100 Jose Benn 12
2 12-06-1990 200 Jim Mike 12
3 10-06-1990 300 Kate Benn 12
4 12-06-1990 100 Amy Mike 12
5 10-06-1990 200 Alison Benn 12
6 12-06-1990 300 Mary Mike 12
Expected result
ID DOB SENDER_NAME
---------------------------
1 10-06-1990 Benn
|--------AMOUNT RECEIVER_NAME SETTLE_FEE
100 Jose 12
300 Kate 12
200 Alison 12
2 12-06-1990 Mike
|--------AMOUNT RECEIVER_NAME SETTLE_FEE
200 Jim 12
100 Amy 12
300 Mary 12
I need to get all the data of each sender name.
I tried using group_concat(), But, it can take only 1024 characters.
So, what is the efficient way to achieve this scenario.
I can't use PL/SQL and no session related values allowed.
I need all the receiver's name, amount, fee for all the sender_name. What is the efficient way to do it.
Thanks,
Jose
There is a solution:
Just use an ORDER BY:
SELECT ID, DOB,SENDER, AMOUNT, RECEIVER_NAME, SETTLE_FEE
FROM inputTable
ORDER BY SENDER,DOB;
In displaying as you want the data, you just use code like (dummy code, javscriptish):
var previousSender="";
while(var row=db.fetch()) {
if(row.sender!=previousSender) {
console.log(id+" "+row.dob+" "+row.sender);
previousSend=row.sender();
} else {
console.log(" |-------"+row.amount+" "+row.receiver_name+" "+row.settle_fee);
}
}
As you can see: id is random since it has no relation to the dob or sender in your model. It is just a row counter.
If I have an output dataset from a CTE that looks like
PERIOD FT GROUP DEPT VALUE
1 Actual KINDER MATH 200
2 Actual KINDER MATH 363
3 Actual KINDER MATH 366
1 Budget KINDER MATH 457
2 Budget KINDER MATH 60
3 Budget KINDER MATH 158
1 Actual HIGHSCH ENGLISH 456
2 Actual HIGHSCH ENGLISH 745
3 Actual HIGHSCH ENGLISH 125
1 Budget HIGHSCH ENGLISH 364
2 Budget HIGHSCH ENGLISH 158
3 Budget HIGHSCH ENGLISH 200
6 Budget HIGHSCH ENGLISH 502
7 Budget HIGHSCH ENGLISH 650
1 Actual COLL ENGLISH 700
2 Actual COLL ENGLISH 540
3 Actual COLL ENGLISH 160
1 Budget COLL ENGLISH 820
2 Budget COLL ENGLISH 630
3 Budget COLL ENGLISH 800
but I want to add a column that will have an identifier for each group (the grouping being by FT, Group and Dept) like this:
PERIOD FT GROUP DEPT VALUE GroupID
1 Actual KINDER MATH 200 1
2 Actual KINDER MATH 363 1
3 Actual KINDER MATH 366 1
1 Budget KINDER MATH 457 2
2 Budget KINDER MATH 60 2
3 Budget KINDER MATH 158 2
1 Actual HIGHSCH ENGLISH 456 3
2 Actual HIGHSCH ENGLISH 745 3
3 Actual HIGHSCH ENGLISH 125 3
1 Budget HIGHSCH ENGLISH 364 4
2 Budget HIGHSCH ENGLISH 158 4
3 Budget HIGHSCH ENGLISH 200 4
1 Budget HIGHSCH ENGLISH 502 5
2 Budget HIGHSCH ENGLISH 650 5
3 Budget HIGHSCH ENGLISH 336 5
1 Actual COLL ENGLISH 700 6
2 Actual COLL ENGLISH 540 6
3 Actual COLL ENGLISH 160 6
1 Budget COLL ENGLISH 820 7
2 Budget COLL ENGLISH 630 7
3 Budget COLL ENGLISH 800 7
Please do you know how to go about it?
EDIT:
I feel like something in this direction may be useful
SELECT *,
CASE WHEN FT = 'Actual' THEN <something_incremental_to_do_with_row_num> OVER (PARTITION DEPT, GROUP, FT) END as GROUPID
FROM cte
I can't use ORDER BY in the OVER clause because I am on 2008
It's hard to say without seeing the SQL for the query, but I would think a variant on 'row_number() (partition by [some fields])' would give you this. Have you looked into that?
This existing question might give you what you need:
How to add sequence number for groups in a SQL query without temp tables
Chen, I believe your answer is already available in another thread:
How to return a incremental group number per group in SQL
Additionally, you could just create a unique_id for each group by concatenating fields. For example, if you wanted to group by FT, GROUP, and DEPT, then just put them all together. Ex:
SELECT *,
CAST(FT AS VARCHAR) +
CAST([GROUP] AS VARCHAR) +
CAST(DEPT AS VARCHAR) AS UNIQUE_ID
FROM MYTABLE
If you wanted to use it for a while to do a more complex query, then just throw it into a temp table:
SELECT *,
CAST(FT AS VARCHAR) +
CAST([GROUP] AS VARCHAR) +
CAST(DEPT AS VARCHAR) AS UNIQUE_ID
INTO #MYTEMPTABLE
FROM MYTABLE
You only need the CAST function if there are different data types in the fields you want to join. Best to keep it in to avoid any unnecessary headaches. Hope this helps.