HI Guys i have a question...
I have a table
Name Country Date
abc Singapore 1/1/1111
cde Korea 1/1/1111
fgh Korea 1/1/1111
ijk Singapore 1/1/1111
mno Singapore 1/1/1111
pqr Singapore 2/1/1111
stuv Korea 2/1/1111
wxy Korea 2/1/1111
z Korea 2/1/1111
I want it to come out like this
Country Date(1/1/1111 Date2 (2/1/1111
Singapore 3 1
Korea 2 3
Note. Date and Date 2 either one or both can be temporary columns
I tried
SELECT Country, Count(Country) AS Quantity , 'Date1' As Expr1
FROM Reg
WHERE Date= #6/16/2014#
GROUP BY Country;
But as for the part i tried casing But it dosnt seems to work...
SELECT Reg.Date, Reg.Country, Reg.Date1
CASE Date
WHEN (((Reg.Date)=#6/16/2014#)) THEN Reg.Date1 = #6/16/2014#
END
Please help !!!
MS ACCESS
I noticed you tagged your post with both "MySQL" and "SQL-Server", but then referenced MS Access in the text. Which database system are you, in fact, using? How you implement something like this very much depends on the DB system, as there is no common support for it in the SQL standard. That said:
If you are using MS Access, then you want a "crosstab" query (just Google it; plenty of how-to articles already out there)
If you are using SQL Server, then you want a "pivot" query (again, just Google)
If you are using MySQL, then AFAIK you have to roll your own using an approach something like the one outlined here: http://stratosprovatopoulos.com/web-development/mysql/pivot-a-table-in-mysql/
Good luck!
Related
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
)
I've been trying to find an Access version of the subject and testing many concepts. All I want to do is what I thought was simple. Let's say I have a single column in a table. Values are like
California
Florida
California
California
New York
California
New York
let's leave it that simple. All I'm trying to do which seems easy in SQL Server or Oracle is show the distinct values and how many times they are referenced in the table. So output would be:
California 4
Florida 1
New York 2
On Access 2019 if it matters.
Standard SQL; would be the same in SQL-Server, Oracle, MsAccess (and almost all other SQL dialects); just use:
select State, count(*)
from YourTbl
group by State
Here's my situation : I have a table that has large amounts of records, I need to pull out a number of these records for each name in the database, note that TOP will not work for my use case. My end user wants the report formatted in such a way that each user shows up only once, and up to 3 different dates are shown for the user.
Table format
AutoID
Enum
TNum
Date
Comments
1
25
18
2/2/22
2
25
18
1/2/21
Blah
3
18
18
1/2/21
4
18
18
1/2/20
5
25
17
1/2/22
6
25
17
1/2/20
Now the Enum and TNum fields are fk with other tables, I have created a join that pulls the correct information from the other tables. In the end my query provides this output
RecordID
Training
CompletedDate
FirstName
LastName
Location
2821
MaP
1/1/21
David
Simpson
123 Sesame St.
2822
1/2/22
Fuller
MaP
Dough
GHI
David
123 Sesame St.
2825
1/1/20
Simpson
The two "Blank fields" represent information that is pulled and may or may not be needed in some future report.
So to my question : How do I manage to get a report, with this query's pull to look like this:
Place
LastName
FirstName
Training
FirstCuttoff
Secondcutoff
ThirdCutoff
Comments
123 Sesame St.
David
Simpson
MaP
1/1/20
1/1/21
123 Sesame St.
John
Dough
MaP
1/1/22
I was originally planning on joining my query to itself using where clauses. But when I tried that it just added two extra columns of the same date. In addition it is possible that each record is not identical; locations may be different but since the report needs the most recent location and the name of the trainee. In addition, to add more complexity, there are a number of people in the company with effectively the same name as far as the database is concerned, so rejoining on the name is out. I did pull the Enum in my query, I can join on that if needed.
Is there an easier way to do this, or do I need to sort out a multiple self-joining query?
I have a project I am working on where I am going to have to do this. Some of the suggestions I received were to use a Pivot query. It wouldn't work in my case but it might for yours. Here is a good example
Pivot Columns
id_no doc_id item_no product customer
123 2 1 A Daisy
123 2 9 A Ben
123 4 3 A Daisy
123 4 4 A Ben
123 6 11 B Daisy
123 6 13 B Ben
when I put it in my report it results to
Daisy Daisy
Ben
And it is also the result in mysql
select distinct customer from receipt where id_no like '123'
result:
Daisy
Daisy
Ben
Another query that I tried:
select distinct id_no, customer, product from receipt where id_no like '123'
result:
123 Daisy A
123 Daisy B
123 Daisy A
123 Ben A
123 Ben B
desired result:
Daisy
Ben
Please help me please.
Thank you guys for the help I found out why the other one keeps on showing. It is because the other Daisy is spelled as Daissy that's why.
Most likely your Customer name contains additional characters between the two records. Depending on how the datatype is implemented, spaces could matter and have contributed to the difference.
Try concatenating a character before and after customer.
I am unfamiliar with the concepts in Crystal Reports, but from what I understand, you would have to create a formula like so:
"XXX" & {Receipt.Customer} & "XXX"
If you run it again, you might recognize there is additional space like so:
XXXDaisyXXX
XXXDaisy XXX
^____ Additional Space
There is no chance of error while you using distinct ..it should return distinct value ...any way you can try another way
SELECT customer FROM receipt WHERE id_no like '123' GROUP BY customer
I don't see why you are fetching three records. I tried implementing your database and ran your query. It returned the result as expected.
See the above pic. There may be some issue with the data type you used. You may try grouping via customer, but I don't think it should affect your result anyway.
Also Check if the data types match.
The selection you made from customer id and id_no is unique and with distinct it should return only two rows
plase try this code
i get solution
select distinct `customer` from receipt where `id_no`='123'
this is right
i tryied this is my past project
best of luck
This question already has an answer here:
How to output table results by using cfoutput group by date?
(1 answer)
Closed 7 years ago.
I'm working on outputting values from data base in the table. My table has 5 columns: Date, FirstName, LastName, City, State. Here is example of my data base table:
DateMeeting FirstName LastName City State
2015-12-11 Mike Johns Dallas TX
2015-12-11 John Cook Dallas TX
2015-12-11 Nick Roberts Dallas TX
2015-12-11 Oliver Ryan New York NY
2015-12-11 Michael Best New York NY
2015-12-11 David Holmes New York NY
So I want to have output table that will display just one date for multiple records. I tried to use DISTINCT on the date and that works fine but if I include my WHERE clause for City and State my query breaks. Also I tried to use GROUP BY but same problem, I can get Date values only once as long as I do not include other columns. In this case I need all columns but my Date value only once. Here is my query that I use:
Select Distinct(DateMeeting),FirstName, LastName, City, State
From Customers
Where City = 'Dallas'
and State = 'TX'
This query does not work with all columns that I have in my select, only if I run DISTINCT(DateMeeting). I would like to output my values in the table to look like this:
Date First Name Last Name City State
Mike Johns Dallas TX
John Cook Dallas TX
Nick Roberts Dallas TX
2015-12-11 Oliver Ryan New York NY
Michael Best New York NY
David Holmes New York NY
If anyone knows how this can be done please let me know. Thank you.
Each layer in the technology stack has its strengths and weaknesses.
As for mysql, do not turn it into a report engine as described with blank date columns except for one per date somewhere in the middle of a date chunk as shown. Subsequent dates as ordered will get muddled and confused.
True, one could use slightly interesting mysql variables and dump it just on the first row of a chunk. But for what.
Play to mysql's strengths, return all the data. And have the front-end (coldfusion or whatever), deal with the reporting features you desire for the output.