How to use a pivot in access - sql

use three tables (TB_HOPE_INDUSTRY, TB_M_INDUSTRY, TB_PROFILE),
I want to get a red outline table.
How can I make a sql query?
Please help me.

You could create a query that uses all 3 tables, and
Join TB_PROFILE and TB_HOPE_INDUSTRY on both PROFILE_ID fields.
Join TB_M_INDUSTRY and TB_HOPE_INDUSTRY on both INDUSTRY_CD fields
SELECT the columns PROFILE_ID and NAME from table TB_PROFILE
Add the column INDUSTRY_NO from table TB_HOPE_INDUSTRY
Add the column INDUSTRY_NAME from table TB_M_INDUSTRY
Click CROSSTAB on the ribbon
Then, define the following settings fot the Function and Crosstab properties in the design grid:
group by PROFILE_ID and INDUSTRY_NO
select the First function for the columns NAME and INDUSTRY_NAME
change the name of the NAME column to something like PROFILE
Display PROFILE as row header, INDUSTRY_NO as column header, (first of) INDUSTRY_NAME as value.
Do not display PROFILE_ID (just use it for grouping)
The resulting SQL query should be something like this:
TRANSFORM First(TB_M_INDUSTRY.INDUSTRY_NAME) AS FirstOfINDUSTRY_NAME
SELECT First(TB_PROFILE.NAME) AS PROFILE
FROM TB_PROFILE INNER JOIN
(TB_M_INDUSTRY INNER JOIN TB_HOPE_INDUSTRY ON TB_M_INDUSTRY.INDUSTRY_CD = TB_HOPE_INDUSTRY.INDUSTRY_CD)
ON TB_PROFILE.PROFILE_ID = TB_HOPE_INDUSTRY.PROFILE_ID
GROUP BY TB_PROFILE.NAME
PIVOT TB_HOPE_INDUSTRY.INDUSTRY_NO;

Related

How to query 2 different schema tables with different column name same information in postgresql

When I query I do not get match list side by side and database reference fails, why?
I tried using one connection and two different schemas, first schema name table has same reference details with second table column, and I would like to compare side by side what equals exactly from rw.reference table 1, with table 2 qt.ref_number because they should match between both. I only get output data from top first query with rd.reference and second table does not list anything with, qt.ref_number, why postgresql does not cross database reference?
Query:
Select distinct
rd.reference,
region1_scorecard
From local_dev.user.rawdata rd
Inner join customerid as id
on rd.site=id.site
union all
Select distinct
qt.ref_number,
region2_decision
From local_dev.account.quote qt
Inner join rep_id id
on qt.application=id.application
Order by rd.reference

SQL select with three tables

Hi guys I'm new with databases and I'm trying to make a query where I join 3 tables. I could make it and I want to clean up the result. I want to know how can I delete the column "pin" from users table and maybe some "ids" columns.
Select * from "wish-list"
Join products
On "wish-list".id = products.holiday_id
Join users
On "wish-list".user_id = users.id
Where "wish-list".id = 1
You need to specify which columns you really need in your output. At the moment you are using
SELECT * which outputs all columns of all joined tables.
Here is what it should look like:
SELECT holiday, products.description, users.pin FROM "wish-list"
JOIN products ON "wish-list".id = products.holiday_id
JOIN users ON "wish-list".user_id = users.id
WHERE "wish-list".id = 1
It's important that you reference all columns which are not your main entity (here wish-list) with tablename.column (products.description and not only description). It will work without referencing strictly but only if the column name is unique in your query.
Furthermore you can rename columns. This is useful for example if you want to get the id's of the product table and the wish-list table.
SELECT product.id AS product_id, id AS wishlist_id FROM "wish-list"
...
Hope that helps!

JOIN of 4 tables, how to restrict SELECT columns to one table only?

