I'm trying to bring data from two different columns into one query field. Example: Table1 [Field1] and [Field2]. I don't know if that's possible but in my Query I'm trying to bring the datas from these two Fields and show into one in Query. e.g.
|Table|
|DepartureDate1 | DepartureDate2|
| 15 Nov 2021 | 20 Nov 2021 |
|Query|
|DepartureDate1&2|
15 Nov 2021
20 Nov 2021
Thank you in advance.
Try using a UNION.
SELECT DepartureDate1 AS [DepartureDate] FROM Table1
UNION
SELECT DepartureDate2 AS [DepartureDate] FROM Table1
Related
I have two tables , I want to fetch correct data rows, but when I make a join in between incorrect extra rows are getting create. I want to exclude them.
Table T1
Employee_ID
Work_START
1111
10 Nov
1111
14 Nov
1111
18 Nov
Table T2
Employee_ID
Work_END
1111
12 Nov
1111
15 Nov
1111
20 Nov
I can not use Rank Function since MySQL version is 5.6 and I have Read access to DB so can not create INDEX or use SET function
I tried to make a join with Below SQL Query:
Select T1.Employee_ID, T1.Work_START, T2.Work_END from T1
Left Join T2 On T1.Employee_ID = T2.Employee_ID
where T2.Work_END > T1.Work_START
(used this condition to reduce the incorrect joined rows)
I tried using Left , Right join, using Distinct function as well
I am getting the result as below
Order_ID
Order_Date
Ship_Date
1111
10 Nov
12 Nov
1111
10 Nov
15 Nov
1111
10 Nov
20 Nov
1111
14 Nov
15 Nov
1111
14 Nov
20 Nov
1111
18 Nov
20 Nov
Expected Result is as below
Logic of Output : an employee has worked on a task on 3 different times, so to get those correct 3 rows I want the expected table to follow the below condition
row 1 work_END should be higher than row 1 Work_START and
2nd row Work_START should be higher than 1st row Work_END and so on
next row Work_start should be higher than previos row Work_END
Expected Table
Order_ID
Order_Date
Ship_Date
1111
10 Nov
12 Nov
1111
14 Nov
15 Nov
1111
18 Nov
20 Nov
Please Note: I Have read access to DB and Can not use Rank function since MySQL version is 5.6
Disclaimer : you should consider fixing your data model. Spreading that data over two different tables does not look like appropriate design.
With the current data model, we could approach the question like so: starting from each beginning date in the first table, bring the closest end date with a subquery:
select t1.employee_id, t1.work_start,
(
select min(t2.work_end)
from t2
where t2.employee_id = t1.employee_id and t2.work_end > t1.work_start
) work_end
from t1
This guarantees as many rows in the resultset as there are in the first table (not more, not less). If the dates of the two tables do not properly interleave, you might see results that look inconsistent somehow (using row_number() would not avoid this).
I'm building a php configurator with a series of relationships which I'm controlling with MySQL XREF tables.
There is one XREF table which has multiple dependencies as below:
Table: cto_body_deck_rear_chassis_xref
body_id
deck_type_id
rear_id
chassis_id
22
20
23
13
23
20
18
17
23
20
21
17
23
20
24
17
24
20
18
17
25
21
22
14
Each complete combination is unique although there are similarities between columns; however, I'm getting a duplication problem when selecting from a deck type table, relative to a body id variable passed in the URL.
Table: cto_deck_type
deck_type_id
deck_type_content
20
Single Deck
21
3/4 Length Fixed 2nd Deck
22
Full Length Fixed 2nd Deck
If I use the following MySQL statement:
SELECT d.deck_type_id, d.deck_type_content
FROM cto_deck_type d
LEFT JOIN cto_body_deck_rear_chassis_xref xref
ON xref.deck_type_id = d.deck_type_id
WHERE xref.body_id = 23
I get 3 results, even though each result is identical because the body_id and deck_type_id match 3 times (20).
If the results are identical, I want to group them together or select distinct but I'm not sure what the statement should look like?
Any assistance would be appreciated.
SELECT d.deck_type_id, d.deck_type_content
FROM cto_deck_type d
LEFT JOIN cto_body_deck_rear_chassis_xref xref
ON xref.deck_type_id = d.deck_type_id
WHERE xref.body_id = 23
;; and add the line
GROUP BY d.deck_type_id, d.deck_type_content
Is there a way in SSRS to have an additional row within your row group, to look at a different column group than the rest of the row group
Let's say I have STATES, SALES, MONTH, and BUCKET_MONTH as my dataset fields BUCKET_MONTH is already calculated for me, based off of the MONTH. I want to show something like this:
SAMPLE DATA LIKE THIS FOR FLORIDA (and other months but BUCKET_MONTH only matters for florida let's pretend)
STATE MONTH SALES BUCKET_MONTH
FL JAN 50 FEB
FL FEB 125 FEB
FL MAR 100 MAY
FL APR 0 MAY
FL MAY 100 MAY
SSRS MATRIX MIGHT LOOK LIKE THIS: ?
| 2 groups ?
| MONTH
| BUCKET_MONTH (I can hide this header)
-----------------------------------
1 col group|
STATE | SALES
BUCKET | SALES <-- this row is only visibile for FL which I know how to do
EXPECTED RESULTS WOULD LOOK LIKE THIS
JAN FEB MAR APR MAY JUN JUL
---------------------------------------------------------------------
CA 100 300 150
FL 50 125 100 0 100
FL BUCKET 175 200 <-- BUCKET_MONTH**
MA 0 200 250 50
BUCKET_MONTH in ds shows FEB for the rows with Jan,Feb MONTH, and shows MAY for Mar,Apr, May MONTH
Is there a way to do this in SSRS? Where one of the rows looks at a different column group to establish what column to put the SUM of SALES in?
Much appreciation in advance!
You have to add BUCKET_MONTH as parent column group in your matrix.
Add BUCKET_MONTH in the Column Groups pane, then delete the created row in the matrix selecting Delete groups only option. Now add MONTH as child group in column groups pane.
Add STATE in rows group pane and add a row for bucket total.
Use this expression for BUCKET TOTAL:
=IIF(
Fields!BUCKET_MONTH.Value=Fields!MONTH.Value,
SUM(Fields!SALES.Value,"BUCKET_MONTH"),
Nothing
)
It should produce:
UPDATE: Expression updated taking in account that MONTH and BUCKET_MONTH fields are actually dates.
=IIF(
UCASE(format(Fields!BUCKET_MONTH.Value,"MMMM yy"))=
UCASE(format(Fields!MONTH.Value,"MMMM yy")),
SUM(Fields!SALES.Value,"BUCKET_MONTH"),
Nothing
)
Let me know if this helps.
Let's say I have the table ABC
RENT
- 3 5 6 7 9 10
MONTH
- Jan Mar Jan Jul Dec Feb
How would I go and select the MONTH corresponding to the Minimum Rent?
This is basically performing a MIN operation on the RENT but then I'm completely unaware of how to relate it to the MONTH column and extract the correspondent value.
Can you help?
Supposing you mean a table ABC like this
MONTH RENT
----- ----
Jan 3
Mar 5
Jan 6
Jul 7
Dec 9
Feb 10
Then your SELECT to get minimum rent would be
SELECT MONTH FROM ABC WHERE RENT=(SELECT MIN(RENT) FROM ABC);
When working my MySQL, you need to think about relationships in your database design. Is the month to rent relationship a 1-to-1, 1-to-many, many-to-1, many-to-many?
If you have a relationship that isn't 1-to-1, the best way to implement if is to have 3 tables. A MONTH table, a RENT table, and a table that correlates them.
TABLE_NAME column1 column2
MONTH pkid month
RENT pkid amount
MONTHLY RENT pkid fk_month fk_rent
From here, you can just do a join on the three tables using the correct columns to get the answer you want.
what I'm trying to do:
from these tables
---------------------------------
name date state
---------------------------------
ali jan 12 started
ali jan 12 drop out
masa jan 12 registered
masa jan 12 started
sami jan 12 started
I want the results to be
---------------------------------
name date state
---------------------------------
masa jan 12 started
sami jan 12 started
So basically what i want is to have all the started users without the ones who dropped out
so the filtering should be based on the state
thanks
Those two rows in your result example are not the only started people on that date.
SELECT * FROM table WHERE state = 'started';
Would return 3 rows:
---------------------------------
name date state
---------------------------------
ali jan 12 started
masa jan 12 started
sami jan 12 started
To get the two rows in your example you need:
SELECT * FROM table WHERE name IN ('sami', 'mesa');
Update, added this example
You could limit if you only wanted two rows:
SELECT * FROM table WHERE state = 'started' LIMIT 2;
Perhaps something along the following lines is what you need...
SELECT NAME,
DATE,
STATE
FROM MYTAB
WHERE STATE = 'Started' AND
NOT EXISTS (SELECT *
FROM MYTAB MYTAB2
WHERE MYTAB2.NAME = MYTAB.NAME AND
MYTAB2.STATE = 'Drop Out');
That should get everyone who started and didn't drop out.
SELECT name, date, state FROM table GROUP BY name HAVING state = 'started';
I believe this would eliminate people who dropped out eventually because the GROUP BY would put the people with the same name into the same group, and then eliminate them once they drop out with the HAVING statement.
select * from table where state != 'dropped out'