SQL query to populate relational combo php - mysql

Query: Network
I am having 2 Combobox ( Country and Network), i need to populate Network combo on the basis of item selected in Country combo.
Table1: Network
Platform Network Location_ID
Platform 1 Operator 1 56
Platform 1 Operator 2 59
Table2 : Location
Location_ID City Country
56 Hong Kong Hong Kong
109 Tafuna American Samoa
59 Delhi India
Help me to write sql query for same please.
Thanks in advance.

Bind your country dropdown to Location_ID and Country of Location table as value and text respectively.
You can use the following query for binding Location -
select Location_ID, Country from Location
And then for binding your Network combo box, use the following query :
select Network, Platform
from Network
where Location_ID = #Location_ID
#Location_ID is the selected value in your country dropdown list.
Note : Populate your network combo in the change event of country combo box.

Related

SQL: how to select where one column does not match another column for ALL records within a given group

I have a table named sales in a MySQL database that looks like this:
company manufactured shipped
Mercedes Germany United States
Mercedes Germany Germany
Mercedes Germany United States
Toyota Japan Canada
Toyota Japan England
Audi Germany United States
Audi Germany France
Audi Germany Canada
Tesla United States Mexico
Tesla United States Canada
Tesla United States United States
Here is a Fiddle: http://www.sqlfiddle.com/#!17/145ff/3
I would like to return the list of companies that ship ALL of their products internationally (that is, where the value in the manufactured column differs from the value in the shipped column for ALL records of a particular company).
Using the example above, the desired result set would be:
company
Toyota
Audi
Here is my (hackish) attempt:
WITH temp_table AS (
SELECT
s.company AS company
, SUM(CASE
WHEN s.manufactured != s.shipped THEN 1
ELSE 0
END
) AS count_international
, COUNT(s.company) AS total_within_company
FROM
sales s
GROUP BY
s.company
)
SELECT
company
FROM
temp_table
WHERE count_international = total_within_company
Essentially, I count the instances where the columns do not match. Then I check whether the sum of those mismatched instances matches the number of records within a given group.
This approach works, but it's far from an elegant solution!
Can anyone offer advice as to a more idiomatic way to implement this query?
Thanks!
We can GROUP BY company and use a HAVING clause to say all countries in shipped must differ to the country in manufactured:
SELECT company
FROM sales
GROUP BY company
HAVING COUNT(CASE WHEN manufactured = shipped THEN 1 END) = 0;
Try out here: db<>fiddle
The fiddle linked in the question is a Postgres DB, but MySQL is taged as DBMS.
In a MySQL DB, the above query can be simplified to:
SELECT company
FROM sales
GROUP BY company
HAVING SUM(manufactured = shipped) = 0;
In a Postgres DB, this is not possible.
You have to think in sets... you want to display all without a match -- find the matches display the rest
SELECT DISTINCT company
FROM sales
WHERE company NOT IN (
SELECT company
FROM sales
WHERE manufactured = shipped
)

Data warehouse logical model

I have drawn a logical model for a hotel room booking OLTP system Logical model
I'm facing a problem querying it.
the question is: For each Country and quarter, produce the cumulative income of 3-star-rooms
Noted that RoomBand contain how many starts for each room
I have tried this
SELECT Quarter, Country
FROM DT_Date, Country, RoomBand
where RoomBand = 3
the output was this
CountyName Quarter
USA 2
Uk 1
USA 2
UK 1
as it has shown it repeated USA and UK
why is that happening
Thanks for the support all
Try the DISTINCT operator with your query. For example: SELECT DISTINCT column1, column2, .... You can read more about it here.

How to avoid case sensitivity in group by using apache drill

Drill Environment:-
OS:- Window 10,
Version:- 1.9,
Mode:- embedded mode,
I have a column name 'State' in db, which have data like(e.g:- Florida,
florida,texas, etc).
My problem is while using SUM(Price) and group by in query,
Florida and florida are showing two seperate rows.
So how to avoid case sensitivity so that both Florida and florida act as 1
rows while firing count(Sate).?
EXAMPLE:->
This is my input table in db:-
State Price
Alaska 75
Texas 80
Alaska 90
Florida 100
florida 70
Sql Server Query:- select State, Sum(Price) from testTable group by State.
Sql Server Output:-
State Price
Alaska 165
Texas 80
Florida 170
Drill Query:- select T1.State, Sum(T1.Price) from . T1 group by T1.State.
Drill Output:-
State Price
Alaska 165
Texas 80
Florida 100
florida 70
I want same output as shown in Sql Server Output. Please help.
Drill provides a lowercase string function. You can do a subquery to first convert all your states to lower case and then do the group by.
select lstate, sum(lprice) from (select lower(T1.State) as lstate, T1.Price as lprice from . T1) group by lstate

MySQL table JOIN with LIKE without adding another ID column

I have two tables named "regions" and "city_list":
REGIONS TABLE:
r_id r_code r_name
1 REGION I Ilocos Region
2 REGION II Cagayan Valley
3 REGION III Central Luzon
4 REGION IV-A CALABARZON
5 REGION IV-B MIMAROPA
CITY_LIST TABLE:
city_id city_name city_region
32 Catbalogan REGION VIII (Eastern Visayas)
33 Cauayan REGION II (Cagayan Valley)
34 Cavite City REGION IV-A (CALABARZON)
35 Cebu City REGION VII (Central Visayas)
87 City of Naga REGION VII (Central Visayas)
I want to reference the regions table when a user selects a city name from the city_list table. For example:
User selects "Cauauyan" from a drop-down list, the server returns the region code which is the "r_code" column from the Regions table.
Can I use a JOIN statement with LIKE without adding a column for a region ID in the city_list table? Is it possible to get the value in city_region like "REGION II" without the "(Cagayan Valley)" and use that value to get r_id in the REGIONS table?
SELECT r.r_id
FROM REGIONS r
WHERE r.`r_code` IN(
SELECT LEFT(`CITY_LIST`.`city_region`,INSTR(`CITY_LIST`.`city_region`,"(")-1) AS Region_id FROM `CITY_LIST`);

Add the columns automatically in SSRS report

Currently we are having 6 columns in our database table which we are showing in the SSRS report but in future if we add 1 more column then without any manual changes on RDL it will included in the report.
Current report Example :-
Name Address Code City County Country
xyz Lane 1 466001 Bang dbc Africa
abc Lane 2 466002 Bpl bbn Nepal
dcb Lane 3 466003 sbc wad Bhutan
Expected report without adding the column manually in SSRS.
Name Address Code City County Country DOB
xyz Lane 1 466001 Bang dbc Africa 19/06/1986
abc Lane 2 466002 Bpl bbn Nepal 20/06/1990
dcb Lane 3 466003 sbc wad Bhutan 21/8/2000
Thanks for any help.
Please follow below steps..
Step 1. Create Proc using UNPIVOT and Property(ColunName) & Value with ID column (PKey) like
SELECT Pkey,tblPivot.Property, tblPivot.Value
FROM (SELECT EmpNo AS Pkey, CONVERT(sql_variant,EmpNo) AS EmpNo, CONVERT(sql_variant,EName) AS EName, CONVERT(sql_variant,JOB) AS JOB,
CONVERT(sql_variant,Sal) AS Sal FROM EMP) EMP
UNPIVOT (Value For Property In (EmpNo,EName, JOB, Sal)) as tblPivot
Step 2.
Create a Matrix Report using above SP with row-grouping on [Pkey] and col-grouping on [Property] and Display value ...
Step 3 Now you can add/remove column in SP (step 1) based on your requirement