I am working on ABAP program - user input is to query column ANLAGE and output is to get all records from table EADZ (and only fields of EADZ) based on ANLAGE.
Statement and joins should work like this:
Input ANLAGE, find in table EASTL, gets LOGIKNR
Input LOGIKNR, find in table EGERR, gets EQUNR
Input EQUNR, find in table ETDZ, gets LOGIKZW
Input LOGIKZW, find in table EADZ, gets all records (this is the final output)
Here is the code I tried:
DATA: gt_cas_rezy TYPE STANDARD TABLE OF eadz,
lv_dummy_eanl LIKE eanl-anlage.
SELECT-OPTIONS: so_anl FOR lv_dummy_eanl NO INTERVALS NO-EXTENSION.
SELECT * FROM eadz
INNER JOIN etdz ON eadz~logikzw EQ etdz~logikzw
INNER JOIN egerr ON etdz~equnr EQ egerr~equnr
INNER JOIN eastl ON egerr~logiknr EQ eastl~logiknr
INTO CORRESPONDING FIELDS OF TABLE #gt_cas_rezy
WHERE eastl~anlage IN #so_anl.
I got the records from table EADZ except that the date fields are empty (even though, they are filled in database table). I am assuming there is a problem with JOINs since in statement like this I join all the fields of all 4 tables into one "record" and then to corresponding fields of internal table.
How to get the values of date fields?
You can find the answer in the documentation.
If a column name appears multiple times and no alternative column name was granted, the last column listed is assigned.
In your case, at least two tables share the same column name. Therefore the values from the last mentioned table are used in the join.
You can solve this by listing the columns explicitly (or eadz~* in your case), giving an alias if required.
SELECT EADZ~* FROM EADZ INNER JOIN ETDZ ON EADZ~LOGIKZW = ETDZ~LOGIKZW
INNER JOIN EGERR ON ETDZ~EQUNR = EGERR~EQUNR
INNER JOIN EASTL ON EGERR~LOGIKNR = EASTL~LOGIKNR
INTO CORRESPONDING FIELDS OF TABLE #gt_cas_rezy
WHERE EASTL~ANLAGE IN #SO_ANL.
If you require additional fields, you can add them explicily with e.g. EADZ~*, EASTL~A.

Additional Columns with Other non Criteria

Sorry the title isn't clear
I have a script (using Pl/Sql Oracle), i have created a report that will optout a list of cities chosen by a user. I have a column that will list that city but i wanted to include an additional column that list other cities associated with the user (the cities list should not include his/her pick on that column).
I am not exactly sure who to do that so that the additional column will not list the picked city or cities. Is there a function i can use?
I am also doing it on Crystal reporting 10 (if it possible there)
Iex: This is just an idea of what i am trying to do.
##Table Name: Giving Cities##
##Andrew - Peru##
##Andrew - Venezuela##
##Andrew - France##
##​Paul - USA##
Pick cities where user = Andrew and City = Peru
Output
User, City, Other Given Cities
In SQL adding extra additional columns:
select user,cities,'' as additional_columns from yourtable
I think this is what you are looking for: SQL Query to concatenate column values from multiple rows in Oracle
It looks like exactly what you are trying to do with concatenating the results of multiple rows together.
As for not selecting the "selected" city for the additional column, you would want to use a subselect. A subselect allows you to apply a different where clause to the additional column.
select t.user, t.city, t2.concatenated_cities
from table t
inner join
(
select distinct <see above link for how to concatenate rows here> as concatenated_cities
from table sub_t
where sub_t.city <> 'CITY'
) t2
on t.user = t2.user
where t.city = 'CITY'

New report from two tables with reportviewer doesnt have true result

I create a new view in SQL Server 2008 from two tables that have relation on a field. I want to create a report and do grouping on that common field.
For example:
table1: student(ID,first-name,last-name,phone,address,...)
table2: courses(ID,fk_ID,Course,....)
Now I want to have report that shows all data from both tables with grouping on ID from student table, that must show courses information separated for every student.
my query is:
SELECT TOP (100) PERCENT
dbo.tbl_student.ID,
dbo.tbl_student.firstname, dbo.tbl_student.lastname,
dbo.tbl_courses.Coursename,
dbo.tbl_Courses.CourseDate, dbo.tbl_courses.coursetype,
FROM
dbo.tbl_student LEFT OUTER JOIN
dbo.tbl_courses ON dbo.tbl_student.ID = dbo.tbl_courses.fk_id
ORDER BY
dbo.tbl_student.firstname DESC
But when I create a new report from this view, it shows just one record for every group. I spent 2 hours to solve the problem but I did not succeed.
please help me to create report from two or more tables.
Now it shows one record duplicates for several times for every group
Have you tried a query like this:
SELECT s.[ID], s.[first-name], s.[last-name], s.[phone], ...
c.[ID], c.[Course], ...
FROM student s
LEFT OUTER JOIN
courses c ON s.[ID] = c.[fk_ID]